2022-09-18 16:46:31 +08:00
|
|
|
|
using Hei.Captcha;
|
|
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
2022-04-07 22:48:10 +08:00
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Threading.Tasks;
|
2022-09-25 19:56:14 +08:00
|
|
|
|
using Yi.Framework.Common.Const;
|
2022-09-09 19:22:14 +08:00
|
|
|
|
using Yi.Framework.Common.Enum;
|
2022-05-01 18:31:06 +08:00
|
|
|
|
using Yi.Framework.Common.Helper;
|
2022-04-07 22:48:10 +08:00
|
|
|
|
using Yi.Framework.Common.Models;
|
2022-04-08 23:44:25 +08:00
|
|
|
|
using Yi.Framework.Core;
|
2022-04-07 22:48:10 +08:00
|
|
|
|
using Yi.Framework.DTOModel;
|
|
|
|
|
|
using Yi.Framework.Interface;
|
|
|
|
|
|
using Yi.Framework.Model.Models;
|
|
|
|
|
|
using Yi.Framework.Repository;
|
|
|
|
|
|
using Yi.Framework.WebCore;
|
|
|
|
|
|
using Yi.Framework.WebCore.AttributeExtend;
|
|
|
|
|
|
using Yi.Framework.WebCore.AuthorizationPolicy;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Yi.Framework.ApiMicroservice.Controllers
|
|
|
|
|
|
{
|
2022-04-24 23:09:34 +08:00
|
|
|
|
/// <summary>
|
2022-04-26 01:31:14 +08:00
|
|
|
|
/// 账户管理
|
2022-04-24 23:09:34 +08:00
|
|
|
|
/// </summary>
|
2022-04-07 22:48:10 +08:00
|
|
|
|
[ApiController]
|
2022-09-15 18:59:01 +08:00
|
|
|
|
[Authorize]
|
2022-04-07 22:48:10 +08:00
|
|
|
|
[Route("api/[controller]/[action]")]
|
2022-05-01 18:31:06 +08:00
|
|
|
|
public class AccountController : ControllerBase
|
2022-04-07 22:48:10 +08:00
|
|
|
|
{
|
2022-05-01 18:31:06 +08:00
|
|
|
|
private IUserService _iUserService;
|
2022-04-08 23:44:25 +08:00
|
|
|
|
private JwtInvoker _jwtInvoker;
|
2022-09-09 19:22:14 +08:00
|
|
|
|
private ILogger _logger;
|
2022-09-18 16:46:31 +08:00
|
|
|
|
private SecurityCodeHelper _securityCode;
|
2022-10-02 14:02:21 +08:00
|
|
|
|
private IRepository<UserEntity> _repository;
|
2022-09-18 16:46:31 +08:00
|
|
|
|
public AccountController(ILogger<UserEntity> logger, IUserService iUserService, JwtInvoker jwtInvoker, SecurityCodeHelper securityCode)
|
2022-04-07 22:48:10 +08:00
|
|
|
|
{
|
|
|
|
|
|
_iUserService = iUserService;
|
2022-04-08 23:44:25 +08:00
|
|
|
|
_jwtInvoker = jwtInvoker;
|
2022-09-09 19:22:14 +08:00
|
|
|
|
_logger = logger;
|
2022-09-18 16:46:31 +08:00
|
|
|
|
_securityCode = securityCode;
|
2022-10-02 14:02:21 +08:00
|
|
|
|
_repository = iUserService._repository;
|
2022-04-07 22:48:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-09-14 20:35:45 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 重置管理员CC的密码
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
[HttpGet]
|
2022-09-15 18:59:01 +08:00
|
|
|
|
[AllowAnonymous]
|
2022-09-14 20:35:45 +08:00
|
|
|
|
public async Task<Result> RestCC()
|
|
|
|
|
|
{
|
2022-09-18 16:46:31 +08:00
|
|
|
|
var user = await _iUserService._repository.GetFirstAsync(u => u.UserName == "cc");
|
2022-09-14 20:35:45 +08:00
|
|
|
|
user.Password = "123456";
|
|
|
|
|
|
user.BuildPassword();
|
|
|
|
|
|
await _iUserService._repository.UpdateIgnoreNullAsync(user);
|
|
|
|
|
|
return Result.Success();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-05-01 18:31:06 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 没啥说,登录
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="loginDto"></param>
|
|
|
|
|
|
/// <returns></returns>
|
2022-04-07 22:48:10 +08:00
|
|
|
|
[AllowAnonymous]
|
|
|
|
|
|
[HttpPost]
|
|
|
|
|
|
public async Task<Result> Login(LoginDto loginDto)
|
|
|
|
|
|
{
|
2022-09-18 17:22:47 +08:00
|
|
|
|
//跳过,需要redis缓存获取uuid与code的关系,进行比较即可
|
2022-09-07 18:22:15 +08:00
|
|
|
|
//先效验验证码和UUID
|
2022-10-02 14:02:21 +08:00
|
|
|
|
//登录还需要进行登录日志的落库
|
|
|
|
|
|
|
|
|
|
|
|
var loginInfo = HttpContext.GetLoginLogInfo();
|
|
|
|
|
|
loginInfo.LoginUser = loginDto.UserName;
|
|
|
|
|
|
loginInfo.LogMsg = "登录成功!";
|
|
|
|
|
|
var loginLogRepository = _repository.ChangeRepository<Repository<LoginLogEntity>>();
|
2022-05-01 18:31:06 +08:00
|
|
|
|
UserEntity user = new();
|
|
|
|
|
|
if (await _iUserService.Login(loginDto.UserName, loginDto.Password, o => user = o))
|
2022-04-08 23:44:25 +08:00
|
|
|
|
{
|
2022-09-09 19:22:14 +08:00
|
|
|
|
var userRoleMenu = await _iUserService.GetUserAllInfo(user.Id);
|
2022-10-02 14:02:21 +08:00
|
|
|
|
await loginLogRepository.InsertReturnSnowflakeIdAsync(loginInfo);
|
|
|
|
|
|
return Result.Success(loginInfo.LogMsg).SetData(new { token = _jwtInvoker.GetAccessToken(userRoleMenu.User, userRoleMenu.Menus) });
|
2022-04-07 22:48:10 +08:00
|
|
|
|
}
|
2022-10-02 14:02:21 +08:00
|
|
|
|
loginInfo.LogMsg = "登录失败!用户名或者密码错误!";
|
|
|
|
|
|
await loginLogRepository.InsertReturnSnowflakeIdAsync(loginInfo);
|
|
|
|
|
|
return Result.Error(loginInfo.LogMsg);
|
2022-04-07 22:48:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-09-09 19:22:14 +08:00
|
|
|
|
|
|
|
|
|
|
|
2022-05-01 18:31:06 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 没啥说,注册
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="registerDto"></param>
|
|
|
|
|
|
/// <returns></returns>
|
2022-04-07 22:48:10 +08:00
|
|
|
|
[AllowAnonymous]
|
|
|
|
|
|
[HttpPost]
|
|
|
|
|
|
public async Task<Result> Register(RegisterDto registerDto)
|
|
|
|
|
|
{
|
|
|
|
|
|
UserEntity user = new();
|
|
|
|
|
|
if (await _iUserService.Register(WebCore.Mapper.MapperHelper.Map<UserEntity, RegisterDto>(registerDto), o => user = o))
|
|
|
|
|
|
{
|
|
|
|
|
|
return Result.Success("注册成功!").SetData(user);
|
|
|
|
|
|
}
|
|
|
|
|
|
return Result.SuccessError("注册失败!用户名已存在!");
|
|
|
|
|
|
}
|
2022-04-30 21:48:18 +08:00
|
|
|
|
|
2022-05-19 23:29:37 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 没啥说,登出
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns></returns>
|
2022-05-03 17:34:38 +08:00
|
|
|
|
[HttpPost]
|
2022-09-15 18:59:01 +08:00
|
|
|
|
[AllowAnonymous]
|
2022-09-09 19:22:14 +08:00
|
|
|
|
public Result Logout()
|
2022-05-03 17:34:38 +08:00
|
|
|
|
{
|
|
|
|
|
|
return Result.Success("安全登出成功!");
|
|
|
|
|
|
}
|
2022-04-30 21:48:18 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2022-09-09 19:22:14 +08:00
|
|
|
|
/// 通过已登录的用户获取用户信息
|
2022-04-30 21:48:18 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
[HttpGet]
|
2022-09-11 02:39:33 +08:00
|
|
|
|
//[Authorize]
|
2022-04-30 21:48:18 +08:00
|
|
|
|
public async Task<Result> GetUserAllInfo()
|
|
|
|
|
|
{
|
2022-05-01 18:31:06 +08:00
|
|
|
|
//通过鉴权jwt获取到用户的id
|
2022-09-15 18:44:12 +08:00
|
|
|
|
var userId = HttpContext.GetUserIdInfo();
|
2022-09-07 18:22:15 +08:00
|
|
|
|
var data = await _iUserService.GetUserAllInfo(userId);
|
2022-09-25 18:58:17 +08:00
|
|
|
|
//系统用户数据被重置,老前端访问重新授权
|
|
|
|
|
|
if (data is null)
|
|
|
|
|
|
{
|
|
|
|
|
|
return Result.UnAuthorize();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-09-07 18:22:15 +08:00
|
|
|
|
data.Menus.Clear();
|
|
|
|
|
|
return Result.Success().SetData(data);
|
2022-04-30 21:48:18 +08:00
|
|
|
|
}
|
2022-05-01 18:31:06 +08:00
|
|
|
|
|
2022-09-09 19:22:14 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 获取当前登录用户的前端路由
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
[HttpGet]
|
|
|
|
|
|
public async Task<Result> GetRouterInfo()
|
|
|
|
|
|
{
|
2022-09-15 18:44:12 +08:00
|
|
|
|
var userId = HttpContext.GetUserIdInfo();
|
2022-09-09 19:22:14 +08:00
|
|
|
|
var data = await _iUserService.GetUserAllInfo(userId);
|
2022-09-25 19:56:14 +08:00
|
|
|
|
var menus = data.Menus.ToList();
|
2022-09-14 19:53:53 +08:00
|
|
|
|
|
2022-09-25 19:56:14 +08:00
|
|
|
|
//为超级管理员直接给全部路由
|
|
|
|
|
|
if (SystemConst.Admin.Equals(data.User.UserName))
|
|
|
|
|
|
{
|
|
|
|
|
|
menus = await _iUserService._repository.ChangeRepository<Repository<MenuEntity>>().GetListAsync();
|
|
|
|
|
|
}
|
2022-09-09 19:22:14 +08:00
|
|
|
|
//将后端菜单转换成前端路由,组件级别需要过滤
|
2022-09-25 19:56:14 +08:00
|
|
|
|
List<VueRouterModel> routers = MenuEntity.RouterBuild(menus);
|
2022-09-09 19:22:14 +08:00
|
|
|
|
return Result.Success().SetData(routers);
|
|
|
|
|
|
}
|
2022-05-01 18:31:06 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 更新已登录用户的用户信息
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="user"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
[HttpPut]
|
|
|
|
|
|
public async Task<Result> UpdateUserByHttp(UserEntity user)
|
|
|
|
|
|
{
|
|
|
|
|
|
//当然,密码是不能给他修改的
|
|
|
|
|
|
user.Password = null;
|
|
|
|
|
|
user.Salt = null;
|
|
|
|
|
|
|
|
|
|
|
|
//修改需要赋值上主键哦
|
2022-09-15 18:44:12 +08:00
|
|
|
|
user.Id = HttpContext.GetUserIdInfo();
|
2022-05-01 18:31:06 +08:00
|
|
|
|
return Result.Success().SetStatus(await _iUserService._repository.UpdateIgnoreNullAsync(user));
|
|
|
|
|
|
}
|
2022-09-14 19:53:53 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 自己更新密码
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="dto"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
[HttpPut]
|
|
|
|
|
|
public async Task<Result> UpdatePassword(UpdatePasswordDto dto)
|
|
|
|
|
|
{
|
2022-09-15 18:44:12 +08:00
|
|
|
|
long userId = HttpContext.GetUserIdInfo();
|
2022-09-18 16:46:31 +08:00
|
|
|
|
|
2022-09-14 19:53:53 +08:00
|
|
|
|
if (await _iUserService.UpdatePassword(dto, userId))
|
|
|
|
|
|
{
|
|
|
|
|
|
return Result.Success();
|
|
|
|
|
|
}
|
|
|
|
|
|
return Result.Error("更新失败!");
|
|
|
|
|
|
}
|
2022-09-16 19:57:56 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 验证码
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
[AllowAnonymous]
|
|
|
|
|
|
[HttpGet]
|
2022-09-18 16:46:31 +08:00
|
|
|
|
public Result CaptchaImage()
|
2022-09-16 19:57:56 +08:00
|
|
|
|
{
|
2022-09-18 16:46:31 +08:00
|
|
|
|
var uuid = Guid.NewGuid();
|
|
|
|
|
|
var code = _securityCode.GetRandomEnDigitalText(4);
|
2022-09-18 17:22:47 +08:00
|
|
|
|
//将uuid与code,Redis缓存中心化保存起来,登录根据uuid比对即可
|
2022-09-18 16:46:31 +08:00
|
|
|
|
var imgbyte = _securityCode.GetEnDigitalCodeByte(code);
|
|
|
|
|
|
return Result.Success().SetData(new { uuid = uuid, img = imgbyte });
|
2022-09-16 19:57:56 +08:00
|
|
|
|
}
|
2022-04-07 22:48:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|