SystemAdmin.php 7.8 KB

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