From 4430e6f4e0f2e201eebff0c801ec910d4ac2eadc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=A7=E7=81=B0=E7=81=B0?= Date: Wed, 28 Dec 2022 17:15:01 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E4=BF=AE=E5=A4=8D=E3=80=91=E4=BF=AE?= =?UTF-8?q?=E5=A4=8Dappsetting.json=E5=BE=AE=E4=BF=A1=E5=85=AC=E4=BC=97?= =?UTF-8?q?=E5=8F=B7=E9=85=8D=E7=BD=AE=E9=A1=B9=E5=91=BD=E5=90=8D=E9=94=99?= =?UTF-8?q?=E8=AF=AF=E7=9A=84=E9=97=AE=E9=A2=98=E3=80=82=20=E3=80=90?= =?UTF-8?q?=E6=96=B0=E5=A2=9E=E3=80=91=E5=A2=9E=E5=8A=A0=E5=BE=AE=E4=BF=A1?= =?UTF-8?q?=E5=85=AC=E4=BC=97=E5=8F=B7=E8=8E=B7=E5=8F=96JS-Token=E5=B9=B6?= =?UTF-8?q?=E4=B8=94=E5=85=A8=E5=B1=80=E7=BC=93=E5=AD=98=E5=8A=9F=E8=83=BD?= =?UTF-8?q?=E3=80=82=20=E3=80=90=E6=96=B0=E5=A2=9E=E3=80=91=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E5=BE=AE=E4=BF=A1=E5=85=AC=E4=BC=97=E5=8F=B7=E8=8E=B7?= =?UTF-8?q?=E5=8F=96JS-SDK=E4=BD=BF=E7=94=A8=E6=9D=83=E9=99=90=E7=AD=BE?= =?UTF-8?q?=E5=90=8D=E7=AE=97=E6=B3=95=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../WeChatCacheAccessTokenHelper.cs | 15 +++++ CoreCms.Net.Configuration/GlobalEnumVars.cs | 16 +++++ .../RefreshWeChatAccessTokenJob.cs | 12 ++++ CoreCms.Net.Utility/Helper/CommonHelper.cs | 49 ++++++++++++++- .../Options/WechatOptions.cs | 2 +- CoreCms.Net.Web.Admin/appsettings.json | 4 +- .../WeChatOffiaccountController.cs | 59 +++++++++++++++++++ CoreCms.Net.Web.WebApi/appsettings.json | 4 +- 8 files changed, 154 insertions(+), 7 deletions(-) create mode 100644 CoreCms.Net.Web.WebApi/Controllers/WeChatOffiaccountController.cs diff --git a/CoreCms.Net.Caching/AccressToken/WeChatCacheAccessTokenHelper.cs b/CoreCms.Net.Caching/AccressToken/WeChatCacheAccessTokenHelper.cs index 64eb7e64..0ee8b340 100644 --- a/CoreCms.Net.Caching/AccressToken/WeChatCacheAccessTokenHelper.cs +++ b/CoreCms.Net.Caching/AccressToken/WeChatCacheAccessTokenHelper.cs @@ -13,6 +13,7 @@ using System.Threading.Tasks; using CoreCms.Net.Caching.Manual; using CoreCms.Net.Configuration; using CoreCms.Net.Model.Entities; +using SKIT.FlurlHttpClient.Wechat.Api.Models; namespace CoreCms.Net.Caching.AccressToken { @@ -43,5 +44,19 @@ namespace CoreCms.Net.Caching.AccressToken return cacheAccessToken?.accessToken; } + + /// + /// 获取微信公众号WeiXinJsApiTicket + /// + /// + public static string GetWeChatJsApiTicket() + { + //获取微信AccessToken + var cacheAccessToken = ManualDataCache.Instance.Get(GlobalEnumVars.JsApiTicketEnum.WeiXinJsApiTicket.ToString()); + return cacheAccessToken.Ticket; + } + + + } } diff --git a/CoreCms.Net.Configuration/GlobalEnumVars.cs b/CoreCms.Net.Configuration/GlobalEnumVars.cs index e4cfb621..3bb27822 100644 --- a/CoreCms.Net.Configuration/GlobalEnumVars.cs +++ b/CoreCms.Net.Configuration/GlobalEnumVars.cs @@ -2924,6 +2924,22 @@ namespace CoreCms.Net.Configuration /// YiLianYunAccessToken = 3, } + + public enum JsApiTicketEnum + { + + /// + /// 微信小程序 + /// + WxOpenJsApiTicket = 1, + + /// + /// 微信公众号 + /// + WeiXinJsApiTicket = 2, + + } + #endregion #region 自定义交易组件 diff --git a/CoreCms.Net.Task/RefreshWeChatAccessTokenJob.cs b/CoreCms.Net.Task/RefreshWeChatAccessTokenJob.cs index fe394989..e416d73b 100644 --- a/CoreCms.Net.Task/RefreshWeChatAccessTokenJob.cs +++ b/CoreCms.Net.Task/RefreshWeChatAccessTokenJob.cs @@ -102,6 +102,18 @@ namespace CoreCms.Net.Task } await _redisOperationRepository.SetAsync(GlobalEnumVars.AccessTokenEnum.WeiXinAccessToken.ToString(), entity, TimeSpan.FromMinutes(120)); + //获取js-token并且全局缓存 + var requestJsTicket = new CgibinTicketGetTicketRequest(); + requestJsTicket.AccessToken = response.AccessToken; + + var responseJsTicket = await client.ExecuteCgibinTicketGetTicketAsync(requestJsTicket); + if (responseJsTicket.IsSuccessful()) + { + //插入缓存 + await _redisOperationRepository.SetAsync(GlobalEnumVars.JsApiTicketEnum.WeiXinJsApiTicket.ToString(), responseJsTicket, TimeSpan.FromMinutes(120)); + } + + //插入日志 var model = new SysTaskLog { diff --git a/CoreCms.Net.Utility/Helper/CommonHelper.cs b/CoreCms.Net.Utility/Helper/CommonHelper.cs index 7d436212..0f93408f 100644 --- a/CoreCms.Net.Utility/Helper/CommonHelper.cs +++ b/CoreCms.Net.Utility/Helper/CommonHelper.cs @@ -169,7 +169,7 @@ namespace CoreCms.Net.Utility.Helper } #endregion - + #region 转MD5 /// /// 转MD5 /// @@ -191,7 +191,8 @@ namespace CoreCms.Net.Utility.Helper } // 返回加密的字符串 return sb.ToString(); - } + } + #endregion #region 获取32位md5加密 /// @@ -636,5 +637,49 @@ namespace CoreCms.Net.Utility.Helper #endregion + #region 获取随机字符串 + /// + /// 获取随机字符串 + /// + /// + public static string GetSerialNumber() + { + var str = string.Empty; + Random rand = new Random(); + var charsStr2 = new[] { 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'P', 'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', 'Z', 'X', 'C', 'V', 'B', 'N', 'M', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0' }; + var charsLen2 = charsStr2.Length - 1; + // shuffle($chars); + str = ""; + for (int i = 0; i < 16; i++) + { + str += charsStr2[rand.Next(0, charsLen2)]; + } + return str; + } + + #endregion + + #region Sha1签名 + /// + /// Sha1签名 + /// + /// 内容 + /// 编码 + /// + public static string Sha1Signature(string str, Encoding encoding = null) + { + if (encoding == null) encoding = Encoding.UTF8; + var buffer = encoding.GetBytes(str); + var data = SHA1.Create().ComputeHash(buffer); + StringBuilder sub = new StringBuilder(); + foreach (var t in data) + { + sub.Append(t.ToString("x2")); + } + + return sub.ToString(); + } + #endregion + } } diff --git a/CoreCms.Net.WeChat.Service/Options/WechatOptions.cs b/CoreCms.Net.WeChat.Service/Options/WechatOptions.cs index 39c577b4..d80b5738 100644 --- a/CoreCms.Net.WeChat.Service/Options/WechatOptions.cs +++ b/CoreCms.Net.WeChat.Service/Options/WechatOptions.cs @@ -19,7 +19,7 @@ namespace CoreCms.Net.WeChat.Service.Options /// /// /// - public string WeiXinEncodingAESKey { get; set; } = string.Empty; + public string WeiXinEncodingAesKey { get; set; } = string.Empty; /// /// /// diff --git a/CoreCms.Net.Web.Admin/appsettings.json b/CoreCms.Net.Web.Admin/appsettings.json index 6722b789..02059ca1 100644 --- a/CoreCms.Net.Web.Admin/appsettings.json +++ b/CoreCms.Net.Web.Admin/appsettings.json @@ -221,8 +221,8 @@ //公众号 "WeiXinAppId": "", "WeiXinAppSecret": "", - "EncodingAESKey": "", - "Token": "", + "WeiXinEncodingAesKey": "", + "WeiXinToken": "", //小程序 "WxOpenAppId": "", "WxOpenAppSecret": "", diff --git a/CoreCms.Net.Web.WebApi/Controllers/WeChatOffiaccountController.cs b/CoreCms.Net.Web.WebApi/Controllers/WeChatOffiaccountController.cs new file mode 100644 index 00000000..2c6734b1 --- /dev/null +++ b/CoreCms.Net.Web.WebApi/Controllers/WeChatOffiaccountController.cs @@ -0,0 +1,59 @@ +using CoreCms.Net.Caching.AccressToken; +using CoreCms.Net.WeChat.Service.Options; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Options; + +namespace CoreCms.Net.Web.WebApi.Controllers +{ + /// + /// 微信公众号通用接口 + /// + [Route("api/[controller]/[action]")] + [ApiController] + public class WeChatOffiaccountController : Controller + { + private readonly WeChatOptions _weChatOptions; + + /// + /// 构造函数 + /// + /// + public WeChatOffiaccountController(IOptions weChatOptions) + { + _weChatOptions = weChatOptions.Value; + } + + + /// + /// JS-SDK使用权限签名算法 + /// + /// + [HttpPost] + [Authorize] + public IActionResult GetWeChatConfig() + { + //获取全局缓存的jsapi_ticket + var jsApiTicket = WeChatCacheAccessTokenHelper.GetWeChatJsApiTicket(); + var nonceStr = CoreCms.Net.Utility.Helper.CommonHelper.GetSerialNumber(); + var timestamp = CoreCms.Net.Utility.Helper.CommonHelper.GetTimeStampByTotalSeconds().ToString(); + var url = Request.GetTypedHeaders().Referer; + //获取前面 + var signatureStr = "jsapi_ticket=" + jsApiTicket + "&noncestr=" + nonceStr + "×tamp=" + timestamp + "&url=" + url; + var signature = CoreCms.Net.Utility.Helper.CommonHelper.Sha1Signature(signatureStr); + + return Json(new + { + jsApiTicket, + appId = _weChatOptions.WeiXinAppId, + timestamp, + nonceStr, + signature, + url + }); + } + + + } +} diff --git a/CoreCms.Net.Web.WebApi/appsettings.json b/CoreCms.Net.Web.WebApi/appsettings.json index 6722b789..02059ca1 100644 --- a/CoreCms.Net.Web.WebApi/appsettings.json +++ b/CoreCms.Net.Web.WebApi/appsettings.json @@ -221,8 +221,8 @@ //公众号 "WeiXinAppId": "", "WeiXinAppSecret": "", - "EncodingAESKey": "", - "Token": "", + "WeiXinEncodingAesKey": "", + "WeiXinToken": "", //小程序 "WxOpenAppId": "", "WxOpenAppSecret": "",