Main.xaml.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. using K4os.Compression.LZ4;
  2. using K4os.Compression.LZ4.Encoders;
  3. using K4os.Compression.LZ4.Streams;
  4. using Newtonsoft.Json;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Collections.ObjectModel;
  8. using System.IO;
  9. using System.Linq;
  10. using System.Reflection.PortableExecutable;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. using System.Windows;
  14. using System.Windows.Controls;
  15. using System.Windows.Data;
  16. using System.Windows.Documents;
  17. using System.Windows.Input;
  18. using System.Windows.Media;
  19. using System.Windows.Media.Imaging;
  20. using WechatPCMsgBakTool.Helpers;
  21. using WechatPCMsgBakTool.Interface;
  22. using WechatPCMsgBakTool.Model;
  23. namespace WechatPCMsgBakTool
  24. {
  25. /// <summary>
  26. /// Main.xaml 的交互逻辑
  27. /// </summary>
  28. public partial class Main : Window
  29. {
  30. private UserBakConfig? CurrentUserBakConfig = null;
  31. private WXUserReader? UserReader = null;
  32. private ObservableCollection<UserBakConfig> userBakConfigs = new ObservableCollection<UserBakConfig>();
  33. public Main()
  34. {
  35. Application.Current.DispatcherUnhandledException += Current_DispatcherUnhandledException;
  36. InitializeComponent();
  37. LoadWorkspace();
  38. this.Title += $" {Application.ResourceAssembly.GetName().Version}";
  39. }
  40. private void Current_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
  41. {
  42. MessageBox.Show("发生了未知错误,记录已写入到根目录err.log,如果可以,欢迎反馈给开发人员,非常感谢", "错误");
  43. File.AppendAllText("err.log", "\r\n\r\n\r\n=============================\r\n");
  44. File.AppendAllText("err.log", string.Format("异常时间:{0}\r\n", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")));
  45. File.AppendAllText("err.log", e.Exception.ToString());
  46. return;
  47. }
  48. private void LoadWorkspace()
  49. {
  50. userBakConfigs.Clear();
  51. string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "workspace");
  52. if (Directory.Exists(path))
  53. {
  54. string[] files = Directory.GetFiles(path);
  55. foreach(string file in files)
  56. {
  57. string type = file.Substring(file.Length - 5, 5);
  58. if(type == ".json")
  59. {
  60. string jsonString = File.ReadAllText(file);
  61. UserBakConfig? userBakConfig = null;
  62. try
  63. {
  64. userBakConfig = JsonConvert.DeserializeObject<UserBakConfig>(jsonString);
  65. }
  66. catch
  67. {
  68. MessageBox.Show("读取到异常工作区文件,请确认备份数据是否正常\r\n文件路径:" + file,"错误");
  69. }
  70. if(userBakConfig != null)
  71. {
  72. userBakConfigs.Add(userBakConfig);
  73. }
  74. }
  75. }
  76. }
  77. list_workspace.ItemsSource = userBakConfigs;
  78. }
  79. private void btn_decrypt_Click(object sender, RoutedEventArgs e)
  80. {
  81. if(CurrentUserBakConfig != null)
  82. {
  83. if (!CurrentUserBakConfig.Decrypt)
  84. {
  85. bool? mem_find_key = rb_find_mem.IsChecked;
  86. if(mem_find_key == null)
  87. {
  88. MessageBox.Show("请选择key获取方式");
  89. return;
  90. }
  91. byte[]? key = null;
  92. try
  93. {
  94. key = DecryptionHelper.GetWechatKey((bool)mem_find_key,CurrentUserBakConfig.Account);
  95. }
  96. catch (Exception ex)
  97. {
  98. if(ex.Source == "Newtonsoft.Json")
  99. {
  100. MessageBox.Show("版本文件读取失败,请检查版本文件内容是否为正确的json格式", "错误");
  101. }
  102. else
  103. {
  104. MessageBox.Show(ex.Message);
  105. }
  106. return;
  107. }
  108. //byte[]? key = DecryptionHelper.GetWechatKey();
  109. if (key == null)
  110. {
  111. MessageBox.Show("微信密钥获取失败,请检查微信是否打开,或者版本不兼容");
  112. return;
  113. }
  114. string key_string = BitConverter.ToString(key, 0).Replace("-", string.Empty).ToLower().ToUpper();
  115. string source = Path.Combine(CurrentUserBakConfig.UserWorkspacePath, "OriginalDB");
  116. string to = Path.Combine(CurrentUserBakConfig.UserWorkspacePath, "DecDB");
  117. try
  118. {
  119. WechatDBHelper.DecryUserData(key, source, to);
  120. MessageBox.Show("解密完成,请点击读取数据");
  121. CurrentUserBakConfig.Decrypt = true;
  122. WXWorkspace.SaveConfig(CurrentUserBakConfig);
  123. LoadWorkspace();
  124. }
  125. catch (Exception ex)
  126. {
  127. MessageBox.Show("解密过程出现错误:" + ex.Message);
  128. }
  129. }
  130. }
  131. }
  132. private void btn_read_Click(object sender, RoutedEventArgs e)
  133. {
  134. if(CurrentUserBakConfig == null)
  135. {
  136. MessageBox.Show("请先选择工作区");
  137. return;
  138. }
  139. UserReader = new WXUserReader(CurrentUserBakConfig);
  140. list_sessions.ItemsSource = UserReader.GetWXContacts();
  141. }
  142. private void list_workspace_SelectionChanged(object sender, SelectionChangedEventArgs e)
  143. {
  144. CurrentUserBakConfig = list_workspace.SelectedItem as UserBakConfig;
  145. if(CurrentUserBakConfig != null)
  146. {
  147. user_path.Content = "用户路径:" + CurrentUserBakConfig.UserResPath;
  148. if (CurrentUserBakConfig.Decrypt)
  149. {
  150. btn_decrypt.IsEnabled = false;
  151. btn_read.IsEnabled = true;
  152. }
  153. else
  154. {
  155. btn_decrypt.IsEnabled = true;
  156. btn_read.IsEnabled = false;
  157. }
  158. }
  159. }
  160. private void list_sessions_MouseDoubleClick(object sender, MouseButtonEventArgs e)
  161. {
  162. }
  163. private void Button_Click(object sender, RoutedEventArgs e)
  164. {
  165. WXContact? wXContact = list_sessions.SelectedItem as WXContact;
  166. if(UserReader == null)
  167. {
  168. MessageBox.Show("请先点击读取已解密工作区");
  169. return;
  170. }
  171. if(wXContact == null || CurrentUserBakConfig == null)
  172. {
  173. MessageBox.Show("请先选择要导出的联系人");
  174. return;
  175. }
  176. IExport export = new HtmlExport();
  177. export.InitTemplate(wXContact);
  178. export.SetMsg(UserReader, wXContact);
  179. export.SetEnd();
  180. //string path = UserReader.GetSavePath(wXContact);
  181. string path = Path.Combine(CurrentUserBakConfig.UserWorkspacePath, wXContact.UserName + ".html");
  182. export.Save(path);
  183. MessageBox.Show("导出完成");
  184. }
  185. private void Button_Click_1(object sender, RoutedEventArgs e)
  186. {
  187. SelectWechat selectWechat = new SelectWechat();
  188. selectWechat.ShowDialog();
  189. if(selectWechat.SelectProcess != null)
  190. {
  191. string path = selectWechat.SelectProcess.DBPath.Replace("\\Msg\\MicroMsg.db", "");
  192. try
  193. {
  194. WXWorkspace wXWorkspace = new WXWorkspace(path, selectWechat.SelectProcess.Account);
  195. wXWorkspace.MoveDB();
  196. MessageBox.Show("创建工作区成功");
  197. LoadWorkspace();
  198. }
  199. catch (Exception)
  200. {
  201. MessageBox.Show("创建工作区失败,请检查路径是否正确");
  202. }
  203. }
  204. }
  205. private void btn_search_Click(object sender, RoutedEventArgs e)
  206. {
  207. if(UserReader == null)
  208. {
  209. MessageBox.Show("请先读取工作区数据");
  210. return;
  211. }
  212. if(cb_del_search.IsChecked != null)
  213. {
  214. if (!(bool)cb_del_search.IsChecked)
  215. list_sessions.ItemsSource = UserReader.GetWXContacts(find_user.Text);
  216. else
  217. {
  218. List<WXMsg>? wXMsgs = UserReader.GetWXMsgs(find_user.Text);
  219. if(wXMsgs != null)
  220. {
  221. if(wXMsgs.Count > 0)
  222. {
  223. List<WXContact> wXContacts = new List<WXContact>() { new WXContact() { NickName = wXMsgs[0].StrTalker, UserName = wXMsgs[0].StrTalker } };
  224. list_sessions.ItemsSource = wXContacts;
  225. }
  226. }
  227. }
  228. }
  229. }
  230. private void btn_analyse_Click(object sender, RoutedEventArgs e)
  231. {
  232. if(UserReader == null || CurrentUserBakConfig == null)
  233. {
  234. MessageBox.Show("请先读取数据");
  235. return;
  236. }
  237. Analyse analyse = new Analyse(CurrentUserBakConfig, UserReader);
  238. analyse.Show();
  239. }
  240. private void rb_find_mem_Checked(object sender, RoutedEventArgs e)
  241. {
  242. if(CurrentUserBakConfig!= null)
  243. {
  244. if (string.IsNullOrEmpty(CurrentUserBakConfig.Account))
  245. {
  246. MessageBox.Show("使用该功能需要填写用户名,请务必确认用户名已经正确填写,否则请重建工作区");
  247. return;
  248. }
  249. }
  250. }
  251. }
  252. }