Ejemplo n.º 1
0
        /// <summary>
        /// 发送消息逻辑, 如果用户不在线就记录未读
        /// </summary>
        /// <param name="msg"></param>
        public async Task SendMessage(TextChatMessageViewModel msg)
        {
            var userOnline = socialMsgManager.HasOnline(msg.ToId.ToString());

            var chatRelationShip = await chatLogDataAccessor.OneAsync <ChatLog>(x =>
                                                                                x.ChatCollection.Any(c => c.UserId == msg.FromId) &&
                                                                                x.ChatCollection.Any(c => c.UserId == msg.ToId), "ChatCollection", "ChatMessageList");

            if (chatRelationShip == null)
            {
                chatRelationShip = new ChatLog
                {
                    Id             = Guid.NewGuid(),
                    ChatCollection = new List <ChatCollection>
                    {
                        new ChatCollection {
                            Id         = Guid.NewGuid(),
                            CreateTime = DateTime.Now,
                            UserId     = msg.FromId
                        },
                        new ChatCollection {
                            Id         = Guid.NewGuid(),
                            CreateTime = DateTime.Now,
                            UserId     = msg.ToId
                        }
                    }
                };

                await chatLogDataAccessor.Add(chatRelationShip);
            }

            var msgModel = new ChatMessage();

            msgModel.MessageContent = msg.Content;
            msgModel.TimeStamp      = TimeStamp.Get();
            msgModel.MsgType        = MessageType.Text;
            msgModel.ChageLog       = chatRelationShip;
            msgModel.FromUserId     = msg.FromId;
            msgModel.ToUserId       = msg.ToId;
            msgModel.CreateTime     = DateTime.Now;
            if (userOnline)
            {
                msgModel.IsReaded = true;
            }
            else
            {
                msgModel.IsReaded = false;
            }
            await chatLogDataAccessor.Add(msgModel);

            //当前
            var jsonSetting = new JsonSerializerSettings();

            jsonSetting.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
            var contentJson = JsonConvert.SerializeObject(msg, jsonSetting);

            socialMsgManager.SendMssage(msg.ToId.ToString(), msg.Content);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 因为前端暴露的是accessToken ,
        /// 所以fromid 实际为accessToken, 但ToId 为用户Id
        /// </summary>
        /// <param name="chatModel"></param>
        /// <returns></returns>
        public async Task <IActionResult> SendTxtMsg([FromBody] TextChatMessageViewModel chatModel)
        {
            //var userIdDocument = await distributedCache.GetValue<string>(chatModel.FromId.ToString());
            //chatModel.FromId = Guid.Parse(userIdDocument.Value);
            var apiMsg = await ApiMessage.Wrap(async() =>
            {
                await chatBusiness.SendMessage(chatModel);
            });

            return(Json(apiMsg));
        }