Manager.xaml.cs 6.1 KB

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