SystemAdmin.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\adminapi\controller\v1\setting;
  12. use app\adminapi\controller\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\adminapi\controller\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. return app('json')->success($this->services->getAdminList($where));
  47. }
  48. /**
  49. * 创建表单
  50. * @return mixed
  51. * @throws \FormBuilder\Exception\FormBuilderException
  52. */
  53. public function create()
  54. {
  55. return app('json')->success($this->services->createForm($this->adminInfo['level'] + 1));
  56. }
  57. /**
  58. * 保存管理员
  59. * @return mixed
  60. */
  61. public function save()
  62. {
  63. $data = $this->request->postMore([
  64. ['account', ''],
  65. ['conf_pwd', ''],
  66. ['pwd', ''],
  67. ['real_name', ''],
  68. ['roles', []],
  69. ['status', 0],
  70. ]);
  71. $this->validate($data, \app\adminapi\validate\setting\SystemAdminValidata::class);
  72. $data['level'] = $this->adminInfo['level'] + 1;
  73. $this->services->create($data);
  74. return app('json')->success(100000);
  75. }
  76. /**
  77. * 显示编辑资源表单页.
  78. *
  79. * @param int $id
  80. * @return \think\Response
  81. */
  82. public function edit($id)
  83. {
  84. if (!$id) {
  85. return app('json')->fail(400182);
  86. }
  87. return app('json')->success($this->services->updateForm($this->adminInfo['level'] + 1, (int)$id));
  88. }
  89. /**
  90. * 修改管理员信息
  91. * @param $id
  92. * @return mixed
  93. */
  94. public function update($id)
  95. {
  96. $data = $this->request->postMore([
  97. ['account', ''],
  98. ['conf_pwd', ''],
  99. ['pwd', ''],
  100. ['real_name', ''],
  101. ['roles', []],
  102. ['status', 0],
  103. ]);
  104. $this->validate($data, \app\adminapi\validate\setting\SystemAdminValidata::class, 'update');
  105. if ($this->services->save((int)$id, $data)) {
  106. return app('json')->success(100001);
  107. } else {
  108. return app('json')->fail(100007);
  109. }
  110. }
  111. /**
  112. * 删除管理员
  113. * @param $id
  114. * @return mixed
  115. */
  116. public function delete($id)
  117. {
  118. if (!$id) return app('json')->fail(100100);
  119. if ($this->services->update((int)$id, ['is_del' => 1, 'status' => 0]))
  120. return app('json')->success(100002);
  121. else
  122. return app('json')->fail(100008);
  123. }
  124. /**
  125. * 修改状态
  126. * @param $id
  127. * @param $status
  128. * @return mixed
  129. */
  130. public function set_status($id, $status)
  131. {
  132. $this->services->update((int)$id, ['status' => $status]);
  133. return app('json')->success(100014);
  134. }
  135. /**
  136. * 获取当前登陆管理员的信息
  137. * @return mixed
  138. */
  139. public function info()
  140. {
  141. return app('json')->success($this->adminInfo);
  142. }
  143. /**
  144. * 修改当前登陆admin信息
  145. * @return mixed
  146. */
  147. public function update_admin()
  148. {
  149. $data = $this->request->postMore([
  150. ['real_name', ''],
  151. ['head_pic', ''],
  152. ['pwd', ''],
  153. ['new_pwd', ''],
  154. ['conf_pwd', ''],
  155. ]);
  156. if ($data['pwd']) {
  157. if (!preg_match('/^(?![^a-zA-Z]+$)(?!\D+$).{6,}$/', $data['new_pwd'])) {
  158. return app('json')->fail(400183);
  159. }
  160. }
  161. if ($this->services->updateAdmin($this->adminId, $data))
  162. return app('json')->success(100001);
  163. else
  164. return app('json')->fail(100007);
  165. }
  166. /**
  167. * 修改当前登陆admin的文件管理密码
  168. * @return mixed
  169. */
  170. public function set_file_password()
  171. {
  172. $data = $this->request->postMore([
  173. ['file_pwd', ''],
  174. ['conf_file_pwd', ''],
  175. ]);
  176. if (!preg_match('/^(?![^a-zA-Z]+$)(?!\D+$).{6,}$/', $data['file_pwd'])) {
  177. return app('json')->fail(400183);
  178. }
  179. if ($this->services->setFilePassword($this->adminId, $data))
  180. return app('json')->success(100001);
  181. else
  182. return app('json')->fail(100007);
  183. }
  184. /**
  185. * 退出登陆
  186. * @return mixed
  187. */
  188. public function logout()
  189. {
  190. $key = trim(ltrim($this->request->header(Config::get('cookie.token_name')), 'Bearer'));
  191. CacheService::delete(md5($key));
  192. return app('json')->success();
  193. }
  194. }