/// <summary>
        /// 处理请求消息
        /// </summary>
        /// <returns>处理结果</returns>
        public ResponseMessage HandlerRequestMessage()
        {
            Dictionary <string, object> bugobj = new Dictionary <string, object>();
            ResponseTextMessage         responseTextMessage = null;
            var request = new RequestQREventMessage(this.message.Element);

            try
            {
                if (request != null)
                {
                    // 处理扫描带参数的二维码消息处理器
                    //  bugobj.Add("处理扫描带参数的二维码消息处理器", request);
                }
                responseTextMessage = new ResponseTextMessage(request)
                {
                    Content = subScribeMsg,
                };
            }
            catch (Exception ex)
            {
                TxtLogHelper.WriteLog_Day(new TxtLogInfoObject()
                {
                    LogType    = EnumLogType.Error,
                    LogMessage = string.Format("错误信息{0};堆栈信息{1}", ex.Message, ex.StackTrace),
                });
            }
            finally
            {
                TxtLogHelper.WriteDebugLog(bugobj);
            }

            return(responseTextMessage);
        }
Beispiel #2
0
        private void Run()
        {
            m_mailService = EmailSenderServiceFactory.CreateLocalEmailSenderService();
            while (true)
            {
                EmailMessageBody emailMsgBody;
                if (m_queue.TryDequeue(out emailMsgBody))
                {
                    try
                    {
                        m_mailService.Send(emailMsgBody);
                    }
                    catch (Exception e)
                    {
                        var errMsg = GetExceptionMsg("发送邮件失败", e);
                        System.Console.WriteLine(errMsg);

                        try
                        {
                            TxtLogHelper.WriteLog(errMsg);
                        }
                        catch (Exception logException)
                        {
                            var logErrMsg = GetExceptionMsg("写Log文件失败", logException);
                            System.Console.WriteLine(logErrMsg);
                        }
                    }
                }
                else
                {
                    System.Threading.Thread.Sleep(500);
                }
            }
        }
Beispiel #3
0
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="xml">消息主体</param>
        public RequestMessage(XElement xml)
        {
            Dictionary <string, object> bugobj = new Dictionary <string, object>();

            try
            {
                bugobj.Add("消息主体", xml);
                this.FromUserName = xml.Element("FromUserName").Value;
                bugobj.Add("FromUserName", this.FromUserName);
                this.ToUserName = xml.Element("ToUserName").Value;
                bugobj.Add("ToUserName", this.ToUserName);
                this.CreateTime = Int64.Parse(xml.Element("CreateTime").Value);
                bugobj.Add("CreateTime", this.CreateTime);
                this.MsgId = xml.Element("MsgId") != null?Int64.Parse(xml.Element("MsgId").Value) : 0;

                bugobj.Add("MsgId", this.MsgId);
            }
            catch (Exception ex)
            {
                TxtLogHelper.WriteLog_Day(new TxtLogInfoObject()
                {
                    LogType    = EnumLogType.Error,
                    LogMessage = string.Format("错误信息{0};堆栈信息{1}", ex.Message, ex.StackTrace),
                    LogObject  = ex
                });
            }
            finally
            {
                TxtLogHelper.WriteDebugLog(bugobj);
            }
        }
 private void DeleteLog()
 {
     try
     {
         TxtLogHelper.WriteLog("执行删除日志任务");
         DateTime date = DateTime.Now;
         if (date.Hour > 1)//一点前执行
         {
             return;
         }
         string path = System.Environment.CurrentDirectory + @"\\Log\\TCP\\";
         if (Directory.Exists(path))
         {
             DeleteFile(path, 3); //删除该目录下 超过 3天的文件
         }
         path = System.Environment.CurrentDirectory + @"\\Log\\UDP\\";
         if (Directory.Exists(path))
         {
             DeleteFile(path, 7); //删除该目录下 超过 7天的文件
         }
         path = System.Environment.CurrentDirectory + @"\\Log\\Info\\";
         if (Directory.Exists(path))
         {
             DeleteFile(path, 14); //删除该目录下 超过 14天的文件
         }
     }
     catch (Exception ex)
     {
         TxtLogHelper.WriteLog("执行删除日志任务异常:" + ex.Message);
     }
 }
        public IMessageHandler MessageRole(HandleMessage message)
        {
            Dictionary <string, object> bugobj = new Dictionary <string, object>();

            try
            {
                if (message == null)
                {
                    throw new ArgumentException("message is null");
                }
                return(new VoiceMessageHandler(message));
            }
            catch (Exception ex)
            {
                TxtLogHelper.WriteLog_Day(new TxtLogInfoObject()
                {
                    LogType    = EnumLogType.Error,
                    LogMessage = string.Format("错误信息{0};堆栈信息{1}", ex.Message, ex.StackTrace),
                });
            }
            finally
            {
                TxtLogHelper.WriteDebugLog(bugobj);
            }

            return(new DefaultMessageHandler(message));
        }
        public ResponseMessage HandlerRequestMessage()
        {
            Dictionary <string, object> bugobj = new Dictionary <string, object>();

            try
            {
                if (this.msghander != null)
                {
                    bugobj.Add("消息请求", this.msghander.RequestMessage);
                    return(new ResponseTextMessage(this.msghander.RequestMessage)
                    {
                        CreateTime = DateTime.Now.Ticks,
                        Content = s_defaultMsg
                    });
                }
            }
            catch (Exception ex)
            {
                TxtLogHelper.WriteLog_Day(new TxtLogInfoObject()
                {
                    LogType    = EnumLogType.Error,
                    LogMessage = string.Format("错误信息{0};堆栈信息{1}", ex.Message, ex.StackTrace),
                });
            }
            finally
            {
                TxtLogHelper.WriteDebugLog(bugobj);
            }
            return(null);
        }
Beispiel #7
0
        /// <summary>
        /// 事件消息解析器
        /// </summary>
        /// <param name="element">消息实体</param>
        /// <returns></returns>
        private RequestMessage GetEventRequestMessage(XElement element)
        {
            Dictionary <string, object> bugobj = new Dictionary <string, object>();
            RequestMessage requestMessage      = null;

            try
            {
                if (element == null)
                {
                    return(null);
                }
                var eventType = (Event)Enum.Parse(typeof(Event), element.Element("Event").Value, true);
                bugobj.Add("eventType", eventType);
                bugobj.Add("element", element);
                switch (eventType)
                {
                case Event.Unsubscribe:
                    return(new RequestEventMessage(element));

                case Event.Location:
                    //  return new RequestLocationEventMessage(element);
                    break;

                case Event.Click:
                    requestMessage = new RequestClickEventMessage(element);
                    break;

                case Event.Scan:
                    requestMessage = new RequestQREventMessage(element);
                    break;

                case Event.Subscribe:
                    requestMessage = GetSubscribeRequestMessageForQR(element);
                    break;

                case Event.View:
                    requestMessage = new RequestViewEventMessage(element);
                    break;
                    //default:
                    //    throw new ArgumentException("event type is error");
                }
            }
            catch (Exception ex)
            {
                this.Log(ex);
            }
            finally
            {
                TxtLogHelper.WriteDebugLog(bugobj);
                TxtLogHelper.WriteLog_Hour(new TxtLogInfoObject()
                {
                    LogMessage = "消息实体",
                    LogObject  = requestMessage,
                    LogType    = EnumLogType.Other
                });
            }

            return(requestMessage);
        }
        /// <summary>
        /// 根据微信ID获取ticket
        /// </summary>
        /// <param name="weixinID"></param>
        /// <returns></returns>
        public string GetTicketByWeixinID(string weixinID)
        {
            Dictionary <string, object> bugobjd = new Dictionary <string, object>();
            string ticket = string.Empty;

            try
            {
                bugobjd.Add("根据微信ID获取ticket", "根据微信ID获取ticket");
                bugobjd.Add("WeixinAppInfoDic", WeixinAppInfoDic);
                if (WeixinAppInfoDic.ContainsKey(weixinID))
                {
                    MWeixinParam weixinParam = WeixinAppInfoDic[weixinID];

                    bugobjd.Add("weixinParam", weixinParam);

                    if (Access_TokenInfoDic.ContainsKey(weixinParam.AppID))
                    {
                        MAccess_Token access_Token = Access_TokenInfoDic[weixinParam.AppID];

                        if (String.IsNullOrEmpty(access_Token.Access_Token) || DateTime.Now >= access_Token.OutTime)
                        {
                            RefeshToken(weixinParam.AppID);
                            access_Token = Access_TokenInfoDic[weixinParam.AppID];
                        }

                        if (WeixinTicketDic.ContainsKey(access_Token.Access_Token))
                        {
                            if (String.IsNullOrEmpty(WeixinTicketDic[access_Token.Access_Token].Ticket) || DateTime.Now >= WeixinTicketDic[access_Token.Access_Token].OutTime)
                            {
                                RefeshTicket(Access_TokenInfoDic[weixinParam.AppID].Access_Token);
                            }
                        }
                        else
                        {
                            RefeshTicket(Access_TokenInfoDic[weixinParam.AppID].Access_Token);
                        }

                        bugobjd.Add(" Access_TokenInfo", Access_TokenInfoDic[weixinParam.AppID]);
                        bugobjd.Add(" WeixinTicket", WeixinTicketDic[Access_TokenInfoDic[weixinParam.AppID].Access_Token]);
                        ticket = WeixinTicketDic[Access_TokenInfoDic[weixinParam.AppID].Access_Token].Ticket;
                    }
                }
            }
            catch (Exception ex)
            {
                TxtLogHelper.WriteLog_Day(new TxtLogInfoObject()
                {
                    LogType    = EnumLogType.Error,
                    LogMessage = string.Format(" 根据微信ID获取ticket错误信息{0};堆栈信息{1}", ex.Message, ex.StackTrace),
                });
                throw;
            }
            finally
            {
                TxtLogHelper.WriteDebugLog(bugobjd);
            }

            return(ticket);
        }
Beispiel #9
0
 /// <summary>
 /// 记录异常
 /// </summary>
 /// <param name="ex"></param>
 public void Log(Exception ex)
 {
     TxtLogHelper.WriteLog_Day(new TxtLogInfoObject()
     {
         LogType    = EnumLogType.Error,
         LogMessage = string.Format("错误信息{0};堆栈信息{1}", ex.Message, ex.StackTrace),
     });
 }
Beispiel #10
0
        /// <summary>
        ///
        /// </summary>
        public TimerManager(TcpManager _tcp)
        {
            tcp = _tcp;
            Thread t = new Thread(Load);

            t.Start();
            TxtLogHelper.WriteLog("启动定时任务");
        }
Beispiel #11
0
        /// <summary>
        /// 消息类型分发器
        /// </summary>
        /// <param name="element">消息实体</param>
        /// <returns></returns>
        private RequestMessage GetRequestMessageByElement(XElement element)
        {
            Dictionary <string, object> bugobj = new Dictionary <string, object>();
            RequestMessage requestMessage      = null;

            try
            {
                bugobj.Add("element", element);
                bugobj.Add("MsgType", element.Element("MsgType").Value);
                MsgType msgType = (MsgType)Enum.Parse(typeof(MsgType), element.Element("MsgType").Value, true);
                switch (msgType)
                {
                case MsgType.Text:
                    requestMessage = new RequestTextMessage(element);
                    break;

                case MsgType.Video:
                    //  requestMessage = new RequestVoiceMessage(element);
                    break;

                case MsgType.Voice:
                    requestMessage = new RequestVoiceMessage(element);
                    break;

                case MsgType.Image:
                    // return new RequestImageMessage(element);
                    break;

                case MsgType.Link:
                    // return new RequestLinkMessage(element);
                    break;

                case MsgType.Location:
                    //  return new RequestLocationMessage(element);
                    break;

                case MsgType.Event:
                    requestMessage = GetEventRequestMessage(element);
                    break;
                    //default:
                    //    throw new ArgumentException("msgType is error");
                }
            }
            catch (Exception ex)
            {
                this.Log(ex);
            }
            finally
            {
                TxtLogHelper.WriteDebugLog(bugobj);
            }

            return(requestMessage);
        }
        /// <summary>
        /// 刷新ticket
        /// </summary>
        /// <param name="access_Token"></param>
        private void RefeshTicket(string access_Token)
        {
            Dictionary <string, object> bugobjd = new Dictionary <string, object>();

            try
            {
                bugobjd.Add("方法名", "刷新ticket");
                bugobjd.Add("access_Token", access_Token);
                // 更新ticket
                GetTicketRequest ticketInfo = new GetTicketRequest(access_Token, "jsapi");

                ApiClient client         = new ApiClient();
                DateTime  now            = DateTime.Now;
                var       ticketResponse = client.Execute(ticketInfo);
                bugobjd.Add("ticketResponse", ticketResponse);
                if (this.WeixinTicketDic == null)
                {
                    this.WeixinTicketDic = new Dictionary <string, MTicketInfo>();
                }

                if (this.WeixinTicketDic.ContainsKey(access_Token))
                {
                    this.WeixinTicketDic[access_Token].Ticket  = ticketResponse.Ticket;
                    this.WeixinTicketDic[access_Token].OutTime = now.AddSeconds(ticketResponse.Expires_In);
                }
                else
                {
                    this.WeixinTicketDic.Add(access_Token, new MTicketInfo()
                    {
                        Ticket       = ticketResponse.Ticket,
                        Access_Token = access_Token,
                        OutTime      = now.AddSeconds(ticketResponse.Expires_In)
                    });
                }

                bugobjd.Add("WeixinTicketDic", WeixinTicketDic);
            }
            catch (Exception ex)
            {
                TxtLogHelper.WriteLog_Day(new TxtLogInfoObject()
                {
                    LogType    = EnumLogType.Error,
                    LogMessage = string.Format(" 刷新ticket 错误信息{0};堆栈信息{1}", ex.Message, ex.StackTrace),
                });
                throw;
            }
            finally
            {
                TxtLogHelper.WriteDebugLog(bugobjd);
            }
        }
Beispiel #13
0
 /// <summary>
 /// 调用方法
 /// </summary>
 private void RunMethod(object sender, System.Timers.ElapsedEventArgs e)
 {
     TxtLogHelper.WriteLog("开始执行定时任务");
     _timer.Enabled = false;
     try
     {
         DeleteLog();
     }
     catch (Exception ex)
     {
         TxtLogHelper.WriteLog("执行定时任务异常:" + ex.Message);
     }
     TxtLogHelper.WriteLog("定时任务执行结束");
     _timer.Enabled = true;
 }
        /// <summary>
        /// 语音识别处理
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        public ResponseMessage HandlerRequestMessage()
        {
            Dictionary <string, object> bugobj = new Dictionary <string, object>();
            var request      = this.msghander.RequestMessage as RequestVoiceMessage;
            var responseText = new ResponseTextMessage(request);

            try
            {
                bugobj.Add("语音识别处理!", "语音识别处理");
                if (request != null)
                {
                    bugobj.Add("语音识别结果", request);
                    if (!String.IsNullOrEmpty(request.Recognition))
                    {
                        if (!string.IsNullOrEmpty(request.FromUserName))
                        {
                        }
                        else
                        {
                            responseText.Content = "信息格式不正确。";
                        }
                    }
                    else
                    {
                        responseText.Content = "您说的啥?我没听懂";
                    }
                }
                else
                {
                    responseText.Content = "我暂时还无法理解你所说的意思。";
                }
            }
            catch (Exception ex)
            {
                TxtLogHelper.WriteLog_Day(new TxtLogInfoObject()
                {
                    LogType    = EnumLogType.Error,
                    LogMessage = string.Format("错误信息{0};堆栈信息{1}", ex.Message, ex.StackTrace),
                });
            }
            finally
            {
                TxtLogHelper.WriteDebugLog(bugobj);
            }

            return(responseText);
        }
        /// <summary>
        /// 处理请求消息
        /// </summary>
        /// <returns>处理结果</returns>
        public ResponseMessage HandlerRequestMessage()
        {
            Dictionary <string, object> bugobj           = new Dictionary <string, object>();
            NormalMenuEventMessage      viewEventMessage = null;

            bugobj.Add("message", this.messageHandler);
            var request = new RequestViewEventMessage(this.messageHandler.Element);

            try
            {
                if (request != null)
                {
                    // 点击菜单跳转链接时的事件推送处理
                    bugobj.Add("request", request);
                }
                viewEventMessage = new NormalMenuEventMessage(request)
                {
                    CreateTime   = DateTime.Now.Ticks,
                    ToUserName   = request.ToUserName,
                    FromUserName = request.FromUserName,
                    EventKey     = request.EventKey,
                };
            }
            catch (Exception ex)
            {
                TxtLogHelper.WriteLog_Day(new TxtLogInfoObject()
                {
                    LogType    = EnumLogType.Error,
                    LogMessage = string.Format("错误信息{0};堆栈信息{1}", ex.Message, ex.StackTrace),
                });
            }
            finally
            {
                TxtLogHelper.WriteDebugLog(bugobj);
            }

            return(viewEventMessage);
        }
Beispiel #16
0
        /// <summary>
        /// 处理未关注的带参数二维码扫描事件
        /// </summary>
        /// <param name="element"></param>
        /// <returns></returns>
        private RequestMessage GetSubscribeRequestMessageForQR(XElement element)
        {
            Dictionary <string, object> bugobj = new Dictionary <string, object>();

            try
            {
                bugobj.Add("处理未关注的带参数二维码扫描事件", element);
                bugobj.Add("Ticket", element.Element("Ticket"));
                if (element.Element("Ticket") != null)
                {
                    return(new RequestQREventMessage(element));
                }
            }
            catch (Exception ex)
            {
                this.Log(ex);
            }
            finally
            {
                TxtLogHelper.WriteDebugLog(bugobj);
            }
            return(new RequestEventMessage(element));
        }
Beispiel #17
0
        /// <summary>
        /// 删除目录下超过指定天数的文件
        /// </summary>
        private void DeleteFile(string fileDirect, int saveDay)
        {
            DateTime nowTime = DateTime.Now;

            string[] files = Directory.GetFiles(fileDirect, "*.log", SearchOption.AllDirectories);  //获取该目录下所有 .txt文件
            foreach (string file in files)
            {
                FileInfo fileInfo = new FileInfo(file);
                TimeSpan t        = nowTime - fileInfo.CreationTime; //当前时间  减去 文件创建时间
                int      day      = t.Days;
                if (day > saveDay)                                   //保存的时间,单位:天
                {
                    try
                    {
                        System.IO.File.Delete(fileInfo.FullName); //删除文件
                    }
                    catch (Exception ex)
                    {
                        TxtLogHelper.WriteLog("删除日志文件异常:" + ex.Message);
                    }
                }
            }
        }
Beispiel #18
0
        /// <summary>
        ///消息处理构造函数
        /// </summary>
        /// <param name="element">消息实体</param>
        public HandleMessage(XElement element)
        {
            Dictionary <string, object> bugobj = new Dictionary <string, object>();

            try
            {
                if (element == null)
                {
                    throw new ArgumentNullException("element is null");
                }
                bugobj.Add("element", element.ToString());
                this.Element        = element;
                this.RequestMessage = GetRequestMessageByElement(element);
                bugobj.Add("RequestMessage", this.RequestMessage);
            }
            catch (Exception ex)
            {
                this.Log(ex);
            }
            finally
            {
                TxtLogHelper.WriteDebugLog(bugobj);
            }
        }
Beispiel #19
0
        /// <summary>
        /// 处理请求消息
        /// </summary>
        /// <returns>处理结果</returns>
        public ResponseMessage HandlerRequestMessage()
        {
            Dictionary <string, object> bugobj = new Dictionary <string, object>();

            bugobj.Add("message", this.message);
            var request = new RequestClickEventMessage(this.message.Element);
            /// 返回消息类型
            MsgType msgtype = MsgType.Text;
            // 返回消息
            string result = string.Empty;

            //GetB2CProductInfo getB2CProductInfo = null;
            try
            {
                if (request != null)
                {
                    // 点击菜单事件推送处理
                    bugobj.Add("request", request);
                    switch (request.EventKey)
                    {
                    case "V1001_GOOD":
                        result += "感谢您的支持!";
                        msgtype = MsgType.Text;
                        break;

                    case "V1001_HOT":
                        msgtype = MsgType.Text;
                        MQiushibaike qiushibaike = JsonConvert.DeserializeObject <MQiushibaike>(QiushibaikeHelper.GetJokesByRandom());
                        result = qiushibaike.JokeContent;
                        break;

                    default:
                        break;
                    }
                }

                switch (msgtype)
                {
                case MsgType.Text:
                    return(new ResponseTextMessage(request)
                    {
                        CreateTime = DateTime.Now.Ticks,
                        Content = result,
                    });

                case MsgType.Image:
                    break;

                case MsgType.Voice:
                    break;

                case MsgType.Video:
                    break;

                case MsgType.Location:
                    break;

                case MsgType.Link:
                    break;

                case MsgType.Event:
                    return(new NormalMenuEventMessage(request)
                    {
                        CreateTime = DateTime.Now.Ticks,
                    });

                case MsgType.News:
                case MsgType.Music:
                    break;

                case MsgType.ShortVideo:
                    break;

                case MsgType.transfer_customer_service:
                    break;

                default:
                    break;
                }
            }
            catch (Exception ex)
            {
                TxtLogHelper.WriteLog_Day(new TxtLogInfoObject()
                {
                    LogType    = EnumLogType.Error,
                    LogMessage = string.Format("错误信息{0};堆栈信息{1}", ex.Message, ex.StackTrace),
                });
            }
            finally
            {
                TxtLogHelper.WriteDebugLog(bugobj);
            }

            return(null);
        }
Beispiel #20
0
        protected void Page_Load(object sender, EventArgs e)
        {
            Dictionary <string, object> bugobj = new Dictionary <string, object>();

            try
            {
                bugobj.Add("HttpMethod", Request.HttpMethod.ToString());
                //微信服务器一直把用户发过来的消息,post过来
                if (Request.HttpMethod.ToUpper() == "POST")
                {
                    XDocument doc = null;
                    if (Request.InputStream != null)
                    {
                        var reader = XmlReader.Create(Request.InputStream);
                        doc = XDocument.Load(reader);
                    }

                    if (doc != null)
                    {
                        var xml = doc.Element("xml");
                        // 解析xml消息
                        var msg = new HandleMessage(xml);
                        if (msg != null)
                        {
                            bugobj.Add("消息主体", msg.Element.ToString());
                            TxtLogHelper.WriteDebugLog(bugobj);
                            var responseMessage = new WebMessageRole().MessageRole(msg).HandlerRequestMessage();
                            if (responseMessage != null)
                            {
                                Response.Write(responseMessage.Serializable());
                                bugobj.Add("响应", responseMessage.Serializable());
                            }
                        }
                        else
                        {
                            Response.Write("post数据为空");
                        }
                    }
                    else
                    {
                        Response.Write("post数据为空");
                    }
                }
                else if (Request.HttpMethod.ToUpper() == "GET") //微信服务器在首次验证时,需要进行一些验证,但。。。。
                {
                    bugobj.Add("signature", Request["signature"]);
                    bugobj.Add("timestamp", Request["timestamp"]);
                    bugobj.Add("nonce", Request["nonce"]);
                    bugobj.Add("微信验证", Request["echostr"]);
                    string echostr = string.Empty;
                    if (Request["echostr"] != null)
                    {
                        echostr = Request["echostr"].ToString();
                    }

                    //我仅需返回给他echostr中的值,就为验证成功,可能微信觉得这些安全策略是为了保障我的服务器,要不要随你吧
                    Response.Write(echostr);
                    return;
                }
            }
            catch (Exception ex)
            {
                TxtLogHelper.WriteLog_Day(new TxtLogInfoObject()
                {
                    LogType    = EnumLogType.Error,
                    LogMessage = string.Format("错误信息{0};堆栈信息{1}", ex.Message, ex.StackTrace),
                    LogObject  = ex,
                });

                Response.Write("post数据为空");
            }
            finally
            {
                TxtLogHelper.WriteDebugLog(bugobj);
            }
        }
Beispiel #21
0
        /// <summary>
        /// 消息分类处理
        /// </summary>
        /// <param name="msg">消息处理器</param>
        /// <returns></returns>
        public IMessageHandler MessageRole(HandleMessage msg)
        {
            Dictionary <string, object> bugobj = new Dictionary <string, object>();

            bugobj.Add("消息分类处理", "消息分类处理");
            try
            {
                bugobj.Add("消息请求", msg.RequestMessage);
                if (msg.RequestMessage == null)
                {
                    return(null);
                }

                bugobj.Add("消息去重", "消息去重");
                #region 消息去重
                if (messageQueue == null)
                {
                    messageQueue = new List <BaseMessage>();
                }
                else if (messageQueue.Count >= 50)
                {
                    //保留10秒内未响应的消息
                    messageQueue = messageQueue.Where(q => { return(q.CreateTime.AddSeconds(10) > DateTime.Now); }).ToList();
                    bugobj.Add("保留10秒内未响应的消息", messageQueue);
                }
                bugobj.Add("MsgType", msg.RequestMessage.MsgType);
                bugobj.Add("MsgId", msg.RequestMessage.MsgId.ToString());
                bugobj.Add("FromUserName", msg.RequestMessage.FromUserName);
                // 判断当前请求是否已经在队列里面。如果在里面就不做处理,如果当前请求不再队列里面则加入队列并进行接下来的处理
                string msgFlag = msg.RequestMessage.CreateTime.ToString();
                bugobj.Add("当前消息唯一标识", msgFlag);
                if (messageQueue.Find(m => m.MsgFlag == msgFlag && m.FromUser == msg.RequestMessage.FromUserName) == null)
                {
                    messageQueue.Add(new BaseMessage
                    {
                        CreateTime = DateTime.Now,
                        FromUser   = msg.RequestMessage.FromUserName,
                        MsgFlag    = msgFlag
                    });

                    bugobj.Add("添加当前消息到消息队列中", messageQueue.Last());
                }
                else
                {
                    bugobj.Add("当前消息已经处理过", msgFlag);
                    return(null);
                }

                #endregion
                switch (msg.RequestMessage.MsgType)
                {
                case MsgType.Text:
                    return(new TextMessageRole().MessageRole(msg));

                case MsgType.Event:
                    return(new EventMessageRole().MessageRole(msg));

                case MsgType.Voice:
                    return(new VoiceMessageRole().MessageRole(msg));

                default:
                    return(new DefaultMessageHandler(msg));
                }
            }
            catch (Exception ex)
            {
                TxtLogHelper.WriteLog_Day(new TxtLogInfoObject()
                {
                    LogType    = EnumLogType.Error,
                    LogMessage = string.Format("错误信息{0};堆栈信息{1}", ex.Message, ex.StackTrace),
                });
                throw;
            }
            finally
            {
                TxtLogHelper.WriteDebugLog(bugobj);
            }

            return(null);
        }
        public ResponseMessage HandlerRequestMessage()
        {
            var request      = this.msghander.RequestMessage as RequestTextMessage;
            var responseText = new ResponseTextMessage(request);
            Dictionary <string, object> bugobj = new Dictionary <string, object>();

            try
            {
                bugobj.Add("文字处理!", "文字处理");
                if (request != null)
                {
                    if (!String.IsNullOrEmpty(request.Content))
                    {
                        if (!string.IsNullOrEmpty(request.FromUserName))
                        {
                            // 关键字搜索
                            bugobj.Add("关键字搜索", request.Content);
                            if (request.Content.Contains("笑话"))
                            {
                                List <MQiushibaike> qiushibaikelist = JsonConvert.DeserializeObject <List <MQiushibaike> >(QiushibaikeHelper.GetJokesByRandom());
                                bugobj.Add("笑话", qiushibaikelist);
                                if (qiushibaikelist != null)
                                {
                                    int index = 0;
                                    foreach (var item in qiushibaikelist)
                                    {
                                        if (index == qiushibaikelist.Count - 1)
                                        {
                                            responseText.Content += string.Format(" {0} ---来源:@糗事百科 {1}", item.JokeContent, item.JokerName);
                                        }
                                        else
                                        {
                                            responseText.Content += string.Format(" {0} ---来源:@糗事百科 {1} {2}", item.JokeContent, item.JokerName, "-----我是分割线-----");
                                        }

                                        index++;
                                    }
                                }
                                else
                                {
                                    responseText.Content = "你在逗我?";
                                }
                            }
                        }
                        else
                        {
                            responseText.Content = "你在逗我?";
                        }
                    }
                    else
                    {
                        responseText.Content = "你在逗我?";
                    }
                }
                else
                {
                    responseText.Content = "你在逗我?";
                }
            }
            catch (Exception ex)
            {
                TxtLogHelper.WriteLog_Day(new TxtLogInfoObject()
                {
                    LogType    = EnumLogType.Error,
                    LogMessage = string.Format("错误信息{0};堆栈信息{1}", ex.Message, ex.StackTrace),
                });
            }
            finally
            {
                TxtLogHelper.WriteDebugLog(bugobj);
            }

            if (!string.IsNullOrEmpty(this.Message))
            {
                responseText.Content = this.Message;
            }
            return(responseText);
        }
Beispiel #23
0
 /// <summary>
 /// 记录日志
 /// </summary>
 /// <param name="debugLog"></param>
 public void Log(Dictionary <string, object> debugLog)
 {
     TxtLogHelper.WriteDebugLog(debugLog);
 }
        /// <summary>
        /// 事件消息处理
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        public IMessageHandler MessageRole(HandleMessage msg)
        {
            Dictionary <string, object> bugobj = new Dictionary <string, object>();

            try
            {
                if (msg == null)
                {
                    throw new ArgumentException("msg is null");
                }
                var             eventType       = (Event)Enum.Parse(typeof(Event), msg.Element.Element("Event").Value, true);
                IMessageHandler imessageHandler = null;
                switch (eventType)
                {
                case Event.Subscribe:
                    imessageHandler = new SubScribeEventMessageHandler(msg);
                    break;

                case Event.Unsubscribe:
                    imessageHandler = new UnSubScribeEventMessageHandler(msg);
                    break;

                case Event.Scan:
                    imessageHandler = new ScanEventMessageHandler(msg);
                    break;

                case Event.Location:
                    break;

                case Event.Click:
                    imessageHandler = new ClickEventMessageHandler(msg);
                    break;

                case Event.MASSSENDJOBFINISH:
                    break;

                case Event.View:
                    imessageHandler = new ViewEventMessageHandler(msg);
                    break;

                case Event.Merchant_Order:
                    break;

                case Event.TEMPLATESENDJOBFINISH:
                    break;

                default:
                    imessageHandler = new DefaultMessageHandler(msg);
                    break;
                }


                return(imessageHandler);
            }
            catch (Exception ex)
            {
                TxtLogHelper.WriteLog_Day(new TxtLogInfoObject()
                {
                    LogType    = EnumLogType.Error,
                    LogMessage = string.Format("错误信息{0};堆栈信息{1}", ex.Message, ex.StackTrace),
                });
            }
            finally
            {
                TxtLogHelper.WriteDebugLog(bugobj);
            }
            return(new DefaultMessageHandler(msg));
        }