Admin.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | EasyAdmin
  4. // +----------------------------------------------------------------------
  5. // | PHP交流群: 763822524
  6. // +----------------------------------------------------------------------
  7. // | 开源协议 https://mit-license.org
  8. // +----------------------------------------------------------------------
  9. // | github开源项目:https://github.com/zhongshaofa/EasyAdmin
  10. // +----------------------------------------------------------------------
  11. namespace app\admin\controller\system;
  12. use app\admin\logic\SystemAdminLogic;
  13. use app\common\model\SystemAdmin;
  14. use app\admin\service\TriggerService;
  15. use app\common\constants\AdminConstant;
  16. use app\common\controller\AdminController;
  17. use EasyAdmin\annotation\ControllerAnnotation;
  18. use EasyAdmin\annotation\NodeAnotation;
  19. use think\App;
  20. /**
  21. * Class Admin
  22. * @package app\admin\controller\system
  23. * @ControllerAnnotation(title="管理员管理")
  24. */
  25. class Admin extends AdminController
  26. {
  27. use \app\admin\traits\Curd;
  28. protected $sort = [
  29. 'sort' => 'desc',
  30. 'id' => 'desc',
  31. ];
  32. public function __construct(App $app)
  33. {
  34. parent::__construct($app);
  35. $this->model = new SystemAdmin();
  36. $this->assign('auth_list', $this->model->getAuthList());
  37. }
  38. /**
  39. * @NodeAnotation(title="列表")
  40. */
  41. public function index()
  42. {
  43. if ($this->request->isAjax()) {
  44. if (input('selectFields')) {
  45. return $this->selectList();
  46. }
  47. list($page, $limit, $where) = $this->buildTableParames();
  48. list($count, $list) = SystemAdminLogic::getList($page, $limit, $where, $this->sort);
  49. $data = [
  50. 'code' => 0,
  51. 'msg' => '',
  52. 'count' => $count,
  53. 'data' => $list,
  54. ];
  55. return json($data);
  56. }
  57. return $this->fetch();
  58. }
  59. /**
  60. * @NodeAnotation(title="添加")
  61. */
  62. public function add()
  63. {
  64. if ($this->request->isPost()) {
  65. $post = $this->request->post();
  66. $authIds = $this->request->post('auth_ids', []);
  67. $post['auth_ids'] = implode(',', array_keys($authIds));
  68. $rule = [];
  69. $this->validate($post, $rule);
  70. try {
  71. $save = $this->model->save($post);
  72. } catch (\Exception $e) {
  73. $this->error('保存失败');
  74. }
  75. $save ? $this->success('保存成功') : $this->error('保存失败');
  76. }
  77. return $this->fetch();
  78. }
  79. /**
  80. * @NodeAnotation(title="编辑")
  81. */
  82. public function edit($id)
  83. {
  84. $row = $this->model->find($id);
  85. empty($row) && $this->error('数据不存在');
  86. if ($this->request->isPost()) {
  87. $post = $this->request->post();
  88. $authIds = $this->request->post('auth_ids', []);
  89. $post['auth_ids'] = implode(',', array_keys($authIds));
  90. $rule = [];
  91. $this->validate($post, $rule);
  92. if (isset($row['password'])) {
  93. unset($row['password']);
  94. }
  95. try {
  96. $save = $row->save($post);
  97. TriggerService::updateMenu($id);
  98. } catch (\Exception $e) {
  99. $this->error('保存失败');
  100. }
  101. $save ? $this->success('保存成功') : $this->error('保存失败');
  102. }
  103. $row->auth_ids = explode(',', $row->auth_ids);
  104. $this->assign('row', $row);
  105. return $this->fetch();
  106. }
  107. /**
  108. * @NodeAnotation(title="编辑")
  109. */
  110. public function password($id)
  111. {
  112. $this->checkPostRequest();
  113. $row = $this->model->find($id);
  114. empty($row) && $this->error('数据不存在');
  115. if ($this->request->isAjax()) {
  116. $post = $this->request->post();
  117. $rule = [
  118. 'password|登录密码' => 'require',
  119. 'password_again|确认密码' => 'require',
  120. ];
  121. $this->validate($post, $rule);
  122. if ($post['password'] != $post['password_again']) {
  123. $this->error('两次密码输入不一致');
  124. }
  125. try {
  126. $save = $row->save([
  127. 'password' => password($post['password']),
  128. ]);
  129. } catch (\Exception $e) {
  130. $this->error('保存失败');
  131. }
  132. $save ? $this->success('保存成功') : $this->error('保存失败');
  133. }
  134. $row->auth_ids = explode(',', $row->auth_ids);
  135. $this->assign('row', $row);
  136. return $this->fetch();
  137. }
  138. /**
  139. * @NodeAnotation(title="删除")
  140. */
  141. public function delete($id)
  142. {
  143. $this->checkPostRequest();
  144. $row = $this->model->whereIn('id', $id)->select();
  145. $row->isEmpty() && $this->error('数据不存在');
  146. $id == AdminConstant::SUPER_ADMIN_ID && $this->error('超级管理员不允许修改');
  147. if (is_array($id)) {
  148. if (in_array(AdminConstant::SUPER_ADMIN_ID, $id)) {
  149. $this->error('超级管理员不允许修改');
  150. }
  151. }
  152. try {
  153. $save = $row->delete();
  154. } catch (\Exception $e) {
  155. $this->error('删除失败');
  156. }
  157. $save ? $this->success('删除成功') : $this->error('删除失败');
  158. }
  159. /**
  160. * @NodeAnotation(title="属性修改")
  161. */
  162. public function modify()
  163. {
  164. $this->checkPostRequest();
  165. $post = $this->request->post();
  166. $rule = [
  167. 'id|ID' => 'require',
  168. 'field|字段' => 'require',
  169. 'value|值' => 'require',
  170. ];
  171. $this->validate($post, $rule);
  172. if (!in_array($post['field'], $this->allowModifyFields)) {
  173. $this->error('该字段不允许修改:' . $post['field']);
  174. }
  175. if ($post['id'] == AdminConstant::SUPER_ADMIN_ID && $post['field'] == 'status') {
  176. $this->error('超级管理员状态不允许修改');
  177. }
  178. $row = $this->model->find($post['id']);
  179. empty($row) && $this->error('数据不存在');
  180. try {
  181. $row->save([
  182. $post['field'] => $post['value'],
  183. ]);
  184. } catch (\Exception $e) {
  185. $this->error($e->getMessage());
  186. }
  187. $this->success('保存成功');
  188. }
  189. }