HtmlExport.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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
  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. string appName = "";
  195. string url = "";
  196. XmlNodeList? findNode = xmlObj.DocumentElement.SelectNodes("/msg/appmsg/title");
  197. if (findNode != null)
  198. {
  199. if (findNode.Count > 0)
  200. {
  201. title = findNode[0]!.InnerText;
  202. }
  203. }
  204. findNode = xmlObj.DocumentElement.SelectNodes("/msg/appmsg/sourcedisplayname");
  205. if (findNode != null)
  206. {
  207. if (findNode.Count > 0)
  208. {
  209. appName = findNode[0]!.InnerText;
  210. }
  211. }
  212. findNode = xmlObj.DocumentElement.SelectNodes("/msg/appmsg/url");
  213. if (findNode != null)
  214. {
  215. if (findNode.Count > 0)
  216. {
  217. url = findNode[0]!.InnerText;
  218. }
  219. }
  220. HtmlBody += string.Format("<p class=\"content\">{0}|{1}</p><p><a href=\"{2}\">点击访问</a></p></div>", appName, title, url);
  221. }
  222. }
  223. }
  224. }
  225. }
  226. else if (msg.Type == 34)
  227. {
  228. string? path = reader.GetAttachment(WXMsgType.Audio, msg);
  229. if (path == null)
  230. {
  231. HtmlBody += string.Format("<p class=\"content\">{0}</p></div>", "语音不存在");
  232. continue;
  233. }
  234. HtmlBody += string.Format("<p class=\"content\"><audio controls src=\"{0}\"></audio></p></div>", path);
  235. }
  236. else
  237. {
  238. HtmlBody += string.Format("<p class=\"content\">{0}</p></div>", "暂未支持的消息");
  239. }
  240. }
  241. catch(Exception ex)
  242. {
  243. err = true;
  244. File.AppendAllText("Err.log", JsonConvert.SerializeObject(msg));
  245. File.AppendAllText("Err.log", ex.ToString());
  246. }
  247. msgCount++;
  248. if(msgCount % 50 == 0)
  249. {
  250. streamWriter.WriteLine(HtmlBody);
  251. HtmlBody = "";
  252. viewModel.ExportCount = msgCount.ToString();
  253. }
  254. }
  255. if(msgCount % 50 != 0)
  256. {
  257. streamWriter.WriteLine(HtmlBody);
  258. HtmlBody = "";
  259. viewModel.ExportCount = msgCount.ToString();
  260. if (err)
  261. {
  262. MessageBox.Show("本次导出发生了异常,部分消息被跳过,更新至最新版本后还有此问题,请将Err.log反馈给开发,谢谢。", "错误");
  263. }
  264. }
  265. streamWriter.Close();
  266. streamWriter.Dispose();
  267. }
  268. private static DateTime TimeStampToDateTime(long timeStamp, bool inMilli = false)
  269. {
  270. DateTimeOffset dateTimeOffset = inMilli ? DateTimeOffset.FromUnixTimeMilliseconds(timeStamp) : DateTimeOffset.FromUnixTimeSeconds(timeStamp);
  271. return dateTimeOffset.LocalDateTime;
  272. }
  273. }
  274. }