MerchantAdmin.php 8.1 KB

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