mirror of
http://git.coreshop.cn/jianweie/coreshoppro.git
synced 2025-12-06 16:13:26 +08:00
【修复】修复appsetting.json微信公众号配置项命名错误的问题。
【新增】增加微信公众号获取JS-Token并且全局缓存功能。 【新增】增加微信公众号获取JS-SDK使用权限签名算法。
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 获取微信公众号WeiXinJsApiTicket
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static string GetWeChatJsApiTicket()
|
||||
{
|
||||
//获取微信AccessToken
|
||||
var cacheAccessToken = ManualDataCache.Instance.Get<CgibinTicketGetTicketResponse>(GlobalEnumVars.JsApiTicketEnum.WeiXinJsApiTicket.ToString());
|
||||
return cacheAccessToken.Ticket;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2924,6 +2924,22 @@ namespace CoreCms.Net.Configuration
|
||||
/// </summary>
|
||||
YiLianYunAccessToken = 3,
|
||||
}
|
||||
|
||||
public enum JsApiTicketEnum
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// 微信小程序
|
||||
/// </summary>
|
||||
WxOpenJsApiTicket = 1,
|
||||
|
||||
/// <summary>
|
||||
/// 微信公众号
|
||||
/// </summary>
|
||||
WeiXinJsApiTicket = 2,
|
||||
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 自定义交易组件
|
||||
|
||||
@@ -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
|
||||
{
|
||||
|
||||
@@ -169,7 +169,7 @@ namespace CoreCms.Net.Utility.Helper
|
||||
}
|
||||
#endregion
|
||||
|
||||
|
||||
#region 转MD5
|
||||
/// <summary>
|
||||
/// 转MD5
|
||||
/// </summary>
|
||||
@@ -192,6 +192,7 @@ namespace CoreCms.Net.Utility.Helper
|
||||
// 返回加密的字符串
|
||||
return sb.ToString();
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region 获取32位md5加密
|
||||
/// <summary>
|
||||
@@ -636,5 +637,49 @@ namespace CoreCms.Net.Utility.Helper
|
||||
|
||||
#endregion
|
||||
|
||||
#region 获取随机字符串
|
||||
/// <summary>
|
||||
/// 获取随机字符串
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
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签名
|
||||
/// <summary>
|
||||
/// Sha1签名
|
||||
/// </summary>
|
||||
/// <param name="str">内容</param>
|
||||
/// <param name="encoding">编码</param>
|
||||
/// <returns></returns>
|
||||
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
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,7 +19,7 @@ namespace CoreCms.Net.WeChat.Service.Options
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public string WeiXinEncodingAESKey { get; set; } = string.Empty;
|
||||
public string WeiXinEncodingAesKey { get; set; } = string.Empty;
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
|
||||
@@ -221,8 +221,8 @@
|
||||
//公众号
|
||||
"WeiXinAppId": "",
|
||||
"WeiXinAppSecret": "",
|
||||
"EncodingAESKey": "",
|
||||
"Token": "",
|
||||
"WeiXinEncodingAesKey": "",
|
||||
"WeiXinToken": "",
|
||||
//小程序
|
||||
"WxOpenAppId": "",
|
||||
"WxOpenAppSecret": "",
|
||||
|
||||
@@ -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
|
||||
{
|
||||
/// <summary>
|
||||
/// 微信公众号通用接口
|
||||
/// </summary>
|
||||
[Route("api/[controller]/[action]")]
|
||||
[ApiController]
|
||||
public class WeChatOffiaccountController : Controller
|
||||
{
|
||||
private readonly WeChatOptions _weChatOptions;
|
||||
|
||||
/// <summary>
|
||||
/// 构造函数
|
||||
/// </summary>
|
||||
/// <param name="weChatOptions"></param>
|
||||
public WeChatOffiaccountController(IOptions<WeChatOptions> weChatOptions)
|
||||
{
|
||||
_weChatOptions = weChatOptions.Value;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// JS-SDK使用权限签名算法
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[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
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -221,8 +221,8 @@
|
||||
//公众号
|
||||
"WeiXinAppId": "",
|
||||
"WeiXinAppSecret": "",
|
||||
"EncodingAESKey": "",
|
||||
"Token": "",
|
||||
"WeiXinEncodingAesKey": "",
|
||||
"WeiXinToken": "",
|
||||
//小程序
|
||||
"WxOpenAppId": "",
|
||||
"WxOpenAppSecret": "",
|
||||
|
||||
Reference in New Issue
Block a user