Manager.xaml.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Diagnostics;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Net.Http;
  8. using System.Text;
  9. using System.Text.RegularExpressions;
  10. using System.Threading;
  11. using System.Threading.Tasks;
  12. using System.Windows;
  13. using System.Windows.Controls;
  14. using System.Windows.Data;
  15. using System.Windows.Documents;
  16. using System.Windows.Input;
  17. using System.Windows.Media;
  18. using System.Windows.Media.Imaging;
  19. using System.Windows.Navigation;
  20. using System.Xml;
  21. using WechatBakTool.Export;
  22. using WechatBakTool.Model;
  23. using WechatBakTool.ViewModel;
  24. namespace WechatBakTool.Pages
  25. {
  26. /// <summary>
  27. /// Manager.xaml 的交互逻辑
  28. /// </summary>
  29. public partial class Manager : Page
  30. {
  31. private WorkspaceViewModel workspaceViewModel = new WorkspaceViewModel();
  32. public WXUserReader? UserReader;
  33. private List<WXContact>? ExpContacts;
  34. private bool Suspend = false;
  35. private int Status = 0;
  36. public Manager()
  37. {
  38. DataContext = workspaceViewModel;
  39. InitializeComponent();
  40. UserBakConfig? config = Main2.CurrentUserBakConfig;
  41. if (config != null)
  42. {
  43. UserReader = new WXUserReader(config);
  44. if (!config.Decrypt)
  45. {
  46. MessageBox.Show("请先解密数据库", "错误");
  47. return;
  48. }
  49. }
  50. }
  51. private void btn_export_all_Click(object sender, RoutedEventArgs e)
  52. {
  53. // 0 未开始
  54. if(Status == 0 || Status == 2)
  55. {
  56. Suspend = false;
  57. btn_export_all.Content = "暂停";
  58. }
  59. // 1 进行中
  60. else if (Status == 1)
  61. {
  62. // 开启暂停
  63. Suspend = true;
  64. Status = 2;
  65. btn_export_all.Content = "继续";
  66. return;
  67. }
  68. Task.Run(() =>
  69. {
  70. bool group = false, user = false;
  71. Dispatcher.Invoke(() =>
  72. {
  73. if (cb_group.IsChecked == null || cb_user.IsChecked == null)
  74. return;
  75. group = (bool)cb_group.IsChecked;
  76. user = (bool)cb_user.IsChecked;
  77. });
  78. if (UserReader != null)
  79. {
  80. if (Status == 0)
  81. ExpContacts = UserReader.GetWXContacts().ToList();
  82. else
  83. Suspend = false;
  84. List<WXContact> process = new List<WXContact>();
  85. foreach (var contact in ExpContacts!)
  86. {
  87. if (Suspend)
  88. {
  89. foreach(WXContact p in process)
  90. {
  91. ExpContacts.Remove(p);
  92. }
  93. workspaceViewModel.ExportCount = "已暂停";
  94. return;
  95. }
  96. Status = 1;
  97. if (group && contact.UserName.Contains("@chatroom"))
  98. {
  99. workspaceViewModel.WXContact = contact;
  100. ExportMsg(contact);
  101. }
  102. if (user && !contact.UserName.Contains("@chatroom") && !contact.UserName.Contains("gh_"))
  103. {
  104. workspaceViewModel.WXContact = contact;
  105. ExportMsg(contact);
  106. }
  107. process.Add(contact);
  108. }
  109. Status = 0;
  110. btn_export_all.Content = "导出";
  111. MessageBox.Show("批量导出完成", "提示");
  112. }
  113. });
  114. }
  115. private void ExportMsg(WXContact contact)
  116. {
  117. workspaceViewModel.ExportCount = "";
  118. // string path = Path.Combine(Main2.CurrentUserBakConfig!.UserWorkspacePath, contact.UserName + ".html");
  119. string name = contact.NickName;
  120. name = name.Replace(@"\", "");
  121. name = Regex.Replace(name, "[ \\[ \\] \\^ \\-_*×――(^)$%~!/@#$…&%¥—+=<>《》|!!???::•`·、。,;,.;\"‘’“”-]", "");
  122. string path = Path.Combine(
  123. Main2.CurrentUserBakConfig!.UserWorkspacePath,
  124. string.Format(
  125. "{0}-{1}.html",
  126. contact.UserName,
  127. contact.Remark == "" ? name : contact.Remark
  128. )
  129. );
  130. IExport export = new HtmlExport();
  131. export.InitTemplate(contact, path);
  132. if(export.SetMsg(UserReader!, contact, workspaceViewModel))
  133. {
  134. export.SetEnd();
  135. export.Save(path);
  136. }
  137. }
  138. private void btn_emoji_download_Click(object sender, RoutedEventArgs e)
  139. {
  140. if (UserReader != null)
  141. {
  142. Task.Run(() =>
  143. {
  144. UserReader.PreDownloadEmoji();
  145. MessageBox.Show("所有表情预下载完毕");
  146. });
  147. }
  148. }
  149. private void btn_analyse_Click(object sender, RoutedEventArgs e)
  150. {
  151. if (UserReader == null || Main2.CurrentUserBakConfig == null)
  152. {
  153. MessageBox.Show("请先读取数据");
  154. return;
  155. }
  156. Analyse analyse = new Analyse(Main2.CurrentUserBakConfig, UserReader);
  157. analyse.Show();
  158. }
  159. }
  160. }