From 46d22e489fe615620fc96f7fb8f412c9706a6c0a Mon Sep 17 00:00:00 2001 From: liangkun Date: Sun, 9 Oct 2022 10:19:47 +0800 Subject: [PATCH] =?UTF-8?q?GetWeixinWebaccess=5Ftoken=20=E6=97=A5=E5=BF=97?= =?UTF-8?q?=E8=AE=B0=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Modules/WeixinApi.cs | 59 +++++++++++-------- 1 file changed, 36 insertions(+), 23 deletions(-) diff --git a/Learun.Framework.Ultimate V7/Learun.Application.WebApi/Modules/WeixinApi.cs b/Learun.Framework.Ultimate V7/Learun.Application.WebApi/Modules/WeixinApi.cs index 36e9881ad..2d3415a96 100644 --- a/Learun.Framework.Ultimate V7/Learun.Application.WebApi/Modules/WeixinApi.cs +++ b/Learun.Framework.Ultimate V7/Learun.Application.WebApi/Modules/WeixinApi.cs @@ -323,43 +323,56 @@ namespace Learun.Application.WebApi.Modules public Response GetWeixinWebaccess_token(dynamic _) { - var entity = weChatConfigIbll.GetEnableEntity(); - string appid = entity?.APPId; - string secret = entity?.secret; - string url = Request.Query["url"]; - var responsejson = HttpGet("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appid + "&secret=" + secret); - if (!string.IsNullOrEmpty(responsejson)) + try { - var weixintokenobj = JsonConvert.DeserializeObject(responsejson); - if (string.IsNullOrEmpty(weixintokenobj.errcode)) + var entity = weChatConfigIbll.GetEnableEntity(); + string appid = entity?.APPId; + string secret = entity?.secret; + string url = Request.Query["url"]; + string responsejson = HttpGet("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appid + "&secret=" + secret); + //日志记录 + LogEntity log = new LogEntity(); + log.F_CategoryId = 123; + log.F_ExecuteResultJson = responsejson; + log.WriteLog(); + if (!string.IsNullOrEmpty(responsejson)) { - string access_token = weixintokenobj.access_token; - string jsapi_ticket = HttpGet("https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=" + access_token + "&type=jsapi"); - var weixinjsapiticketobj = JsonConvert.DeserializeObject(jsapi_ticket); - if (weixinjsapiticketobj.errcode == 0) + var weixintokenobj = JsonConvert.DeserializeObject(responsejson); + if (string.IsNullOrEmpty(weixintokenobj.errcode)) { - string ticket = weixinjsapiticketobj.ticket; - //生成签名 - string noncestr = Util.CommonHelper.CreateNo(); - string timestamp = GetTimeStamp(true); - string param = "jsapi_ticket=" + ticket + "&noncestr=" + noncestr + "×tamp=" + timestamp + "&url=" + url; - string certificate = SHA1(param).ToLower(); - return Success(new { appid, noncestr, timestamp, certificate }); + string access_token = weixintokenobj.access_token; + string jsapi_ticket = HttpGet("https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=" + access_token + "&type=jsapi"); + var weixinjsapiticketobj = JsonConvert.DeserializeObject(jsapi_ticket); + if (weixinjsapiticketobj.errcode == 0) + { + string ticket = weixinjsapiticketobj.ticket; + //生成签名 + string noncestr = Util.CommonHelper.CreateNo(); + string timestamp = GetTimeStamp(true); + string param = "jsapi_ticket=" + ticket + "&noncestr=" + noncestr + "×tamp=" + timestamp + "&url=" + url; + string certificate = SHA1(param).ToLower(); + return Success(new { appid, noncestr, timestamp, certificate }); + } + else + { + return Fail("微信授权失败,请重试。" + weixinjsapiticketobj.errcode + weixinjsapiticketobj.errmsg); + } } else { - return Fail("微信授权失败,请重试。" + weixinjsapiticketobj.errcode + weixinjsapiticketobj.errmsg); + return Fail("微信授权失败,请重试。" + weixintokenobj.errcode + weixintokenobj.errmsg); } } else { - return Fail("微信授权失败,请重试。" + weixintokenobj.errcode + weixintokenobj.errmsg); + return Fail("微信授权失败,请重试。"); } } - else + catch (Exception e) { - return Fail("微信授权失败,请重试。"); + return Fail("微信授权失败,请重试。"+e.Message); } + } ///