Esempio n. 1
0
        public IAsyncResult BeginProcessMessage(IWeChatMessage message, IOfficialAccountService service, AsyncCallback callback, object userState)
        {
            AsyncResult <IWeChatMessageResult> result = new AsyncResult <IWeChatMessageResult>(callback, userState);

            this.ProcessMessage(message, service, new Queue <IWeChatMessageHandler>(this.m_handlers), result, true);
            return(result);
        }
Esempio n. 2
0
        private void ProcessMessage(IWeChatMessage message, IOfficialAccountService service, Queue <IWeChatMessageHandler> queue, AsyncResult <IWeChatMessageResult> result, bool completedSynchronously)
        {
            if (queue.Count == 0)
            {
                result.MarkCompleted(null, completedSynchronously, null);
                return;
            }

            IWeChatMessageHandler handler = queue.Dequeue();

            handler.BeginProcessMessage(message, service, (ar) => {
                Exception error = null;
                IWeChatMessageResult messageResult = null;
                bool synchronously = completedSynchronously && ar.CompletedSynchronously;

                try {
                    messageResult = handler.EndProcessMessage(ar);
                } catch (Exception ex) {
                    error = ex;
                }

                if (error != null)
                {
                    result.MarkCompleted(error, synchronously, null);
                }
                else if (messageResult != null)
                {
                    result.MarkCompleted(null, synchronously, messageResult);
                }
                else
                {
                    this.ProcessMessage(message, service, queue, result, synchronously);
                }
            }, null);
        }
        public IWeChatMessage GetMessage(string data, IOfficialAccountService service)
        {
            if (string.IsNullOrWhiteSpace(data))
            {
                return(null);
            }

            WeChatMessage message = null;

            using (TextReader reader = new StringReader(data)) {
                message = (WeChatMessage)this.m_serializer.Deserialize(reader);
            }
            return(message);
        }
        public IAsyncResult BeginProcessMessage(IWeChatMessage message, IOfficialAccountService service, AsyncCallback callback, object userState)
        {
            IWeChatMessageResult messageResult = new WeChatTextReply {
                ToUserName   = message.FromUserName,
                FromUserName = message.ToUserName,
                CreateTime   = message.CreateTime,
                Content      = "你好,欢迎关注南阳仙草药业公众号,<a href=\"https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx4c8496aa5431914e&redirect_uri=http%3A%2F%2Fweixin.xphter.com%2Fhome%2Fbinding&response_type=code&scope=snsapi_base&state=123#wechat_redirect\">点击链接</a>绑定微信号,随时了解您的订单动态。发送\"订单提醒\"消息,系统将向您发送订单提醒。",
                //Content = "你好,欢迎关注南阳仙草药业公众号,<a href=\"https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx4c8496aa5431914e&redirect_uri=http%3A%2F%2Fxphter.gicp.net%2FXphter.Framework.WeChat.OfficialAccountsTest.Portal%2Fhome%2Fbinding&response_type=code&scope=snsapi_base&state=123#wechat_redirect\">点击链接</a>绑定微信号,随时了解您的订单动态。发送\"订单提醒\"消息,系统将向您发送订单提醒。",
            };

            AsyncResult <IWeChatMessageResult> result = new AsyncResult <IWeChatMessageResult>(callback, userState);

            result.MarkCompleted(null, true, messageResult);
            return(result);
        }
        public IWeChatMessageHandler GetHandler(IWeChatMessage message, IOfficialAccountService service)
        {
            IWeChatMessageHandler handler = null;

            switch (message.MsgType)
            {
            case WeChatMessageTypes.EVENT:
                IWeChatEventMessage eventMessage = (IWeChatEventMessage)message;
                if (this.m_eventHandlers.ContainsKey(eventMessage.Event))
                {
                    handler = this.m_eventHandlers[eventMessage.Event];
                }
                break;

            default:
                if (this.m_messageHandlers.ContainsKey(message.MsgType))
                {
                    handler = this.m_messageHandlers[message.MsgType];
                }
                break;
            }

            return(handler);
        }
Esempio n. 6
0
        public IAsyncResult BeginProcessMessage(IWeChatMessage message, IOfficialAccountService service, AsyncCallback callback, object userState)
        {
            AsyncResult <IWeChatMessageResult> result = new AsyncResult <IWeChatMessageResult>(callback, userState);
            IWeChatTextMessage textMessage            = message as IWeChatTextMessage;

            if (string.Equals(textMessage.Content, "模板消息"))
            {
                string content = File.ReadAllText(HostingEnvironment.MapPath("~/order.json"), Encoding.UTF8).Replace("{openid}", textMessage.FromUserName);
                service.BeginSendTemplateMessage(content, (ar) => {
                    IWeChatTemplateMessageReturnValue value = service.EndSendTemplateMessage(ar);
                }, null);

                result.MarkCompleted(null, true, new WeChatTextReply {
                    ToUserName   = message.FromUserName,
                    FromUserName = message.ToUserName,
                    CreateTime   = message.CreateTime,
                    Content      = "系统随后将向您发送模板消息",
                });
            }
            else if (string.Equals(textMessage.Content, "客服消息"))
            {
                string content = File.ReadAllText(HostingEnvironment.MapPath("~/kf.json"), Encoding.UTF8).Replace("{openid}", textMessage.FromUserName);
                service.BeginSendCustomerServiceMessage(content, (ar) => {
                    service.EndSendCustomerServiceMessage(ar);
                }, null);

                service.BeginSendCustomerServiceMessage(new WeChatTextCustomerServiceMessage("嘿嘿哈嘿")
                {
                    ToUserName = message.FromUserName,
                }, (ar) => {
                    service.EndSendCustomerServiceMessage(ar);
                }, null);

                WeChatNewsCustomerServiceMessage news = new WeChatNewsCustomerServiceMessage {
                    ToUserName = message.FromUserName,
                };
                news.News.Items.Add(new WeChatNewsCustomerServiceMessageItem {
                    Title       = "丰满性感尤物洛可可浴室大摆诱人姿势",
                    Description = "丰满性感尤物洛可可浴室大摆诱人姿势",
                    PicUrl      = "http://www.xunmeitu.com/Static/Images/2016/12/24/185706528004254.jpg",
                    Url         = "http://www.xunmeitu.com/a/17798.html",
                });
                news.News.Items.Add(new WeChatNewsCustomerServiceMessageItem {
                    Title       = "性感田妞肤白貌美惹眼上围乳此动人",
                    Description = "性感田妞肤白貌美惹眼上围乳此动人",
                    PicUrl      = "http://www.xunmeitu.com/Static/Images/2016/10/07/153628983003544.jpg",
                    Url         = "http://www.xunmeitu.com/a/9837.html",
                });
                service.BeginSendCustomerServiceMessage(news, (ar) => {
                    service.EndSendCustomerServiceMessage(ar);
                }, null);

                result.MarkCompleted(null, true, new WeChatTextReply {
                    ToUserName   = message.FromUserName,
                    FromUserName = message.ToUserName,
                    CreateTime   = message.CreateTime,
                    Content      = "系统随后将向您发送客服消息",
                });
            }
            else
            {
                service.BeginGetUserInfo(message.FromUserName, WeChatLanguage.SimplifiedChinese, (ar) => {
                    IWeChatUserInfo userInfo = null;

                    try {
                        userInfo = service.EndGetUserInfo(ar);
                    } catch (Exception) {
                    }
                }, null);

                result.MarkCompleted(null, true, new WeChatTextReply {
                    ToUserName   = message.FromUserName,
                    FromUserName = message.ToUserName,
                    CreateTime   = message.CreateTime,
                    Content      = textMessage.Content,
                });
            }

            return(result);
        }
Esempio n. 7
0
        protected override void Initialize(RequestContext requestContext)
        {
            base.Initialize(requestContext);

            this.m_service = MvcApplication.OfficialAccountService;
        }