SystemAdmin.php 8.2 KB

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