2022-05-13 22:13:44 +08:00
|
|
|
|
using Hei.Captcha;
|
|
|
|
|
|
using Infrastructure;
|
|
|
|
|
|
using Infrastructure.Attribute;
|
|
|
|
|
|
using Infrastructure.Model;
|
|
|
|
|
|
using IPTools.Core;
|
|
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
2021-08-23 16:57:25 +08:00
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2022-05-13 22:13:44 +08:00
|
|
|
|
using Microsoft.Extensions.Options;
|
2021-08-23 16:57:25 +08:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
2022-05-13 22:13:44 +08:00
|
|
|
|
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;
|
2022-05-13 22:13:44 +08:00
|
|
|
|
using ZR.Common;
|
2021-08-23 16:57:25 +08:00
|
|
|
|
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;
|
|
|
|
|
|
private readonly SecurityCodeHelper 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,
|
2022-01-09 17:12:35 +08:00
|
|
|
|
SecurityCodeHelper captcha,
|
|
|
|
|
|
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("请求参数错误"); }
|
|
|
|
|
|
loginBody.LoginIP = HttpContextExtension.GetClientUserIp(HttpContext);
|
2021-12-01 16:56:46 +08:00
|
|
|
|
SysConfig sysConfig = sysConfigService.GetSysConfigByKey("sys.account.captchaOnOff");
|
|
|
|
|
|
if (sysConfig?.ConfigValue != "off" && CacheHelper.Get(loginBody.Uuid) is string str && !str.ToLower().Equals(loginBody.Code.ToLower()))
|
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);
|
|
|
|
|
|
|
2022-01-07 21:39:22 +08:00
|
|
|
|
LoginUser loginUser = new(user, roles, permissions);
|
2022-04-10 16:52:10 +08:00
|
|
|
|
CacheService.SetUserPerms(GlobalConstant.UserPermKEY + user.UserId, permissions);
|
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);
|
|
|
|
|
|
|
2021-09-27 16:07:55 +08:00
|
|
|
|
return ToResponse(ToJson(1, sysMenuService.BuildMenus(menus)));
|
2021-08-23 16:57:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 生成图片验证码
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
[HttpGet("captchaImage")]
|
|
|
|
|
|
public ApiResult CaptchaImage()
|
|
|
|
|
|
{
|
|
|
|
|
|
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";
|
|
|
|
|
|
|
2022-10-11 21:45:15 +08:00
|
|
|
|
var length = AppSettings.GetAppConfig<int>("CaptchaOptions:length", 4);
|
|
|
|
|
|
var code = SecurityCodeHelper.GetRandomEnDigitalText(length);
|
|
|
|
|
|
byte[] imgByte = GenerateCaptcha(captchaOff, code);
|
|
|
|
|
|
string base64Str = Convert.ToBase64String(imgByte);
|
|
|
|
|
|
CacheHelper.SetCache(uuid, code);
|
2022-10-28 21:40:53 +08:00
|
|
|
|
var obj = new { captchaOff, uuid, img = base64Str };// File(stream, "image/png")
|
2022-10-11 21:45:15 +08:00
|
|
|
|
|
|
|
|
|
|
return ToJson(1, obj);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 生成图片验证码
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="captchaOff"></param>
|
|
|
|
|
|
/// <param name="code"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
private byte[] GenerateCaptcha(string captchaOff, string code)
|
|
|
|
|
|
{
|
2021-12-01 16:56:46 +08:00
|
|
|
|
byte[] imgByte;
|
2021-12-06 13:28:12 +08:00
|
|
|
|
if (captchaOff == "1")
|
2021-12-01 16:56:46 +08:00
|
|
|
|
{
|
|
|
|
|
|
imgByte = SecurityCodeHelper.GetGifEnDigitalCodeByte(code);//动态gif数字字母
|
|
|
|
|
|
}
|
2021-12-06 13:28:12 +08:00
|
|
|
|
else if (captchaOff == "2")
|
2021-12-01 16:56:46 +08:00
|
|
|
|
{
|
|
|
|
|
|
imgByte = SecurityCodeHelper.GetGifBubbleCodeByte(code);//动态gif泡泡
|
|
|
|
|
|
}
|
2021-12-06 13:28:12 +08:00
|
|
|
|
else if (captchaOff == "3")
|
2021-12-01 16:56:46 +08:00
|
|
|
|
{
|
|
|
|
|
|
imgByte = SecurityCodeHelper.GetBubbleCodeByte(code);//泡泡
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
imgByte = SecurityCodeHelper.GetEnDigitalCodeByte(code);//英文字母加数字
|
|
|
|
|
|
}
|
2021-08-23 16:57:25 +08:00
|
|
|
|
|
2022-10-11 21:45:15 +08:00
|
|
|
|
return imgByte;
|
2021-08-23 16:57:25 +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,
|
|
|
|
|
|
UserName = context.GetName(),
|
|
|
|
|
|
LoginLocation = ip_info?.Province + "-" + ip_info?.City
|
2022-04-14 18:30:10 +08:00
|
|
|
|
};
|
2022-10-25 07:52:43 +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");
|
|
|
|
|
|
if (sysConfig?.ConfigValue != "off" && CacheHelper.Get(dto.Uuid) is string str && !str.ToLower().Equals(dto.Code.ToLower()))
|
|
|
|
|
|
{
|
|
|
|
|
|
return ToResponse(ResultCode.CAPTCHA_ERROR, "验证码错误");
|
|
|
|
|
|
}
|
|
|
|
|
|
if (UserConstants.NOT_UNIQUE.Equals(sysUserService.CheckUserNameUnique(dto.Username)))
|
|
|
|
|
|
{
|
|
|
|
|
|
return ToResponse(ResultCode.CUSTOM_ERROR, $"保存用户{dto.Username}失败,注册账号已存在");
|
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
}
|