123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259 |
- using Attribute;
- using Common;
- using Dto;
- using Enums;
- using Feign;
- 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;
- using Vo;
- namespace Controllers
- {
- /// <summary>
- /// 用户管理
- /// </summary>
- [Verify]
- public class SysUserController : BaseController
- {
- private readonly ISysUserService UserService;
- private readonly ISysRoleService RoleService;
- private readonly ISysPostService PostService;
- private readonly ISysUserPostService UserPostService;
- private readonly SysDeptFeign SysDept;
- public SysUserController(
- ISysUserService userService,
- ISysRoleService roleService,
- ISysPostService postService,
- ISysUserPostService userPostService,
- SysDeptFeign sysDeptFeign)
- {
- UserService = userService;
- RoleService = roleService;
- PostService = postService;
- UserPostService = userPostService;
- SysDept = sysDeptFeign;
- }
- /// <summary>
- /// 用户管理 -> 获取用户
- /// /system/user/list
- /// </summary>
- /// <returns></returns>
- [HttpGet("/v1/tel/sysServer/user/page")]
- public IActionResult List([FromQuery] SysUserQueryDto user, PagerInfo pager)
- {
- var list = UserService.SelectUserList(user, pager);
- return SUCCESS(list);
- }
- [AllowAnonymous]
- [HttpGet("/feign/user/test")]
- public SysUserVo Test([FromQuery] long id)
- {
- var info = UserService.Queryable()
- .Includes(m => m.deptJoin)
- // .CrossQuery(typeof(Users), "tel")
- // .Includes(m => m.user)
- .First(m => m.userId == id)
- .Adapt<SysUserVo>() ?? new SysUserVo();
- return info;
- }
- [AllowAnonymous]
- [HttpGet("/feign/user/testlist")]
- public PagedInfo<SysUserVo> TestList()
- {
- var response = UserService.TestList(SysDept);
- return response;
- }
- /// <summary>
- /// 用户管理 -> 编辑、添加用户获取用户,信息查询
- /// </summary>
- /// <param name="userId"></param>
- /// <returns></returns>
- [HttpGet("")]
- [HttpGet("{userId:int=0}")]
- [ActionPermissionFilter(Permission = "system:user:query")]
- public IActionResult GetInfo(int userId)
- {
- Dictionary<string, object> 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);
- }
- /// <summary>
- /// 添加用户
- /// </summary>
- /// <param name="parm"></param>
- /// <returns></returns>
- [HttpPost("/v1/tel/sysServer/user/add")]
- public IActionResult AddUser([FromBody] SysUserDto parm)
- {
- var user = parm.Adapt<SysUser>().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));
- }
- /// <summary>
- /// 修改用户
- /// </summary>
- /// <param name="parm"></param>
- /// <returns></returns>
- [HttpPut("/v1/tel/sysServer/user/update")]
- public IActionResult UpdateUser([FromBody] SysUserDto parm)
- {
- var user = parm.Adapt<SysUser>().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);
- }
- /// <summary>
- /// 改变用户状态
- /// </summary>
- /// <param name="user"></param>
- /// <returns></returns>
- [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);
- }
- /// <summary>
- /// 删除用户
- /// </summary>
- /// <param name="userid"></param>
- /// <returns></returns>
- [HttpDelete("/v1/tel/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);
- }
- /// <summary>
- /// 重置密码
- /// </summary>
- /// <returns></returns>
- [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);
- }
- /// <summary>
- /// 导入
- /// </summary>
- /// <param name="formFile">使用IFromFile必须使用name属性否则获取不到文件</param>
- /// <returns></returns>
- [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<SysUser> users = new();
- using (var stream = formFile.OpenReadStream())
- {
- users = stream.Query<SysUser>(startCell: "A2").ToList();
- }
- return SUCCESS(UserService.ImportUsers(users));
- }
- /// <summary>
- /// 用户导入模板下载
- /// </summary>
- /// <returns></returns>
- [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);
- }
- /// <summary>
- /// 用户导出
- /// </summary>
- /// <param name="user"></param>
- /// <returns></returns>
- [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);
- }
- }
- }
|