예제 #1
0
        public JsonResult GetMessageType(string message)
        {
            try
            {
                var msgContent = string.Empty;
                var msgType    = 0;
                var imgList    = new List <string>();
                MsgServices.GetMessageType(message, out msgContent, out msgType, out imgList);

                //去掉html标签
                string strText = System.Text.RegularExpressions.Regex.Replace(msgContent, @"<[^>]*>", "");
                strText = System.Text.RegularExpressions.Regex.Replace(strText, "&[^;]+;", "");


                return(Json(
                           new
                {
                    Success = true,
                    Content = strText,
                    msgType = msgType,
                    imgList = imgList,
                },
                           JsonRequestBehavior.AllowGet));
            }
            catch (Exception ex)
            {
                return(Json(
                           new
                {
                    Success = true,
                    Content = ex.ToString()
                },
                           JsonRequestBehavior.AllowGet));
            }
        }
예제 #2
0
 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 }));
     }
 }
예제 #3
0
        /// <summary>
        /// 取最近聊天记录 这是群聊的最近聊天记录 2018年12月21日 修改
        /// </summary>
        /// <param name="groupid"></param>
        /// <returns></returns>
        public ActionResult RecentlyChat(string groupid)
        { //先从缓存中取未读的消息,如果缓存中没有未读的消息,那么再从数据库中取最近的5条聊天记录
            var selfuid = LoginUserService.ssoUserID;
            //var all =  RedisHelper.Hash_Get<List<SingleMsg>>("IMGroupMsg",groupid+selfuid);
            var all = MsgServices.GetRedisKeyValue <Msg>("IMGroupMsg", groupid + selfuid);

            if (all != null && all.Any())
            {
                /*从缓存中删除这些消息*/
                RedisHelper.Hash_Remove("IMGroupMsg", groupid + selfuid);
                return(PartialView("_RecentlyChat", all));
            }
            var listResult = new List <Msg>();
            var query      =
                unitOfWork.DIMMsg.Get(
                    p => (p.TouID == groupid && 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));
        }
예제 #4
0
 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));
     }
 }
예제 #5
0
        public IHttpActionResult GetHistoryMessage(string fromuid, string touid, string datetime, string isgroup)
        {
            try
            {
                var pageSize = 5;
                var dt       = Convert.ToDateTime(datetime);
                var list     = new List <Msg>();
                var query    = new List <IMMsg>();
                if (isgroup == "1")
                {
                    query =
                        _unitOfWork.DIMMsg.Get(
                            p => (p.TouID == touid && p.isgroup == 1 && p.CreateDate < dt && p.IsDel != 1)).OrderByDescending(p => p.ID).Take(pageSize).ToList();
                }
                else
                {
                    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(pageSize).ToList();
                }

                foreach (var msg in query)
                {
                    var singleMsg = MsgServices.ImMsg2Msg(msg);
                    list.Add(singleMsg);
                }
                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 }));
            }
        }
예제 #6
0
        /// <summary>
        /// 查询历史聊天记录中的图片文件,这个方法返回的仅仅有图片文件
        /// </summary>
        /// <param name="fromuid"></param>
        /// <param name="touid"></param>
        /// <param name="isgroup"></param>
        /// <param name="pageNo">从1开始</param>
        /// <param name="pageSize">每页默认20个</param>
        /// <returns></returns>
        public JsonResult GetImageMessage(string fromuid, string touid, bool isgroup, int pageNo, int pageSize = 20)
        {
            var list = new List <Msg>();

            try
            {
                var dt         = DateTime.Now;
                var totalCount = 0;
                if (isgroup)
                {
                    var query =
                        unitOfWork.DIMMsg.Get(
                            p => (p.Type == "1" || p.Type == "3") && p.TouID == touid && p.CreateDate <= dt && p.IsDel != 1).OrderByDescending(p => p.CreateDate);
                    if (query.Any())
                    {
                        var queryResult = query.Skip(pageNo - 1).Take(pageSize);
                        list       = MsgServices.ImMsg2Msg(queryResult.ToList());
                        totalCount = query.Count();
                    }
                }
                else
                {
                    var query =
                        unitOfWork.DIMMsg.Get(
                            p => (p.Type == "1" || p.Type == "3") && ((p.FromuID == fromuid && p.TouID == touid) || (p.FromuID == touid && p.TouID == fromuid)) && p.CreateDate <= dt && p.IsDel != 1).OrderByDescending(p => p.CreateDate);
                    if (query.Any())
                    {
                        var queryResult = query.Skip(pageNo - 1).Take(pageSize);
                        list       = MsgServices.ImMsg2Msg(queryResult.ToList());
                        totalCount = query.Count();
                    }
                }
                return(Json(
                           new
                {
                    Success = true,
                    Content = list,
                    Error = "",
                    Message = "查询成功",
                    Count = list.Count,
                    Total = totalCount
                },
                           JsonRequestBehavior.AllowGet));
            }
            catch (Exception ex)
            {
                return(Json(
                           new
                {
                    Success = false,
                    Content = "",
                    Error = ex.ToString(),
                    Message = "查询失败",
                    Count = 0,
                    Total = 0
                },
                           JsonRequestBehavior.AllowGet));
            }
        }
예제 #7
0
        public IHttpActionResult GetChatHistory(string uid, string touid, string keywords, int pageSize, int lastid, string subjectId)
        {
            try
            {
                var result = new List <Msg>()
                {
                };
                var totalCount = 0;

                if (!string.IsNullOrEmpty(keywords))
                {
                    //关键字查询,模糊查询
                }
                else
                {
                    //没有关键字查询,查询全部
                    var query = _unitOfWork.DIMMsg.GetIQueryable(p => ((p.FromuID == uid && p.TouID == touid && p.isgroup == 0) || (p.FromuID == touid && p.TouID == uid && p.isgroup == 0) || (p.TouID == touid && p.isgroup == 1)) && p.IsDel != 1 && (p.ID < lastid)).OrderByDescending(p => p.ID);
                    if (subjectId != "0")
                    {
                        query = query.Where(p => p.SubjectId == subjectId).OrderByDescending(p => p.ID);
                    }



                    if (query != null && query.Any())
                    {
                        totalCount = query.Count();
                        result     = MsgServices.ImMsg2Msg(query.Take(pageSize).ToList());
                    }
                }
                return(Json(
                           new
                {
                    Success = true,
                    Content = result,
                    Error = "",
                    Message = "查询成功",
                    Count = result.Count,
                    Total = totalCount,
                }));
            }
            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,
                }));
            }
        }
예제 #8
0
        /// <summary>
        /// Main
        /// Publisher and Subscribers
        /// Timer
        /// </summary>
        /// <param name="args"></param>
        public static void Main(string[] args)
        {
            var alarm = new Alarm()
            {
                Name = "Server crash"
            };
            var countdown     = new Countdown();
            var mailServices  = new MailServices();
            var emailServices = new EmailServices();
            var msgServices   = new MsgServices();

            countdown.EventIsUp += mailServices.OnEventIsUp;
            countdown.EventIsUp += emailServices.OnEventIsUp;
            countdown.EventIsUp += msgServices.OnEventIsUp;
            countdown.TimeCounter(alarm);
        }
예제 #9
0
        public IHttpActionResult GetWorkGroup(string message)
        {
            try
            {
                var result = new TextAndImageMessage {
                    imgList = new List <string>(), msg = string.Empty, type = 0,
                };
                var msgContent = string.Empty;
                var msgType    = 0;
                var imgList    = new List <string>();
                MsgServices.GetMessageType(message, out msgContent, out msgType, out imgList);

                //去掉html标签
                string strText = System.Text.RegularExpressions.Regex.Replace(msgContent, @"<[^>]*>", "");
                strText = System.Text.RegularExpressions.Regex.Replace(strText, "&[^;]+;", "");

                result.imgList = imgList;
                result.msg     = msgContent;
                result.type    = msgType;

                return(Json(
                           new
                {
                    Success = true,
                    Content = result,
                    Error = "",
                    Message = "查询成功",
                    Count = 0,
                    Total = 0,
                }));
            }
            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,
                }));
            }
        }
예제 #10
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 }));
     }
 }
예제 #11
0
        public IHttpActionResult GetUnreadGroupMsg(string userId, string groupid)
        {
            try
            {
                var all = MsgServices.GetRedisKeyValue <Msg>("IMGroupMsg", groupid + userId);

                var list = new List <Msg>();
                if (all.Any())
                {
                    list.AddRange(all);
                    RedisHelper.Hash_Remove("IMGroupMsg", groupid + userId);
                    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 }));
            }
            catch (Exception ex)
            {
                return(Json(new { Success = false, Content = "", Error = ex.ToString(), Message = "查询出错", Count = 0, Total = 0 }));
            }
        }
예제 #12
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));
        }
예제 #13
0
        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,
                }));
            }
        }
예제 #14
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 }));
            }
        }
예제 #15
0
 public IHttpActionResult GetSelfGroups(string userId, int pageNo = 1, int pageSize = 10)
 {
     try
     {
         var selfuid = userId;
         var query   =
             _unitOfWork.DImGroupDetail.Get(p => p.UserID == userId)
             .OrderByDescending(p => p.ID);
         var totalCount = 0;
         totalCount = query.Count();
         List <ImGroup>     list     = new List <ImGroup>();
         List <ImGroupView> listview = new List <ImGroupView>();
         if (pageNo == 0 && pageSize == 0)
         {
             //获取所有
             totalCount = query.Count();
             list.AddRange(
                 query.Select(item => Convert.ToInt32(item.GroupID))
                 .Select(id => _unitOfWork.DImGroup.Get(p => p.ID == id).FirstOrDefault()));
             foreach (var item in list)
             {
                 var imgroupview = new ImGroupView
                 {
                     ID         = item.ID.ToString(),
                     CreateDate = item.CreateDate.Value.ToString("yyyy/MM/dd HH:mm:ss"),
                     Name       = item.Name,
                     Photo      = System.Configuration.ConfigurationManager.AppSettings.Get("IMWebApiGroupPic") +
                                  item.ID, //这里作为一个接口url返还回去,目的是将base64字符串作为一个图片文件返还回去
                     Des     = item.Des,
                     Creator = item.Creator,
                     IsAdmin = item.Creator == selfuid
                 };
                 //未读消息个数
                 var all = MsgServices.GetRedisKeyValue <Msg>("IMGroupMsg", item.ID + selfuid);
                 imgroupview.UnreadMsgCount = (all == null ? 0 : all.Count);
                 listview.Add(imgroupview);
             }
             return(Json(
                        new
             {
                 Success = true,
                 Content = listview,
                 Error = "",
                 Message = "查询成功",
                 Count = listview.Count,
                 Total = totalCount,
             }));
         }
         else
         {
             var queryPage = query.Skip((pageNo - 1) * pageSize).Take(pageSize);
             list     = new List <ImGroup>();
             listview = new List <ImGroupView>();
             list.AddRange(queryPage.Select(item => Convert.ToInt32(item.GroupID)).Select(id => _unitOfWork.DImGroup.Get(p => p.ID == id).FirstOrDefault()));
             foreach (var item in list)
             {
                 var imgroupview = new ImGroupView
                 {
                     ID         = item.ID.ToString(),
                     CreateDate = item.CreateDate.Value.ToString("yyyy/MM/dd HH:mm:ss"),
                     Name       = item.Name,
                     Photo      = System.Web.HttpUtility.UrlEncode(System.Configuration.ConfigurationManager.AppSettings.Get("IMWebApiGroupPic") + item.ID, System.Text.Encoding.UTF8),//这里作为一个接口url返还回去,目的是将base64字符串作为一个图片文件返还回去
                     Des        = item.Des,
                     Creator    = item.Creator,
                     IsAdmin    = item.Creator == selfuid
                 };
                 //未读消息个数
                 var all = MsgServices.GetRedisKeyValue <Msg>("IMGroupMsg", item.ID + selfuid);
                 imgroupview.UnreadMsgCount = (all == null ? 0 : all.Count);
                 listview.Add(imgroupview);
             }
             return(Json(
                        new
             {
                 Success = true,
                 Content = listview,
                 Error = "",
                 Message = "查询成功",
                 Count = listview.Count,
                 Total = totalCount,
             }));
         }
     }
     catch (Exception ex)
     {
         return(Json(
                    new
         {
             Success = false,
             Content = "",
             Error = ex.ToString(),
             Message = "查询失败",
             Count = 0,
             Total = 0
         }));
     }
 }
예제 #16
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 }));
            }
        }
예제 #17
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 }));
            }
        }