Admin.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. namespace app\admin\controller\system;
  3. use app\admin\model\SystemAdmin;
  4. use app\admin\service\TriggerService;
  5. use app\common\constants\AdminConstant;
  6. use app\common\controller\AdminController;
  7. use app\admin\service\annotation\ControllerAnnotation;
  8. use app\admin\service\annotation\NodeAnnotation;
  9. use app\Request;
  10. use think\App;
  11. use think\response\Json;
  12. #[ControllerAnnotation(title: '管理员管理')]
  13. class Admin extends AdminController
  14. {
  15. protected array $sort = [
  16. 'sort' => 'desc',
  17. 'id' => 'desc',
  18. ];
  19. public function __construct(App $app)
  20. {
  21. parent::__construct($app);
  22. self::$model = SystemAdmin::class;
  23. $this->assign('auth_list', self::$model::getAuthList());
  24. }
  25. #[NodeAnnotation(title: '列表', auth: true)]
  26. public function index(Request $request): Json|string
  27. {
  28. if ($request->isAjax()) {
  29. if (input('selectFields')) {
  30. return $this->selectList();
  31. }
  32. list($page, $limit, $where) = $this->buildTableParams();
  33. $count = self::$model::where($where)->count();
  34. $list = self::$model::withoutField('password')
  35. ->where($where)
  36. ->page($page, $limit)
  37. ->order($this->sort)
  38. ->select()->toArray();
  39. $data = [
  40. 'code' => 0,
  41. 'msg' => '',
  42. 'count' => $count,
  43. 'data' => $list,
  44. ];
  45. return json($data);
  46. }
  47. return $this->fetch();
  48. }
  49. #[NodeAnnotation(title: '添加', auth: true)]
  50. public function add(Request $request): string
  51. {
  52. if ($request->isPost()) {
  53. $post = $request->post();
  54. $authIds = $request->post('auth_ids', []);
  55. $post['auth_ids'] = implode(',', array_keys($authIds));
  56. $rule = [];
  57. $this->validate($post, $rule);
  58. if (empty($post['password'])) $post['password'] = '123456';
  59. $post['password'] = password_hash($post['password'],PASSWORD_DEFAULT);
  60. try {
  61. $save = self::$model::create($post);
  62. }catch (\Exception $e) {
  63. $this->error('保存失败' . $e->getMessage());
  64. }
  65. $save ? $this->success('保存成功') : $this->error('保存失败');
  66. }
  67. return $this->fetch();
  68. }
  69. #[NodeAnnotation(title: '编辑', auth: true)]
  70. public function edit(Request $request, $id = 0): string
  71. {
  72. $row = self::$model::find($id);
  73. empty($row) && $this->error('数据不存在');
  74. if ($request->isPost()) {
  75. $post = $request->post();
  76. $authIds = $request->post('auth_ids', []);
  77. $post['auth_ids'] = implode(',', array_keys($authIds));
  78. $rule = [];
  79. $this->validate($post, $rule);
  80. try {
  81. $save = $row->save($post);
  82. TriggerService::updateMenu($id);
  83. }catch (\Exception $e) {
  84. $this->error('保存失败' . $e->getMessage());
  85. }
  86. $save ? $this->success('保存成功') : $this->error('保存失败');
  87. }
  88. $this->assign('row', $row);
  89. return $this->fetch();
  90. }
  91. #[NodeAnnotation(title: '设置密码', auth: true)]
  92. public function password(Request $request, $id): string
  93. {
  94. $row = self::$model::find($id);
  95. empty($row) && $this->error('数据不存在');
  96. if ($request->isAjax()) {
  97. $post = $request->post();
  98. $rule = [
  99. 'password|登录密码' => 'require',
  100. 'password_again|确认密码' => 'require',
  101. ];
  102. $this->validate($post, $rule);
  103. if ($post['password'] != $post['password_again']) {
  104. $this->error('两次密码输入不一致');
  105. }
  106. try {
  107. $save = $row->save([
  108. 'password' => password_hash($post['password'], PASSWORD_DEFAULT),
  109. ]);
  110. }catch (\Exception $e) {
  111. $this->error('保存失败');
  112. }
  113. $save ? $this->success('保存成功') : $this->error('保存失败');
  114. }
  115. $this->assign('row', $row);
  116. return $this->fetch();
  117. }
  118. #[NodeAnnotation(title: '删除', auth: true)]
  119. public function delete(Request $request): void
  120. {
  121. $this->checkPostRequest();
  122. $id = $request->param('id');
  123. $row = self::$model::whereIn('id', $id)->select();
  124. $row->isEmpty() && $this->error('数据不存在');
  125. $id == AdminConstant::SUPER_ADMIN_ID && $this->error('超级管理员不允许修改');
  126. if (is_array($id)) {
  127. if (in_array(AdminConstant::SUPER_ADMIN_ID, $id)) {
  128. $this->error('超级管理员不允许修改');
  129. }
  130. }
  131. try {
  132. $save = $row->delete();
  133. }catch (\Exception $e) {
  134. $this->error('删除失败');
  135. }
  136. $save ? $this->success('删除成功') : $this->error('删除失败');
  137. }
  138. #[NodeAnnotation(title: '属性修改', auth: true)]
  139. public function modify(Request $request): void
  140. {
  141. $this->checkPostRequest();
  142. $post = $request->post();
  143. $rule = [
  144. 'id|ID' => 'require',
  145. 'field|字段' => 'require',
  146. 'value|值' => 'require',
  147. ];
  148. $this->validate($post, $rule);
  149. if (!in_array($post['field'], $this->allowModifyFields)) {
  150. $this->error('该字段不允许修改:' . $post['field']);
  151. }
  152. if ($post['id'] == AdminConstant::SUPER_ADMIN_ID && $post['field'] == 'status') {
  153. $this->error('超级管理员状态不允许修改');
  154. }
  155. $row = self::$model::find($post['id']);
  156. empty($row) && $this->error('数据不存在');
  157. try {
  158. $row->save([
  159. $post['field'] => $post['value'],
  160. ]);
  161. }catch (\Exception $e) {
  162. $this->error($e->getMessage());
  163. }
  164. $this->success('保存成功');
  165. }
  166. }