SystemAdmin.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\admin\controller\setting;
  12. use app\admin\controller\AuthController;
  13. use app\wap\model\user\User;
  14. use service\FormBuilder as Form;
  15. use service\JsonService as Json;
  16. use think\Request;
  17. use app\admin\model\system\SystemRole;
  18. use think\Url;
  19. use app\admin\model\system\SystemAdmin as AdminModel;
  20. /**
  21. * 管理员列表控制器
  22. * Class SystemAdmin
  23. * @package app\admin\controller\system
  24. */
  25. class SystemAdmin extends AuthController
  26. {
  27. /**
  28. * 显示资源列表
  29. *
  30. * @return \think\Response
  31. */
  32. public function index()
  33. {
  34. $admin = $this->adminInfo;
  35. $where = parent::getMore([
  36. ['name', ''],
  37. ['roles', ''],
  38. ['level', bcadd($admin->level, 1, 0)]
  39. ], $this->request);
  40. $this->assign('where', $where);
  41. $this->assign('role', SystemRole::getRole(bcadd($admin->level, 1, 0)));
  42. $this->assign(AdminModel::systemPage($where));
  43. return $this->fetch();
  44. }
  45. /**
  46. * 显示创建资源表单页.
  47. *
  48. * @return \think\Response
  49. */
  50. public function create()
  51. {
  52. $admin = $this->adminInfo;
  53. $f = array();
  54. $f[] = Form::input('account', '管理员账号');
  55. $f[] = Form::input('pwd', '管理员密码')->type('password');
  56. $f[] = Form::input('conf_pwd', '确认密码')->type('password');
  57. $f[] = Form::input('real_name', '管理员姓名');
  58. $f[] = Form::select('roles', '管理员身份')->setOptions(function () use ($admin) {
  59. $list = SystemRole::getRole(bcadd($admin->level, 1, 0));
  60. $options = [];
  61. foreach ($list as $id => $roleName) {
  62. $options[] = ['label' => $roleName, 'value' => $id];
  63. }
  64. return $options;
  65. })->multiple(1);
  66. $f[] = Form::input('phone', '前端登录手机号')->type('phone');
  67. $f[] = Form::radio('status', '状态', 1)->options([['label' => '开启', 'value' => 1], ['label' => '关闭', 'value' => 0]]);
  68. $form = Form::make_post_form('添加管理员', $f, Url::build('save'));
  69. $this->assign(compact('form'));
  70. return $this->fetch('public/form-builder');
  71. }
  72. /**
  73. * 保存新建的资源
  74. *
  75. * @param \think\Request $request
  76. * @return \think\Response
  77. */
  78. public function save(Request $request)
  79. {
  80. $data = parent::postMore([
  81. 'account',
  82. 'conf_pwd',
  83. 'pwd',
  84. 'real_name',
  85. 'phone',
  86. ['roles', []],
  87. ['status', 0]
  88. ], $request);
  89. if (!$data['account']) return Json::fail('请输入管理员账号');
  90. if (!$data['roles']) return Json::fail('请选择至少一个管理员身份');
  91. foreach ($data['roles'] as $v) {
  92. $role = SystemRole::where('id',$v)->find();
  93. if ($role && $role['sign'] == 'verification') {
  94. if (!$data['phone']) return Json::fail('请填写前端登录电话');
  95. $user = User::where('phone',$data['phone'])->find();
  96. if (!$user) return Json::fail('请至前端-个人中心-点击头像补充个人资料');
  97. }
  98. }
  99. if (!$data['pwd']) return Json::fail('请输入管理员登陆密码');
  100. if ($data['pwd'] != $data['conf_pwd']) return Json::fail('两次输入密码不想同');
  101. if (AdminModel::be($data['account'], 'account')) return Json::fail('管理员账号已存在');
  102. $data['pwd'] = md5($data['pwd']);
  103. unset($data['conf_pwd']);
  104. $data['level'] = $this->adminInfo['level'] + 1;
  105. AdminModel::set($data);
  106. return Json::successful('添加管理员成功!');
  107. }
  108. /**
  109. * 显示编辑资源表单页.
  110. *
  111. * @param int $id
  112. * @return \think\Response
  113. */
  114. public function edit($id)
  115. {
  116. if (!$id) return $this->failed('参数错误');
  117. $admin = AdminModel::get($id);
  118. if (!$admin) return Json::fail('数据不存在!');
  119. $f = array();
  120. $f[] = Form::input('account', '管理员账号', $admin->account);
  121. $f[] = Form::input('pwd', '管理员密码')->type('password');
  122. $f[] = Form::input('conf_pwd', '确认密码')->type('password');
  123. $f[] = Form::input('real_name', '管理员姓名', $admin->real_name);
  124. $f[] = Form::select('roles', '管理员身份', explode(',', $admin->roles))->setOptions(function () use ($admin) {
  125. $list = SystemRole::getRole($admin->level);
  126. $options = [];
  127. foreach ($list as $id => $roleName) {
  128. $options[] = ['label' => $roleName, 'value' => $id];
  129. }
  130. return $options;
  131. })->multiple(1);
  132. $f[] = Form::input('phone', '前端登录手机号', $admin->phone)->type('phone');
  133. $f[] = Form::radio('status', '状态', 1)->options([['label' => '开启', 'value' => 1], ['label' => '关闭', 'value' => 0]]);
  134. $form = Form::make_post_form('编辑管理员', $f, Url::build('update', compact('id')));
  135. $this->assign(compact('form'));
  136. return $this->fetch('public/form-builder');
  137. }
  138. /**
  139. * 保存更新的资源
  140. *
  141. * @param \think\Request $request
  142. * @param int $id
  143. * @return \think\Response
  144. */
  145. public function update(Request $request, $id)
  146. {
  147. $data = parent::postMore([
  148. 'account',
  149. 'conf_pwd',
  150. 'pwd',
  151. 'real_name',
  152. 'phone',
  153. ['roles', []],
  154. ['status', 0]
  155. ], $request);
  156. if (!$data['account']) return Json::fail('请输入管理员账号');
  157. if (!$data['roles']) return Json::fail('请选择至少一个管理员身份');
  158. if (!$data['pwd']) {
  159. unset($data['pwd']);
  160. }else {
  161. if (isset($data['pwd']) && $data['pwd'] != $data['conf_pwd']) return Json::fail('两次输入密码不想同');
  162. $data['pwd'] = md5($data['pwd']);
  163. }
  164. foreach ($data['roles'] as $v) {
  165. $role = SystemRole::where('id',$v)->find();
  166. if ($role && $role['sign'] == 'verification') {
  167. if (!$data['phone']) return Json::fail('请填写前端登录电话');
  168. $user = User::where('phone',$data['phone'])->find();
  169. if (!$user) return Json::fail('请至前端-个人中心-点击头像补充个人资料');
  170. }
  171. }
  172. if (AdminModel::where('account', $data['account'])->where('id', '<>', $id)->count()) return Json::fail('管理员账号已存在');
  173. unset($data['conf_pwd']);
  174. AdminModel::edit($data, $id);
  175. return Json::successful('修改成功!');
  176. }
  177. /**
  178. * 删除指定资源
  179. *
  180. * @param int $id
  181. * @return \think\Response
  182. */
  183. public function delete($id)
  184. {
  185. if (!$id)
  186. return Json::fail('删除失败!');
  187. if (AdminModel::edit(['is_del' => 1, 'status' => 0], $id, 'id'))
  188. return Json::successful('删除成功!');
  189. else
  190. return Json::fail('删除失败!');
  191. }
  192. /**
  193. * 个人资料 展示
  194. * */
  195. public function adminInfo()
  196. {
  197. $adminInfo = $this->adminInfo;//获取当前登录的管理员
  198. $this->assign('adminInfo', $adminInfo);
  199. return $this->fetch('admininfo');
  200. }
  201. public function setAdminInfo(Request $request)
  202. {
  203. $adminInfo = $this->adminInfo;//获取当前登录的管理员
  204. if ($request->isPost()) {
  205. $data = parent::postMore([
  206. ['new_pwd', ''],
  207. ['new_pwd_ok', ''],
  208. ['pwd', ''],
  209. 'real_name',
  210. ], $request);
  211. if ($data['pwd'] != '') {
  212. $pwd = md5($data['pwd']);
  213. if ($adminInfo['pwd'] != $pwd) return Json::fail('原始密码错误');
  214. }
  215. if ($data['new_pwd'] != '') {
  216. if (!$data['new_pwd_ok']) return Json::fail('请输入确认新密码');
  217. if ($data['new_pwd'] != $data['new_pwd_ok']) return Json::fail('俩次密码不一样');
  218. }
  219. if ($data['pwd'] != '' && $data['new_pwd'] != '') {
  220. $data['pwd'] = md5($data['new_pwd']);
  221. } else {
  222. unset($data['pwd']);
  223. }
  224. unset($data['new_pwd']);
  225. unset($data['new_pwd_ok']);
  226. AdminModel::edit($data, $adminInfo['id']);
  227. return Json::successful('修改成功!,请重新登录');
  228. }
  229. }
  230. }