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": "",