SysUserController.cs 9.0 KB

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