Пример #1
0
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Config.Instance.IsInitialized)
            {
                Config.Instance.Load();
            }

            if (WxNormalHandler.IsVerifyingSignature())
            {
                //首次接入校验Signature
                WxNormalHandler.VerifySignature();
            }
            else
            {
                byte[] bReq = Request.BinaryRead(Request.TotalBytes);
                if (bReq.Length > 0)
                {
                    string sReq = Encoding.UTF8.GetString(bReq);
                    if (sReq.Length > 0)
                    {
                        if (WxNormalHandler.IsRcvTecentMsg(ref sReq))
                        {
                            //收到消息
                            WxMsgHandler hMsg = new WxMsgHandler();
                            if (hMsg.Parse(sReq))
                            {
                                if (hMsg.MsgType == WxMsgType.Text)
                                {
                                    hMsg.TextMsg2Db(hMsg.FromUser, hMsg.MsgId, hMsg.MsgType, hMsg.Content);

                                    //响应消息处理
                                    TextMsgResponse(Config.Instance.Wx.Response, ref hMsg);
                                }
                                else
                                {
                                    Response.Write("success");
                                }
                            }
                            else
                            {
                                Response.Write("success");
                            }
                        }
                        else if (WxNormalHandler.IsRcvTecentEvent(ref sReq))
                        {
                            //收到事件
                            WxEventHandler hEvt = new WxEventHandler();
                            if (hEvt.Parse(sReq))
                            {
                                if (hEvt.Event == WxEventType.Subscribe)
                                {
                                    //关注,执行数据库注册操作
                                    if (hEvt.Subscribe(hEvt.FromUser))
                                    {
                                        string _Welcome = Config.Instance.Wx.Welcome;
                                        _Welcome = _Welcome.Replace("{{$date}}", zDate.Format(DateTime.Now, DateFormatStyle.Style(true, false, false, true)));
                                        _Welcome = _Welcome.Replace("{{$name}}", Config.Instance.Wx.Name);

                                        SubscribeResponse(_Welcome, ref hEvt);
                                    }
                                }
                                else if (hEvt.Event == WxEventType.Unsubscribe)
                                {
                                    //取消关注
                                    hEvt.Unsubscribe(hEvt.FromUser);
                                }
                            }
                        }
                    }
                }

                //其他情况直接返回
                Response.Write("");
            }
        }
Пример #2
0
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Config.Instance.IsInitialized)
            {
                Config.Instance.Load();
            }

            //自动获取Token
            string sToken = WxNormalHandler.GetToken();

            if (sToken.Length == 0)
            {
                JObject res = new JObject();
                res.Add("result", false);
                res.Add("err", WxErrors.WXKITS_FAILED_TO_GET_TOKEN.Code);
                res.Add("msg", WxErrors.WXKITS_FAILED_TO_GET_TOKEN.Description);

                Response.Write(res.ToString());
            }
            else
            {
                string action = Request.QueryString["action"];
                if (action == null)
                {
                    action = "";
                }

                int    nError  = 0;
                string sErrMsg = "";

                #region 添加菜单
                if (action == "addMenu")
                {
                    byte[] bReq = Request.BinaryRead(Request.TotalBytes);
                    if (bReq.Length > 0)
                    {
                        string sReq = Encoding.UTF8.GetString(bReq);
                        if (sReq.Length > 0)
                        {
                            try
                            {
                                JObject jReq = JObject.Parse(sReq);
                                WxMenuHandler.Create(ref jReq, ref nError, ref sErrMsg);
                            }
                            catch (Exception ex)
                            {
                                nError  = WxErrors.UNKNOWN_EXCEPTION.Code;
                                sErrMsg = ex.ToString();
                            }
                        }
                        else
                        {
                            nError  = WxErrors.UNKNOWN_EXCEPTION.Code;
                            sErrMsg = "The request is invalid!";
                        }
                    }
                    else
                    {
                        nError  = WxErrors.UNKNOWN_EXCEPTION.Code;
                        sErrMsg = "The request is empty!";
                    }
                }
                #endregion

                #region  除菜单
                else if (action == "delMenu")
                {
                    WxMenuHandler.Delete(ref nError, ref sErrMsg);
                }
                #endregion

                #region 发送模板消息

                /*
                 * {
                 *      "openid" : "openid",
                 *      "drivername" : "司机姓名",
                 *      "url" : "地址",
                 *      "orderid" : "订单编号",
                 *      "customername" : "客户名称",
                 *      "toaddress" : "收货地址",
                 *      "totime" : "收货时间",
                 *      "tocontact" : "联系电话"
                 * }
                 */
                else if (action == "sendTplMsg")
                {
                    byte[] bReq = Request.BinaryRead(Request.TotalBytes);
                    if (bReq.Length > 0)
                    {
                        string sReq = Encoding.UTF8.GetString(bReq);
                        if (sReq.Length > 0)
                        {
                            try
                            {
                                JObject jPara = JObject.Parse(sReq);
                                string  sMsg  = WxTmplMsgHandler.NewOrderWxNotice(jPara["openid"].ToString(),
                                                                                  jPara["drivername"].ToString(),
                                                                                  jPara["url"].ToString(),
                                                                                  jPara["orderid"].ToString(),
                                                                                  jPara["customername"].ToString(),
                                                                                  jPara["toaddress"].ToString(),
                                                                                  jPara["totime"].ToString(),
                                                                                  jPara["toaddress"].ToString(),
                                                                                  "");
                                JObject jReq = JObject.Parse(sMsg);

                                JObject jRes = new JObject();
                                Send(sToken, ref jReq, ref jRes);
                                if (jRes["errcode"] != null)
                                {
                                    if (jRes["errcode"].ToString() == "0")
                                    {
                                        nError  = 0;
                                        sErrMsg = jRes["msgid"].ToString();
                                    }
                                    else
                                    {
                                        nError  = Convert.ToInt32(jRes["errcode"].ToString());
                                        sErrMsg = jRes["errmsg"].ToString();
                                    }
                                }
                                else
                                {
                                    nError  = WxErrors.TENCENT_RESPONSE_FORMAT_WRONG.Code;
                                    sErrMsg = WxErrors.TENCENT_RESPONSE_FORMAT_WRONG.Description;
                                }
                            }
                            catch (Exception ex)
                            {
                                nError  = WxErrors.UNKNOWN_EXCEPTION.Code;
                                sErrMsg = ex.ToString();
                            }
                        }
                        else
                        {
                            nError  = WxErrors.UNKNOWN_EXCEPTION.Code;
                            sErrMsg = "The request is invalid!";
                        }
                    }
                    else
                    {
                        nError  = WxErrors.UNKNOWN_EXCEPTION.Code;
                        sErrMsg = "The request is empty!";
                    }
                }
                #endregion

                else
                {
                    nError  = WxErrors.UNKNOWN_EXCEPTION.Code;
                    sErrMsg = "Unsupported action!";
                }

                JObject res = new JObject();
                res.Add("result", (nError == 0));
                res.Add("err", nError);
                res.Add("msg", sErrMsg);

                Response.Write(res.ToString());
            }
        }