コード例 #1
0
        public async Task <string> index()
        {
            PostModel postModel = TypeChange.UrlToEntities <PostModel>(Request.QueryString.Value);

            if (!CheckSignature.Check(postModel.Signature, postModel.Timestamp, postModel.Nonce, appConfig.WeiXin.Token))
            {
                return("参数错误!");
            }
            else
            {
                Request.EnableBuffering();

                using (var reader = new StreamReader(Request.Body, encoding: Encoding.UTF8))
                {
                    var body = await reader.ReadToEndAsync();

                    var    xml          = TypeChange.XmlToDict(body);
                    String toUserName   = xml.GetValueOrDefault("ToUserName");
                    String fromUserName = xml.GetValueOrDefault("FromUserName");
                    String msgType      = xml.GetValueOrDefault("MsgType");
                    String content      = xml.GetValueOrDefault("Content");
                    String eventType    = xml.GetValueOrDefault("Event");
                    String eventKey     = xml.GetValueOrDefault("EventKey");
                    String ticket       = xml.GetValueOrDefault("Ticket");
                    await weiXin.saveLog(new WxQuestLogEntity
                    {
                        id           = await SequenceRepository.GetNextID <WxQuestLogEntity>(),
                        toUserName   = toUserName,
                        fromUserName = fromUserName,
                        msgType      = msgType,
                        content      = content,
                        eventType    = eventType,
                        eventKey     = eventKey,
                        ticket       = ticket,
                    });

                    if (MessageUtil.MESSAGE_EVENT.Equals(msgType))
                    {
                        switch (eventType.ToLower())
                        {
                        case "subscribe":     //订阅
                            if (!string.IsNullOrEmpty(fromUserName))
                            {
                                var userinfo = Helper.WeiChat.Utility.GetUserInfo(fromUserName);
                                await weiXin.saveUser(userinfo, new List <string> {
                                    "nickname", "headimgurl", "subscribe", "subscribe_time", "remark", "groupid", "tagidListStr", "subscribe_scene", "qr_scene", "qr_scene_str"
                                });
                            }
                            break;

                        case "unsubscribe":     //取消订阅
                            var unsubscribeUser = new WxUserEntity()
                            {
                                subscribe      = 0,
                                openid         = fromUserName,
                                subscribe_time = Helper.DataTimeHelper.getDateLongTimestamp(DateTime.Now)
                            };
                            await weiXin.saveUser(unsubscribeUser, new List <string> {
                                "subscribe", "subscribe_time"
                            });

                            break;

                        case "click":     //点击事件
                            String replay = "";
                            switch (eventKey)
                            {
                            case MessageUtil.CLICK_DOWNURL:
                                replay = MessageUtil.downloadApp();
                                break;

                            case MessageUtil.CLICK_ETC_INSTALL:
                                replay = MessageUtil.etcInstallPlace();
                                break;

                            case MessageUtil.CLICK_GetMoney:
                                replay = MessageUtil.etcGetMoney(fromUserName);
                                break;
                            }
                            string message = MessageUtil.initText(toUserName, fromUserName, replay);
                            return(message);

                        case "view":     //url类型
                            break;
                        }
                    }

                    // Do some processing with body…
                    // Reset the request body stream position so the next middleware can read it
                    Request.Body.Position = 0;
                    return("");
                }
            }
        }