Exemplo n.º 1
0
        public async Task <IActionResult> Service([FromServices] IWechatGateway gateway,
                                                  [FromQuery] string signature,
                                                  [FromQuery] string timestamp,
                                                  [FromQuery] string nonce,
                                                  [FromQuery] string echostr,
                                                  [FromRoute] string appID             = "",
                                                  [FromServices] ILoggerFactory logger = null
                                                  )
        {
            if (gateway == null)
            {
                return(Content("请先注册微信小程序"));
            }

            logger?.CreateLogger("miniprogram")?.Log(LogLevel.Trace, $"微信调用:signature={signature},timestamp={timestamp},nonce={nonce},echostr={echostr}");

            if (string.IsNullOrWhiteSpace(appID))
            {
                return(Content("AppID不能为空"));
            }

            var config = gateway.Get <MiniProgramConfiguration>(appID);

            if (config == null)
            {
                return(Content("该AppID非小程序配置"));
            }

            if (CheckSignature.Check(signature, timestamp, nonce, config.Token))
            {
                return(Content(echostr)); //返回随机字符串则表示验证通过
            }
            else
            {
                return(Content($"failed:{signature},{CheckSignature.GetSignature(timestamp, nonce, config.Token).ToString()}。如果你在浏览器中看到这句话,说明此地址可以被作为微信公众账号后台的Url,请注意保持Token一致。"));
            }
        }
Exemplo n.º 2
0
 public MessageService(ICommonApi api, IWechatGateway gateway) : base(api)
 {
     _gateway = gateway;
 }
Exemplo n.º 3
0
        public async Task <IActionResult> ServicePost([FromServices] IWechatGateway gateway,
                                                      [FromQuery] string signature,
                                                      [FromQuery] string timestamp,
                                                      [FromQuery] string nonce,
                                                      [FromQuery] string echostr,
                                                      [FromRoute] string appID             = "",
                                                      [FromServices] ILoggerFactory logger = null,
                                                      [FromServices] IMiniProgramMessageExecutor executor = null
                                                      )
        {
            Request.EnableBuffering();


            var jsonStr = Request.Body.ReadToEnd();

            if (Request.ContentType.Contains("json"))
            {
                var json = JObject.Parse(jsonStr);

                var eventType = json.GetString("Event");

                MiniProgramMsgBase msg = null;

                switch (eventType)
                {
                case "wxa_media_check":
                {
                    msg = new MiniProgramEvent_MediaCheck();
                    break;
                }
                }

                if (msg == null)
                {
                    return(NotFound());
                }

                msg.FromJson(json);

                if (executor != null)
                {
                    await executor.Execute(msg);
                }
            }

            return(Content(""));

            //if (gateway == null)
            //{
            //    return Content("请先注册微信公众号服务");
            //}

            //logger?.CreateLogger("weixin")?.Log(LogLevel.Trace, $"微信调用:signature={signature},timestamp={timestamp},nonce={nonce},echostr={echostr}");

            //if (string.IsNullOrWhiteSpace(appID))
            //{
            //    return Content("AppID不能为空");
            //}

            //var config = gateway.Get(appID) as MPConfiguration;

            //if (config == null)
            //{
            //    return Content("该AppID非公众号配置");
            //}

            //if (CheckSignature.Check(signature, timestamp, nonce, config.Token))
            //{
            //    return Content("校验无效,请检查token");
            //}

            //// v4.2.2之后的版本,可以设置每个人上下文消息储存的最大数量,防止内存占用过多,如果该参数小于等于0,则不限制
            //var maxRecordCount = 10;

            //Request.EnableBuffering();

            ////自定义MessageHandler,对微信请求的详细判断操作都在这里面。
            //var inputStream = Request.Body;
            //inputStream.Position = 0;

            //var xml = new XmlDocument();
            //xml.Load(inputStream);



            //return null;
        }
Exemplo n.º 4
0
        public async Task <IActionResult> ServicePost([FromServices] IWechatGateway gateway,
                                                      [FromQuery] string signature,
                                                      [FromQuery] string timestamp,
                                                      [FromQuery] string nonce,
                                                      [FromQuery] string echostr,
                                                      [FromRoute] string appID                          = "",
                                                      [FromServices] ILoggerFactory logger              = null,
                                                      [FromServices] IWechatMPApi mpApi                 = null,
                                                      [FromServices] MessageQueue msgHandler            = null,
                                                      [FromServices] IMPMessageExecutor messageExecutor = null
                                                      )
        {
            //return Content("");

            if (gateway == null)
            {
                return(Content("请先注册微信公众号服务"));
            }

            logger?.CreateLogger("weixin")?.Log(LogLevel.Trace, $"微信调用:signature={signature},timestamp={timestamp},nonce={nonce},echostr={echostr}");

            if (string.IsNullOrWhiteSpace(appID))
            {
                return(Content("AppID不能为空"));
            }

            var config = gateway.Get(appID) as MPConfiguration;

            if (config == null)
            {
                return(Content("该AppID非公众号配置或AppID不存在"));
            }

            if (CheckSignature.Check(signature, timestamp, nonce, config.Token))
            {
                return(Content("校验无效,请检查token"));
            }

            if (messageExecutor == null)
            {
                return(Content("success"));
            }

            Request.EnableBuffering();

            //自定义MessageHandler,对微信请求的详细判断操作都在这里面。
            var inputStream = Request.Body;

            inputStream.Position = 0;

            var xml = inputStream.ReadToEnd();

            var msg = mpApi.Message.DecodeMPRequestMsg(xml);

            if (msg.IsSuccess)
            {
                if (await msgHandler.AddMessage(msg.ReturnData))
                {
                    var response = await messageExecutor.Execute(msg.ReturnData);

                    if (response == null)
                    {
                        return(Content("success"));
                    }

                    //if (!string.IsNullOrWhiteSpace(config.EncryptAESKey))
                    //{
                    //    return Content(mpApi.Message.EncryptMessage(appID, response.ToXml()));
                    //}

                    return(Content(response.ToXml()));
                }
                else
                {
                    return(Content("success"));
                }
            }
            else
            {
                if (msg.ReturnCode == 1000)
                {
                    return(Content("success"));
                }

                return(Content("error"));
            }
        }
Exemplo n.º 5
0
 public OAuthService(ICommonApi api, IWechatGateway gateway, ISdkTicketContrainer sdkTicketContrainer) : base(api)
 {
     _gateway             = gateway;
     _sdkTicketContrainer = sdkTicketContrainer;
 }
Exemplo n.º 6
0
 public OAuthService(ICommonApi api, IWechatGateway gateway) : base(api)
 {
     _gateway = gateway;
 }