优化代码

This commit is contained in:
不做码农 2022-03-02 21:55:30 +08:00
parent c273ec2d25
commit 9dddbd874b
4 changed files with 15 additions and 11 deletions

View File

@ -66,7 +66,7 @@ namespace ZR.Admin.WebApi.Controllers.System
/// <returns></returns> /// <returns></returns>
[Route("login")] [Route("login")]
[HttpPost] [HttpPost]
[Log(Title = "登录")] //[Log(Title = "登录")]
public IActionResult Login([FromBody] LoginBodyDto loginBody) public IActionResult Login([FromBody] LoginBodyDto loginBody)
{ {
if (loginBody == null) { throw new CustomException("请求参数错误"); } if (loginBody == null) { throw new CustomException("请求参数错误"); }
@ -79,11 +79,9 @@ namespace ZR.Admin.WebApi.Controllers.System
var user = sysLoginService.Login(loginBody, AsyncFactory.RecordLogInfo(httpContextAccessor.HttpContext, "0", "login")); var user = sysLoginService.Login(loginBody, AsyncFactory.RecordLogInfo(httpContextAccessor.HttpContext, "0", "login"));
#region cookie Action校验权限使用
List<SysRole> roles = roleService.SelectUserRoleListByUserId(user.UserId); List<SysRole> roles = roleService.SelectUserRoleListByUserId(user.UserId);
//权限集合 eg *:*:*,system:user:list //权限集合 eg *:*:*,system:user:list
List<string> permissions = permissionService.GetMenuPermission(user); List<string> permissions = permissionService.GetMenuPermission(user);
#endregion
LoginUser loginUser = new(user, roles, permissions); LoginUser loginUser = new(user, roles, permissions);
CacheHelper.SetCache(GlobalConstant.UserPermKEY + user.UserId, loginUser); CacheHelper.SetCache(GlobalConstant.UserPermKEY + user.UserId, loginUser);
@ -104,9 +102,10 @@ namespace ZR.Admin.WebApi.Controllers.System
// await HttpContext.SignOutAsync(); // await HttpContext.SignOutAsync();
//}).Wait(); //}).Wait();
var id = HttpContext.GetUId(); var id = HttpContext.GetUId();
var name = HttpContext.GetName();
CacheHelper.Remove(GlobalConstant.UserPermKEY + id); CacheHelper.Remove(GlobalConstant.UserPermKEY + id);
return SUCCESS(1); return SUCCESS(name);
} }
/// <summary> /// <summary>

View File

@ -1,4 +1,5 @@
using Infrastructure; using Infrastructure;
using Infrastructure.Model;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters; using Microsoft.AspNetCore.Mvc.Filters;
using System.Collections.Generic; using System.Collections.Generic;
@ -68,12 +69,16 @@ namespace ZR.Admin.WebApi.Filters
if (!HasPermi && !Permission.Equals("common")) if (!HasPermi && !Permission.Equals("common"))
{ {
logger.Info($"用户{info.UserName}没有权限访问{url},当前权限[{Permission}]"); logger.Info($"用户{info.UserName}没有权限访问{url},当前权限[{Permission}]");
context.Result = new JsonResult(new JsonResult result = new(new ApiResult()
{ {
code = ResultCode.FORBIDDEN, Code = (int)ResultCode.FORBIDDEN,
msg = $"你当前没有权限[{Permission}]访问,请联系管理员", Msg = $"你当前没有权限[{Permission}]访问,请联系管理员",
data = url Data = url
}); })
{
ContentType = "text/json",
};
context.Result = result;
} }
} }

View File

@ -56,7 +56,7 @@ namespace ZR.Admin.WebApi.Filters
string ip = HttpContextExtension.GetClientUserIp(context.HttpContext); string ip = HttpContextExtension.GetClientUserIp(context.HttpContext);
var ip_info = IpTool.Search(ip); var ip_info = IpTool.Search(ip);
SysOperLog sysOperLog = new SysOperLog SysOperLog sysOperLog = new()
{ {
status = 0, status = 0,
operName = userName, operName = userName,

View File

@ -52,7 +52,7 @@ namespace ZR.Admin.WebApi.Filters
if (info != null && info.UserId > 0) if (info != null && info.UserId > 0)
{ {
logger.Info($"[{info.UserId}-{userName}-{ip}]用户登录校验成功"); //logger.Info($"[{info.UserId}-{userName}-{ip}]用户登录校验成功");
} }
else else
{ {