SystemAdmin.php 8.3 KB

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