using Attribute;
using Common;
using Dto;
using Enums;
using Filters;
using Infrastructure;
using Infrastructure.Model;
using Mapster;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Middleware;
using MiniExcelLibs;
using Model;
using Model.Base;
using Services;
using Util;
namespace Controllers
{
///
/// 用户管理
///
[Verify]
public class SysUserController : BaseController
{
private readonly ISysUserService UserService;
private readonly ISysRoleService RoleService;
private readonly ISysPostService PostService;
private readonly ISysUserPostService UserPostService;
public SysUserController(
ISysUserService userService,
ISysRoleService roleService,
ISysPostService postService,
ISysUserPostService userPostService)
{
UserService = userService;
RoleService = roleService;
PostService = postService;
UserPostService = userPostService;
}
///
/// 用户管理 -> 获取用户
/// /system/user/list
///
///
[HttpGet("/v1/skb/sysServer/user/page")]
public IActionResult List([FromQuery] SysUserQueryDto user, PagerInfo pager)
{
var list = UserService.SelectUserList(user, pager);
return SUCCESS(list);
}
///
/// 用户管理 -> 编辑、添加用户获取用户,信息查询
///
///
///
[HttpGet("")]
[HttpGet("{userId:int=0}")]
[ActionPermissionFilter(Permission = "system:user:query")]
public IActionResult GetInfo(int userId)
{
Dictionary dic = new();
var roles = RoleService.SelectRoleAll();
dic.Add("roles", roles);
//dic.Add("roles", SysUser.IsAdmin(userId) ? roles : roles.FindAll(f => !f.IsAdmin()));
dic.Add("posts", PostService.GetAll());
//编辑
if (userId > 0)
{
SysUser sysUser = UserService.SelectUserById(userId);
dic.Add("user", sysUser);
dic.Add("postIds", UserPostService.GetUserPostsByUserId(userId));
dic.Add("roleIds", sysUser.RoleIds);
}
return SUCCESS(dic);
}
///
/// 添加用户
///
///
///
[HttpPost("/v1/skb/sysServer/user/add")]
public IActionResult AddUser([FromBody] SysUserDto parm)
{
var user = parm.Adapt().ToCreate(HttpContext);
if (user == null) { return ToResponse(ApiResult.Error(101, "请求参数错误")); }
if (UserConstants.NOT_UNIQUE.Equals(UserService.CheckUserNameUnique(user.username)))
{
return ToResponse(ApiResult.Error($"新增用户 '{user.username}'失败,登录账号已存在"));
}
user.RoleIds = parm.role;
user.PostIds = parm.post;
user.salt = Function.get_Random(6);
user.password = Function.MD532(Function.CheckNull(user.password) + user.salt);
TokenModel loginUser = JwtUtil.GetLoginUser(HttpContext);
user.CreateBy = loginUser.username;
user.UpdateBy = loginUser.username;
user.LockFlag = "0";
user.DelFlag = "0";
return SUCCESS(UserService.InsertUser(user));
}
///
/// 修改用户
///
///
///
[HttpPut("/v1/skb/sysServer/user/update")]
public IActionResult UpdateUser([FromBody] SysUserDto parm)
{
var user = parm.Adapt().ToUpdate(HttpContext);
if (user == null || user.userId <= 0) { return ToResponse(ApiResult.Error(101, "请求参数错误")); }
user.RoleIds = parm.role;
user.PostIds = parm.post;
if(!string.IsNullOrEmpty(parm.password))
{
user.salt = Function.get_Random(6);
user.password = Function.MD532(Function.CheckNull(user.password) + user.salt);
}
TokenModel loginUser = JwtUtil.GetLoginUser(HttpContext);
user.UpdateBy = loginUser.username;
int upResult = UserService.UpdateUser(user);
return SUCCESS(upResult);
}
///
/// 改变用户状态
///
///
///
[HttpPut("changeStatus")]
[Log(Title = "修改用户状态", BusinessType = BusinessType.UPDATE)]
[ActionPermissionFilter(Permission = "system:user:update")]
public IActionResult ChangeStatus([FromBody] SysUser user)
{
if (user == null) { return ToResponse(ApiResult.Error(101, "请求参数错误")); }
int result = UserService.ChangeUserStatus(user);
return ToResponse(result);
}
///
/// 删除用户
///
///
///
[HttpDelete("/v1/skb/sysServer/user/removeById/{id}")]
public IActionResult Remove(int id = 0)
{
if (id <= 0) { return ToResponse(ApiResult.Error(101, "请求参数错误")); }
if (id == 1) return ToResponse(ResultCode.FAIL, "不能删除管理员账号");
int result = UserService.DeleteUser(id);
return SUCCESS(result);
}
///
/// 重置密码
///
///
[HttpPut("resetPwd")]
[Log(Title = "重置密码", BusinessType = BusinessType.UPDATE)]
[ActionPermissionFilter(Permission = "system:user:resetPwd")]
public IActionResult ResetPwd([FromBody] SysUserDto sysUser)
{
//密码md5
sysUser.password = Function.MD532(sysUser.password);
int result = UserService.ResetPwd(sysUser.userId, sysUser.password);
return ToResponse(result);
}
///
/// 导入
///
/// 使用IFromFile必须使用name属性否则获取不到文件
///
[HttpPost("importData")]
[Log(Title = "用户导入", BusinessType = BusinessType.IMPORT, IsSaveRequestData = false, IsSaveResponseData = true)]
[ActionPermissionFilter(Permission = "system:user:import")]
public IActionResult ImportData([FromForm(Name = "file")] IFormFile formFile)
{
List users = new();
using (var stream = formFile.OpenReadStream())
{
users = stream.Query(startCell: "A2").ToList();
}
return SUCCESS(UserService.ImportUsers(users));
}
///
/// 用户导入模板下载
///
///
[HttpGet("importTemplate")]
[Log(Title = "用户模板", BusinessType = BusinessType.EXPORT, IsSaveRequestData = true, IsSaveResponseData = false)]
[AllowAnonymous]
public IActionResult ImportTemplateExcel()
{
(string, string) result = DownloadImportTemplate("user");
return ExportExcel(result.Item2, result.Item1);
}
///
/// 用户导出
///
///
///
[HttpGet("export")]
[Log(Title = "用户导出", BusinessType = BusinessType.EXPORT)]
[ActionPermissionFilter(Permission = "system:user:export")]
public IActionResult UserExport([FromQuery] SysUserQueryDto user)
{
var list = UserService.SelectUserList(user, new PagerInfo(1, 10000));
var result = ExportExcelMini(list.Records, "user", "用户列表");
return ExportExcel(result.Item2, result.Item1);
}
}
}