HtmlExport.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using WechatPCMsgBakTool.Interface;
  8. using WechatPCMsgBakTool.Model;
  9. namespace WechatPCMsgBakTool
  10. {
  11. public class HtmlExport : IExport
  12. {
  13. private string HtmlBody = "";
  14. private WXSession? Session = null;
  15. public void InitTemplate(WXSession session)
  16. {
  17. Session = session;
  18. HtmlBody = "<!DOCTYPE html><html><head><meta charset=\"utf-8\"><title>溯雪微信聊天记录备份工具</title><style>p{margin:0px;}.msg{padding-bottom:10px;}.nickname{font-size:10px;}.content{font-size:14px;}</style></head><body>";
  19. HtmlBody += string.Format("<div class=\"msg\"><p class=\"nickname\"><b>与 {0}({1}) 的聊天记录</b></p>", Session.NickName, Session.UserName);
  20. HtmlBody += string.Format("<div class=\"msg\"><p class=\"nickname\"><b>导出时间:{0}</b></p><hr/>", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
  21. }
  22. public void InitTemplate(WXContact contact)
  23. {
  24. WXSession session = new WXSession();
  25. session.NickName = contact.NickName;
  26. session.UserName = contact.UserName;
  27. InitTemplate(session);
  28. }
  29. public void Save(string path = "",bool append = false)
  30. {
  31. if (!append)
  32. {
  33. File.WriteAllText(path, HtmlBody);
  34. }
  35. else
  36. {
  37. File.AppendAllText(path, HtmlBody);
  38. HtmlBody = "";
  39. }
  40. }
  41. public void SetEnd()
  42. {
  43. HtmlBody += "</body></html>";
  44. }
  45. public void SetMsg(WXUserReader reader,WXContact contact)
  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. foreach (var msg in msgList)
  54. {
  55. HtmlBody += string.Format("<div class=\"msg\"><p class=\"nickname\">{0} <span style=\"padding-left:10px;\">{1}</span></p>", msg.IsSender ? "我" : Session.NickName, TimeStampToDateTime(msg.CreateTime).ToString("yyyy-MM-dd HH:mm:ss"));
  56. if (msg.Type == 1)
  57. HtmlBody += string.Format("<p class=\"content\">{0}</p></div>", msg.StrContent);
  58. else if (msg.Type == 3)
  59. {
  60. string? path = reader.GetAttachment(WXMsgType.Image, msg);
  61. if (path == null)
  62. {
  63. HtmlBody += string.Format("<p class=\"content\">{0}</p></div>", "图片转换出现错误或文件不存在");
  64. continue;
  65. }
  66. HtmlBody += string.Format("<p class=\"content\"><img src=\"{0}\" style=\"max-height:1000px;max-width:1000px;\"/></p></div>", path);
  67. }
  68. else if (msg.Type == 43)
  69. {
  70. string? path = reader.GetAttachment(WXMsgType.Video, msg);
  71. if (path == null)
  72. {
  73. HtmlBody += string.Format("<p class=\"content\">{0}</p></div>", "视频不存在");
  74. continue;
  75. }
  76. 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);
  77. }
  78. else if (msg.Type == 34)
  79. {
  80. string? path = reader.GetAttachment(WXMsgType.Audio, msg);
  81. if (path == null)
  82. {
  83. HtmlBody += string.Format("<p class=\"content\">{0}</p></div>", "语音不存在");
  84. continue;
  85. }
  86. HtmlBody += string.Format("<p class=\"content\"><audio controls src=\"{0}\"></audio></p></div>", path);
  87. }
  88. else
  89. {
  90. HtmlBody += string.Format("<p class=\"content\">{0}</p></div>", "暂未支持的消息");
  91. }
  92. }
  93. }
  94. private static DateTime TimeStampToDateTime(long timeStamp, bool inMilli = false)
  95. {
  96. DateTimeOffset dateTimeOffset = inMilli ? DateTimeOffset.FromUnixTimeMilliseconds(timeStamp) : DateTimeOffset.FromUnixTimeSeconds(timeStamp);
  97. return dateTimeOffset.LocalDateTime;
  98. }
  99. }
  100. }