public ActionResult AddMsgToRedisNew(string model) { try { var msg = new Msg(); //收到的消息 var selfuid = LoginUserService.ssoUserID; msg = JsonConvert.DeserializeObject <Msg>(model); if (msg.isgroup == 0) { //单人消息 MsgServices.ResetRedisKeyValue <Msg>("IMMsg", selfuid, msg); return(Json(new { r = true }, JsonRequestBehavior.AllowGet)); } else if (msg.isgroup == 1) { //群组消息 MsgServices.ResetRedisKeyValue <Msg>("IMGroupMsg", msg.touid + selfuid, msg); var unreadgroupmsg = RedisHelper.Hash_Get <List <String> >("IMUnreadGroupMsg", selfuid) ?? new List <String>(); if (!unreadgroupmsg.Contains(msg.touid)) { unreadgroupmsg.Add(msg.touid); RedisHelper.Hash_Remove("IMUnreadGroupMsg", selfuid); RedisHelper.Hash_Set <List <String> >("IMUnreadGroupMsg", selfuid, unreadgroupmsg); } return(Json(new { r = true }, JsonRequestBehavior.AllowGet)); } return(Json(new { r = false, error = "isgroup数据异常" + msg.isgroup }, JsonRequestBehavior.AllowGet)); } catch (Exception ex) { return(Json(new { r = false, error = ex.ToString() }, JsonRequestBehavior.AllowGet)); } }
public IHttpActionResult GetUnreadPersonalMsg(string userId, string touid) { try { var all = MsgServices.GetRedisKeyValue <Msg>("IMMsg", userId); var list = new List <Msg>(); if (all != null && all.Any()) { var queryUnreadMsg = all.Where(p => p.fromuid == touid); var unreadMsg = queryUnreadMsg as Msg[] ?? queryUnreadMsg.ToArray(); if (unreadMsg.Any()) { list.AddRange(unreadMsg); /*从缓存中删除这些消息*/ foreach (var msg in unreadMsg) { all.Remove(msg); } RedisHelper.Hash_Remove("IMMsg", userId); MsgServices.ResetRedisKeyValue("IMMsg", userId, all); return(Json(new { Success = true, Content = list, Error = "", Message = "查询成功", Count = 0, Total = 0 })); } return(Json(new { Success = false, Content = list, Error = "", Message = "未找到未读消息", Count = 0, Total = 0 })); } return(Json(new { Success = false, Content = list, Error = "", Message = "未找到未读消息", Count = 0, Total = 0 })); } catch (Exception ex) { return(Json(new { Success = false, Content = "", Error = ex.ToString(), Message = "查询失败", Count = 0, Total = 0 })); } }
public IHttpActionResult GetUnreadMessage(string fromuid, string touid, string isgroup) { try { var list = new List <Msg>(); if (isgroup == "1") { var all = MsgServices.GetRedisKeyValue <Msg>("IMGroupMsg", touid + fromuid); if (all != null && all.Any()) { list = all; /*从缓存中删除这些消息*/ Task.Run(() => { RedisHelper.Hash_Remove("IMGroupMsg", touid + fromuid); }); } } else { var all = MsgServices.GetRedisKeyValue <Msg>("IMMsg", fromuid); if (all != null && all.Any()) { list = all.Where(p => p.fromuid == touid).ToList(); } if (list.Any()) { /*从缓存中删除这些消息*/ foreach (var msg in list) { all.Remove(msg); } Task.Run(() => { RedisHelper.Hash_Remove("IMMsg", fromuid); MsgServices.ResetRedisKeyValue("IMMsg", fromuid, all); }); } } return(Json(new { Success = true, Content = list, Error = "", Message = "", Count = list.Count, Total = list.Count })); } catch (Exception ex) { return(Json(new { Success = false, Content = "", Error = ex.ToString(), Message = "查询失败", Count = 0, Total = 0 })); } }
/// <summary> /// 取最近聊天记录 5条 显示在聊天窗口中 /// 6月13日补充需求:如果有未读的聊天消息 则取所有未读的聊天消息 如果没有未读的聊天消息 则取最近5条聊天消息 /// 未读的聊天消息从缓存中取 取到之后将其删除 /// </summary> /// <param name="touid"></param> /// <returns></returns> public ActionResult RecentlyChat(string touid) { //先从缓存中取未读的消息,如果缓存中没有未读的消息,那么再从数据库中取最近的5条聊天记录 var selfuid = LoginUserService.ssoUserID; var all = MsgServices.GetRedisKeyValue <Msg>("IMMsg", selfuid); if (all != null && all.Any()) { var queryUnreadMsg = all.Where(p => p.fromuid == touid); var unreadMsg = queryUnreadMsg as Msg[] ?? queryUnreadMsg.ToArray(); if (unreadMsg.Any()) { /*从缓存中删除这些消息*/ foreach (var msg in unreadMsg) { all.Remove(msg); } RedisHelper.Hash_Remove("IMMsg", selfuid); MsgServices.ResetRedisKeyValue <Msg>("IMMsg", selfuid, all); return(PartialView("_RecentlyChat", unreadMsg)); } } var listResult = new List <Msg>(); var query = unitOfWork.DIMMsg.Get( p => ((p.FromuID == selfuid && p.TouID == touid) || (p.FromuID == touid && p.TouID == selfuid)) && p.IsDel != 1).OrderByDescending(p => p.CreateDate).Take(5).OrderBy(p => p.CreateDate); foreach (var msg in query) { var singleMsg = MsgServices.ImMsg2Msg(msg); listResult.Add(singleMsg); } return(PartialView("_RecentlyChat", listResult)); }
public IHttpActionResult RecentlyChat(string uid, string touid, int isgroup) { try { //先从缓存中取未读的消息,如果有缓存消息,则返回所有的缓存消息, //如果缓存中没有未读的消息,那么再从数据库中取最近的10条聊天记录 var result = new List <Msg>() { }; if (isgroup == 1) { //------群组消息--start------ var all = MsgServices.GetRedisKeyValue <Msg>("IMGroupMsg", touid + uid); if (all != null && all.Any()) { Task.Run(() => { //异步执行 //从缓存中删除这些消息 RedisHelper.Hash_Remove("IMGroupMsg", touid + uid); }); //result = all; } else { var query = _unitOfWork.DIMMsg.Get( p => (p.TouID == touid && p.IsDel != 1)).OrderByDescending(p => p.ID); if (query != null && query.Any()) { result = MsgServices.ImMsg2Msg(query.Take(10).ToList()); } } //------群组消息--end------ } else { //------单人聊天消息--start------ var all = MsgServices.GetRedisKeyValue <Msg>("IMMsg", uid); if (all != null && all.Any()) { var queryUnreadMsg = all.Where(p => p.fromuid == touid); if (queryUnreadMsg != null && queryUnreadMsg.Any()) { result = queryUnreadMsg.ToList(); } } if (result.Any()) { //1.从缓存中取 Task.Run(() => { //异步执行 //从缓存中删除这些消息 foreach (var msg in result) { all.Remove(msg); } RedisHelper.Hash_Remove("IMMsg", uid); MsgServices.ResetRedisKeyValue <Msg>("IMMsg", uid, all); }); } else { //2.从数据库中取 var query = _unitOfWork.DIMMsg.Get( p => ((p.FromuID == uid && p.TouID == touid) || (p.FromuID == touid && p.TouID == uid)) && p.IsDel != 1).OrderByDescending(p => p.ID); if (query != null && query.Any()) { result = MsgServices.ImMsg2Msg(query.Take(10).OrderBy(p => p.ID).ToList()); } } //------单人聊天消息--end------ } return(Json( new { Success = true, Content = result, Error = "", Message = "查询成功", Count = result.Count, Total = result.Count })); } catch (Exception ex) { LoggerHelper.Error(System.Reflection.MethodBase.GetCurrentMethod().Name, ex); return(Json( new { Success = false, Content = "", Error = ex.Message, Message = "查询失败", Count = 0, Total = 0, })); } }
public IHttpActionResult GetRecentlyChatMessage(string fromuid, string touid, int isgroup, string datetime) { try { var listResult = new List <Msg>(); if (string.IsNullOrEmpty(datetime)) { if (isgroup == 1) { //先从缓存中取未读的消息,如果缓存中没有未读的消息,那么再从数据库中取最近的10条聊天记录 var all = MsgServices.GetRedisKeyValue <Msg>("IMGroupMsg", touid + fromuid); if (all != null && all.Any()) { /*从缓存中删除这些消息*/ RedisHelper.Hash_Remove("IMGroupMsg", touid + fromuid); listResult.AddRange(all); } else { var query = _unitOfWork.DIMMsg.Get( p => (p.TouID == touid && p.isgroup == 1 && p.IsDel != 1)).OrderByDescending(p => p.ID).Take(10); foreach (var msg in query) { var singleMsg = MsgServices.ImMsg2Msg(msg); listResult.Add(singleMsg); } } } else { // 先从缓存中取未读的消息,如果缓存中没有未读的消息,那么再从数据库中取最近的10条聊天记录 var all = MsgServices.GetRedisKeyValue <Msg>("IMMsg", fromuid); if (all != null && all.Any()) { var queryUnreadMsg = all.Where(p => p.fromuid == touid); var unreadMsg = queryUnreadMsg as Msg[] ?? queryUnreadMsg.ToArray(); if (unreadMsg.Any()) { /*从缓存中删除这些消息*/ foreach (var msg in unreadMsg) { all.Remove(msg); } RedisHelper.Hash_Remove("IMMsg", fromuid); MsgServices.ResetRedisKeyValue <Msg>("IMMsg", fromuid, all); } listResult.AddRange(unreadMsg.ToList()); } else { var query = _unitOfWork.DIMMsg.Get( p => ((p.FromuID == fromuid && p.TouID == touid) || (p.FromuID == touid && p.TouID == fromuid)) && p.IsDel != 1).OrderByDescending(p => p.ID).Take(10); foreach (var msg in query) { var singleMsg = MsgServices.ImMsg2Msg(msg); listResult.Add(singleMsg); } } } } else { var dt = Convert.ToDateTime(datetime); if (isgroup == 1) { var query = _unitOfWork.DIMMsg.Get( p => (p.TouID == touid && p.isgroup == 1 && p.CreateDate < dt && p.IsDel != 1)).OrderByDescending(p => p.ID).Take(10); foreach (var msg in query) { var singleMsg = MsgServices.ImMsg2Msg(msg); listResult.Add(singleMsg); } } else { var query = _unitOfWork.DIMMsg.Get( p => ((p.FromuID == fromuid && p.TouID == touid) || (p.FromuID == touid && p.TouID == fromuid)) && p.CreateDate < dt && p.IsDel != 1).OrderByDescending(p => p.ID).Take(10); foreach (var msg in query) { var singleMsg = MsgServices.ImMsg2Msg(msg); listResult.Add(singleMsg); } } } //listResult = listResult.OrderBy(p => Convert.ToInt32(p.id0)).ToList(); return(Json(new { Success = true, Content = listResult, Error = "", Message = "", Count = listResult.Count, Total = listResult.Count })); } catch (Exception ex) { return(Json(new { Success = false, Content = "", Error = ex.ToString(), Message = "查询失败", Count = 0, Total = 0 })); } }
public async Task <IHttpActionResult> DelMsg() { string uid = HttpContext.Current.Request.Form["uid"]; string msgid = HttpContext.Current.Request.Form["msgid"]; string touid = HttpContext.Current.Request.Form["touid"]; string isgroup = HttpContext.Current.Request.Form["isgroup"]; if (string.IsNullOrEmpty(uid)) { return(Json(new { Success = false, Content = "", Error = "用户id为空", Message = "操作失败", Count = 0, Total = 0 })); } if (string.IsNullOrEmpty(msgid)) { return(Json(new { Success = false, Content = "", Error = "消息id为空", Message = "操作失败", Count = 0, Total = 0 })); } if (string.IsNullOrEmpty(touid)) { return(Json(new { Success = false, Content = "", Error = "接收人id为空", Message = "操作失败", Count = 0, Total = 0 })); } if (string.IsNullOrEmpty(isgroup)) { return(Json(new { Success = false, Content = "", Error = "isgroup为空", Message = "操作失败", Count = 0, Total = 0 })); } try { var msgidInt = Convert.ToInt32(msgid); var query = _unitOfWork.DIMMsg.Get(p => p.ID == msgidInt && p.CreateUser == uid && p.TouID == touid && p.IsDel != 1).FirstOrDefault(); if (query != null) { if (query.Type == "2") { //如果是图片消息,则执行软删除 query.IsDel = 1; //更新下载链接 query.FileUrl = ConfigHelper.GetConfigString("DownloadDfile"); _unitOfWork.DIMMsg.Update(query); } else { //如果是其他类型消息,则直接删除 _unitOfWork.DIMMsg.Delete(query); } var result = _unitOfWork.Save(); if (result.ResultType == OperationResultType.Success) { Task.Run(() => { //查询缓存中的消息 if (isgroup == "1") { var touids = new List <string>(); touids = touid.Length == 36 ? HttpRequestService.GetWorkGroupMembers(touid) : HttpRequestService.GetSelfGroupMembers(touid); foreach (var id in touids) { var msgUnread = MsgServices.GetRedisKeyValue <Msg>("IMGroupMsg", touid + id); if (msgUnread != null && msgUnread.Any()) { var newmsgUnread = msgUnread.Where(p => p.id0 != msgid).ToList(); RedisHelper.Hash_Remove("IMGroupMsg", touid + id); if (newmsgUnread.Any()) { MsgServices.ResetRedisKeyValue <Msg>("IMGroupMsg", touid + id, newmsgUnread); } else { RedisHelper.Hash_Remove("IMGroupMsg", touid + id); var groups = RedisHelper.Hash_Get <List <String> >("IMUnreadGroupMsg", id); if (groups != null && groups.Any() && groups.Contains(touid)) { groups.Remove(touid); //重新存储未读消息群组的Redis变量 RedisHelper.Hash_Remove("IMUnreadGroupMsg", id); RedisHelper.Hash_Set <List <String> >("IMUnreadGroupMsg", id, groups); } } } } } else if (isgroup == "0") { var msgUnread = MsgServices.GetRedisKeyValue <Msg>("IMMsg", touid); if (msgUnread != null && msgUnread.Any()) { var newmsgUnread = msgUnread.Where(p => p.id0 != msgid).ToList(); RedisHelper.Hash_Remove("IMMsg", touid); if (newmsgUnread.Any()) { MsgServices.ResetRedisKeyValue <Msg>("IMMsg", touid, newmsgUnread); } else { RedisHelper.Hash_Remove("IMMsg", touid); } } } }); return(Json(new { Success = true, Content = "", Error = "", Message = "操作成功", Count = 0, Total = 0 })); } return(Json(new { Success = false, Content = "", Error = result.Message, Message = "操作失败", Count = 0, Total = 0 })); } return(Json(new { Success = false, Content = "", Error = "未查询到对应的消息", Message = "操作失败", Count = 0, Total = 0 })); } catch (Exception ex) { return(Json(new { Success = false, Content = "", Error = ex.ToString(), Message = "操作失败", Count = 0, Total = 0 })); } }
public IHttpActionResult ClearUnreadMsg() { string uid = HttpContext.Current.Request.Form["uid"]; string touid = HttpContext.Current.Request.Form["touid"]; string isgroup = HttpContext.Current.Request.Form["isgroup"]; if (string.IsNullOrEmpty(uid)) { return(Json(new { Success = false, Content = "", Error = "uid", Message = "操作失败", Count = 0, Total = 0 })); } if (string.IsNullOrEmpty(touid)) { return(Json(new { Success = false, Content = "", Error = "touid为空", Message = "操作失败", Count = 0, Total = 0 })); } if (string.IsNullOrEmpty(isgroup)) { return(Json(new { Success = false, Content = "", Error = "isgroup为空", Message = "操作失败", Count = 0, Total = 0 })); } if (isgroup == "1") { var unreadgroupmsg = RedisHelper.Hash_Get <List <String> >("IMUnreadGroupMsg", uid) ?? new List <String>(); if (unreadgroupmsg.Any() && unreadgroupmsg.Contains(touid)) { unreadgroupmsg.Remove(touid); RedisHelper.Hash_Remove("IMUnreadGroupMsg", uid); RedisHelper.Hash_Set <List <String> >("IMUnreadGroupMsg", uid, unreadgroupmsg); } var unreadmsg = MsgServices.GetRedisKeyValue <Msg>("IMGroupMsg", touid + uid); if (unreadmsg != null && unreadmsg.Any()) { RedisHelper.Hash_Remove("IMGroupMsg", touid + uid); } return(Json(new { Success = true, Content = "", Error = "", Message = "操作成功", Count = 0, Total = 0 })); } else if (isgroup == "0") { var all = MsgServices.GetRedisKeyValue <Msg>("IMMsg", uid); if (all != null && all.Any()) { var queryUnreadMsg = all.Where(p => p.fromuid == touid); var unreadMsg = queryUnreadMsg as Msg[] ?? queryUnreadMsg.ToArray(); if (unreadMsg.Any()) { foreach (var msg in unreadMsg) { all.Remove(msg); } RedisHelper.Hash_Remove("IMMsg", uid); MsgServices.ResetRedisKeyValue <Msg>("IMMsg", uid, all); } return(Json(new { Success = true, Content = "", Error = "", Message = "操作成功", Count = 0, Total = 0 })); } return(Json(new { Success = true, Content = "没有未读消息", Error = "", Message = "操作成功", Count = 0, Total = 0 })); } else { return(Json(new { Success = false, Content = "", Error = "isgroup参数不对", Message = "操作失败", Count = 0, Total = 0 })); } }