MerchantAdmin.php 7.5 KB

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