Admin.php 7.0 KB

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