diff --git a/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Controllers/System/AccountController.cs b/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Controllers/System/AccountController.cs index 26396acd..cb7199d8 100644 --- a/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Controllers/System/AccountController.cs +++ b/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Controllers/System/AccountController.cs @@ -35,7 +35,11 @@ namespace Yi.Framework.ApiMicroservice.Controllers private SecurityCodeHelper _securityCode; private IRepository _repository; private CacheInvoker _cacheDb; - public AccountController(ILogger logger, IUserService iUserService, JwtInvoker jwtInvoker, SecurityCodeHelper securityCode, CacheInvoker cacheInvoker) + public AccountController(ILogger logger, + IUserService iUserService, + JwtInvoker jwtInvoker, + SecurityCodeHelper securityCode, + CacheInvoker cacheInvoker) { _iUserService = iUserService; _jwtInvoker = jwtInvoker; @@ -87,15 +91,38 @@ namespace Yi.Framework.ApiMicroservice.Controllers var loginInfo = HttpContext.GetLoginLogInfo(); + loginInfo.LoginUser = loginDto.UserName; loginInfo.LogMsg = "登录成功!"; + + var loginLogRepository = _repository.ChangeRepository>(); UserEntity user = new(); + + //这里其实可以返回Dto if (await _iUserService.Login(loginDto.UserName, loginDto.Password, o => user = o)) { + //根据用户id获取改用户的完整信息 var userRoleMenu = await _iUserService.GetUserAllInfo(user.Id); + + //如果该用户没有任何一个菜单,或者没有任何一个角色,无意义的登录 + if (userRoleMenu.PermissionCodes.Count == 0) + { + return Result.Error("登录禁用!该用户分配无任何权限,无意义登录!"); + } + + + //将该用户的完整信息缓存一份至缓存,后续需要完整用户信息,只需通过token中的id从缓存中获取即可 + + //先制作token + var token = _jwtInvoker.GetAccessToken(userRoleMenu.User, userRoleMenu.Menus); + + //需要注意,缓存用户信息时间应大于或等于token过期时间 + _cacheDb.Set($"Yi:UserInfo:{user.Id}", userRoleMenu, _jwtInvoker.GetTokenExpiration()); + + await loginLogRepository.InsertReturnSnowflakeIdAsync(loginInfo); - return Result.Success(loginInfo.LogMsg).SetData(new { token = _jwtInvoker.GetAccessToken(userRoleMenu.User, userRoleMenu.Menus) }); + return Result.Success(loginInfo.LogMsg).SetData(new { token }); } loginInfo.LogMsg = "登录失败!用户名或者密码错误!"; await loginLogRepository.InsertReturnSnowflakeIdAsync(loginInfo); @@ -137,11 +164,13 @@ namespace Yi.Framework.ApiMicroservice.Controllers /// /// [HttpGet] - public async Task GetUserAllInfo() + public Result GetUserAllInfo() { //通过鉴权jwt获取到用户的id var userId = HttpContext.GetUserIdInfo(); - var data = await _iUserService.GetUserAllInfo(userId); + //此处从缓存中获取即可 + var data = _cacheDb.Get($"Yi:UserInfo:{userId}"); + //var data = await _iUserService.GetUserAllInfo(userId); //系统用户数据被重置,老前端访问重新授权 if (data is null) { diff --git a/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Program.cs b/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Program.cs index 2a0d0f38..babe4f1e 100644 --- a/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Program.cs +++ b/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Program.cs @@ -60,10 +60,9 @@ builder.Host.ConfigureLogging(loggingBuilder => #endregion builder.Services.AddIocService(builder.Configuration); #region -//Sqlsugarע,ǷȨ޹ܣҪRedis +//Sqlsugarע,ǷȨ޹ܣ߻ #endregion -builder.Services.AddSqlsugarServer(); -//builder.Services.AddSqlsugarServer(DbFiterExtend.Data); +builder.Services.AddSqlsugarServer(DbFiterExtend.Data); #region //Quartz #endregion diff --git a/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/yi-sqlsugar-dev.db b/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/yi-sqlsugar-dev.db index 949050ab..d02285a5 100644 Binary files a/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/yi-sqlsugar-dev.db and b/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/yi-sqlsugar-dev.db differ diff --git a/Yi.Framework.Net6/Yi.Framework.Core/JwtInvoker.cs b/Yi.Framework.Net6/Yi.Framework.Core/JwtInvoker.cs index 0bb2f670..6742f5ae 100644 --- a/Yi.Framework.Net6/Yi.Framework.Core/JwtInvoker.cs +++ b/Yi.Framework.Net6/Yi.Framework.Core/JwtInvoker.cs @@ -27,6 +27,11 @@ namespace Yi.Framework.Core return this.GetToken(_JWTTokenOptions.ReExpiration, user, null, true); } + public TimeSpan GetTokenExpiration() + { + return new TimeSpan(0, _JWTTokenOptions.Expiration, 0); + } + public string GetAccessToken(UserEntity user, HashSet menus) { return this.GetToken(_JWTTokenOptions.Expiration, user, menus); diff --git a/Yi.Framework.Net6/Yi.Framework.WebCore/DbExtend/DbFiterExtend.cs b/Yi.Framework.Net6/Yi.Framework.WebCore/DbExtend/DbFiterExtend.cs index b5b33dab..3d8cbc69 100644 --- a/Yi.Framework.Net6/Yi.Framework.WebCore/DbExtend/DbFiterExtend.cs +++ b/Yi.Framework.Net6/Yi.Framework.WebCore/DbExtend/DbFiterExtend.cs @@ -21,7 +21,7 @@ namespace Yi.Framework.Core } //无需授权情况 - var userName = httpContext?.GetUserNameInfo(); + var userName = httpContext?.GetUserIdInfo(); if (userName is null) { return; @@ -33,9 +33,10 @@ namespace Yi.Framework.Core return; } - //这里可以优化一下 + var userId = httpContext?.GetUserIdInfo(); + //根据缓存获取全部用户信息 - var userRoleMenu = ServiceLocator.Instance?.GetService()?.Get("用户id"); + var userRoleMenu = ServiceLocator.Instance?.GetService()?.Get($"Yi:UserInfo:{userId}"); var roles = userRoleMenu?.Roles; @@ -43,9 +44,8 @@ namespace Yi.Framework.Core { roles = new(); } - //先测试部门就是LEBG + long deptId = userRoleMenu?.User.DeptId ?? -1; - long userId = httpContext?.GetUserIdInfo()??-1; //根据角色的数据范围,来添加相对于的数据权限 if (roles is not null) { diff --git a/Yi.Vue3.X.RuoYi/src/views/system/user/index.vue b/Yi.Vue3.X.RuoYi/src/views/system/user/index.vue index 3a6e781e..e4ac251f 100644 --- a/Yi.Vue3.X.RuoYi/src/views/system/user/index.vue +++ b/Yi.Vue3.X.RuoYi/src/views/system/user/index.vue @@ -143,8 +143,8 @@ - - + +