Admin.php 6.5 KB

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