Ejemplo n.º 1
0
        public async Task <ContentResult> Post(string signature, string timestamp, string nonce, string openid)
        {
            //验证微信签名
            if (!WxBase.CheckSignature(signature, timestamp, nonce))
            {
                Console.WriteLine($"receive error check signature:{signature} with {timestamp},{nonce}");

                return(new ContentResult()
                {
                    StatusCode = 401
                });
            }

            //初始化菜单
            await WxBase.InitMenu();

            using (Stream stream = Request.Body)
            {
                byte[] buffer = new byte[Request.ContentLength.Value];
                await stream.ReadAsync(buffer, 0, buffer.Length);

                string xml = Encoding.UTF8.GetString(buffer);
                Console.WriteLine($"收到:{xml}");
                var msg = WxTextMsg.FromXml(xml);

                if (msg.MsgType.Contains("text") &&
                    msg.Content.Contains("磁芯大战"))
                {
                    WxUser user = null;
                    if (await DbEntityManager.Exist <WxUser>("openid", openid))
                    {
                        //get user from db
                        user = await DbEntityManager.SelectOne <WxUser>("openid", openid);

                        //update salt only
                        user.ResetSalt();
                        await DbEntityManager.Update(user);
                    }
                    else
                    {
                        //
                        var info = await WxBase.GetUserInfo(openid);

                        Console.WriteLine($"获取:{info.nickname} 信息");
                        user = WxUser.CreateFromWxUserInfo(info);
                        await DbEntityManager.Insert(user);
                    }

                    //
                    var article = new WxArticle("MagCore - 磁芯大战", "进入房间创建游戏",
                                                HelperConfig.Current.WxInterfaceHost + "assets/images/icon.png",
                                                HelperConfig.Current.WxInterfaceHost + "pages/creator.html"
                                                + "?t=" + DateTime.Now.Ticks.ToString()
                                                + "&amp;oid=" + openid
                                                + $"&amp;name={user.nickname}({HashManager.Md5(openid).Substring(12, 4)})"
                                                + "&amp;s=" + user.salt);
                    var reply = new WxArticleMsg(msg.FromUserName, msg.ToUserName,
                                                 msg.CreateTime, new WxArticle[] { article });
                    string text = reply.ToXml();
                    Console.WriteLine($"回复:{text}");

                    return(new ContentResult()
                    {
                        StatusCode = 200, Content = text
                    });
                }
                else
                {
                    //
                    var defaultReply = new WxTextMsg(msg.FromUserName, msg.ToUserName, msg.CreateTime,
                                                     "未正确识别,磁芯大战比赛期间,其他回复暂时停止服务");

                    return(new ContentResult()
                    {
                        StatusCode = 200, Content = defaultReply.ToXml()
                    });
                }
            }
        }
Ejemplo n.º 2
0
        public string POST(string signature, string token, string timestamp, string nonce, string echostr)
        {
            try
            {
                Stream requestStream = System.Web.HttpContext.Current.Request.InputStream;
                byte[] requestByte   = new byte[requestStream.Length];
                requestStream.Read(requestByte, 0, (int)requestStream.Length);
                string requestStr = Encoding.UTF8.GetString(requestByte);

                ClassLoger.Info("checkWeChatSignatureController接收到的信息:", requestStr);
                XmlDocument xd = new XmlDocument();
                xd.LoadXml(requestStr);

                var msg = WeChatXMLMessageHelper.GetWeChatMsg(xd);
                if (msg != null)
                {
                    WxTextMsg msgs = new WxTextMsg();
                    msgs.ToUserName   = msg.FromUserName;
                    msgs.FromUserName = msg.ToUserName;
                    msgs.CreateTime   = DateTime.Now.Ticks;
                    msgs.MsgType      = "text";
                    //订阅服务号
                    if (msg.Event.Equals(MsgTypeEvent.subscribe))
                    {
                        WeChatUserInfo uinfo  = WeChatUserInfoAPI.GetWeChatUserInfo(msg.FromUserName);
                        WeChatUser     weuser = userbll.GetWeChatUserByUnionID(uinfo.unionid);
                        if (weuser == null)
                        {
                            weuser = new WeChatUser();
                        }
                        weuser.ServiceOpenID = uinfo.openid;
                        weuser.UnionID       = uinfo.unionid;
                        userbll.UpdateWeChatUser(weuser);

                        UserInfo      user       = ubll.GetUserInfoByOpenID(uinfo.unionid);
                        List <string> openIDList = new List <string>(1)
                        {
                            weuser.ServiceOpenID
                        };
                        if (user == null)
                        {
                            user            = new UserInfo();
                            user.Openid     = uinfo.unionid;
                            user.CreateTime = DateTime.Now;
                            user.Headimgurl = uinfo.headimgurl;
                            user.Nickname   = uinfo.nickname;
                            user.Sex        = (SexEnum)uinfo.sex;
                            user.Name       = uinfo.nickname;
                            user.city       = uinfo.city;
                            user.province   = uinfo.province;
                            ubll.UpdateUserinfo(user);
                        }
                        else
                        {
                            if (user.IsAdmin)
                            {
                                WeChatTagsAPI.batchtagging(openIDList, SystemSet.AdminUserTagID);
                            }
                        }
                        msgs.Content = "感谢您关注【....】";
                    }
                    return(XmlEntityExchange <WxTextMsg> .ConvertEntity2Xml(msgs));
                }
            } catch (Exception ex)
            {
                ClassLoger.Error("WeChatSig.POST", ex);
            }
            return(string.Empty);
        }