HtmlExport.cs 8.3 KB

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