SystemAdmins.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. /**
  3. * @Created by PhpStorm
  4. * @author: Kirin
  5. * @day: 2024/11/21
  6. * @time: 17:39
  7. */
  8. namespace app\controller\admin\system;
  9. use app\common\AdminBaseController;
  10. use app\Request;
  11. use app\services\system\admin\SystemAdminServices;
  12. use app\services\system\admin\SystemRoleServices;
  13. use app\validate\admin\SystemAdminValidate;
  14. use think\exception\ValidateException;
  15. class SystemAdmins extends AdminBaseController
  16. {
  17. public function __construct(Request $request, SystemAdminServices $service)
  18. {
  19. parent::__construct($request);
  20. $this->service = $service;
  21. $this->request->filter(['addslashes', 'trim']);
  22. $this->validate = new SystemAdminValidate();
  23. $this->createParams = [
  24. ['account', ''],
  25. ['conf_pwd', ''],
  26. ['pwd', ''],
  27. ['real_name', ''],
  28. ['phone', ''],
  29. ['roles', []],
  30. ['status', 0]
  31. ];
  32. $this->searchable = [
  33. ['name', '', '', 'account_like'],
  34. ['roles', ''],
  35. ['status', '']
  36. ];
  37. $this->saveDeal = $this->updateDeal = function (&$data) {
  38. $data['level'] = $this->adminInfo['level'] + 1;
  39. };
  40. }
  41. /**
  42. * 显示管理员资源列表
  43. *
  44. * @return \think\Response
  45. */
  46. public function index()
  47. {
  48. $where = $this->request->getMore($this->searchable);
  49. $where['level'] = $this->adminInfo['level'] + 1;
  50. $where['admin_type'] = 1;
  51. return $this->success($this->service->getAdminList($where));
  52. }
  53. /**
  54. * @param $id
  55. * @return mixed
  56. */
  57. public function read($id)
  58. {
  59. $info = $this->service->get($id);
  60. if (!$info)
  61. return $this->error('数据不存在');
  62. if ($info['level'] != $this->adminInfo['level'] + 1)
  63. return $this->error('数据不存在');
  64. /** @var SystemRoleServices $services */
  65. $services = app()->make(SystemRoleServices::class);
  66. $info['roles'] = $services->getRoleArray(['level' => $this->adminInfo['level'] + 1]);
  67. return $this->success('ok', $info->toArray());
  68. }
  69. /**
  70. * 修改状态
  71. * @param $id
  72. * @param $status
  73. * @return mixed
  74. */
  75. public function setStatus($id, $status)
  76. {
  77. $this->service->update((int)$id, ['status' => $status]);
  78. return $this->success($status == 0 ? '关闭成功' : '开启成功');
  79. }
  80. /**
  81. * 修改当前登陆admin信息
  82. * @return mixed
  83. * @throws \Psr\SimpleCache\InvalidArgumentException
  84. * @throws \think\db\exception\DataNotFoundException
  85. * @throws \think\db\exception\DbException
  86. * @throws \think\db\exception\ModelNotFoundException
  87. */
  88. public function update_admin()
  89. {
  90. $data = $this->request->postMore([
  91. ['real_name', ''],
  92. ['head_pic', ''],
  93. ['pwd', ''],
  94. ['new_pwd', ''],
  95. ['conf_pwd', ''],
  96. ['phone', ''],
  97. ['code', '']
  98. ]);
  99. if ($this->service->updateAdmin($this->adminId, $data))
  100. return $this->success('修改成功');
  101. else
  102. return $this->error('修改失败');
  103. }
  104. }