SystemAdmin.php 7.6 KB

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