User.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\common\AdminController;
  4. use app\admin\api\UserApi;
  5. use think\exception\ValidateException;
  6. use app\admin\validate\User as UserValidate;
  7. /**
  8. * Description of Role
  9. *
  10. * @author sgq
  11. */
  12. class User extends AdminController {
  13. /**
  14. * @auth {{/mgr}}
  15. * @return type
  16. */
  17. public function index() {
  18. return view();
  19. }
  20. /**
  21. * @auth {{/mgr/list}}
  22. * @return type
  23. */
  24. public function list() {
  25. $list = UserApi::getList($this->request->param());
  26. return json($list);
  27. }
  28. /**
  29. * @auth {{/mgr/add}}
  30. * @return type
  31. */
  32. public function add() {
  33. if ($this->request->isPost()) {
  34. try {
  35. $params = $this->request->param();
  36. validate(UserValidate::class)->scene("add")->check($params);
  37. $res = UserApi::create($params);
  38. switch ($res) {
  39. case 10001:
  40. return json(["msg" => "添加成功"]);
  41. default:
  42. throw new ValidateException("未知原因,请联系管理员");
  43. }
  44. } catch (ValidateException $e) {
  45. return json(["msg" => $e->getMessage()], 500);
  46. }
  47. }
  48. return view();
  49. }
  50. /**
  51. * @auth {{/mgr/edit}}
  52. * @return type
  53. */
  54. public function edit() {
  55. $params = $this->request->param();
  56. if ($this->request->isPost()) {
  57. try {
  58. validate(UserValidate::class)->scene("edit")->check($params);
  59. $res = UserApi::update($params);
  60. switch ($res) {
  61. case 10001:
  62. return json(["msg" => "编辑成功"]);
  63. case 10002:
  64. throw new ValidateException("账户已经被删除,不能编辑");
  65. case 10003:
  66. throw new ValidateException("不能编辑管理员账户");
  67. default:
  68. throw new ValidateException("未知原因,请联系管理员");
  69. }
  70. } catch (ValidateException $e) {
  71. return json(["msg" => $e->getMessage()], 500);
  72. }
  73. }
  74. $id = $params["userId"];
  75. return view("", ["user" => UserApi::getOne($id)]);
  76. }
  77. public function info() {
  78. $userId = $this->user["uid"];
  79. if ($this->request->isPost()) {
  80. try {
  81. $params = $this->request->param();
  82. $params["id"] = $userId;
  83. validate(UserValidate::class)->scene("info")->check($params);
  84. $res = UserApi::update($params);
  85. switch ($res) {
  86. case 10001:
  87. return json(["msg" => "编辑成功"]);
  88. case 10002:
  89. throw new ValidateException("账户已经被删除,不能编辑");
  90. case 10003:
  91. throw new ValidateException("不能编辑管理员账户");
  92. default:
  93. throw new ValidateException("未知原因,请联系管理员");
  94. }
  95. } catch (ValidateException $e) {
  96. return json(["msg" => $e->getMessage()], 500);
  97. }
  98. }
  99. $user = UserApi::getOne($userId);
  100. return view("", ["user" => $user]);
  101. }
  102. public function change_pwd() {
  103. $params = $this->request->param();
  104. $id = $this->user["uid"];
  105. if ($this->request->isPost()) {
  106. try {
  107. validate(UserValidate::class)->scene("change_pwd")->check($params);
  108. $oldPwd = $params["old_password"];
  109. $pwd = $params["password"];
  110. $res = UserApi::setPwd($id, $oldPwd, $pwd);
  111. switch ($res) {
  112. case 10001:
  113. return json(["msg" => "密码修改成功"]);
  114. case 10002:
  115. throw new ValidateException("账户已经被删除,密码修改失败");
  116. case 10003:
  117. throw new ValidateException("不能修改管理员账户的密码");
  118. case 10004:
  119. throw new ValidateException("原密码错误,修改密码失败");
  120. default:
  121. throw new ValidateException("未知原因,请联系管理员");
  122. }
  123. } catch (ValidateException $e) {
  124. return json(["msg" => $e->getMessage()], 500);
  125. }
  126. }
  127. return view("", ["user" => UserApi::getOne($id)]);
  128. }
  129. /**
  130. * @auth {{/mgr/delete}}
  131. */
  132. public function delete() {
  133. if ($this->request->isPost()) {
  134. $res = UserApi::delete($this->request->param("userId"));
  135. switch ($res) {
  136. case 10001:
  137. return json(["msg" => "删除成功"]);
  138. case 10002:
  139. return json(["msg" => "不能重复删除"], 500);
  140. case 10003:
  141. return json(["msg" => "不能操作管理员账户"], 500);
  142. default:
  143. return json(["msg" => "未知原因,请联系管理员"], 500);
  144. }
  145. }
  146. }
  147. /**
  148. * @auth {{/mgr/reset}}
  149. */
  150. public function reset() {
  151. if ($this->request->isPost()) {
  152. $id = $this->request->param("userId");
  153. $info = UserApi::getOne($id);
  154. if (!$info)
  155. return json(["msg" => "没有对应的管理员账户"]);
  156. $salt = $info["salt"];
  157. $def_pwd = "JJrc@123";
  158. $password = UserApi::getPwd($def_pwd, $salt);
  159. $res = UserApi::reset($id, $password);
  160. switch ($res) {
  161. case 10001:
  162. return json(["msg" => "密码重置成功"]);
  163. case 10002:
  164. return json(["msg" => "账户已经被删除,密码重置失败"], 500);
  165. case 10003:
  166. return json(["msg" => "不能重置管理员账户"], 500);
  167. default:
  168. return json(["msg" => "未知原因,请联系管理员"], 500);
  169. }
  170. }
  171. return view();
  172. }
  173. /**
  174. * @auth {{/mgr/freeze}}
  175. */
  176. public function freeze() {
  177. if ($this->request->isPost()) {
  178. $params = $this->request->param();
  179. $res = UserApi::setFreeze($params["userId"], 2);
  180. switch ($res) {
  181. case 10001:
  182. return json(["msg" => "冻结成功"]);
  183. case 10002:
  184. return json(["msg" => "账户已经被删除,冻结失败"], 500);
  185. case 10003:
  186. return json(["msg" => "不能重置管理员账户"], 500);
  187. default:
  188. return json(["msg" => "未知原因,请联系管理员"], 500);
  189. }
  190. }
  191. }
  192. /**
  193. * @auth {{/mgr/unfreeze}}
  194. */
  195. public function unfreeze() {
  196. if ($this->request->isPost()) {
  197. $params = $this->request->param();
  198. $res = UserApi::setFreeze($params["userId"], 1);
  199. switch ($res) {
  200. case 10001:
  201. return json(["msg" => "解除冻结成功"]);
  202. case 10002:
  203. return json(["msg" => "账户已经被删除,解除冻结失败"], 500);
  204. case 10003:
  205. return json(["msg" => "不能重置管理员账户"], 500);
  206. default:
  207. return json(["msg" => "未知原因,请联系管理员"], 500);
  208. }
  209. }
  210. }
  211. /**
  212. * @auth {{/mgr/setRole}}
  213. */
  214. public function setRole() {
  215. if ($this->request->isPost()) {
  216. $params = $this->request->param();
  217. $res = UserApi::setRole($params["userId"], $params["roleIds"]);
  218. switch ($res) {
  219. case 10001:
  220. return json(["msg" => "角色分配成功"]);
  221. case 10002:
  222. return json(["msg" => "账户已经被删除,角色分配失败"], 500);
  223. case 10003:
  224. return json(["msg" => "不能分配管理员账户角色"], 500);
  225. default:
  226. return json(["msg" => "未知原因,请联系管理员"], 500);
  227. }
  228. }
  229. }
  230. /**
  231. * @auth {{/mgr/role_assign}}
  232. */
  233. public function role_assign() {
  234. $user = UserApi::getOne($this->request->param("userId"));
  235. return view("", ["user" => $user]);
  236. }
  237. /**
  238. * 上传头像
  239. */
  240. public function upload() {
  241. if ($this->request->file()) {
  242. $avatar = $this->request->file("file");
  243. $upload = new \app\common\api\UploadApi();
  244. $result = $upload->uploadOne($avatar, "image", "user/avatar");
  245. if ($result->code == 200) {
  246. $url = $result->filepath;
  247. return json([$url]);
  248. }
  249. return json(["msg" => $result->msg], 500);
  250. }
  251. }
  252. }