😎1、修复token失效登录及权限问题 2、优化系统升级版本号相关处理代码 3、清理代码及同步前端请求文件
This commit is contained in:
parent
87af743d88
commit
5ee5f2e1d6
@ -19,6 +19,7 @@ public class Startup : AppStartup
|
||||
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 初始化与升级数据,在初始化表结构之前调用
|
||||
/// </summary>
|
||||
@ -29,6 +30,7 @@ public class Startup : AppStartup
|
||||
{
|
||||
// 比较版本号对数据库进行升级结构、种子数据等
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 初始化与升级数据,在初始化种子数据之后调用
|
||||
/// </summary>
|
||||
@ -57,6 +59,7 @@ public class TestStartup : AppStartup
|
||||
{
|
||||
// 比较版本号对数据库进行升级结构、种子数据等
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 初始化与升级数据,在初始化种子数据之后调用
|
||||
/// </summary>
|
||||
|
||||
@ -15,8 +15,9 @@ public class ConfigConst
|
||||
/// 系统版本code
|
||||
/// </summary>
|
||||
public const string SysVersion = "sys_version";
|
||||
|
||||
/// <summary>
|
||||
/// 当前系统版本值,标准版本号为 x.xx.xxxxxx。比如 1.01.250409,也可以是变体,比如 1.2.5 v1.2.5
|
||||
/// 当前系统版本值,标准版本号为 x.xx.xxxxxx。比如 1.01.250409,也可以是变体,比如 1.2.5 => v1.2.5
|
||||
/// </summary>
|
||||
public const string SysCurrentVersion = "1.01.000001";
|
||||
|
||||
|
||||
@ -40,7 +40,7 @@ public class SysConfigSeedData : ISqlSugarEntitySeedData<SysConfig>
|
||||
new SysConfig{ Id=1300000000271, Name="显示系统更新日志", Code=ConfigConst.SysUpgrade, Value="True", SysFlag=YesNoEnum.Y, Remark="是否显示系统更新日志", OrderNo=220, GroupCode=ConfigConst.SysDefaultGroup, CreateTime=DateTime.Parse("2024-12-20 00:00:00") },
|
||||
new SysConfig{ Id=1300000000281, Name="开启多语言切换", Code=ConfigConst.SysI18NSwitch, Value="True", SysFlag=YesNoEnum.Y, Remark="是否显示多语言切换按钮", OrderNo=230, GroupCode=ConfigConst.SysDefaultGroup, CreateTime=DateTime.Parse("2024-12-20 00:00:00") },
|
||||
|
||||
new SysConfig{ Id=1300000000999, Name="系统版本", Code=ConfigConst.SysVersion, Value="0", SysFlag=YesNoEnum.Y, Remark= "系统版本,用于自动升级,请勿手动填写", OrderNo=1000, GroupCode=ConfigConst.SysDefaultGroup, CreateTime=DateTime.Parse("2022-02-10 00:00:00") },
|
||||
new SysConfig{ Id=1300000000999, Name="系统版本号", Code=ConfigConst.SysVersion, Value="0", SysFlag=YesNoEnum.Y, Remark= "系统版本号,用于自动升级,请勿手动填写", OrderNo=1000, GroupCode=ConfigConst.SysDefaultGroup, CreateTime=DateTime.Parse("2025-04-10 00:00:00") },
|
||||
];
|
||||
}
|
||||
}
|
||||
@ -101,7 +101,7 @@ public class DeleteUserInput : BaseIdInput
|
||||
public class ResetPwdUserInput : BaseIdInput
|
||||
{
|
||||
/// <summary>
|
||||
/// 新密码(如果为空使用系统配置的默认密码)
|
||||
/// 新密码(若空则用默认密码)
|
||||
/// </summary>
|
||||
public string NewPassword { get; set; }
|
||||
}
|
||||
|
||||
@ -373,7 +373,7 @@ public class SysUserService : IDynamicApiController, ITransient
|
||||
public virtual async Task<string> ResetPwd(ResetPwdUserInput input)
|
||||
{
|
||||
var user = await _sysUserRep.GetByIdAsync(input.Id) ?? throw Oops.Oh(ErrorCodeEnum.D0009);
|
||||
string password = await _sysConfigService.GetConfigValueByCode<string>(ConfigConst.SysPassword);
|
||||
var password = await _sysConfigService.GetConfigValueByCode<string>(ConfigConst.SysPassword);
|
||||
if (!string.IsNullOrEmpty(input.NewPassword))
|
||||
password = input.NewPassword;
|
||||
user.Password = CryptogramUtil.Encrypt(password);
|
||||
|
||||
@ -352,11 +352,6 @@ public static class SqlSugarSetup
|
||||
};
|
||||
}
|
||||
|
||||
private static int GetStartupOrder(Type type)
|
||||
{
|
||||
return !type.IsDefined(typeof(AppStartupAttribute), true) ? 0 : type.GetCustomAttribute<AppStartupAttribute>(true).Order;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 初始化数据库
|
||||
/// </summary>
|
||||
@ -373,45 +368,8 @@ public static class SqlSugarSetup
|
||||
if (config.DbType != SqlSugar.DbType.Oracle) dbProvider.DbMaintenance.CreateDatabase();
|
||||
}
|
||||
|
||||
// 扫描所有继承 AppStartup 的类
|
||||
var startups = App.EffectiveTypes
|
||||
.Where(u => typeof(AppStartup).IsAssignableFrom(u) && u.IsClass && !u.IsAbstract && !u.IsGenericType)
|
||||
.OrderByDescending(u => GetStartupOrder(u));
|
||||
//数据库前处理
|
||||
long oldVerion = 0, currentVersion = 0;
|
||||
SysConfig versionCfg = null;
|
||||
try
|
||||
{
|
||||
//获取旧版本号
|
||||
try
|
||||
{
|
||||
if (dbProvider.CurrentConnectionConfig.ConfigId.ToString() == SqlSugarConst.MainConfigId)
|
||||
{
|
||||
versionCfg = dbProvider.Queryable<SysConfig>().Where(n => n.Code == ConfigConst.SysVersion).First();
|
||||
oldVerion = versionCfg != null ? CommonUtil.ConvertVersionToLong(versionCfg.Value) : 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
oldVerion = -1;
|
||||
}
|
||||
}
|
||||
catch (Exception ex) { }
|
||||
finally
|
||||
{
|
||||
currentVersion = CommonUtil.ConvertVersionToLong(ConfigConst.SysCurrentVersion);
|
||||
}
|
||||
//执行
|
||||
foreach (var type in startups)
|
||||
{
|
||||
var startup = Activator.CreateInstance(type) as AppStartup;
|
||||
var initDataMethod = type.GetMethod("BeforeInitTable");
|
||||
initDataMethod?.Invoke(startup, new[] { (object)dbProvider, oldVerion, currentVersion });
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Information($"数据库前处理有错 {config.DbType} - {config.ConfigId} : {ex.Message}");
|
||||
}
|
||||
// 初始化表结构之前——系统版本号
|
||||
var (startups, oldVerion, currentVersion) = BeforeInitTable(dbProvider, config);
|
||||
|
||||
// 初始化表结构
|
||||
if (config.TableSettings.EnableInitTable)
|
||||
@ -454,34 +412,99 @@ public static class SqlSugarSetup
|
||||
// 初始化种子数据
|
||||
if (config.SeedSettings.EnableInitSeed) InitSeedData(db, config);
|
||||
|
||||
//数据后处理
|
||||
// 初始化种子数据之后——系统版本号
|
||||
AfterInitSeed(dbProvider, config, startups, oldVerion, currentVersion);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 初始化表结构之前(版本号)
|
||||
/// </summary>
|
||||
/// <param name="dbProvider"></param>
|
||||
/// <param name="config"></param>
|
||||
/// <returns></returns>
|
||||
private static (IOrderedEnumerable<Type> startups, long oldVerion, long currentVersion) BeforeInitTable(SqlSugarScopeProvider dbProvider, DbConnectionConfig config)
|
||||
{
|
||||
// 扫描所有继承 AppStartup 的类(排序执行顺序)
|
||||
var startups = App.EffectiveTypes
|
||||
.Where(u => typeof(AppStartup).IsAssignableFrom(u) && u.IsClass && !u.IsAbstract && !u.IsGenericType && u.GetMethod("BeforeInitTable") != null)
|
||||
.OrderByDescending(u => !u.IsDefined(typeof(AppStartupAttribute), true) ? 0 : u.GetCustomAttribute<AppStartupAttribute>(true).Order);
|
||||
if (startups == null || !startups.Any()) return (startups, 0, 0);
|
||||
|
||||
long oldVerion = 0, currentVersion = 0;
|
||||
SysConfig versionCfg = null;
|
||||
try
|
||||
{
|
||||
try
|
||||
{
|
||||
// 获取系统版本号
|
||||
if (dbProvider.CurrentConnectionConfig.ConfigId.ToString() == SqlSugarConst.MainConfigId)
|
||||
{
|
||||
versionCfg = dbProvider.Queryable<SysConfig>().Where(u => u.Code == ConfigConst.SysVersion).First();
|
||||
oldVerion = versionCfg != null ? CommonUtil.ConvertVersionToLong(versionCfg.Value) : 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
oldVerion = -1;
|
||||
}
|
||||
}
|
||||
catch { }
|
||||
finally
|
||||
{
|
||||
currentVersion = CommonUtil.ConvertVersionToLong(ConfigConst.SysCurrentVersion);
|
||||
}
|
||||
foreach (var type in startups)
|
||||
{
|
||||
var startup = Activator.CreateInstance(type) as AppStartup;
|
||||
var initDataMethod = type.GetMethod("BeforeInitTable");
|
||||
initDataMethod?.Invoke(startup, [dbProvider, oldVerion, currentVersion]);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Information($"数据库初始化表结构之前有错 {config.DbType} - {config.ConfigId} : {ex.Message}");
|
||||
}
|
||||
|
||||
return (startups, oldVerion, currentVersion);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 初始化种子数据之后(版本号)
|
||||
/// </summary>
|
||||
/// <param name="dbProvider"></param>
|
||||
/// <param name="config"></param>
|
||||
/// <param name="startups"></param>
|
||||
/// <param name="oldVerion"></param>
|
||||
/// <param name="currentVersion"></param>
|
||||
private static void AfterInitSeed(SqlSugarScopeProvider dbProvider, DbConnectionConfig config, IOrderedEnumerable<Type> startups, long oldVerion, long currentVersion)
|
||||
{
|
||||
if (startups == null || !startups.Any()) return;
|
||||
|
||||
try
|
||||
{
|
||||
//执行后处理
|
||||
foreach (var type in startups)
|
||||
{
|
||||
var startup = Activator.CreateInstance(type) as AppStartup;
|
||||
if (startup == null) continue;
|
||||
var initDataMethod = type.GetMethod("AfterInitSeed");
|
||||
if (initDataMethod == null) continue;
|
||||
initDataMethod?.Invoke(startup, new[] { (object)dbProvider, oldVerion, currentVersion });
|
||||
initDataMethod?.Invoke(startup, [dbProvider, oldVerion, currentVersion]);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
string errr = $"数据库后处理有错 {config.DbType} - {config.ConfigId} : {ex.Message}";
|
||||
string errr = $"数据库初始化种子数据之后有错 {config.DbType} - {config.ConfigId} : {ex.Message}";
|
||||
Log.Information(errr);
|
||||
Console.WriteLine(errr);
|
||||
Console.WriteLine(ex.StackTrace);
|
||||
}
|
||||
finally
|
||||
{
|
||||
//最后更新版本
|
||||
// 更新系统版本号
|
||||
if (dbProvider.CurrentConnectionConfig.ConfigId.ToString() == SqlSugarConst.MainConfigId)
|
||||
{
|
||||
IEnumerable<SysConfig> cfgs =
|
||||
[
|
||||
new SysConfig{ Id=1300000000999, Name="系统版本", Code=ConfigConst.SysVersion, Value=ConfigConst.SysCurrentVersion, SysFlag=YesNoEnum.Y, Remark= "系统版本,用于自动升级,请勿手动填写", OrderNo=1000, GroupCode=ConfigConst.SysDefaultGroup, CreateTime=DateTime.Parse("2022-02-10 00:00:00") }
|
||||
new SysConfig{ Id=1300000000999, Name="系统版本号", Code=ConfigConst.SysVersion, Value=ConfigConst.SysCurrentVersion, SysFlag=YesNoEnum.Y, Remark= "系统版本号,用于自动升级,请勿手动填写", OrderNo=1000, GroupCode=ConfigConst.SysDefaultGroup, CreateTime=DateTime.Parse("2025-04-10 00:00:00") }
|
||||
];
|
||||
var storage = dbProvider.StorageableByObject(cfgs.ToList()).ToStorage();
|
||||
storage.AsInsertable.ExecuteCommand();
|
||||
|
||||
@ -39,6 +39,7 @@ public static class CommonUtil
|
||||
return Math.Abs(hash1 + (hash2 * 1566083941));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将版本号转换为长整型,版本格式要求 x.xx.xxxxxx,比如 v1.2.5=>102000005 V3.10.42=>310000042
|
||||
/// </summary>
|
||||
@ -82,6 +83,7 @@ public static class CommonUtil
|
||||
throw new ArgumentException("版本号转换失败,结果超出 long 范围。");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 生成百分数
|
||||
/// </summary>
|
||||
|
||||
@ -30,20 +30,36 @@ namespace Admin.NET.Web.Core
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 自动刷新Token
|
||||
/// 令牌/Token校验核心逻辑
|
||||
/// </summary>
|
||||
/// <param name="context"></param>
|
||||
/// <param name="httpContext"></param>
|
||||
/// <returns></returns>
|
||||
public override async Task HandleAsync(AuthorizationHandlerContext context, DefaultHttpContext httpContext)
|
||||
{
|
||||
// var serviceProvider = context.GetCurrentHttpContext().RequestServices;
|
||||
using var serviceScope = _serviceProvider.CreateScope();
|
||||
var sysCacheService = serviceScope.ServiceProvider.GetRequiredService<SysCacheService>();
|
||||
|
||||
var sysConfigService = serviceScope.ServiceProvider.GetRequiredService<SysConfigService>();
|
||||
var tokenExpire = await sysConfigService.GetTokenExpire();
|
||||
var refreshTokenExpire = await sysConfigService.GetRefreshTokenExpire();
|
||||
if (JWTEncryption.AutoRefreshToken(context, context.GetCurrentHttpContext(), tokenExpire, refreshTokenExpire))
|
||||
{
|
||||
await AuthorizeHandleAsync(context);
|
||||
}
|
||||
else
|
||||
{
|
||||
context.Fail(new AuthorizationFailureReason(this, "登录已过期,请重新登录。"));
|
||||
var currentHttpContext = context.GetCurrentHttpContext();
|
||||
// 跳过签名 SignatureAuthentication 引发的失败
|
||||
if (currentHttpContext.Items.ContainsKey(SignatureAuthenticationDefaults.AuthenticateFailMsgKey)) return;
|
||||
currentHttpContext.SignoutToSwagger();
|
||||
return;
|
||||
}
|
||||
|
||||
// 验证Token版本号
|
||||
var userId = context.User.FindFirst(ClaimConst.UserId)?.Value;
|
||||
var tokenVersion1 = context.User.FindFirst(ClaimConst.TokenVersion)?.Value;
|
||||
var userId = httpContext.User.FindFirst(ClaimConst.UserId)?.Value;
|
||||
var tokenVersion1 = httpContext.User.FindFirst(ClaimConst.TokenVersion)?.Value;
|
||||
var tokenVersion2 = sysCacheService.Get<string>($"{CacheConst.KeyUserToken}{userId}");
|
||||
if (string.IsNullOrWhiteSpace(tokenVersion2) && !string.IsNullOrWhiteSpace(userId))
|
||||
{
|
||||
@ -66,7 +82,7 @@ namespace Admin.NET.Web.Core
|
||||
}
|
||||
|
||||
// 验证租户有效期
|
||||
var tenantId = context.User.FindFirst(ClaimConst.TenantId)?.Value;
|
||||
var tenantId = httpContext.User.FindFirst(ClaimConst.TenantId)?.Value;
|
||||
if (!string.IsNullOrWhiteSpace(tenantId))
|
||||
{
|
||||
var tenant = sysCacheService.Get<List<SysTenant>>(CacheConst.KeyTenant)?.FirstOrDefault(u => u.Id == long.Parse(tenantId));
|
||||
@ -74,26 +90,8 @@ namespace Admin.NET.Web.Core
|
||||
{
|
||||
context.Fail(new AuthorizationFailureReason(this, "租户已过期,请联系管理员。"));
|
||||
context.GetCurrentHttpContext().SignoutToSwagger();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
var sysConfigService = serviceScope.ServiceProvider.GetRequiredService<SysConfigService>();
|
||||
var tokenExpire = await sysConfigService.GetTokenExpire();
|
||||
var refreshTokenExpire = await sysConfigService.GetRefreshTokenExpire();
|
||||
if (JWTEncryption.AutoRefreshToken(context, context.GetCurrentHttpContext(), tokenExpire, refreshTokenExpire))
|
||||
{
|
||||
await AuthorizeHandleAsync(context);
|
||||
}
|
||||
else
|
||||
{
|
||||
// 授权失败
|
||||
context.Fail();
|
||||
var currentHttpContext = context.GetCurrentHttpContext();
|
||||
// 跳过签名 SignatureAuthentication 引发的失败
|
||||
if (currentHttpContext.Items.ContainsKey(SignatureAuthenticationDefaults.AuthenticateFailMsgKey)) return;
|
||||
currentHttpContext.SignoutToSwagger();
|
||||
}
|
||||
}
|
||||
|
||||
public override async Task<bool> PipelineAsync(AuthorizationHandlerContext context, DefaultHttpContext httpContext)
|
||||
|
||||
@ -703,7 +703,7 @@ export const SysWechatPayApiAxiosParamCreator = function (configuration?: Config
|
||||
};
|
||||
},
|
||||
/**
|
||||
* 必填字段:OutTradeNumber,Reason
|
||||
*
|
||||
* @summary 退款申请 🔖 https://pay.weixin.qq.com/docs/merchant/apis/mini-program-payment/create.html
|
||||
* @param {RefundRequestInput} [body]
|
||||
* @param {*} [options] Override http request option.
|
||||
@ -954,7 +954,7 @@ export const SysWechatPayApiFp = function(configuration?: Configuration) {
|
||||
};
|
||||
},
|
||||
/**
|
||||
* 必填字段:OutTradeNumber,Reason
|
||||
*
|
||||
* @summary 退款申请 🔖 https://pay.weixin.qq.com/docs/merchant/apis/mini-program-payment/create.html
|
||||
* @param {RefundRequestInput} [body]
|
||||
* @param {*} [options] Override http request option.
|
||||
@ -1115,7 +1115,7 @@ export const SysWechatPayApiFactory = function (configuration?: Configuration, b
|
||||
return SysWechatPayApiFp(configuration).apiSysWechatPayRefundListGet(transactionId, outTradeNumber, options).then((request) => request(axios, basePath));
|
||||
},
|
||||
/**
|
||||
* 必填字段:OutTradeNumber,Reason
|
||||
*
|
||||
* @summary 退款申请 🔖 https://pay.weixin.qq.com/docs/merchant/apis/mini-program-payment/create.html
|
||||
* @param {RefundRequestInput} [body]
|
||||
* @param {*} [options] Override http request option.
|
||||
@ -1287,7 +1287,7 @@ export class SysWechatPayApi extends BaseAPI {
|
||||
return SysWechatPayApiFp(this.configuration).apiSysWechatPayRefundListGet(transactionId, outTradeNumber, options).then((request) => request(this.axios, this.basePath));
|
||||
}
|
||||
/**
|
||||
* 必填字段:OutTradeNumber,Reason
|
||||
*
|
||||
* @summary 退款申请 🔖 https://pay.weixin.qq.com/docs/merchant/apis/mini-program-payment/create.html
|
||||
* @param {RefundRequestInput} [body]
|
||||
* @param {*} [options] Override http request option.
|
||||
|
||||
@ -29,7 +29,7 @@ export interface ResetPwdUserInput {
|
||||
id: number;
|
||||
|
||||
/**
|
||||
* 新密码(如果为空使用系统配置的默认密码)
|
||||
* 新密码(若空则用默认密码)
|
||||
*
|
||||
* @type {string}
|
||||
* @memberof ResetPwdUserInput
|
||||
|
||||
@ -293,15 +293,11 @@ const changeStatus = (row: any) => {
|
||||
|
||||
// 重置密码
|
||||
const resetQueryPwd = async (row: any) => {
|
||||
ElMessageBox.prompt(
|
||||
t('message.list.confirmResetPassword', { account: row.account }),
|
||||
t('message.list.resetPassword'),
|
||||
{
|
||||
ElMessageBox.prompt(t('message.list.confirmResetPassword', { account: row.account }), t('message.list.resetPassword'), {
|
||||
confirmButtonText: t('message.list.confirmButtonText'),
|
||||
cancelButtonText: t('message.list.cancelButtonText'),
|
||||
type: 'warning',
|
||||
}
|
||||
)
|
||||
})
|
||||
.then(async ({ value }) => {
|
||||
await getAPI(SysUserApi)
|
||||
.apiSysUserResetPwdPost({ id: row.id, newPassword: value })
|
||||
|
||||
Loading…
Reference in New Issue
Block a user