MerchantAdmin.php 7.6 KB

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