SysUserController.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. using Attribute;
  2. using Common;
  3. using Dto;
  4. using Enums;
  5. using Filters;
  6. using Infrastructure;
  7. using Infrastructure.Model;
  8. using Mapster;
  9. using Microsoft.AspNetCore.Authorization;
  10. using Microsoft.AspNetCore.Mvc;
  11. using Middleware;
  12. using MiniExcelLibs;
  13. using Model;
  14. using Model.Base;
  15. using Services;
  16. using Util;
  17. namespace Controllers
  18. {
  19. /// <summary>
  20. /// 用户管理
  21. /// </summary>
  22. [Verify]
  23. public class SysUserController : BaseController
  24. {
  25. private readonly ISysUserService UserService;
  26. private readonly ISysRoleService RoleService;
  27. private readonly ISysPostService PostService;
  28. private readonly ISysUserPostService UserPostService;
  29. public SysUserController(
  30. ISysUserService userService,
  31. ISysRoleService roleService,
  32. ISysPostService postService,
  33. ISysUserPostService userPostService)
  34. {
  35. UserService = userService;
  36. RoleService = roleService;
  37. PostService = postService;
  38. UserPostService = userPostService;
  39. }
  40. /// <summary>
  41. /// 用户管理 -> 获取用户
  42. /// /system/user/list
  43. /// </summary>
  44. /// <returns></returns>
  45. [HttpGet("/v1/skb/sysServer/user/page")]
  46. public IActionResult List([FromQuery] SysUserQueryDto user, PagerInfo pager)
  47. {
  48. var list = UserService.SelectUserList(user, pager);
  49. return SUCCESS(list);
  50. }
  51. /// <summary>
  52. /// 用户管理 -> 编辑、添加用户获取用户,信息查询
  53. /// </summary>
  54. /// <param name="userId"></param>
  55. /// <returns></returns>
  56. [HttpGet("")]
  57. [HttpGet("{userId:int=0}")]
  58. [ActionPermissionFilter(Permission = "system:user:query")]
  59. public IActionResult GetInfo(int userId)
  60. {
  61. Dictionary<string, object> dic = new();
  62. var roles = RoleService.SelectRoleAll();
  63. dic.Add("roles", roles);
  64. //dic.Add("roles", SysUser.IsAdmin(userId) ? roles : roles.FindAll(f => !f.IsAdmin()));
  65. dic.Add("posts", PostService.GetAll());
  66. //编辑
  67. if (userId > 0)
  68. {
  69. SysUser sysUser = UserService.SelectUserById(userId);
  70. dic.Add("user", sysUser);
  71. dic.Add("postIds", UserPostService.GetUserPostsByUserId(userId));
  72. dic.Add("roleIds", sysUser.RoleIds);
  73. }
  74. return SUCCESS(dic);
  75. }
  76. /// <summary>
  77. /// 添加用户
  78. /// </summary>
  79. /// <param name="parm"></param>
  80. /// <returns></returns>
  81. [HttpPost("/v1/skb/sysServer/user/add")]
  82. public IActionResult AddUser([FromBody] SysUserDto parm)
  83. {
  84. var user = parm.Adapt<SysUser>().ToCreate(HttpContext);
  85. if (user == null) { return ToResponse(ApiResult.Error(101, "请求参数错误")); }
  86. if (UserConstants.NOT_UNIQUE.Equals(UserService.CheckUserNameUnique(user.username)))
  87. {
  88. return ToResponse(ApiResult.Error($"新增用户 '{user.username}'失败,登录账号已存在"));
  89. }
  90. user.RoleIds = parm.role;
  91. user.PostIds = parm.post;
  92. user.salt = Function.get_Random(6);
  93. user.password = Function.MD532(Function.CheckNull(user.password) + user.salt);
  94. TokenModel loginUser = JwtUtil.GetLoginUser(HttpContext);
  95. user.CreateBy = loginUser.username;
  96. user.UpdateBy = loginUser.username;
  97. user.LockFlag = "0";
  98. user.DelFlag = "0";
  99. return SUCCESS(UserService.InsertUser(user));
  100. }
  101. /// <summary>
  102. /// 修改用户
  103. /// </summary>
  104. /// <param name="parm"></param>
  105. /// <returns></returns>
  106. [HttpPut("/v1/skb/sysServer/user/update")]
  107. public IActionResult UpdateUser([FromBody] SysUserDto parm)
  108. {
  109. var user = parm.Adapt<SysUser>().ToUpdate(HttpContext);
  110. if (user == null || user.userId <= 0) { return ToResponse(ApiResult.Error(101, "请求参数错误")); }
  111. user.RoleIds = parm.role;
  112. user.PostIds = parm.post;
  113. if(!string.IsNullOrEmpty(parm.password))
  114. {
  115. user.salt = Function.get_Random(6);
  116. user.password = Function.MD532(Function.CheckNull(user.password) + user.salt);
  117. }
  118. TokenModel loginUser = JwtUtil.GetLoginUser(HttpContext);
  119. user.UpdateBy = loginUser.username;
  120. int upResult = UserService.UpdateUser(user);
  121. return SUCCESS(upResult);
  122. }
  123. /// <summary>
  124. /// 改变用户状态
  125. /// </summary>
  126. /// <param name="user"></param>
  127. /// <returns></returns>
  128. [HttpPut("changeStatus")]
  129. [Log(Title = "修改用户状态", BusinessType = BusinessType.UPDATE)]
  130. [ActionPermissionFilter(Permission = "system:user:update")]
  131. public IActionResult ChangeStatus([FromBody] SysUser user)
  132. {
  133. if (user == null) { return ToResponse(ApiResult.Error(101, "请求参数错误")); }
  134. int result = UserService.ChangeUserStatus(user);
  135. return ToResponse(result);
  136. }
  137. /// <summary>
  138. /// 删除用户
  139. /// </summary>
  140. /// <param name="userid"></param>
  141. /// <returns></returns>
  142. [HttpDelete("/v1/skb/sysServer/user/removeById/{id}")]
  143. public IActionResult Remove(int id = 0)
  144. {
  145. if (id <= 0) { return ToResponse(ApiResult.Error(101, "请求参数错误")); }
  146. if (id == 1) return ToResponse(ResultCode.FAIL, "不能删除管理员账号");
  147. int result = UserService.DeleteUser(id);
  148. return SUCCESS(result);
  149. }
  150. /// <summary>
  151. /// 重置密码
  152. /// </summary>
  153. /// <returns></returns>
  154. [HttpPut("resetPwd")]
  155. [Log(Title = "重置密码", BusinessType = BusinessType.UPDATE)]
  156. [ActionPermissionFilter(Permission = "system:user:resetPwd")]
  157. public IActionResult ResetPwd([FromBody] SysUserDto sysUser)
  158. {
  159. //密码md5
  160. sysUser.password = Function.MD532(sysUser.password);
  161. int result = UserService.ResetPwd(sysUser.userId, sysUser.password);
  162. return ToResponse(result);
  163. }
  164. /// <summary>
  165. /// 导入
  166. /// </summary>
  167. /// <param name="formFile">使用IFromFile必须使用name属性否则获取不到文件</param>
  168. /// <returns></returns>
  169. [HttpPost("importData")]
  170. [Log(Title = "用户导入", BusinessType = BusinessType.IMPORT, IsSaveRequestData = false, IsSaveResponseData = true)]
  171. [ActionPermissionFilter(Permission = "system:user:import")]
  172. public IActionResult ImportData([FromForm(Name = "file")] IFormFile formFile)
  173. {
  174. List<SysUser> users = new();
  175. using (var stream = formFile.OpenReadStream())
  176. {
  177. users = stream.Query<SysUser>(startCell: "A2").ToList();
  178. }
  179. return SUCCESS(UserService.ImportUsers(users));
  180. }
  181. /// <summary>
  182. /// 用户导入模板下载
  183. /// </summary>
  184. /// <returns></returns>
  185. [HttpGet("importTemplate")]
  186. [Log(Title = "用户模板", BusinessType = BusinessType.EXPORT, IsSaveRequestData = true, IsSaveResponseData = false)]
  187. [AllowAnonymous]
  188. public IActionResult ImportTemplateExcel()
  189. {
  190. (string, string) result = DownloadImportTemplate("user");
  191. return ExportExcel(result.Item2, result.Item1);
  192. }
  193. /// <summary>
  194. /// 用户导出
  195. /// </summary>
  196. /// <param name="user"></param>
  197. /// <returns></returns>
  198. [HttpGet("export")]
  199. [Log(Title = "用户导出", BusinessType = BusinessType.EXPORT)]
  200. [ActionPermissionFilter(Permission = "system:user:export")]
  201. public IActionResult UserExport([FromQuery] SysUserQueryDto user)
  202. {
  203. var list = UserService.SelectUserList(user, new PagerInfo(1, 10000));
  204. var result = ExportExcelMini(list.Records, "user", "用户列表");
  205. return ExportExcel(result.Item2, result.Item1);
  206. }
  207. }
  208. }