Exemplo n.º 1
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));
        }
Exemplo n.º 2
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 }));
            }
        }
Exemplo n.º 3
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));
            }
        }
Exemplo n.º 4
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,
                }));
            }
        }
Exemplo n.º 5
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));
        }
Exemplo n.º 6
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,
                }));
            }
        }
Exemplo n.º 7
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 }));
            }
        }