SystemAdmin.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <?php
  2. namespace app\admin\controller\setting;
  3. use app\admin\controller\AuthController;
  4. use crmeb\services\{FormBuilder as Form, JsonService as Json, UtilService as Util};
  5. use app\admin\model\system\{SystemRole, SystemAdmin as AdminModel};
  6. use think\facade\Route as Url;
  7. /**
  8. * 管理员列表控制器
  9. * Class SystemAdmin
  10. * @package app\admin\controller\system
  11. */
  12. class SystemAdmin extends AuthController
  13. {
  14. /**
  15. * 显示资源列表
  16. *
  17. * @return \think\Response
  18. */
  19. public function index()
  20. {
  21. $admin = $this->adminInfo;
  22. $where = Util::getMore([
  23. ['name', ''],
  24. ['roles', ''],
  25. ['level', bcadd($admin->level, 1, 0)]
  26. ]);
  27. $this->assign('where', $where);
  28. $this->assign('role', SystemRole::getRole(bcadd($admin->level, 1, 0)));
  29. $this->assign(AdminModel::systemPage($where));
  30. return $this->fetch();
  31. }
  32. /**
  33. * 显示创建资源表单页.
  34. *
  35. * @return \think\Response
  36. */
  37. public function create()
  38. {
  39. $admin = $this->adminInfo;
  40. $f = array();
  41. $f[] = Form::input('account', '管理员账号');
  42. $f[] = Form::input('pwd', '管理员密码')->type('password');
  43. $f[] = Form::input('conf_pwd', '确认密码')->type('password');
  44. $f[] = Form::input('real_name', '管理员姓名');
  45. $f[] = Form::select('roles', '管理员身份')->setOptions(function () use ($admin) {
  46. $list = SystemRole::getRole(bcadd($admin->level, 1, 0));
  47. $options = [];
  48. foreach ($list as $id => $roleName) {
  49. $options[] = ['label' => $roleName, 'value' => $id];
  50. }
  51. return $options;
  52. })->multiple(1);
  53. $f[] = Form::radio('status', '状态', 1)->options([['label' => '开启', 'value' => 1], ['label' => '关闭', 'value' => 0]]);
  54. $form = Form::make_post_form('添加管理员', $f, Url::buildUrl('save'));
  55. $this->assign(compact('form'));
  56. return $this->fetch('public/form-builder');
  57. }
  58. /**
  59. * 保存新建的资源
  60. *
  61. * @param \think\Request $request
  62. * @return \think\Response
  63. */
  64. public function save()
  65. {
  66. $data = Util::postMore([
  67. 'account',
  68. 'conf_pwd',
  69. 'pwd',
  70. 'real_name',
  71. ['roles', []],
  72. ['status', 0]
  73. ]);
  74. if (!$data['account']) return Json::fail('请输入管理员账号');
  75. if (!$data['roles']) return Json::fail('请选择至少一个管理员身份');
  76. if (!$data['pwd']) return Json::fail('请输入管理员登陆密码');
  77. if ($data['pwd'] != $data['conf_pwd']) return Json::fail('两次输入密码不想同');
  78. if (AdminModel::be($data['account'], 'account')) return Json::fail('管理员账号已存在');
  79. $data['pwd'] = md5($data['pwd']);
  80. $data['add_time'] = time();
  81. unset($data['conf_pwd']);
  82. $data['level'] = $this->adminInfo['level'] + 1;
  83. $data['add_time'] = time();
  84. if (!AdminModel::create($data)) return Json::fail('添加管理员失败');
  85. return Json::successful('添加管理员成功!');
  86. }
  87. /**
  88. * 显示编辑资源表单页.
  89. *
  90. * @param int $id
  91. * @return \think\Response
  92. */
  93. public function edit($id)
  94. {
  95. if (!$id) return $this->failed('参数错误');
  96. $admin = AdminModel::get($id);
  97. if (!$admin) return Json::fail('数据不存在!');
  98. $f = array();
  99. $f[] = Form::input('account', '管理员账号', $admin->account);
  100. $f[] = Form::input('pwd', '管理员密码')->type('password');
  101. $f[] = Form::input('conf_pwd', '确认密码')->type('password');
  102. $f[] = Form::input('real_name', '管理员姓名', $admin->real_name);
  103. $f[] = Form::select('roles', '管理员身份', explode(',', $admin->roles))->setOptions(function () use ($admin) {
  104. $list = SystemRole::getRole($admin->level);
  105. $options = [];
  106. foreach ($list as $id => $roleName) {
  107. $options[] = ['label' => $roleName, 'value' => $id];
  108. }
  109. return $options;
  110. })->multiple(1);
  111. $f[] = Form::radio('status', '状态', 1)->options([['label' => '开启', 'value' => 1], ['label' => '关闭', 'value' => 0]]);
  112. $form = Form::make_post_form('编辑管理员', $f, Url::buildUrl('update', compact('id')));
  113. $this->assign(compact('form'));
  114. return $this->fetch('public/form-builder');
  115. }
  116. /**
  117. * 保存更新的资源
  118. *
  119. * @param \think\Request $request
  120. * @param int $id
  121. * @return \think\Response
  122. */
  123. public function update($id)
  124. {
  125. $data = Util::postMore([
  126. 'account',
  127. 'conf_pwd',
  128. 'pwd',
  129. 'real_name',
  130. ['roles', []],
  131. ['status', 0]
  132. ]);
  133. if (!$data['account']) return Json::fail('请输入管理员账号');
  134. if (!$data['roles']) return Json::fail('请选择至少一个管理员身份');
  135. if (!$data['pwd'])
  136. unset($data['pwd']);
  137. else {
  138. if (isset($data['pwd']) && $data['pwd'] != $data['conf_pwd']) return Json::fail('两次输入密码不想同');
  139. $data['pwd'] = md5($data['pwd']);
  140. }
  141. if (AdminModel::where('account', $data['account'])->where('id', '<>', $id)->count()) return Json::fail('管理员账号已存在');
  142. unset($data['conf_pwd']);
  143. if (!AdminModel::edit($data, $id)) return Json::fail('修改失败');
  144. return Json::successful('修改成功!');
  145. }
  146. /**
  147. * 删除指定资源
  148. *
  149. * @param int $id
  150. * @return \think\Response
  151. */
  152. public function delete($id)
  153. {
  154. if (!$id)
  155. return Json::fail('删除失败!');
  156. if (AdminModel::edit(['is_del' => 1, 'status' => 0], $id, 'id'))
  157. return Json::successful('删除成功!');
  158. else
  159. return Json::fail('删除失败!');
  160. }
  161. /**
  162. * 个人资料 展示
  163. * @return string
  164. */
  165. public function admin_info()
  166. {
  167. $adminInfo = $this->adminInfo;//获取当前登录的管理员
  168. $this->assign('adminInfo', $adminInfo);
  169. return $this->fetch();
  170. }
  171. /**
  172. * 保存信息
  173. */
  174. public function setAdminInfo()
  175. {
  176. $adminInfo = $this->adminInfo;//获取当前登录的管理员
  177. if ($this->request->isPost()) {
  178. $data = Util::postMore([
  179. ['new_pwd', ''],
  180. ['new_pwd_ok', ''],
  181. ['pwd', ''],
  182. 'real_name',
  183. ]);
  184. if ($data['pwd'] != '') {
  185. $pwd = md5($data['pwd']);
  186. if ($adminInfo['pwd'] != $pwd) return Json::fail('原始密码错误');
  187. }
  188. if ($data['new_pwd'] != '') {
  189. if (!$data['new_pwd_ok']) return Json::fail('请输入确认新密码');
  190. if ($data['new_pwd'] != $data['new_pwd_ok']) return Json::fail('俩次密码不一样');
  191. }
  192. if ($data['pwd'] != '' && $data['new_pwd'] != '') {
  193. $data['pwd'] = md5($data['new_pwd']);
  194. } else {
  195. unset($data['pwd']);
  196. }
  197. unset($data['new_pwd']);
  198. unset($data['new_pwd_ok']);
  199. if (!AdminModel::edit($data, $adminInfo['id'])) return Json::fail('修改失败');
  200. return Json::successful('修改成功!,请重新登录');
  201. }
  202. }
  203. }