using System.Net; using System.Text; using System.Collections.Generic; using System; using System.IO; using ProtoCSStruct; using Sog; using LitJson; using Operation; using SimpleHttpServer; namespace Operation { [MenuMapping(refc = typeof(PlayerInfoOp))] public static class ChatLog { //ChatServerUtils.GetApp().ServerID.ToString() static string chatLogFilePath = ""; private static Dictionary> cache; [RequestMapping("重新加载聊天文件", PermissionCode.CHAT_RELOAD_CHAT_FILE)] public static int OnReloadChatFile(string httpApiCmd, JsonData jsondata, HttpResponse rsp, HttpRequest request, HttpQueryParams query, uint httpContextId) { var worldId = query.GetValue("worldId").Tolong(); Reload(worldId); rsp.IsWaitFor = true; return 0; } private static void Reload(long chatId) { try { chatLogFilePath = "./ChannalChatCacheData_" + chatId + ".fdb"; if (!File.Exists(chatLogFilePath)) { chatId = OperationServerUtils.GetOperationServerData().m_operationWebsite.ChatId; chatLogFilePath = "./ChannalChatCacheData_" + chatId + ".fdb"; } TraceLog.Trace("ChatLog.Reload load file path={0}", chatLogFilePath); if (cache == null) { cache = new Dictionary>(); } if (!File.Exists(chatLogFilePath)) { TraceLog.Trace("ChatLog.Reload load file not found path={0}", chatLogFilePath); return; } cache.Clear(); FileStream fileStream = File.OpenRead(chatLogFilePath); BinaryReader sw = new BinaryReader(fileStream); long length = fileStream.Length; long pos = 0; while (pos < length) { int size = sw.ReadInt32(); //读长度 byte[] data = sw.ReadBytes(size); //读数据 pos += (sizeof(Int32) + size); if (pos <= length) { CodedInputStream input = new CodedInputStream(data); CSChatRes chatRes = new CSChatRes(); chatRes.ReadFrom(input); if (cache.ContainsKey((int)chatRes.ChatChannelType)) { List list = cache[(int)chatRes.ChatChannelType]; list.Add(chatRes); } else { List list = new List(); list.Add(chatRes); cache[(int)chatRes.ChatChannelType] = list; } } } sw.Dispose(); fileStream.Dispose(); } catch (Exception ex) { TraceLog.Error("ChatCacheOp.LoadCacheFromFile parse message failed! {0}", chatLogFilePath); TraceLog.Exception(ex); } TraceLog.Trace("ChatCacheOp.LoadCacheFromFile load file {0} success time {1}", chatLogFilePath, AppTime.GetNowSysMs()); } [RequestMapping("聊天记录查询", PermissionCode.CHAT_LIST_INFO, true)] public static int OnGetChatListHttpReq(string httpApiCmd, JsonData jsondata, HttpResponse rsp, HttpRequest request, HttpQueryParams query, uint httpContextId) { int channel = query.GetValue("channel").Toint32(0) == 0 ? 0 : query.GetValue("channel").Toint32(0); int page = query.GetValue("page").Toint32(0) == 0 ? 0 : query.GetValue("page").Toint32(0); int limit = query.GetValue("limit").Toint32(0) == 0 ? 0 : query.GetValue("limit").Toint32(0); if (cache == null) { var chatId = OperationServerUtils.GetOperationServerData().m_operationWebsite.ChatId; Reload(chatId); } if (cache == null) { jsondata["msg"] = "load message failed"; jsondata["data"] = null; return 0; } if (channel >= 0 && !cache.ContainsKey(channel)) { jsondata["msg"] = "channel not found"; jsondata["data"] = null; return 0; } List list = new List(); if (channel < 0) { foreach (var _list in cache) { _list.Value.ForEach(item => { list.Add(item); }); } } else { list = cache[channel]; } JsonData chatList = new JsonData(); for (int i = 0; i < list.Count; i++) { CSChatRes chat = list[i]; if (int.TryParse(chat.Message.ToString(), out int type)) { continue; } if (i < page * limit) { continue; } JsonData chatPo = new JsonData(); chatPo["ChannelParam"] = chat.ChannelParam; chatPo["Uid"] = chat.Uid; chatPo["Nick"] = chat.Nick.ToString(); chatPo["Icon"] = chat.Icon.ToString(); chatPo["Gender"] = chat.Gender; chatPo["Message"] = chat.Message.ToString(); chatPo["TargetUid"] = chat.TargetUid; chatPo["Ret"] = chat.Ret; DateTime dateTime = AppTime.ConvertUnixTimeToDateTime(chat.SendTime); chatPo["SendTime"] = dateTime.ToString("yyyy-MM-dd HH:mm:ss"); chatPo["RealmId"] = chat.RealmId; chatPo["MessageType"] = chat.MessageType.ToString(); chatPo["RealmName"] = chat.RealmName.ToString(); chatPo["EnableMsgTime"] = chat.EnableMsgTime; chatPo["IconFrameId"] = chat.IconFrameId; chatList.Add(chatPo); limit--; if (limit < 0) { break; } } jsondata["count"] = list.Count; jsondata["data"] = chatList; jsondata["ret"] = 0; return 0; } [RequestMapping("下载聊天文件", PermissionCode.CHAT_DOWNLOAD_FILE)] public static int OnDownloadChatListCsvHttpReq(string httpApiCmd, JsonData jsondata, HttpResponse rsp, HttpRequest request, HttpQueryParams query, uint httpContextId) { string filePath = "./chat_list.csv"; if (File.Exists(filePath)) { File.Delete(filePath); } if (cache == null) { var chatId = OperationServerUtils.GetOperationServerData().m_operationWebsite.ChatId; Reload(chatId); } if (cache == null || cache.Count == 0) { return 0; } Encoding shiftJis = Encoding.GetEncoding("shift_jis"); using (StreamWriter writer = new StreamWriter(File.Open(filePath, FileMode.Create), Encoding.UTF8)) { // 写入CSV内容 writer.WriteLine( "ChannelParam,Uid,Nick,Icon,Gender,Message,TargetUid,Ret,SendTime,RealmId,MessageType,RealmName,EnableMsgTime,IconFrameId"); foreach (var one in cache) { var list = one.Value; for (int j = 0; j < list.Count; j++) { var chat = list[j]; List chatPo = new List(); chatPo.Add(chat.ChannelParam.ToString()); chatPo.Add(chat.Uid.ToString()); chatPo.Add(chat.Nick.ToString()); chatPo.Add(chat.Icon.ToString()); chatPo.Add(chat.Gender.ToString()); chatPo.Add(chat.Message.ToString()); chatPo.Add(chat.TargetUid.ToString()); chatPo.Add(chat.Ret.ToString()); DateTime dateTime = AppTime.ConvertUnixTimeToDateTime(chat.SendTime); chatPo.Add(dateTime.ToString("yyyy-MM-dd HH:mm:ss")); chatPo.Add(chat.RealmId.ToString()); chatPo.Add(chat.MessageType.ToString()); chatPo.Add(chat.RealmName.ToString()); chatPo.Add(chat.EnableMsgTime.ToString()); chatPo.Add(chat.IconFrameId.ToString()); string data = string.Join(",", chatPo); writer.WriteLine(data); } } writer.Close(); writer.Dispose(); } string fileName = Path.GetFileName(filePath); var bytes = Convert.ToBase64String(File.ReadAllBytes(filePath)); jsondata["data"] = bytes; jsondata["ret"] = 0; // // 创建一个文件流 // FileStream fileStream = new FileStream(filePath, FileMode.Open); // rsp.Headers["Content-Type"] = "application/octet-stream"; // rsp.Headers["Content-Disposition"] = $"attachment; filename=chat_list.csv"; // rsp.Headers["Content-Length"] = fileStream.Length.ToString(); // rsp.SetContent(fileStream.ToString()); return 0; } } }