HtmlExport.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. namespace WechatBakTool.Export
  15. {
  16. public class HtmlExport : IExport
  17. {
  18. private string HtmlBody = "";
  19. private WXSession? Session = null;
  20. private string Path = "";
  21. public void InitTemplate(WXSession session)
  22. {
  23. Session = session;
  24. 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>";
  25. HtmlBody += string.Format("<div class=\"msg\"><p class=\"nickname\"><b>与 {0}({1}) 的聊天记录</b></p>", Session.NickName, Session.UserName);
  26. HtmlBody += string.Format("<div class=\"msg\"><p class=\"nickname\"><b>导出时间:{0}</b></p><hr/>", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
  27. File.WriteAllText(Path, HtmlBody);
  28. }
  29. public void InitTemplate(WXContact contact, string p)
  30. {
  31. Path = p;
  32. WXSession session = new WXSession();
  33. session.NickName = contact.NickName;
  34. session.UserName = contact.UserName;
  35. InitTemplate(session);
  36. }
  37. public void Save(string path = "")
  38. {
  39. }
  40. public void SetEnd()
  41. {
  42. HtmlBody += "</body></html>";
  43. File.AppendAllText(Path, HtmlBody);
  44. }
  45. public void SetMsg(WXUserReader reader, WXContact contact,WorkspaceViewModel viewModel)
  46. {
  47. if (Session == null)
  48. throw new Exception("请初始化模版:Not Use InitTemplate");
  49. List<WXMsg>? msgList = reader.GetWXMsgs(contact.UserName);
  50. if (msgList == null)
  51. throw new Exception("获取消息失败,请确认数据库读取正常");
  52. msgList.Sort((x, y) => x.CreateTime.CompareTo(y.CreateTime));
  53. int msgCount = 0;
  54. HtmlBody = "";
  55. StreamWriter streamWriter = new StreamWriter(Path, true);
  56. foreach (var msg in msgList)
  57. {
  58. 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"));
  59. if (msg.Type == 1)
  60. HtmlBody += string.Format("<p class=\"content\">{0}</p></div>", msg.StrContent);
  61. else if (msg.Type == 3)
  62. {
  63. string? path = reader.GetAttachment(WXMsgType.Image, msg);
  64. if (path == null)
  65. {
  66. #if DEBUG
  67. File.AppendAllText("debug.log", string.Format("[D]{0} {1}:{2}\n", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), "Img Error Path=>", path));
  68. 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)));
  69. #endif
  70. HtmlBody += string.Format("<p class=\"content\">{0}</p></div>", "图片转换出现错误或文件不存在");
  71. continue;
  72. }
  73. HtmlBody += string.Format("<p class=\"content\"><img src=\"{0}\" style=\"max-height:1000px;max-width:1000px;\"/></p></div>", path);
  74. }
  75. else if (msg.Type == 43)
  76. {
  77. string? path = reader.GetAttachment(WXMsgType.Video, msg);
  78. if (path == null)
  79. {
  80. HtmlBody += string.Format("<p class=\"content\">{0}</p></div>", "视频不存在");
  81. continue;
  82. }
  83. 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);
  84. }
  85. else if(msg.Type == 47)
  86. {
  87. string? path = reader.GetAttachment(WXMsgType.Emoji, msg);
  88. if (path == null)
  89. {
  90. #if DEBUG
  91. File.AppendAllText("debug.log", string.Format("[D]{0} {1}:{2}\n", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), "Emoji Error Path=>", path));
  92. 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)));
  93. #endif
  94. HtmlBody += string.Format("<p class=\"content\">{0}</p></div>", "表情未预下载或加密表情");
  95. continue;
  96. }
  97. HtmlBody += string.Format("<p class=\"content\"><img src=\"{0}\" style=\"max-height:300px;max-width:300px;\"/></p></div>", path);
  98. }
  99. else if (msg.Type == 49)
  100. {
  101. if(msg.SubType == 6||msg.SubType == 19||msg.SubType == 40)
  102. {
  103. string? path = reader.GetAttachment(WXMsgType.File, msg);
  104. if(path == null)
  105. {
  106. HtmlBody += string.Format("<p class=\"content\">{0}</p></div>", "文件不存在");
  107. continue;
  108. }
  109. else
  110. {
  111. HtmlBody += string.Format("<p class=\"content\">{0}</p><p><a href=\"{1}\">点击访问</a></p></div>", "文件:" + path, path);
  112. }
  113. }
  114. else
  115. {
  116. using (var decoder = LZ4Decoder.Create(true, 64))
  117. {
  118. byte[] target = new byte[10240];
  119. int res = 0;
  120. if (msg.CompressContent != null)
  121. res = LZ4Codec.Decode(msg.CompressContent, 0, msg.CompressContent.Length, target, 0, target.Length);
  122. byte[] data = target.Skip(0).Take(res).ToArray();
  123. string xml = Encoding.UTF8.GetString(data);
  124. if (!string.IsNullOrEmpty(xml))
  125. {
  126. xml = xml.Replace("\n", "");
  127. XmlDocument xmlObj = new XmlDocument();
  128. xmlObj.LoadXml(xml);
  129. if (xmlObj.DocumentElement != null)
  130. {
  131. string title = "";
  132. string appName = "";
  133. string url = "";
  134. XmlNodeList? findNode = xmlObj.DocumentElement.SelectNodes("/msg/appmsg/title");
  135. if (findNode != null)
  136. {
  137. if (findNode.Count > 0)
  138. {
  139. title = findNode[0]!.InnerText;
  140. }
  141. }
  142. findNode = xmlObj.DocumentElement.SelectNodes("/msg/appmsg/sourcedisplayname");
  143. if (findNode != null)
  144. {
  145. if (findNode.Count > 0)
  146. {
  147. appName = findNode[0]!.InnerText;
  148. }
  149. }
  150. findNode = xmlObj.DocumentElement.SelectNodes("/msg/appmsg/url");
  151. if (findNode != null)
  152. {
  153. if (findNode.Count > 0)
  154. {
  155. url = findNode[0]!.InnerText;
  156. }
  157. }
  158. HtmlBody += string.Format("<p class=\"content\">{0}|{1}</p><p><a href=\"{2}\">点击访问</a></p></div>", appName, title, url);
  159. }
  160. }
  161. }
  162. }
  163. }
  164. else if (msg.Type == 34)
  165. {
  166. string? path = reader.GetAttachment(WXMsgType.Audio, msg);
  167. if (path == null)
  168. {
  169. HtmlBody += string.Format("<p class=\"content\">{0}</p></div>", "语音不存在");
  170. continue;
  171. }
  172. HtmlBody += string.Format("<p class=\"content\"><audio controls src=\"{0}\"></audio></p></div>", path);
  173. }
  174. else
  175. {
  176. HtmlBody += string.Format("<p class=\"content\">{0}</p></div>", "暂未支持的消息");
  177. }
  178. msgCount++;
  179. if(msgCount % 50 == 0)
  180. {
  181. streamWriter.WriteLine(HtmlBody);
  182. HtmlBody = "";
  183. viewModel.ExportCount = msgCount.ToString();
  184. }
  185. }
  186. if(msgCount % 50 != 0)
  187. {
  188. streamWriter.WriteLine(HtmlBody);
  189. HtmlBody = "";
  190. viewModel.ExportCount = msgCount.ToString();
  191. }
  192. streamWriter.Close();
  193. streamWriter.Dispose();
  194. }
  195. private static DateTime TimeStampToDateTime(long timeStamp, bool inMilli = false)
  196. {
  197. DateTimeOffset dateTimeOffset = inMilli ? DateTimeOffset.FromUnixTimeMilliseconds(timeStamp) : DateTimeOffset.FromUnixTimeSeconds(timeStamp);
  198. return dateTimeOffset.LocalDateTime;
  199. }
  200. }
  201. }