SystemAdmin.php 10 KB

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