Main.xaml.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. using Newtonsoft.Json;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Collections.ObjectModel;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Reflection.PortableExecutable;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows;
  11. using System.Windows.Controls;
  12. using System.Windows.Data;
  13. using System.Windows.Documents;
  14. using System.Windows.Input;
  15. using System.Windows.Media;
  16. using System.Windows.Media.Imaging;
  17. using WechatPCMsgBakTool.Helpers;
  18. using WechatPCMsgBakTool.Interface;
  19. using WechatPCMsgBakTool.Model;
  20. namespace WechatPCMsgBakTool
  21. {
  22. /// <summary>
  23. /// Main.xaml 的交互逻辑
  24. /// </summary>
  25. public partial class Main : Window
  26. {
  27. private UserBakConfig? CurrentUserBakConfig = null;
  28. private WXUserReader? UserReader = null;
  29. private ObservableCollection<UserBakConfig> userBakConfigs = new ObservableCollection<UserBakConfig>();
  30. public Main()
  31. {
  32. InitializeComponent();
  33. LoadWorkspace();
  34. }
  35. private void LoadWorkspace()
  36. {
  37. userBakConfigs.Clear();
  38. string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "workspace");
  39. if (Directory.Exists(path))
  40. {
  41. string[] files = Directory.GetFiles(path);
  42. foreach(string file in files)
  43. {
  44. string type = file.Substring(file.Length - 5, 5);
  45. if(type == ".json")
  46. {
  47. string jsonString = File.ReadAllText(file);
  48. UserBakConfig? userBakConfig = JsonConvert.DeserializeObject<UserBakConfig>(jsonString);
  49. if(userBakConfig != null)
  50. {
  51. userBakConfigs.Add(userBakConfig);
  52. }
  53. }
  54. }
  55. }
  56. list_workspace.ItemsSource = userBakConfigs;
  57. }
  58. private void btn_decrypt_Click(object sender, RoutedEventArgs e)
  59. {
  60. if(CurrentUserBakConfig != null)
  61. {
  62. if (!CurrentUserBakConfig.Decrypt)
  63. {
  64. byte[]? key = DecryptionHelper.GetWechatKey();
  65. if (key == null)
  66. {
  67. MessageBox.Show("微信密钥获取失败,请检查微信是否打开,或者版本不兼容");
  68. return;
  69. }
  70. string source = Path.Combine(CurrentUserBakConfig.UserWorkspacePath, "OriginalDB");
  71. string to = Path.Combine(CurrentUserBakConfig.UserWorkspacePath, "DecDB");
  72. try
  73. {
  74. WechatDBHelper.DecryUserData(key, source, to);
  75. MessageBox.Show("解密完成,请点击读取数据");
  76. CurrentUserBakConfig.Decrypt = true;
  77. WXWorkspace.SaveConfig(CurrentUserBakConfig);
  78. LoadWorkspace();
  79. }
  80. catch (Exception ex)
  81. {
  82. MessageBox.Show("解密过程出现错误:" + ex.Message);
  83. }
  84. }
  85. }
  86. }
  87. private void btn_read_Click(object sender, RoutedEventArgs e)
  88. {
  89. if(CurrentUserBakConfig == null)
  90. {
  91. MessageBox.Show("请先选择工作区");
  92. return;
  93. }
  94. UserReader = new WXUserReader(CurrentUserBakConfig);
  95. list_sessions.ItemsSource = UserReader.GetWXContacts();
  96. }
  97. private void list_workspace_SelectionChanged(object sender, SelectionChangedEventArgs e)
  98. {
  99. CurrentUserBakConfig = list_workspace.SelectedItem as UserBakConfig;
  100. if(CurrentUserBakConfig != null)
  101. {
  102. user_path.Content = "用户路径:" + CurrentUserBakConfig.UserResPath;
  103. if (CurrentUserBakConfig.Decrypt)
  104. {
  105. btn_decrypt.IsEnabled = false;
  106. btn_read.IsEnabled = true;
  107. }
  108. else
  109. {
  110. btn_decrypt.IsEnabled = true;
  111. btn_read.IsEnabled = false;
  112. }
  113. }
  114. }
  115. private void list_sessions_MouseDoubleClick(object sender, MouseButtonEventArgs e)
  116. {
  117. }
  118. private void Button_Click(object sender, RoutedEventArgs e)
  119. {
  120. WXContact? wXContact = list_sessions.SelectedItem as WXContact;
  121. if(UserReader == null)
  122. {
  123. MessageBox.Show("请先点击读取已解密工作区");
  124. return;
  125. }
  126. if(wXContact == null || CurrentUserBakConfig == null)
  127. {
  128. MessageBox.Show("请先选择要导出的联系人");
  129. return;
  130. }
  131. IExport export = new HtmlExport();
  132. export.InitTemplate(wXContact);
  133. export.SetMsg(UserReader, wXContact);
  134. export.SetEnd();
  135. //string path = UserReader.GetSavePath(wXContact);
  136. string path = Path.Combine(CurrentUserBakConfig.UserWorkspacePath, wXContact.UserName + ".html");
  137. export.Save(path);
  138. MessageBox.Show("导出完成");
  139. }
  140. private void Button_Click_1(object sender, RoutedEventArgs e)
  141. {
  142. SelectWechat selectWechat = new SelectWechat();
  143. selectWechat.ShowDialog();
  144. if(selectWechat.SelectProcess != null)
  145. {
  146. string path = selectWechat.SelectProcess.DBPath.Replace("\\Msg\\MicroMsg.db", "");
  147. try
  148. {
  149. WXWorkspace wXWorkspace = new WXWorkspace(path);
  150. wXWorkspace.MoveDB();
  151. MessageBox.Show("创建工作区成功");
  152. LoadWorkspace();
  153. }
  154. catch (Exception)
  155. {
  156. MessageBox.Show("创建工作区失败,请检查路径是否正确");
  157. }
  158. }
  159. }
  160. private void btn_search_Click(object sender, RoutedEventArgs e)
  161. {
  162. if(UserReader == null)
  163. {
  164. MessageBox.Show("请先读取工作区数据");
  165. return;
  166. }
  167. list_sessions.ItemsSource = UserReader.GetWXContacts(find_user.Text);
  168. }
  169. }
  170. }