Admin.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2024 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\system\admin;
  12. use app\common\repositories\system\auth\RoleRepository;
  13. use crmeb\basic\BaseController;
  14. use app\common\repositories\system\admin\AdminRepository;
  15. use app\validate\admin\AdminEditValidate;
  16. use app\validate\admin\AdminValidate;
  17. use Exception;
  18. use FormBuilder\Exception\FormBuilderException;
  19. use think\App;
  20. use think\db\exception\DataNotFoundException;
  21. use think\db\exception\DbException;
  22. use think\db\exception\ModelNotFoundException;
  23. use think\response\Json;
  24. /**
  25. * 后台管理员
  26. */
  27. class Admin extends BaseController
  28. {
  29. protected $repository;
  30. public function __construct(App $app, AdminRepository $repository)
  31. {
  32. parent::__construct($app);
  33. $this->repository = $repository;
  34. }
  35. /**
  36. * 列表
  37. * @return mixed
  38. * @throws DataNotFoundException
  39. * @throws DbException
  40. * @throws ModelNotFoundException
  41. * @author xaboy
  42. * @day 2020-04-09
  43. */
  44. public function getList()
  45. {
  46. $where = $this->request->params(['keyword', 'date', 'status','region_id']);
  47. [$page, $limit] = $this->getPage();
  48. return app('json')->success($this->repository->getList($where, $page, $limit));
  49. }
  50. /**
  51. * 修改状态
  52. * @param int $id
  53. * @return mixed
  54. * @throws DbException
  55. * @author xaboy
  56. * @day 2020-04-09
  57. */
  58. public function switchStatus($id)
  59. {
  60. $status = $this->request->param('status');
  61. if (!$this->repository->exists($id))
  62. return app('json')->fail('数据不存在');
  63. $this->repository->update($id, ['status' => $status == 1 ? 1 : 0]);
  64. return app('json')->success('编辑成功');
  65. }
  66. /**
  67. * 添加表单
  68. * @return mixed
  69. * @throws FormBuilderException
  70. * @author xaboy
  71. * @day 2020-04-09
  72. */
  73. public function createForm()
  74. {
  75. return app('json')->success(formToData($this->repository->form()));
  76. }
  77. /**
  78. * 修改表单
  79. * @param $id
  80. * @return Json
  81. * @author Qinii
  82. */
  83. public function updateForm($id)
  84. {
  85. if (!$this->repository->exists($id))
  86. return app('json')->fail('数据不存在');
  87. return app('json')->success(formToData($this->repository->updateForm($id)));
  88. }
  89. /**
  90. * 修改密码表单
  91. * @param $id
  92. * @return Json
  93. * @author Qinii
  94. */
  95. public function passwordForm($id)
  96. {
  97. if (!$this->repository->exists($id))
  98. return app('json')->fail('数据不存在');
  99. return app('json')->success(formToData($this->repository->passwordForm($id)));
  100. }
  101. /**
  102. * 添加
  103. * @param AdminValidate $validate
  104. * @return Json
  105. * @author Qinii
  106. */
  107. public function create(AdminValidate $validate)
  108. {
  109. $data = $this->request->params(['account', 'pwd', 'phone', 'againPassword', 'real_name', ['roles', []], ['status', 0]]);
  110. $region_ids = $this->request->param('region_ids',[]);
  111. if ($region_ids) {
  112. $data['region_ids'] = ','.implode(',', $region_ids).',';
  113. }
  114. $validate->check($data);
  115. if ($data['pwd'] !== $data['againPassword'])
  116. return app('json')->fail('两次密码输入不一致');
  117. unset($data['againPassword']);
  118. if ($this->repository->fieldExists('account', $data['account']))
  119. return app('json')->fail('账号已存在');
  120. $data['pwd'] = $this->repository->passwordEncode($data['pwd']);
  121. $check = app()->make(RoleRepository::class)->checkRole($data['roles'], 0);
  122. if (!$check) {
  123. return app('json')->fail('未开启或者不存在的身份不能添加');
  124. }
  125. $this->repository->create($data);
  126. return app('json')->success('添加成功');
  127. }
  128. /**
  129. * 编辑
  130. * @param $id
  131. * @param AdminValidate $validate
  132. * @return Json
  133. * @author Qinii
  134. */
  135. public function update($id, AdminValidate $validate)
  136. {
  137. $data = $this->request->params(['account', 'real_name', 'phone', ['roles', []], ['status', 0]]);
  138. $region_ids = $this->request->param('region_ids',[]);
  139. $data['region_ids'] = '';
  140. if ($region_ids) {
  141. $data['region_ids'] = ','.implode(',', $region_ids).',';
  142. }
  143. $validate->isUpdate()->check($data);
  144. if ($this->repository->fieldExists('account', $data['account'], $id))
  145. return app('json')->fail('账号已存在');
  146. $check = app()->make(RoleRepository::class)->checkRole($data['roles'], 0);
  147. if (!$check) {
  148. return app('json')->fail('未开启或者不存在的身份不能添加');
  149. }
  150. $this->repository->update($id, $data);
  151. return app('json')->success('编辑成功');
  152. }
  153. /**
  154. * 修改密码
  155. * @param $id
  156. * @param AdminValidate $validate
  157. * @return Json
  158. * @author Qinii
  159. */
  160. public function password($id, AdminValidate $validate)
  161. {
  162. $data = $this->request->params(['pwd', 'againPassword']);
  163. $validate->isPassword()->check($data);
  164. if ($data['pwd'] !== $data['againPassword'])
  165. return app('json')->fail('两次密码输入不一致');
  166. if (!$this->repository->exists($id))
  167. return app('json')->fail('管理员不存在');
  168. $data['pwd'] = $this->repository->passwordEncode($data['pwd']);
  169. unset($data['againPassword']);
  170. $this->repository->update($id, $data);
  171. return app('json')->success('修改密码成功');
  172. }
  173. /**
  174. * 删除
  175. * @param int $id
  176. * @return Json
  177. * @throws Exception
  178. * @author 张先生
  179. * @date 2020-03-30
  180. */
  181. public function delete($id)
  182. {
  183. if (!$this->repository->exists($id))
  184. return app('json')->fail('数据不存在');
  185. $this->repository->update($id, ['is_del' => 1]);
  186. return app('json')->success('删除成功');
  187. }
  188. /**
  189. * 修改信息
  190. * @param AdminEditValidate $validate
  191. * @return mixed
  192. * @throws DbException
  193. * @author xaboy
  194. * @day 2020-04-20
  195. */
  196. public function edit(AdminEditValidate $validate)
  197. {
  198. $data = $this->request->params(['real_name', 'phone']);
  199. $validate->check($data);
  200. $this->repository->update($this->request->adminId(), $data);
  201. return app('json')->success('修改成功');
  202. }
  203. /**
  204. * 修改表单
  205. * @return mixed
  206. * @throws FormBuilderException
  207. * @author xaboy
  208. * @day 2020-04-20
  209. */
  210. public function editForm()
  211. {
  212. $adminInfo = $this->request->adminInfo();
  213. return app('json')->success(formToData($this->repository->editForm(['real_name' => $adminInfo->real_name, 'phone' => $adminInfo->phone, 'admin_id' => $adminInfo->admin_id])));
  214. }
  215. /**
  216. * 修改自己的密码
  217. * @param AdminValidate $validate
  218. * @return mixed
  219. * @throws DbException
  220. * @author xaboy
  221. * @day 2020-04-20
  222. */
  223. public function editPassword(AdminValidate $validate)
  224. {
  225. $data = $this->request->params(['pwd', 'againPassword']);
  226. $validate->isPassword()->check($data);
  227. if ($data['pwd'] !== $data['againPassword'])
  228. return app('json')->fail('两次密码输入不一致');
  229. $data['pwd'] = $this->repository->passwordEncode($data['pwd']);
  230. unset($data['againPassword']);
  231. $this->repository->update($this->request->adminId(), $data);
  232. return app('json')->success('修改密码成功');
  233. }
  234. /**
  235. * 修改自己的密码表单
  236. * @return mixed
  237. * @throws FormBuilderException
  238. * @author xaboy
  239. * @day 2020-04-20
  240. */
  241. public function editPasswordForm()
  242. {
  243. return app('json')->success(formToData($this->repository->passwordForm($this->request->adminId(), true)));
  244. }
  245. }