コード例 #1
0
        public string SendPrivateMessage()
        {
            if (!string.IsNullOrWhiteSpace(Token))
            {
                if (Token != "ocean")
                {
                    return(JsonHelper.AnalysisJson <string>("凭证错误", "error", null));
                }
            }

            try
            {
                string messageText = Request["messageText"];

                if (string.IsNullOrWhiteSpace(messageText))
                {
                    return(JsonHelper.AnalysisJson <string>("消息不能为空", "error", null));
                }

                messageText = messageText.Trim();

                if (messageText == "接入")
                {
                    return(StartKfMeeting());
                }

                if (messageText == "断开")
                {
                    return(EndKfMeeting());
                }

                // 防止XSS攻击
                //messageText = Sanitizer.GetSafeHtmlFragment(messageText);
                //messageText = HttpUtility.HtmlEncode(StringHelper.StripHtml(messageText));
                messageText = HttpUtility.HtmlEncode(messageText);
                //把消息存入数据库
                //ICommunicationRepository iCommunicationRepository = new PersistedRepository();

                if (KfMeetingId == Guid.Empty)
                {
                    return(JsonHelper.AnalysisJson <string>(string.Format("本条消息发送失败[{0}]", "您还没有建立会话"), "error", null));
                }

                _kfMeetingMessageService.InsertKfMeetingMessage(KfMeetingId, 2, messageText);

                string message2 = string.Empty;

                if (new CommandManager(null).TryHandleCommand("SendPrivateMessage", new string[] { SendUserId.ToString(), ReceiveUserId.ToString(), messageText }, ref message2) && string.IsNullOrWhiteSpace(message2))
                {
                    return(JsonHelper.AnalysisJson <string>(string.Empty, "ok", null));
                }

                this.log.Warn(string.Format("消息发送失败,{0}", message2));
                return(JsonHelper.AnalysisJson <string>(string.Format("本条消息发送失败[{0}]", message2), "error", null));
            }
            catch (Exception ex)
            {
                this.log.Warn(ex.Message);
                return(JsonHelper.AnalysisJson <string>(string.Format("本条消息发送失败[{0}]", ex.Message), "error", null));
            }
        }
コード例 #2
0
ファイル: ServiceController.cs プロジェクト: CocacolaSh/temp
        public string SendPrivateMessage()
        {
            try
            {
                KfNumber kfNumber = _kfNumberService.GetById(KfNumberId);

                if (kfNumber == null)
                {
                    return(JsonHelper.AnalysisJson <string>("KfNumber对象为null", "error", null));
                }

                KfMeeting kfMeeting = _kfMeetingService.GetById(KfMeetingId);

                if (kfMeeting.IsEnd)
                {
                    return(JsonHelper.AnalysisJson <string>(string.Format("本条消息发送失败[{0}]", "本次会话已经结束"), "error", null));
                }

                string messageText = Request["messageText"];

                if (string.IsNullOrWhiteSpace(messageText))
                {
                    return(JsonHelper.AnalysisJson <string>("消息不能为空", "error", null));
                }

                messageText = messageText.Trim();
                // 防止XSS攻击
                //messageText = Sanitizer.GetSafeHtmlFragment(messageText);
                //messageText = HttpUtility.HtmlEncode(StringHelper.StripHtml(messageText));
                messageText = HttpUtility.HtmlEncode(messageText);
                //把消息存入数据库
                //ICommunicationRepository iCommunicationRepository = new PersistedRepository();
                _kfMeetingMessageService.InsertKfMeetingMessage(KfMeetingId, 1, messageText);
                //把消息发送给微信端客户
                MpUser mpUser = _mpUserService.GetById(ReceiveUserId);

                if (mpUser != null && !string.IsNullOrWhiteSpace(mpUser.OpenID))
                {
                    _mpUserService.SendMessage(mpUser.OpenID, messageText);
                }
                //把消息发给通信线程进行中转(适用于web聊天)
                string message2 = string.Empty;

                if (new CommandManager(null).TryHandleCommand("SendPrivateMessage", new string[] { KfNumberId.ToString(), ReceiveUserId.ToString(), messageText }, ref message2) && string.IsNullOrWhiteSpace(message2))
                {
                    return(JsonHelper.AnalysisJson <string>(string.Empty, "ok", null));
                }

                this.log.Warn(string.Format("消息发送失败,{0}", message2));
                return(JsonHelper.AnalysisJson <string>(string.Format("本条消息发送失败[{0}]", message2), "error", null));
            }
            catch (Exception ex)
            {
                this.log.Warn(ex.Message);
                return(JsonHelper.AnalysisJson <string>(string.Format("本条消息发送失败[{0}]", ex.Message), "error", null));
            }
        }