Main.xaml.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. byte[]? key = null;
  86. try
  87. {
  88. key = DecryptionHelper.GetWechatKey();
  89. }
  90. catch (Exception ex)
  91. {
  92. if(ex.Source == "Newtonsoft.Json")
  93. {
  94. MessageBox.Show("版本文件读取失败,请检查版本文件内容是否为正确的json格式", "错误");
  95. }
  96. else
  97. {
  98. MessageBox.Show(ex.Message);
  99. }
  100. return;
  101. }
  102. //byte[]? key = DecryptionHelper.GetWechatKey();
  103. if (key == null)
  104. {
  105. MessageBox.Show("微信密钥获取失败,请检查微信是否打开,或者版本不兼容");
  106. return;
  107. }
  108. string key_string = BitConverter.ToString(key, 0).Replace("-", string.Empty).ToLower().ToUpper();
  109. string source = Path.Combine(CurrentUserBakConfig.UserWorkspacePath, "OriginalDB");
  110. string to = Path.Combine(CurrentUserBakConfig.UserWorkspacePath, "DecDB");
  111. try
  112. {
  113. WechatDBHelper.DecryUserData(key, source, to);
  114. MessageBox.Show("解密完成,请点击读取数据");
  115. CurrentUserBakConfig.Decrypt = true;
  116. WXWorkspace.SaveConfig(CurrentUserBakConfig);
  117. LoadWorkspace();
  118. }
  119. catch (Exception ex)
  120. {
  121. MessageBox.Show("解密过程出现错误:" + ex.Message);
  122. }
  123. }
  124. }
  125. }
  126. private void btn_read_Click(object sender, RoutedEventArgs e)
  127. {
  128. if(CurrentUserBakConfig == null)
  129. {
  130. MessageBox.Show("请先选择工作区");
  131. return;
  132. }
  133. UserReader = new WXUserReader(CurrentUserBakConfig);
  134. list_sessions.ItemsSource = UserReader.GetWXContacts();
  135. }
  136. private void list_workspace_SelectionChanged(object sender, SelectionChangedEventArgs e)
  137. {
  138. CurrentUserBakConfig = list_workspace.SelectedItem as UserBakConfig;
  139. if(CurrentUserBakConfig != null)
  140. {
  141. user_path.Content = "用户路径:" + CurrentUserBakConfig.UserResPath;
  142. if (CurrentUserBakConfig.Decrypt)
  143. {
  144. btn_decrypt.IsEnabled = false;
  145. btn_read.IsEnabled = true;
  146. }
  147. else
  148. {
  149. btn_decrypt.IsEnabled = true;
  150. btn_read.IsEnabled = false;
  151. }
  152. }
  153. }
  154. private void list_sessions_MouseDoubleClick(object sender, MouseButtonEventArgs e)
  155. {
  156. }
  157. private void Button_Click(object sender, RoutedEventArgs e)
  158. {
  159. WXContact? wXContact = list_sessions.SelectedItem as WXContact;
  160. if(UserReader == null)
  161. {
  162. MessageBox.Show("请先点击读取已解密工作区");
  163. return;
  164. }
  165. if(wXContact == null || CurrentUserBakConfig == null)
  166. {
  167. MessageBox.Show("请先选择要导出的联系人");
  168. return;
  169. }
  170. IExport export = new HtmlExport();
  171. export.InitTemplate(wXContact);
  172. export.SetMsg(UserReader, wXContact);
  173. export.SetEnd();
  174. //string path = UserReader.GetSavePath(wXContact);
  175. string path = Path.Combine(CurrentUserBakConfig.UserWorkspacePath, wXContact.UserName + ".html");
  176. export.Save(path);
  177. MessageBox.Show("导出完成");
  178. }
  179. private void Button_Click_1(object sender, RoutedEventArgs e)
  180. {
  181. SelectWechat selectWechat = new SelectWechat();
  182. selectWechat.ShowDialog();
  183. if(selectWechat.SelectProcess != null)
  184. {
  185. string path = selectWechat.SelectProcess.DBPath.Replace("\\Msg\\MicroMsg.db", "");
  186. try
  187. {
  188. WXWorkspace wXWorkspace = new WXWorkspace(path);
  189. wXWorkspace.MoveDB();
  190. MessageBox.Show("创建工作区成功");
  191. LoadWorkspace();
  192. }
  193. catch (Exception)
  194. {
  195. MessageBox.Show("创建工作区失败,请检查路径是否正确");
  196. }
  197. }
  198. }
  199. private void btn_search_Click(object sender, RoutedEventArgs e)
  200. {
  201. if(UserReader == null)
  202. {
  203. MessageBox.Show("请先读取工作区数据");
  204. return;
  205. }
  206. if(cb_del_search.IsChecked != null)
  207. {
  208. if (!(bool)cb_del_search.IsChecked)
  209. list_sessions.ItemsSource = UserReader.GetWXContacts(find_user.Text);
  210. else
  211. {
  212. List<WXMsg>? wXMsgs = UserReader.GetWXMsgs(find_user.Text);
  213. if(wXMsgs != null)
  214. {
  215. if(wXMsgs.Count > 0)
  216. {
  217. List<WXContact> wXContacts = new List<WXContact>() { new WXContact() { NickName = wXMsgs[0].StrTalker, UserName = wXMsgs[0].StrTalker } };
  218. list_sessions.ItemsSource = wXContacts;
  219. }
  220. }
  221. }
  222. }
  223. }
  224. private void btn_analyse_Click(object sender, RoutedEventArgs e)
  225. {
  226. if(UserReader == null || CurrentUserBakConfig == null)
  227. {
  228. MessageBox.Show("请先读取数据");
  229. return;
  230. }
  231. Analyse analyse = new Analyse(CurrentUserBakConfig, UserReader);
  232. analyse.Show();
  233. }
  234. }
  235. }