SystemAdmin.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. $salt = substr(md5(rand(1, 999999)), 0, 6);
  80. $data['pwd'] = md5(md5($data['pwd']) . md5($salt));
  81. $data['salt'] = $salt;
  82. $data['add_time'] = time();
  83. unset($data['conf_pwd']);
  84. $data['level'] = $this->adminInfo['level'] + 1;
  85. $data['add_time'] = time();
  86. if (!AdminModel::create($data)) return Json::fail('添加管理员失败');
  87. return Json::successful('添加管理员成功!');
  88. }
  89. /**
  90. * 显示编辑资源表单页.
  91. *
  92. * @param int $id
  93. * @return \think\Response
  94. */
  95. public function edit($id)
  96. {
  97. if (!$id) return $this->failed('参数错误');
  98. $admin = AdminModel::get($id);
  99. if (!$admin) return Json::fail('数据不存在!');
  100. $f = array();
  101. $f[] = Form::input('account', '管理员账号', $admin->account);
  102. $f[] = Form::input('pwd', '管理员密码')->type('password');
  103. $f[] = Form::input('conf_pwd', '确认密码')->type('password');
  104. $f[] = Form::input('real_name', '管理员姓名', $admin->real_name);
  105. $f[] = Form::select('roles', '管理员身份', explode(',', $admin->roles))->setOptions(function () use ($admin) {
  106. $list = SystemRole::getRole($admin->level);
  107. $options = [];
  108. foreach ($list as $id => $roleName) {
  109. $options[] = ['label' => $roleName, 'value' => $id];
  110. }
  111. return $options;
  112. })->multiple(1);
  113. $f[] = Form::radio('status', '状态', 1)->options([['label' => '开启', 'value' => 1], ['label' => '关闭', 'value' => 0]]);
  114. $form = Form::make_post_form('编辑管理员', $f, Url::buildUrl('update', compact('id')));
  115. $this->assign(compact('form'));
  116. return $this->fetch('public/form-builder');
  117. }
  118. /**
  119. * 保存更新的资源
  120. *
  121. * @param \think\Request $request
  122. * @param int $id
  123. * @return \think\Response
  124. */
  125. public function update($id)
  126. {
  127. $data = Util::postMore([
  128. 'account',
  129. 'conf_pwd',
  130. 'pwd',
  131. 'real_name',
  132. ['roles', []],
  133. ['status', 0]
  134. ]);
  135. if (!$data['account']) return Json::fail('请输入管理员账号');
  136. if (!$data['roles']) return Json::fail('请选择至少一个管理员身份');
  137. if (!$data['pwd'])
  138. unset($data['pwd']);
  139. else {
  140. if (isset($data['pwd']) && $data['pwd'] != $data['conf_pwd']) return Json::fail('两次输入密码不想同');
  141. $salt = substr(md5(rand(1, 999999)), 0, 6);
  142. $data['pwd'] = md5(md5($data['pwd']) . md5($salt));
  143. $data['salt'] = $salt;
  144. }
  145. if (AdminModel::where('account', $data['account'])->where('id', '<>', $id)->count()) return Json::fail('管理员账号已存在');
  146. unset($data['conf_pwd']);
  147. if (!AdminModel::edit($data, $id)) return Json::fail('修改失败');
  148. return Json::successful('修改成功!');
  149. }
  150. /**
  151. * 删除指定资源
  152. *
  153. * @param int $id
  154. * @return \think\Response
  155. */
  156. public function delete($id)
  157. {
  158. if (!$id)
  159. return Json::fail('删除失败!');
  160. if (AdminModel::edit(['is_del' => 1, 'status' => 0], $id, 'id'))
  161. return Json::successful('删除成功!');
  162. else
  163. return Json::fail('删除失败!');
  164. }
  165. /**
  166. * 个人资料 展示
  167. * @return string
  168. */
  169. public function admin_info()
  170. {
  171. $adminInfo = $this->adminInfo;//获取当前登录的管理员
  172. $this->assign('adminInfo', $adminInfo);
  173. return $this->fetch();
  174. }
  175. /**
  176. * 保存信息
  177. */
  178. public function setAdminInfo()
  179. {
  180. $adminInfo = $this->adminInfo;//获取当前登录的管理员
  181. if ($this->request->isPost()) {
  182. $data = Util::postMore([
  183. ['new_pwd', ''],
  184. ['new_pwd_ok', ''],
  185. ['pwd', ''],
  186. 'real_name',
  187. ]);
  188. if ($data['pwd'] != '') {
  189. $pwd = md5(md5($data['pwd']) . md5($adminInfo['salt']));
  190. if ($adminInfo['pwd'] != $pwd) return Json::fail('原始密码错误');
  191. }
  192. if ($data['new_pwd'] != '') {
  193. if (!$data['new_pwd_ok']) return Json::fail('请输入确认新密码');
  194. if ($data['new_pwd'] != $data['new_pwd_ok']) return Json::fail('俩次密码不一样');
  195. }
  196. if ($data['pwd'] != '' && $data['new_pwd'] != '') {
  197. $salt = substr(md5(rand(1, 999999)), 0, 6);
  198. $data['pwd'] = md5(md5($data['new_pwd']) . md5($salt));
  199. $data['salt'] = $salt;
  200. } else {
  201. unset($data['pwd']);
  202. }
  203. unset($data['new_pwd']);
  204. unset($data['new_pwd_ok']);
  205. if (!AdminModel::edit($data, $adminInfo['id'])) return Json::fail('修改失败');
  206. return Json::successful('修改成功!,请重新登录');
  207. }
  208. }
  209. }