Files
coreshoppro/CoreCms.Net.Task/RefreshWeChatAccessTokenJob.cs
大灰灰 7b68f352d7 ### 0.6.0 专业版(大版本升级,破坏性升级,请酌情处理):
【新增】弃用现在sku前端,启用全新sku组件,更加灵活,体验更好。
【新增】新增通过商品序列获取sku全新列表功能。
【新增】仓储层底层增加二级缓存功能,后面将逐步完善底层缓存中心模块。
【新增】0元购,积分兑换模式下,也去计算用户是否科技升级。
【新增】数据及业务仓储增加二级缓存功能。curd可自主控制是否缓存和清除。
【新增】订单导出excel数据增加商品名称+货品sku组合展示的方式。
【新增】自定义交易组件增加【获取商家信息】【更新商家信息】两个接口处理。
【新增】增加公告列表及公告详情页面,首页组件公告点击跳转列表展示。
【新增】个人中心增加【公告中心】入口。
【新增】后台余额变动增加说明录入。

【调整】将前端能进行分包的文件夹都进行分包,减少主包占用,方便进行二开。
【调整】因ckeditor5存在图片不可设置宽度,上传不支持mp4,排版不畅等情况,降级使用ckeditor4版本。
【修复】修复0.5.5版本售后积分返还机制积分模式判断异常的问题。
【修复】修复使用积分全额抵扣,或其他优惠政策导致的0元购,未进行短信提醒及小票打印机未打印的问题。
【修复】修复更换ckeditor4编辑器后接龙添加编辑调用失败的问题。
【修复】修复积分全额抵扣,金额0元购的情况下,进行售后执行完毕,订单未完结的情况。
【优化】去除分销申请面板按钮无用并失效报错的customStyle属性。
【优化】优化部分方法中使用手写字符串的遗留问题,统一采用enum方式。
【优化】优化前端及接口部分命名错误的问题。错将skill误写成seckill。
【优化】去除uniapp端多个客服代码。
【优化】商品详情底部完善购物车数量显示的问题。
【优化】优化团购列表,拼团列表,秒杀页面页面样式布局差异问题。
【优化】调整支付结果界面样式效果,仿微信支付结果界面。更加清晰明朗。
【优化】优化售后提交页面json计算,开放当用户下单后但未发货情况下,可以申请直接售后的操作需求。
【优化】后台商家手机号码支持设置多个,使用小写逗号分隔,方便多个商家管理员接收下单提醒。
【优化】后台售后单审核,调整售后商品为必选项。
2022-09-14 00:53:04 +08:00

221 lines
11 KiB
C#

/***********************************************************************
* Project: CoreCms.Net *
* Web: https://CoreCms.Net *
* ProjectName: 核心内容管理系统 *
* Author: 大灰灰 *
* Email: JianWeie@163.com *
* CreateTime: 2020-08-25 1:25:29
* Description: 暂无
***********************************************************************/
using System;
using CoreCms.Net.Caching.AutoMate.RedisCache;
using CoreCms.Net.Configuration;
using CoreCms.Net.IServices;
using CoreCms.Net.Model.Entities;
using CoreCms.Net.WeChat.Service.HttpClients;
using CoreCms.Net.WeChat.Service.Options;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;
using SKIT.FlurlHttpClient;
using SKIT.FlurlHttpClient.Wechat;
using SKIT.FlurlHttpClient.Wechat.Api;
using SKIT.FlurlHttpClient.Wechat.Api.Models;
namespace CoreCms.Net.Task
{
/// <summary>
/// 定时刷新获取微信AccessToken
/// </summary>
public class RefreshWeChatAccessTokenJob
{
private readonly ISysTaskLogServices _taskLogServices;
private readonly IRedisOperationRepository _redisOperationRepository;
private readonly WeChatOptions _weChatOptions;
private readonly IWeChatApiHttpClientFactory _weChatApiHttpClientFactory;
private readonly IWeChatAccessTokenServices _weChatAccessTokenServices;
public RefreshWeChatAccessTokenJob(IRedisOperationRepository redisOperationRepository, ISysTaskLogServices taskLogServices, IOptions<WeChatOptions> weChatOptions, IWeChatApiHttpClientFactory weChatApiHttpClientFactory, IWeChatAccessTokenServices weChatAccessTokenServices)
{
_redisOperationRepository = redisOperationRepository;
_taskLogServices = taskLogServices;
_weChatApiHttpClientFactory = weChatApiHttpClientFactory;
_weChatAccessTokenServices = weChatAccessTokenServices;
_weChatOptions = weChatOptions.Value;
}
public async System.Threading.Tasks.Task Execute()
{
try
{
//微信公众号刷新
if (!string.IsNullOrEmpty(_weChatOptions.WeiXinAppId) && !string.IsNullOrEmpty(_weChatOptions.WeiXinAppSecret))
{
var entity = await _weChatAccessTokenServices.QueryByClauseAsync(p => p.appId == _weChatOptions.WeiXinAppId && p.appType == (int)GlobalEnumVars.AccessTokenEnum.WeiXinAccessToken);
if (entity == null || entity.expireTimestamp <= DateTimeOffset.Now.ToUnixTimeSeconds())
{
var client = _weChatApiHttpClientFactory.CreateWeXinClient();
var request = new CgibinTokenRequest();
var response = await client.ExecuteCgibinTokenAsync(request);
if (!response.IsSuccessful())
{
//插入日志
var log = new SysTaskLog
{
createTime = DateTime.Now,
isSuccess = false,
name = "定时刷新获取微信AccessToken",
parameters = $"刷新 AppId 为 {_weChatOptions.WeiXinAppId} 微信 AccessToken 失败(状态码:{response.RawStatus},错误代码:{response.ErrorCode},错误描述:{response.ErrorMessage})。"
};
await _taskLogServices.InsertAsync(log);
}
else
{
// 提前十分钟过期,以便于系统能及时刷新,防止因在过期临界点时出现问题
long nextExpireTimestamp = DateTimeOffset.Now.AddSeconds(response.ExpiresIn).AddMinutes(-10).ToUnixTimeSeconds();
if (entity == null)
{
entity = new WeChatAccessToken();
entity.appId = _weChatOptions.WeiXinAppId;
entity.accessToken = response.AccessToken;
entity.appType = (int)GlobalEnumVars.AccessTokenEnum.WeiXinAccessToken;
entity.expireTimestamp = nextExpireTimestamp;
entity.createTimestamp = DateTimeOffset.Now.ToUnixTimeSeconds();
entity.updateTimestamp = entity.createTimestamp;
entity.id = await _weChatAccessTokenServices.InsertAsync(entity);
}
else
{
entity.accessToken = response.AccessToken;
entity.expireTimestamp = nextExpireTimestamp;
entity.updateTimestamp = DateTimeOffset.Now.ToUnixTimeSeconds();
await _weChatAccessTokenServices.UpdateAsync(entity);
}
await _redisOperationRepository.Set(GlobalEnumVars.AccessTokenEnum.WeiXinAccessToken.ToString(), entity, TimeSpan.FromMinutes(120));
//插入日志
var model = new SysTaskLog
{
createTime = DateTime.Now,
isSuccess = true,
name = "定时刷新获取微信AccessToken",
parameters = JsonConvert.SerializeObject(entity)
};
await _taskLogServices.InsertAsync(model);
}
}
else
{
//插入日志
var model = new SysTaskLog
{
createTime = DateTime.Now,
isSuccess = true,
name = "定时刷新获取微信AccessToken",
parameters = "无需刷新AccessToken,AccessToken 未过期"
};
await _taskLogServices.InsertAsync(model);
}
}
//微信小程序也刷新
if (!string.IsNullOrEmpty(_weChatOptions.WxOpenAppId) && !string.IsNullOrEmpty(_weChatOptions.WxOpenAppSecret))
{
var entity = await _weChatAccessTokenServices.QueryByClauseAsync(p => p.appId == _weChatOptions.WxOpenAppId && p.appType == (int)GlobalEnumVars.AccessTokenEnum.WxOpenAccessToken);
if (entity == null || entity.expireTimestamp <= DateTimeOffset.Now.ToUnixTimeSeconds())
{
var client = _weChatApiHttpClientFactory.CreateWxOpenClient();
var request = new CgibinTokenRequest();
var response = await client.ExecuteCgibinTokenAsync(request);
if (response.IsSuccessful())
{
// 提前十分钟过期,以便于系统能及时刷新,防止因在过期临界点时出现问题
long nextExpireTimestamp = DateTimeOffset.Now.AddSeconds(response.ExpiresIn).AddMinutes(-10).ToUnixTimeSeconds();
if (entity == null)
{
entity = new WeChatAccessToken();
entity.appId = _weChatOptions.WxOpenAppId;
entity.accessToken = response.AccessToken;
entity.appType = (int)GlobalEnumVars.AccessTokenEnum.WxOpenAccessToken;
entity.expireTimestamp = nextExpireTimestamp;
entity.createTimestamp = DateTimeOffset.Now.ToUnixTimeSeconds();
entity.updateTimestamp = entity.createTimestamp;
await _weChatAccessTokenServices.InsertAsync(entity);
}
else
{
entity.accessToken = response.AccessToken;
entity.expireTimestamp = nextExpireTimestamp;
entity.updateTimestamp = DateTimeOffset.Now.ToUnixTimeSeconds();
await _weChatAccessTokenServices.UpdateAsync(entity);
}
await _redisOperationRepository.Set(
GlobalEnumVars.AccessTokenEnum.WxOpenAccessToken.ToString(), entity,
TimeSpan.FromMinutes(120));
//插入日志
var model = new SysTaskLog
{
createTime = DateTime.Now,
isSuccess = true,
name = "定时刷新获取微信AccessToken",
parameters = JsonConvert.SerializeObject(entity)
};
await _taskLogServices.InsertAsync(model);
}
else
{
//插入日志
var log = new SysTaskLog
{
createTime = DateTime.Now,
isSuccess = false,
name = "定时刷新获取微信AccessToken",
parameters = $"刷新 AppId 为 {_weChatOptions.WeiXinAppId} 微信 AccessToken 失败(状态码:{response.RawStatus},错误代码:{response.ErrorCode},错误描述:{response.ErrorMessage})。"
};
await _taskLogServices.InsertAsync(log);
}
}
else
{
//插入日志
var model = new SysTaskLog
{
createTime = DateTime.Now,
isSuccess = true,
name = "定时刷新获取微信AccessToken",
parameters = "无需刷新AccessToken,AccessToken 未过期"
};
await _taskLogServices.InsertAsync(model);
}
}
}
catch (Exception ex)
{
//插入日志
var model = new SysTaskLog
{
createTime = DateTime.Now,
isSuccess = false,
name = "定时刷新获取微信AccessToken",
parameters = JsonConvert.SerializeObject(ex)
};
await _taskLogServices.InsertAsync(model);
}
}
}
}