2
0

Main.xaml.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. }
  39. private void Current_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
  40. {
  41. MessageBox.Show("发生了未知错误,记录已写入到根目录err.log,如果可以,欢迎反馈给开发人员,非常感谢", "错误");
  42. File.AppendAllText("err.log", "\r\n\r\n\r\n=============================\r\n");
  43. File.AppendAllText("err.log", string.Format("异常时间:{0}\r\n", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")));
  44. File.AppendAllText("err.log", e.Exception.ToString());
  45. return;
  46. }
  47. private void LoadWorkspace()
  48. {
  49. userBakConfigs.Clear();
  50. string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "workspace");
  51. if (Directory.Exists(path))
  52. {
  53. string[] files = Directory.GetFiles(path);
  54. foreach(string file in files)
  55. {
  56. string type = file.Substring(file.Length - 5, 5);
  57. if(type == ".json")
  58. {
  59. string jsonString = File.ReadAllText(file);
  60. UserBakConfig? userBakConfig = null;
  61. try
  62. {
  63. userBakConfig = JsonConvert.DeserializeObject<UserBakConfig>(jsonString);
  64. }
  65. catch
  66. {
  67. MessageBox.Show("读取到异常工作区文件,请确认备份数据是否正常\r\n文件路径:" + file,"错误");
  68. }
  69. if(userBakConfig != null)
  70. {
  71. userBakConfigs.Add(userBakConfig);
  72. }
  73. }
  74. }
  75. }
  76. list_workspace.ItemsSource = userBakConfigs;
  77. }
  78. private void btn_decrypt_Click(object sender, RoutedEventArgs e)
  79. {
  80. if(CurrentUserBakConfig != null)
  81. {
  82. if (!CurrentUserBakConfig.Decrypt)
  83. {
  84. byte[]? key = null;
  85. try
  86. {
  87. key = DecryptionHelper.GetWechatKey();
  88. }
  89. catch (Exception ex)
  90. {
  91. if(ex.Source == "Newtonsoft.Json")
  92. {
  93. MessageBox.Show("版本文件读取失败,请检查版本文件内容是否为正确的json格式", "错误");
  94. }
  95. else
  96. {
  97. MessageBox.Show(ex.Message);
  98. }
  99. return;
  100. }
  101. //byte[]? key = DecryptionHelper.GetWechatKey();
  102. if (key == null)
  103. {
  104. MessageBox.Show("微信密钥获取失败,请检查微信是否打开,或者版本不兼容");
  105. return;
  106. }
  107. string key_string = BitConverter.ToString(key, 0).Replace("-", string.Empty).ToLower().ToUpper();
  108. string source = Path.Combine(CurrentUserBakConfig.UserWorkspacePath, "OriginalDB");
  109. string to = Path.Combine(CurrentUserBakConfig.UserWorkspacePath, "DecDB");
  110. try
  111. {
  112. WechatDBHelper.DecryUserData(key, source, to);
  113. MessageBox.Show("解密完成,请点击读取数据");
  114. CurrentUserBakConfig.Decrypt = true;
  115. WXWorkspace.SaveConfig(CurrentUserBakConfig);
  116. LoadWorkspace();
  117. }
  118. catch (Exception ex)
  119. {
  120. MessageBox.Show("解密过程出现错误:" + ex.Message);
  121. }
  122. }
  123. }
  124. }
  125. private void btn_read_Click(object sender, RoutedEventArgs e)
  126. {
  127. if(CurrentUserBakConfig == null)
  128. {
  129. MessageBox.Show("请先选择工作区");
  130. return;
  131. }
  132. UserReader = new WXUserReader(CurrentUserBakConfig);
  133. list_sessions.ItemsSource = UserReader.GetWXContacts();
  134. }
  135. private void list_workspace_SelectionChanged(object sender, SelectionChangedEventArgs e)
  136. {
  137. CurrentUserBakConfig = list_workspace.SelectedItem as UserBakConfig;
  138. if(CurrentUserBakConfig != null)
  139. {
  140. user_path.Content = "用户路径:" + CurrentUserBakConfig.UserResPath;
  141. if (CurrentUserBakConfig.Decrypt)
  142. {
  143. btn_decrypt.IsEnabled = false;
  144. btn_read.IsEnabled = true;
  145. }
  146. else
  147. {
  148. btn_decrypt.IsEnabled = true;
  149. btn_read.IsEnabled = false;
  150. }
  151. }
  152. }
  153. private void list_sessions_MouseDoubleClick(object sender, MouseButtonEventArgs e)
  154. {
  155. }
  156. private void Button_Click(object sender, RoutedEventArgs e)
  157. {
  158. WXContact? wXContact = list_sessions.SelectedItem as WXContact;
  159. if(UserReader == null)
  160. {
  161. MessageBox.Show("请先点击读取已解密工作区");
  162. return;
  163. }
  164. if(wXContact == null || CurrentUserBakConfig == null)
  165. {
  166. MessageBox.Show("请先选择要导出的联系人");
  167. return;
  168. }
  169. IExport export = new HtmlExport();
  170. export.InitTemplate(wXContact);
  171. export.SetMsg(UserReader, wXContact);
  172. export.SetEnd();
  173. //string path = UserReader.GetSavePath(wXContact);
  174. string path = Path.Combine(CurrentUserBakConfig.UserWorkspacePath, wXContact.UserName + ".html");
  175. export.Save(path);
  176. MessageBox.Show("导出完成");
  177. }
  178. private void Button_Click_1(object sender, RoutedEventArgs e)
  179. {
  180. SelectWechat selectWechat = new SelectWechat();
  181. selectWechat.ShowDialog();
  182. if(selectWechat.SelectProcess != null)
  183. {
  184. string path = selectWechat.SelectProcess.DBPath.Replace("\\Msg\\MicroMsg.db", "");
  185. try
  186. {
  187. WXWorkspace wXWorkspace = new WXWorkspace(path);
  188. wXWorkspace.MoveDB();
  189. MessageBox.Show("创建工作区成功");
  190. LoadWorkspace();
  191. }
  192. catch (Exception)
  193. {
  194. MessageBox.Show("创建工作区失败,请检查路径是否正确");
  195. }
  196. }
  197. }
  198. private void btn_search_Click(object sender, RoutedEventArgs e)
  199. {
  200. if(UserReader == null)
  201. {
  202. MessageBox.Show("请先读取工作区数据");
  203. return;
  204. }
  205. if(cb_del_search.IsChecked != null)
  206. {
  207. if (!(bool)cb_del_search.IsChecked)
  208. list_sessions.ItemsSource = UserReader.GetWXContacts(find_user.Text);
  209. else
  210. {
  211. List<WXMsg>? wXMsgs = UserReader.GetWXMsgs(find_user.Text);
  212. if(wXMsgs != null)
  213. {
  214. if(wXMsgs.Count > 0)
  215. {
  216. List<WXContact> wXContacts = new List<WXContact>() { new WXContact() { NickName = wXMsgs[0].StrTalker, UserName = wXMsgs[0].StrTalker } };
  217. list_sessions.ItemsSource = wXContacts;
  218. }
  219. }
  220. }
  221. }
  222. }
  223. private void btn_analyse_Click(object sender, RoutedEventArgs e)
  224. {
  225. if(UserReader == null || CurrentUserBakConfig == null)
  226. {
  227. MessageBox.Show("请先读取数据");
  228. return;
  229. }
  230. Analyse analyse = new Analyse(CurrentUserBakConfig, UserReader);
  231. analyse.Show();
  232. }
  233. }
  234. }