HtmlExport.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. using K4os.Compression.LZ4.Encoders;
  2. using K4os.Compression.LZ4;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using WechatBakTool.Model;
  10. using System.Xml;
  11. using Newtonsoft.Json;
  12. using WechatBakTool.ViewModel;
  13. using System.Security.Policy;
  14. using System.Windows;
  15. namespace WechatBakTool.Export
  16. {
  17. public class HtmlExport : IExport
  18. {
  19. private string HtmlBody = "";
  20. private WXSession? Session = null;
  21. private string Path = "";
  22. public void InitTemplate(WXSession session)
  23. {
  24. Session = session;
  25. HtmlBody = "<!DOCTYPE html><html><head><meta charset=\"utf-8\"><title>WechatBakTool</title><style>p{margin:0px;}.msg{padding-bottom:10px;}.nickname{font-size:10px;}.content{font-size:14px;}</style></head><body>";
  26. HtmlBody += string.Format("<div class=\"msg\"><p class=\"nickname\"><b>与 {0}({1}) 的聊天记录</b></p>", Session.NickName, Session.UserName);
  27. HtmlBody += string.Format("<div class=\"msg\"><p class=\"nickname\"><b>导出时间:{0}</b></p><hr/>", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
  28. File.WriteAllText(Path, HtmlBody);
  29. }
  30. public void InitTemplate(WXContact contact, string p)
  31. {
  32. Path = p;
  33. WXSession session = new WXSession();
  34. session.NickName = contact.NickName;
  35. session.UserName = contact.UserName;
  36. InitTemplate(session);
  37. }
  38. public void Save(string path = "")
  39. {
  40. }
  41. public void SetEnd()
  42. {
  43. HtmlBody += "</body></html>";
  44. File.AppendAllText(Path, HtmlBody);
  45. }
  46. public void SetMsg(WXUserReader reader, WXContact contact,WorkspaceViewModel viewModel)
  47. {
  48. if (Session == null)
  49. throw new Exception("请初始化模版:Not Use InitTemplate");
  50. List<WXMsg>? msgList = reader.GetWXMsgs(contact.UserName);
  51. if (msgList == null)
  52. throw new Exception("获取消息失败,请确认数据库读取正常");
  53. msgList.Sort((x, y) => x.CreateTime.CompareTo(y.CreateTime));
  54. bool err = false;
  55. int msgCount = 0;
  56. HtmlBody = "";
  57. StreamWriter streamWriter = new StreamWriter(Path, true);
  58. foreach (var msg in msgList)
  59. {
  60. try
  61. {
  62. HtmlBody += string.Format("<div class=\"msg\"><p class=\"nickname\">{0} <span style=\"padding-left:10px;\">{1}</span></p>", msg.IsSender ? "我" : msg.NickName, TimeStampToDateTime(msg.CreateTime).ToString("yyyy-MM-dd HH:mm:ss"));
  63. if (msg.Type == 1)
  64. HtmlBody += string.Format("<p class=\"content\">{0}</p></div>", msg.StrContent);
  65. else if (msg.Type == 3)
  66. {
  67. string? path = reader.GetAttachment(WXMsgType.Image, msg);
  68. if (path == null)
  69. {
  70. #if DEBUG
  71. File.AppendAllText("debug.log", string.Format("[D]{0} {1}:{2}\n", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), "Img Error Path=>", path));
  72. File.AppendAllText("debug.log", string.Format("[D]{0} {1}:{2}\n", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), "Img Error Msg=>", JsonConvert.SerializeObject(msg)));
  73. #endif
  74. HtmlBody += string.Format("<p class=\"content\">{0}</p></div>", "图片转换出现错误或文件不存在");
  75. continue;
  76. }
  77. HtmlBody += string.Format("<p class=\"content\"><img src=\"{0}\" style=\"max-height:1000px;max-width:1000px;\"/></p></div>", path);
  78. }
  79. else if (msg.Type == 43)
  80. {
  81. string? path = reader.GetAttachment(WXMsgType.Video, msg);
  82. if (path == null)
  83. {
  84. HtmlBody += string.Format("<p class=\"content\">{0}</p></div>", "视频不存在");
  85. continue;
  86. }
  87. HtmlBody += string.Format("<p class=\"content\"><video controls style=\"max-height:300px;max-width:300px;\"><source src=\"{0}\" type=\"video/mp4\" /></video></p></div>", path);
  88. }
  89. else if (msg.Type == 47)
  90. {
  91. string? path = reader.GetAttachment(WXMsgType.Emoji, msg);
  92. if (path == null)
  93. {
  94. #if DEBUG
  95. File.AppendAllText("debug.log", string.Format("[D]{0} {1}:{2}\n", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), "Emoji Error Path=>", path));
  96. File.AppendAllText("debug.log", string.Format("[D]{0} {1}:{2}\n", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), "Emoji Error Msg=>", JsonConvert.SerializeObject(msg)));
  97. #endif
  98. HtmlBody += string.Format("<p class=\"content\">{0}</p></div>", "表情未预下载或加密表情");
  99. continue;
  100. }
  101. HtmlBody += string.Format("<p class=\"content\"><img src=\"{0}\" style=\"max-height:300px;max-width:300px;\"/></p></div>", path);
  102. }
  103. else if (msg.Type == 49)
  104. {
  105. if (msg.SubType == 6 || msg.SubType == 40)
  106. {
  107. string? path = reader.GetAttachment(WXMsgType.File, msg);
  108. if (path == null)
  109. {
  110. HtmlBody += string.Format("<p class=\"content\">{0}</p></div>", "文件不存在");
  111. continue;
  112. }
  113. else
  114. {
  115. HtmlBody += string.Format("<p class=\"content\">{0}</p><p><a href=\"{1}\">点击访问</a></p></div>", "文件:" + path, path);
  116. }
  117. }
  118. else if (msg.SubType == 19)
  119. {
  120. using (var decoder = LZ4Decoder.Create(true, 64))
  121. {
  122. byte[] target = new byte[10240];
  123. int res = 0;
  124. if (msg.CompressContent != null)
  125. res = LZ4Codec.Decode(msg.CompressContent, 0, msg.CompressContent.Length, target, 0, target.Length);
  126. byte[] data = target.Skip(0).Take(res).ToArray();
  127. string xml = Encoding.UTF8.GetString(data);
  128. if (!string.IsNullOrEmpty(xml))
  129. {
  130. xml = xml.Replace("\n", "");
  131. XmlDocument xmlObj = new XmlDocument();
  132. xmlObj.LoadXml(xml);
  133. if (xmlObj.DocumentElement != null)
  134. {
  135. string title = "";
  136. string record = "";
  137. string url = "";
  138. XmlNodeList? findNode = xmlObj.DocumentElement.SelectNodes("/msg/appmsg/title");
  139. if (findNode != null)
  140. {
  141. if (findNode.Count > 0)
  142. {
  143. title = findNode[0]!.InnerText;
  144. }
  145. }
  146. HtmlBody += string.Format("<p class=\"content\">{0}</p>", title);
  147. findNode = xmlObj.DocumentElement.SelectNodes("/msg/appmsg/recorditem");
  148. if (findNode != null)
  149. {
  150. if (findNode.Count > 0)
  151. {
  152. XmlDocument itemObj = new XmlDocument();
  153. itemObj.LoadXml(findNode[0]!.InnerText);
  154. XmlNodeList? itemNode = itemObj.DocumentElement.SelectNodes("/recordinfo/datalist/dataitem");
  155. if (itemNode.Count > 0)
  156. {
  157. foreach (XmlNode node in itemNode)
  158. {
  159. string nodeMsg;
  160. string name = node["sourcename"].InnerText;
  161. if (node.Attributes["datatype"].InnerText == "1")
  162. nodeMsg = node["datadesc1"].InnerText;
  163. else if (node.Attributes["datatype"].InnerText == "2")
  164. nodeMsg = "不支持的消息";
  165. else
  166. nodeMsg = node["datatitle"].InnerText;
  167. HtmlBody += string.Format("<p class=\"content\">{0}:{1}</p>", name, nodeMsg);
  168. }
  169. }
  170. }
  171. }
  172. }
  173. }
  174. }
  175. }
  176. else if (msg.SubType == 57)
  177. {
  178. using (var decoder = LZ4Decoder.Create(true, 64))
  179. {
  180. byte[] target = new byte[10240];
  181. int res = 0;
  182. if (msg.CompressContent != null)
  183. res = LZ4Codec.Decode(msg.CompressContent, 0, msg.CompressContent.Length, target, 0, target.Length);
  184. byte[] data = target.Skip(0).Take(res).ToArray();
  185. string xml = Encoding.UTF8.GetString(data);
  186. if (!string.IsNullOrEmpty(xml))
  187. {
  188. xml = xml.Replace("\n", "");
  189. XmlDocument xmlObj = new XmlDocument();
  190. xmlObj.LoadXml(xml);
  191. if (xmlObj.DocumentElement != null)
  192. {
  193. string title = "";
  194. XmlNodeList? findNode = xmlObj.DocumentElement.SelectNodes("/msg/appmsg/title");
  195. if (findNode != null)
  196. {
  197. if (findNode.Count > 0)
  198. {
  199. title = findNode[0]!.InnerText;
  200. }
  201. }
  202. HtmlBody += string.Format("<p class=\"content\">{0}</p>", title);
  203. XmlNode? type = xmlObj.DocumentElement.SelectSingleNode("/msg/appmsg/refermsg/type");
  204. if(type != null)
  205. {
  206. XmlNode? source = xmlObj.DocumentElement.SelectSingleNode("/msg/appmsg/refermsg/displayname");
  207. XmlNode? text = xmlObj.DocumentElement.SelectSingleNode("/msg/appmsg/refermsg/content");
  208. if(type.InnerText == "1" && source != null && text != null)
  209. {
  210. HtmlBody += string.Format("<p class=\"content\">[引用]{0}:{1}</p>", source.InnerText, text.InnerText);
  211. }
  212. else if(type.InnerText != "1" && source != null && text != null)
  213. {
  214. HtmlBody += string.Format("<p class=\"content\">[引用]{0}:非文本消息类型-{1}</p>", source.InnerText, type);
  215. }
  216. else
  217. {
  218. HtmlBody += string.Format("<p class=\"content\">未知的引用消息</p>");
  219. }
  220. }
  221. }
  222. }
  223. }
  224. }
  225. else
  226. {
  227. using (var decoder = LZ4Decoder.Create(true, 64))
  228. {
  229. byte[] target = new byte[10240];
  230. int res = 0;
  231. if (msg.CompressContent != null)
  232. res = LZ4Codec.Decode(msg.CompressContent, 0, msg.CompressContent.Length, target, 0, target.Length);
  233. byte[] data = target.Skip(0).Take(res).ToArray();
  234. string xml = Encoding.UTF8.GetString(data);
  235. if (!string.IsNullOrEmpty(xml))
  236. {
  237. xml = xml.Replace("\n", "");
  238. XmlDocument xmlObj = new XmlDocument();
  239. xmlObj.LoadXml(xml);
  240. if (xmlObj.DocumentElement != null)
  241. {
  242. string title = "";
  243. string appName = "";
  244. string url = "";
  245. XmlNodeList? findNode = xmlObj.DocumentElement.SelectNodes("/msg/appmsg/title");
  246. if (findNode != null)
  247. {
  248. if (findNode.Count > 0)
  249. {
  250. title = findNode[0]!.InnerText;
  251. }
  252. }
  253. findNode = xmlObj.DocumentElement.SelectNodes("/msg/appmsg/sourcedisplayname");
  254. if (findNode != null)
  255. {
  256. if (findNode.Count > 0)
  257. {
  258. appName = findNode[0]!.InnerText;
  259. }
  260. }
  261. findNode = xmlObj.DocumentElement.SelectNodes("/msg/appmsg/url");
  262. if (findNode != null)
  263. {
  264. if (findNode.Count > 0)
  265. {
  266. url = findNode[0]!.InnerText;
  267. }
  268. }
  269. HtmlBody += string.Format("<p class=\"content\">{0}|{1}</p><p><a href=\"{2}\">点击访问</a></p></div>", appName, title, url);
  270. }
  271. }
  272. }
  273. }
  274. }
  275. else if (msg.Type == 34)
  276. {
  277. string? path = reader.GetAttachment(WXMsgType.Audio, msg);
  278. if (path == null)
  279. {
  280. HtmlBody += string.Format("<p class=\"content\">{0}</p></div>", "语音不存在");
  281. continue;
  282. }
  283. HtmlBody += string.Format("<p class=\"content\"><audio controls src=\"{0}\"></audio></p></div>", path);
  284. }
  285. else
  286. {
  287. HtmlBody += string.Format("<p class=\"content\">{0}</p></div>", "暂未支持的消息");
  288. }
  289. }
  290. catch(Exception ex)
  291. {
  292. err = true;
  293. File.AppendAllText("Err.log", JsonConvert.SerializeObject(msg));
  294. File.AppendAllText("Err.log", ex.ToString());
  295. }
  296. msgCount++;
  297. if(msgCount % 50 == 0)
  298. {
  299. streamWriter.WriteLine(HtmlBody);
  300. HtmlBody = "";
  301. viewModel.ExportCount = msgCount.ToString();
  302. }
  303. }
  304. if(msgCount % 50 != 0)
  305. {
  306. streamWriter.WriteLine(HtmlBody);
  307. HtmlBody = "";
  308. viewModel.ExportCount = msgCount.ToString();
  309. if (err)
  310. {
  311. MessageBox.Show("本次导出发生了异常,部分消息被跳过,更新至最新版本后还有此问题,请将Err.log反馈给开发,谢谢。", "错误");
  312. }
  313. }
  314. streamWriter.Close();
  315. streamWriter.Dispose();
  316. }
  317. private static DateTime TimeStampToDateTime(long timeStamp, bool inMilli = false)
  318. {
  319. DateTimeOffset dateTimeOffset = inMilli ? DateTimeOffset.FromUnixTimeMilliseconds(timeStamp) : DateTimeOffset.FromUnixTimeSeconds(timeStamp);
  320. return dateTimeOffset.LocalDateTime;
  321. }
  322. }
  323. }