SystemAdmin.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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\controller\admin\v1\system\admin;
  12. use app\controller\admin\AuthController;
  13. use app\services\system\admin\SystemAdminServices;
  14. use crmeb\services\CacheService;
  15. use think\facade\{App, Config};
  16. /**
  17. * Class SystemAdmin
  18. * @package app\controller\admin\v1\setting
  19. */
  20. class SystemAdmin extends AuthController
  21. {
  22. /**
  23. * SystemAdmin constructor.
  24. * @param App $app
  25. * @param SystemAdminServices $services
  26. */
  27. public function __construct(App $app, SystemAdminServices $services)
  28. {
  29. parent::__construct($app);
  30. $this->services = $services;
  31. }
  32. /**
  33. * 显示管理员资源列表
  34. *
  35. * @return \think\Response
  36. */
  37. public function index()
  38. {
  39. $where = $this->request->getMore([
  40. ['name', '', '', 'account_like'],
  41. ['roles', ''],
  42. ['is_del', 1],
  43. ['status', '']
  44. ]);
  45. $where['level'] = $this->adminInfo['level'] + 1;
  46. $where['admin_type'] = 1;
  47. return $this->success($this->services->getAdminList($where));
  48. }
  49. /**
  50. * 创建表单
  51. * @return mixed
  52. * @throws \FormBuilder\Exception\FormBuilderException
  53. */
  54. public function create()
  55. {
  56. return $this->success($this->services->createForm($this->adminInfo['level'] + 1));
  57. }
  58. /**
  59. * 保存管理员
  60. * @return mixed
  61. */
  62. public function save()
  63. {
  64. $data = $this->request->postMore([
  65. ['account', ''],
  66. ['conf_pwd', ''],
  67. ['pwd', ''],
  68. ['real_name', ''],
  69. ['phone', ''],
  70. ['roles', []],
  71. ['status', 0],
  72. ]);
  73. $this->validate($data, \app\validate\admin\setting\SystemAdminValidate::class);
  74. $data['level'] = $this->adminInfo['level'] + 1;
  75. $this->services->create($data);
  76. return $this->success('添加成功');
  77. }
  78. /**
  79. * 显示编辑资源表单页.
  80. *
  81. * @param int $id
  82. * @return \think\Response
  83. */
  84. public function edit($id)
  85. {
  86. if (!$id) {
  87. return $this->fail('管理员信息读取失败');
  88. }
  89. return $this->success($this->services->updateForm($this->adminInfo['level'] + 1, (int)$id));
  90. }
  91. /**
  92. * 修改管理员信息
  93. * @param $id
  94. * @return mixed
  95. */
  96. public function update($id)
  97. {
  98. $data = $this->request->postMore([
  99. ['account', ''],
  100. ['conf_pwd', ''],
  101. ['pwd', ''],
  102. ['real_name', ''],
  103. ['phone', ''],
  104. ['roles', []],
  105. ['status', 0],
  106. ]);
  107. $this->validate($data, \app\validate\admin\setting\SystemAdminValidate::class, 'update');
  108. if ($this->services->save((int)$id, $data)) {
  109. return $this->success('修改成功');
  110. } else {
  111. return $this->fail('修改失败');
  112. }
  113. }
  114. /**
  115. * 删除管理员
  116. * @param $id
  117. * @return mixed
  118. */
  119. public function delete($id)
  120. {
  121. if (!$id) return $this->fail('删除失败,缺少参数');
  122. if ($this->services->update((int)$id, ['is_del' => 1, 'status' => 0]))
  123. return $this->success('删除成功!');
  124. else
  125. return $this->fail('删除失败');
  126. }
  127. /**
  128. * 修改状态
  129. * @param $id
  130. * @param $status
  131. * @return mixed
  132. */
  133. public function set_status($id, $status)
  134. {
  135. $this->services->update((int)$id, ['status' => $status]);
  136. return $this->success($status == 0 ? '关闭成功' : '开启成功');
  137. }
  138. /**
  139. * 获取当前登陆管理员的信息
  140. * @return mixed
  141. */
  142. public function info()
  143. {
  144. return $this->success($this->adminInfo);
  145. }
  146. /**
  147. * 修改当前登陆admin信息
  148. * @return mixed
  149. */
  150. public function update_admin()
  151. {
  152. $data = $this->request->postMore([
  153. ['real_name', ''],
  154. ['head_pic', ''],
  155. ['pwd', ''],
  156. ['new_pwd', ''],
  157. ['conf_pwd', ''],
  158. ['phone', ''],
  159. ['code', '']
  160. ]);
  161. if ($this->services->updateAdmin($this->adminId, $data))
  162. return $this->success('修改成功');
  163. else
  164. return $this->fail('修改失败');
  165. }
  166. /**
  167. * 退出登陆
  168. * @return mixed
  169. */
  170. public function logout()
  171. {
  172. $key = trim(ltrim($this->request->header(Config::get('cookie.token_name')), 'Bearer'));
  173. CacheService::redisHandler()->delete(md5($key));
  174. return $this->success();
  175. }
  176. }