using K4os.Compression.LZ4.Encoders; using K4os.Compression.LZ4; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using WechatPCMsgBakTool.Interface; using WechatPCMsgBakTool.Model; using System.Xml; namespace WechatPCMsgBakTool { public class HtmlExport : IExport { private string HtmlBody = ""; private WXSession? Session = null; public void InitTemplate(WXSession session) { Session = session; HtmlBody = "溯雪微信聊天记录备份工具"; HtmlBody += string.Format("

与 {0}({1}) 的聊天记录

", Session.NickName, Session.UserName); HtmlBody += string.Format("

导出时间:{0}


", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); } public void InitTemplate(WXContact contact) { WXSession session = new WXSession(); session.NickName = contact.NickName; session.UserName = contact.UserName; InitTemplate(session); } public void Save(string path = "",bool append = false) { if (!append) { File.WriteAllText(path, HtmlBody); } else { File.AppendAllText(path, HtmlBody); HtmlBody = ""; } } public void SetEnd() { HtmlBody += ""; } public void SetMsg(WXUserReader reader,WXContact contact) { if (Session == null) throw new Exception("请初始化模版:Not Use InitTemplate"); List? msgList = reader.GetWXMsgs(contact.UserName); if (msgList == null) throw new Exception("获取消息失败,请确认数据库读取正常"); msgList.Sort((x, y) => x.CreateTime.CompareTo(y.CreateTime)); foreach (var msg in msgList) { HtmlBody += string.Format("

{0} {1}

", msg.IsSender ? "我" : Session.NickName, TimeStampToDateTime(msg.CreateTime).ToString("yyyy-MM-dd HH:mm:ss")); if (msg.Type == 1) HtmlBody += string.Format("

{0}

", msg.StrContent); else if (msg.Type == 3) { string? path = reader.GetAttachment(WXMsgType.Image, msg); if (path == null) { HtmlBody += string.Format("

{0}

", "图片转换出现错误或文件不存在"); continue; } HtmlBody += string.Format("

", path); } else if (msg.Type == 43) { string? path = reader.GetAttachment(WXMsgType.Video, msg); if (path == null) { HtmlBody += string.Format("

{0}

", "视频不存在"); continue; } HtmlBody += string.Format("

", path); } else if(msg.Type== 49) { using (var decoder = LZ4Decoder.Create(true, 64)) { byte[] target = new byte[10240]; int res = 0; if(msg.CompressContent != null) res = LZ4Codec.Decode(msg.CompressContent, 0, msg.CompressContent.Length, target, 0, target.Length); byte[] data = target.Skip(0).Take(res).ToArray(); string xml = Encoding.UTF8.GetString(data); if (!string.IsNullOrEmpty(xml)) { xml = xml.Replace("\n", ""); XmlDocument xmlObj = new XmlDocument(); xmlObj.LoadXml(xml); if(xmlObj.DocumentElement != null) { string title = ""; string appName = ""; string url = ""; XmlNodeList? findNode = xmlObj.DocumentElement.SelectNodes("/msg/appmsg/title"); if(findNode != null) { if(findNode.Count > 0) { title = findNode[0]!.InnerText; } } findNode = xmlObj.DocumentElement.SelectNodes("/msg/appmsg/sourcedisplayname"); if (findNode != null) { if (findNode.Count > 0) { appName = findNode[0]!.InnerText; } } findNode = xmlObj.DocumentElement.SelectNodes("/msg/appmsg/url"); if (findNode != null) { if (findNode.Count > 0) { url = findNode[0]!.InnerText; } } HtmlBody += string.Format("

{0}|{1}

点击访问

", appName, title, url); } } } } else if (msg.Type == 34) { string? path = reader.GetAttachment(WXMsgType.Audio, msg); if (path == null) { HtmlBody += string.Format("

{0}

", "语音不存在"); continue; } HtmlBody += string.Format("

", path); } else { HtmlBody += string.Format("

{0}

", "暂未支持的消息"); } } } private static DateTime TimeStampToDateTime(long timeStamp, bool inMilli = false) { DateTimeOffset dateTimeOffset = inMilli ? DateTimeOffset.FromUnixTimeMilliseconds(timeStamp) : DateTimeOffset.FromUnixTimeSeconds(timeStamp); return dateTimeOffset.LocalDateTime; } } }