HtmlExport.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 Save(string path = "",bool append = false)
  23. {
  24. if (!append)
  25. {
  26. File.WriteAllText(path, HtmlBody);
  27. }
  28. else
  29. {
  30. File.AppendAllText(path, HtmlBody);
  31. HtmlBody = "";
  32. }
  33. }
  34. public void SetEnd()
  35. {
  36. HtmlBody += "</body></html>";
  37. }
  38. public void SetMsg(WXReader reader, WXSession session)
  39. {
  40. List<WXMsg> msgList = reader.GetMsgs(session.UserName);
  41. msgList.Sort((x, y) => x.CreateTime.CompareTo(y.CreateTime));
  42. foreach (var msg in msgList)
  43. {
  44. if (Session == null)
  45. throw new Exception("请初始化模版:Not Use InitTemplate");
  46. 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"));
  47. if (msg.Type == 1)
  48. HtmlBody += string.Format("<p class=\"content\">{0}</p></div>", msg.StrContent);
  49. else if(msg.Type == 3)
  50. {
  51. string? path = reader.GetImage(msg);
  52. if (path == null)
  53. {
  54. HtmlBody += string.Format("<p class=\"content\">{0}</p></div>", "图片转换出现错误或文件不存在");
  55. continue;
  56. }
  57. HtmlBody += string.Format("<p class=\"content\"><img src=\"{0}\" style=\"max-height:1000px;max-width:1000px;\"/></p></div>", path);
  58. }
  59. else if(msg.Type == 43)
  60. {
  61. string? path = reader.GetVideo(msg);
  62. if (path == null)
  63. {
  64. HtmlBody += string.Format("<p class=\"content\">{0}</p></div>", "视频不存在");
  65. continue;
  66. }
  67. 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);
  68. }
  69. else if(msg.Type == 34)
  70. {
  71. string? path = reader.GetVoice(msg);
  72. if (path == null)
  73. {
  74. HtmlBody += string.Format("<p class=\"content\">{0}</p></div>", "视频不存在");
  75. continue;
  76. }
  77. HtmlBody += string.Format("<p class=\"content\"><audio controls src=\"{0}\"></audio></p></div>", path);
  78. }
  79. else
  80. {
  81. HtmlBody += string.Format("<p class=\"content\">{0}</p></div>", "暂未支持的消息");
  82. }
  83. }
  84. }
  85. private static DateTime TimeStampToDateTime(long timeStamp, bool inMilli = false)
  86. {
  87. DateTimeOffset dateTimeOffset = inMilli ? DateTimeOffset.FromUnixTimeMilliseconds(timeStamp) : DateTimeOffset.FromUnixTimeSeconds(timeStamp);
  88. return dateTimeOffset.LocalDateTime;
  89. }
  90. }
  91. }