2023-07-31 18:41:23 +08:00
|
|
|
|
using IPTools.Core;
|
2023-03-15 16:00:16 +08:00
|
|
|
|
using Lazy.Captcha.Core;
|
2021-08-23 16:57:25 +08:00
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2022-05-13 22:13:44 +08:00
|
|
|
|
using Microsoft.Extensions.Options;
|
|
|
|
|
|
using UAParser;
|
2021-08-23 16:57:25 +08:00
|
|
|
|
using ZR.Admin.WebApi.Extensions;
|
|
|
|
|
|
using ZR.Admin.WebApi.Filters;
|
|
|
|
|
|
using ZR.Admin.WebApi.Framework;
|
|
|
|
|
|
using ZR.Model.System;
|
2021-09-16 19:07:49 +08:00
|
|
|
|
using ZR.Model.System.Dto;
|
2021-12-01 16:56:46 +08:00
|
|
|
|
using ZR.Service.System;
|
2022-05-13 22:13:44 +08:00
|
|
|
|
using ZR.Service.System.IService;
|
2021-08-23 16:57:25 +08:00
|
|
|
|
|
|
|
|
|
|
namespace ZR.Admin.WebApi.Controllers.System
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 登录
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public class SysLoginController : BaseController
|
|
|
|
|
|
{
|
|
|
|
|
|
static readonly NLog.Logger logger = NLog.LogManager.GetLogger("LoginController");
|
|
|
|
|
|
private readonly IHttpContextAccessor httpContextAccessor;
|
|
|
|
|
|
private readonly ISysUserService sysUserService;
|
|
|
|
|
|
private readonly ISysMenuService sysMenuService;
|
|
|
|
|
|
private readonly ISysLoginService sysLoginService;
|
|
|
|
|
|
private readonly ISysPermissionService permissionService;
|
2023-03-15 16:00:16 +08:00
|
|
|
|
private readonly ICaptcha SecurityCodeHelper;
|
2021-12-01 16:56:46 +08:00
|
|
|
|
private readonly ISysConfigService sysConfigService;
|
2021-12-26 18:26:38 +08:00
|
|
|
|
private readonly ISysRoleService roleService;
|
2022-01-09 17:12:35 +08:00
|
|
|
|
private readonly OptionsSetting jwtSettings;
|
|
|
|
|
|
|
2021-08-23 16:57:25 +08:00
|
|
|
|
public SysLoginController(
|
|
|
|
|
|
IHttpContextAccessor contextAccessor,
|
|
|
|
|
|
ISysMenuService sysMenuService,
|
|
|
|
|
|
ISysUserService sysUserService,
|
|
|
|
|
|
ISysLoginService sysLoginService,
|
|
|
|
|
|
ISysPermissionService permissionService,
|
2021-12-01 16:56:46 +08:00
|
|
|
|
ISysConfigService configService,
|
2021-12-26 18:26:38 +08:00
|
|
|
|
ISysRoleService sysRoleService,
|
2023-03-15 16:00:16 +08:00
|
|
|
|
ICaptcha captcha,
|
2022-01-09 17:12:35 +08:00
|
|
|
|
IOptions<OptionsSetting> jwtSettings)
|
2021-08-23 16:57:25 +08:00
|
|
|
|
{
|
|
|
|
|
|
httpContextAccessor = contextAccessor;
|
|
|
|
|
|
SecurityCodeHelper = captcha;
|
|
|
|
|
|
this.sysMenuService = sysMenuService;
|
|
|
|
|
|
this.sysUserService = sysUserService;
|
|
|
|
|
|
this.sysLoginService = sysLoginService;
|
|
|
|
|
|
this.permissionService = permissionService;
|
2021-12-01 16:56:46 +08:00
|
|
|
|
this.sysConfigService = configService;
|
2021-12-26 18:26:38 +08:00
|
|
|
|
roleService = sysRoleService;
|
2022-01-09 17:12:35 +08:00
|
|
|
|
this.jwtSettings = jwtSettings.Value;
|
2021-08-23 16:57:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 登录
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="loginBody">登录对象</param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
[Route("login")]
|
|
|
|
|
|
[HttpPost]
|
2023-03-03 20:13:29 +08:00
|
|
|
|
[Log(Title = "登录")]
|
2021-08-23 16:57:25 +08:00
|
|
|
|
public IActionResult Login([FromBody] LoginBodyDto loginBody)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (loginBody == null) { throw new CustomException("请求参数错误"); }
|
2024-01-04 09:01:03 +08:00
|
|
|
|
// todo: HttpContext存了什么东西?????
|
|
|
|
|
|
//todo: ControllerBase.HttpContext和httpContextAccessor.HttpContext 有啥区别啊???
|
2021-08-23 16:57:25 +08:00
|
|
|
|
loginBody.LoginIP = HttpContextExtension.GetClientUserIp(HttpContext);
|
2024-01-04 09:01:03 +08:00
|
|
|
|
|
|
|
|
|
|
//todo 判断验证码
|
2021-12-01 16:56:46 +08:00
|
|
|
|
SysConfig sysConfig = sysConfigService.GetSysConfigByKey("sys.account.captchaOnOff");
|
2023-03-21 11:13:12 +08:00
|
|
|
|
if (sysConfig?.ConfigValue != "off" && !SecurityCodeHelper.Validate(loginBody.Uuid, loginBody.Code))
|
2021-08-23 16:57:25 +08:00
|
|
|
|
{
|
2022-01-11 10:49:38 +08:00
|
|
|
|
return ToResponse(ResultCode.CAPTCHA_ERROR, "验证码错误");
|
2021-08-23 16:57:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-03-05 18:54:10 +08:00
|
|
|
|
var user = sysLoginService.Login(loginBody, RecordLogInfo(httpContextAccessor.HttpContext));
|
2022-04-14 18:30:10 +08:00
|
|
|
|
|
2022-01-07 21:39:22 +08:00
|
|
|
|
List<SysRole> roles = roleService.SelectUserRoleListByUserId(user.UserId);
|
2021-08-23 16:57:25 +08:00
|
|
|
|
//权限集合 eg *:*:*,system:user:list
|
|
|
|
|
|
List<string> permissions = permissionService.GetMenuPermission(user);
|
2024-01-04 09:01:03 +08:00
|
|
|
|
// 权限
|
2022-01-07 21:39:22 +08:00
|
|
|
|
LoginUser loginUser = new(user, roles, permissions);
|
2023-08-02 13:36:44 +08:00
|
|
|
|
//todo 把权限加到缓存里
|
2022-04-10 16:52:10 +08:00
|
|
|
|
CacheService.SetUserPerms(GlobalConstant.UserPermKEY + user.UserId, permissions);
|
2024-01-04 09:01:03 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Console.WriteLine("1 用户信息--》" + HttpContext.User.Identity.Name);
|
|
|
|
|
|
|
2022-03-24 18:05:52 +08:00
|
|
|
|
return SUCCESS(JwtUtil.GenerateJwtToken(JwtUtil.AddClaims(loginUser), jwtSettings.JwtSettings));
|
2021-08-23 16:57:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 注销
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
[Log(Title = "注销")]
|
|
|
|
|
|
[HttpPost("logout")]
|
|
|
|
|
|
public IActionResult LogOut()
|
|
|
|
|
|
{
|
2021-12-03 17:42:44 +08:00
|
|
|
|
//Task.Run(async () =>
|
|
|
|
|
|
//{
|
|
|
|
|
|
// //注销登录的用户,相当于ASP.NET中的FormsAuthentication.SignOut
|
|
|
|
|
|
// await HttpContext.SignOutAsync();
|
|
|
|
|
|
//}).Wait();
|
2022-03-24 18:05:52 +08:00
|
|
|
|
var userid = HttpContext.GetUId();
|
2022-03-02 21:55:30 +08:00
|
|
|
|
var name = HttpContext.GetName();
|
2022-04-14 18:30:10 +08:00
|
|
|
|
|
2022-04-10 16:52:10 +08:00
|
|
|
|
CacheService.RemoveUserPerms(GlobalConstant.UserPermKEY + userid);
|
2022-04-14 18:30:10 +08:00
|
|
|
|
return SUCCESS(new { name, id = userid });
|
2021-08-23 16:57:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 获取用户信息
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
[Verify]
|
|
|
|
|
|
[HttpGet("getInfo")]
|
|
|
|
|
|
public IActionResult GetUserInfo()
|
|
|
|
|
|
{
|
|
|
|
|
|
long userid = HttpContext.GetUId();
|
|
|
|
|
|
var user = sysUserService.SelectUserById(userid);
|
|
|
|
|
|
|
|
|
|
|
|
//前端校验按钮权限使用
|
|
|
|
|
|
//角色集合 eg: admin,yunying,common
|
|
|
|
|
|
List<string> roles = permissionService.GetRolePermission(user);
|
|
|
|
|
|
//权限集合 eg *:*:*,system:user:list
|
|
|
|
|
|
List<string> permissions = permissionService.GetMenuPermission(user);
|
2022-02-28 21:37:25 +08:00
|
|
|
|
user.WelcomeContent = GlobalConstant.WelcomeMessages[new Random().Next(0, GlobalConstant.WelcomeMessages.Length)];
|
2021-08-23 16:57:25 +08:00
|
|
|
|
return SUCCESS(new { user, roles, permissions });
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 获取路由信息
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
[Verify]
|
|
|
|
|
|
[HttpGet("getRouters")]
|
|
|
|
|
|
public IActionResult GetRouters()
|
|
|
|
|
|
{
|
|
|
|
|
|
long uid = HttpContext.GetUId();
|
|
|
|
|
|
var menus = sysMenuService.SelectMenuTreeByUserId(uid);
|
|
|
|
|
|
|
2023-06-02 18:33:07 +08:00
|
|
|
|
return SUCCESS(sysMenuService.BuildMenus(menus));
|
2021-08-23 16:57:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 生成图片验证码
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
[HttpGet("captchaImage")]
|
2023-06-02 18:33:07 +08:00
|
|
|
|
public IActionResult CaptchaImage()
|
2021-08-23 16:57:25 +08:00
|
|
|
|
{
|
|
|
|
|
|
string uuid = Guid.NewGuid().ToString().Replace("-", "");
|
2021-12-01 16:56:46 +08:00
|
|
|
|
|
|
|
|
|
|
SysConfig sysConfig = sysConfigService.GetSysConfigByKey("sys.account.captchaOnOff");
|
2021-12-06 13:28:12 +08:00
|
|
|
|
var captchaOff = sysConfig?.ConfigValue ?? "0";
|
2023-03-15 16:00:16 +08:00
|
|
|
|
var info = SecurityCodeHelper.Generate(uuid, 60);
|
|
|
|
|
|
var obj = new { captchaOff, uuid, img = info.Base64 };// File(stream, "image/png")
|
2022-10-11 21:45:15 +08:00
|
|
|
|
|
2023-06-02 18:33:07 +08:00
|
|
|
|
return SUCCESS(obj);
|
2022-10-11 21:45:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-03-05 18:54:10 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 记录用户登陆信息
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="context"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public SysLogininfor RecordLogInfo(HttpContext context)
|
|
|
|
|
|
{
|
|
|
|
|
|
var ipAddr = context.GetClientUserIp();
|
|
|
|
|
|
var ip_info = IpTool.Search(ipAddr);
|
|
|
|
|
|
ClientInfo clientInfo = context.GetClientInfo();
|
|
|
|
|
|
SysLogininfor sysLogininfor = new()
|
|
|
|
|
|
{
|
2022-10-25 07:52:43 +08:00
|
|
|
|
Browser = clientInfo.ToString(),
|
2022-09-01 21:54:53 +08:00
|
|
|
|
Os = clientInfo.OS.ToString(),
|
|
|
|
|
|
Ipaddr = ipAddr,
|
2024-01-04 09:01:03 +08:00
|
|
|
|
UserName = context.GetName(),//空的 获取不到 null
|
2022-09-01 21:54:53 +08:00
|
|
|
|
LoginLocation = ip_info?.Province + "-" + ip_info?.City
|
2022-04-14 18:30:10 +08:00
|
|
|
|
};
|
2023-03-15 16:00:16 +08:00
|
|
|
|
|
2022-03-05 18:54:10 +08:00
|
|
|
|
return sysLogininfor;
|
|
|
|
|
|
}
|
2022-04-14 18:30:10 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 注册
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="dto"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
[HttpPost("/register")]
|
|
|
|
|
|
[AllowAnonymous]
|
|
|
|
|
|
[Log(Title = "注册", BusinessType = Infrastructure.Enums.BusinessType.INSERT)]
|
|
|
|
|
|
public IActionResult Register([FromBody] RegisterDto dto)
|
|
|
|
|
|
{
|
|
|
|
|
|
SysConfig config = sysConfigService.GetSysConfigByKey("sys.account.register");
|
|
|
|
|
|
if (config?.ConfigValue != "true")
|
|
|
|
|
|
{
|
|
|
|
|
|
return ToResponse(ResultCode.CUSTOM_ERROR, "当前系统没有开启注册功能!");
|
|
|
|
|
|
}
|
|
|
|
|
|
SysConfig sysConfig = sysConfigService.GetSysConfigByKey("sys.account.captchaOnOff");
|
2023-03-21 11:13:12 +08:00
|
|
|
|
if (sysConfig?.ConfigValue != "off" && !SecurityCodeHelper.Validate(dto.Uuid, dto.Code))
|
2022-04-14 18:30:10 +08:00
|
|
|
|
{
|
|
|
|
|
|
return ToResponse(ResultCode.CAPTCHA_ERROR, "验证码错误");
|
|
|
|
|
|
}
|
2023-05-18 18:06:19 +08:00
|
|
|
|
|
2022-04-14 18:30:10 +08:00
|
|
|
|
SysUser user = sysUserService.Register(dto);
|
|
|
|
|
|
if (user.UserId > 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
return SUCCESS(user);
|
|
|
|
|
|
}
|
|
|
|
|
|
return ToResponse(ResultCode.CUSTOM_ERROR, "注册失败,请联系管理员");
|
|
|
|
|
|
}
|
2021-08-23 16:57:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|