SystemAdmin.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. <?php
  2. namespace app\adminapi\controller\v1\setting;
  3. use app\adminapi\controller\AuthController;
  4. use crmeb\services\CacheService;
  5. use crmeb\services\FormBuilder as Form;
  6. use crmeb\services\UtilService;
  7. use Exception;
  8. use FormBuilder\exception\FormBuilderException;
  9. use Psr\SimpleCache\InvalidArgumentException;
  10. use think\db\exception\DataNotFoundException;
  11. use think\db\exception\DbException;
  12. use think\db\exception\ModelNotFoundException;
  13. use think\facade\Config;
  14. use think\facade\Route as Url;
  15. use app\models\system\SystemRole;
  16. use app\models\system\SystemAdmin as SystemAdminModel;
  17. use think\Request;
  18. use think\Response;
  19. class SystemAdmin extends AuthController
  20. {
  21. /**
  22. * 显示管理员资源列表
  23. *
  24. * @return Response
  25. * @throws Exception
  26. */
  27. public function index()
  28. {
  29. [$name, $roles, $page, $limit] = UtilService::getMore([
  30. ['name', ''],
  31. ['roles', ''],
  32. ['page', 1],
  33. ['limit', 10],
  34. ], $this->request, true);
  35. return $this->success(SystemAdminModel::getAdminList($name, bcadd($this->adminInfo['level'], 1, 0), $roles, $page, $limit, $this->merId));
  36. }
  37. /**
  38. * 创建表单
  39. * @return mixed
  40. * @throws FormBuilderException
  41. */
  42. public function create()
  43. {
  44. $f[] = Form::input('account', '管理员账号')->required('请填写管理员账号');
  45. $f[] = Form::input('pwd', '管理员密码')->type('password')->required('请填写管理员密码');
  46. $f[] = Form::input('conf_pwd', '确认密码')->type('password')->required('请输入确认密码');
  47. $f[] = Form::input('real_name', '管理员姓名')->required('请输入管理员姓名');
  48. $list = SystemRole::getRole(bcadd($this->adminInfo['level'], 1, 0));
  49. $options = [];
  50. foreach ($list as $id => $roleName) {
  51. $options[] = ['label' => $roleName, 'value' => $id];
  52. }
  53. $f[] = Form::select('roles', '管理员身份')->setOptions($options)->multiple(true)->required('请选择管理员身份');
  54. $f[] = Form::radio('status', '状态', 1)->options([['label' => '开启', 'value' => 1], ['label' => '关闭', 'value' => 0]]);
  55. return $this->makePostForm('管理员添加', $f, Url::buildUrl('/setting/admin')->suffix(false));
  56. }
  57. /**
  58. * 保存管理员
  59. * @param Request $request
  60. * @return mixed
  61. * @throws Exception
  62. */
  63. public function save(Request $request)
  64. {
  65. $data = UtilService::postMore([
  66. ['account', ''],
  67. ['conf_pwd', ''],
  68. ['pwd', ''],
  69. ['real_name', ''],
  70. ['roles', []],
  71. ['status', 0],
  72. ], $request);
  73. $this->validate($data, \app\adminapi\validates\setting\SystemAdminValidata::class);
  74. if ($data['conf_pwd'] != $data['pwd']) return $this->fail('两次输入的密码不相同');
  75. unset($data['conf_pwd']);
  76. if (SystemAdminModel::be(['account' => $data['account']])) return $this->fail('管理员账号已存在');
  77. $data['pwd'] = password_hash($data['pwd'], PASSWORD_BCRYPT);
  78. $data['add_time'] = time();
  79. $data['level'] = $this->adminInfo['level'] + 1;
  80. $data['mer_id'] = $this->merId;
  81. $data['roles'] = implode(',', $data['roles']);
  82. if (SystemAdminModel::create($data))
  83. return $this->success('添加成功');
  84. else
  85. return $this->fail('添加失败');
  86. }
  87. /**
  88. * 显示编辑资源表单页.
  89. *
  90. * @param int $id
  91. * @return Response
  92. * @throws FormBuilderException
  93. * @throws DataNotFoundException
  94. * @throws DbException
  95. * @throws ModelNotFoundException
  96. */
  97. public function edit($id)
  98. {
  99. if (!$id || !($adminInfo = SystemAdminModel::where("mer_id", 'in', [0, $this->merId])->where('id', $id)->find()))
  100. return $this->fail('管理员信息读取失败');
  101. $f[] = Form::input('account', '管理员账号', $adminInfo->getData('account'))->required('请填写管理员账号');
  102. $f[] = Form::input('pwd', '管理员密码')->type('password')->placeholder('请填写管理员密码');
  103. $f[] = Form::input('conf_pwd', '确认密码')->type('password')->placeholder('请输入确认密码');
  104. $f[] = Form::input('real_name', '管理员姓名', $adminInfo->getData('real_name'))->required('请输入管理员姓名');
  105. $list = SystemRole::getRole(bcadd($this->adminInfo['level'], 1, 0));
  106. $options = [];
  107. foreach ($list as $k => $roleName) {
  108. $options[] = ['label' => $roleName, 'value' => $k];
  109. }
  110. $f[] = Form::select('roles', '管理员身份', $adminInfo->roles)->setOptions($options)->multiple(true)->required('请选择管理员身份');
  111. $f[] = Form::radio('status', '状态', $adminInfo->getData('status'))->options([['label' => '开启', 'value' => 1], ['label' => '关闭', 'value' => 0]]);
  112. return $this->makePostForm('管理员修改', $f, Url::buildUrl('/setting/admin/' . $id)->suffix(false), 'PUT');
  113. }
  114. /**
  115. * 修改管理员信息
  116. * @param Request $request
  117. * @param $id
  118. * @return mixed
  119. * @throws Exception
  120. */
  121. public function update(Request $request, $id)
  122. {
  123. $data = UtilService::postMore([
  124. ['account', ''],
  125. ['conf_pwd', ''],
  126. ['pwd', ''],
  127. ['real_name', ''],
  128. ['roles', []],
  129. ['status', 0],
  130. ], $request);
  131. $this->validate($data, \app\adminapi\validates\setting\SystemAdminValidata::class, 'update');
  132. if (!$adminInfo = SystemAdminModel::where("mer_id", 'in', [0, $this->merId])->where('id', $id)->find())
  133. return $this->fail('管理员不存在,无法修改');
  134. if ($data['pwd']) {
  135. if (!$data['conf_pwd'])
  136. return $this->fail('请输入确认密码');
  137. if ($data['conf_pwd'] != $data['pwd'])
  138. return $this->fail('上次输入的密码不相同');
  139. $adminInfo->pwd = password_hash($data['pwd'], PASSWORD_BCRYPT);
  140. }
  141. if (SystemAdminModel::where(['account' => $data['account']])->where('id', '<>', $id)->count())
  142. return $this->fail('管理员账号已存在');
  143. $adminInfo->roles = implode(',', $data['roles']);
  144. $adminInfo->real_name = $data['real_name'];
  145. $adminInfo->account = $data['account'];
  146. $adminInfo->status = $data['status'];
  147. if ($adminInfo->save())
  148. return $this->success('修改成功');
  149. else
  150. return $this->fail('修改失败');
  151. }
  152. /**
  153. * 删除管理员
  154. * @param $id
  155. * @return mixed
  156. * @throws DataNotFoundException
  157. * @throws DbException
  158. * @throws ModelNotFoundException
  159. */
  160. public function delete($id)
  161. {
  162. if (!$id) return $this->fail('删除失败,缺少参数');
  163. if (!SystemAdminModel::where("mer_id", 'in', [0, $this->merId])->where('id', $id)->find()) {
  164. if (!$id) return $this->fail('删除失败');
  165. }
  166. if (SystemAdminModel::edit(['is_del' => 1, 'status' => 0], $id, 'id'))
  167. return $this->success('删除成功!');
  168. else
  169. return $this->fail('删除失败');
  170. }
  171. /**
  172. * 修改状态
  173. * @param $id
  174. * @param $status
  175. * @return mixed
  176. * @throws DataNotFoundException
  177. * @throws DbException
  178. * @throws ModelNotFoundException
  179. */
  180. public function set_status($id, $status)
  181. {
  182. if (!$id) return $this->fail('修改失败,缺少参数');
  183. if (!SystemAdminModel::where("mer_id", 'in', [0, $this->merId])->where('id', $id)->find()) {
  184. if (!$id) return $this->fail('修改失败');
  185. }
  186. SystemAdminModel::where(['id' => $id])->update(['status' => $status]);
  187. return $this->success($status == 0 ? '关闭成功' : '开启成功');
  188. }
  189. /**
  190. * 获取当前登陆管理员的信息
  191. * @return mixed
  192. * @throws DataNotFoundException
  193. * @throws ModelNotFoundException
  194. * @throws DbException
  195. * @throws DbException
  196. */
  197. public function info()
  198. {
  199. return $this->success(SystemAdminModel::where(['id' => $this->adminId])->find()->hidden(['pwd', 'is_del', 'status'])->toArray());
  200. }
  201. /**
  202. * 修改当前登陆admin信息
  203. * @return mixed
  204. * @throws Exception
  205. */
  206. public function update_admin()
  207. {
  208. $data = UtilService::postMore([
  209. ['real_name', ''],
  210. ['head_pic', ''],
  211. ['pwd', ''],
  212. ['new_pwd', ''],
  213. ['conf_pwd', ''],
  214. ], $this->request);
  215. $adminInfo = SystemAdminModel::get($this->adminId);
  216. if (!$adminInfo)
  217. return $this->fail('管理员信息未查到');
  218. if (!$data['real_name'])
  219. return $this->fail('管理员姓名不能为空');
  220. if ($data['pwd']) {
  221. if (!password_verify($data['pwd'], $this->adminInfo['pwd']))
  222. return $this->fail('原始密码错误');
  223. if (!$data['new_pwd'])
  224. return $this->fail('请输入新密码');
  225. if (!$data['conf_pwd'])
  226. return $this->fail('请输入确认密码');
  227. if ($data['new_pwd'] != $data['conf_pwd'])
  228. return $this->fail('两次输入的密码不一致');
  229. $adminInfo->pwd = password_hash($data['new_pwd'], PASSWORD_BCRYPT);
  230. }
  231. $adminInfo->real_name = $data['real_name'];
  232. $adminInfo->head_pic = $data['head_pic'];
  233. if ($adminInfo->save())
  234. return $this->success('修改成功');
  235. else
  236. return $this->fail('修改失败');
  237. }
  238. /**
  239. * 退出登陆
  240. * @return mixed
  241. * @throws InvalidArgumentException
  242. */
  243. public function logout()
  244. {
  245. $key = trim(ltrim($this->request->header(Config::get('cookie.token_name')), 'Bearer'));
  246. $res = CacheService::redisHandler()->delete($key);
  247. return $this->success();
  248. }
  249. }