Exemplo n.º 1
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 }));
            }
        }