2
0

Manager.xaml.cs 6.2 KB

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