Merchant.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. <?php
  2. namespace app\controller\admin\system\merchant;
  3. use app\common\repositories\store\product\ProductCopyRepository;
  4. use ln\basic\BaseController;
  5. use app\common\repositories\system\merchant\MerchantAdminRepository;
  6. use app\common\repositories\system\merchant\MerchantCategoryRepository;
  7. use app\common\repositories\system\merchant\MerchantRepository;
  8. use app\validate\admin\MerchantValidate;
  9. use ln\jobs\ChangeMerchantStatusJob;
  10. use FormBuilder\Exception\FormBuilderException;
  11. use think\App;
  12. use think\db\exception\DataNotFoundException;
  13. use think\db\exception\DbException;
  14. use think\db\exception\ModelNotFoundException;
  15. use think\facade\Queue;
  16. /**
  17. * Class Merchant
  18. * @package app\controller\admin\system\merchant
  19. * @author xaboy
  20. * @day 2020-04-16
  21. */
  22. class Merchant extends BaseController
  23. {
  24. /**
  25. * @var MerchantRepository
  26. */
  27. protected $repository;
  28. /**
  29. * Merchant constructor.
  30. * @param App $app
  31. * @param MerchantRepository $repository
  32. */
  33. public function __construct(App $app, MerchantRepository $repository)
  34. {
  35. parent::__construct($app);
  36. $this->repository = $repository;
  37. }
  38. public function count()
  39. {
  40. return app('json')->success($this->repository->count());
  41. }
  42. /**
  43. * @return mixed
  44. * @throws DataNotFoundException
  45. * @throws DbException
  46. * @throws ModelNotFoundException
  47. * @author xaboy
  48. * @day 2020-04-16
  49. */
  50. public function lst()
  51. {
  52. [$page, $limit] = $this->getPage();
  53. $where = $this->request->params(['keyword', 'date', 'status', 'statusTag', 'is_trader', 'category_id', 'type_id']);
  54. return app('json')->success($this->repository->lst($where, $page, $limit));
  55. }
  56. /**
  57. * @return mixed
  58. * @throws FormBuilderException
  59. * @author xaboy
  60. * @day 2020-04-16
  61. */
  62. public function createForm()
  63. {
  64. return app('json')->success(formToData($this->repository->form()));
  65. }
  66. /**
  67. * @param MerchantValidate $validate
  68. * @param MerchantCategoryRepository $merchantCategoryRepository
  69. * @param MerchantAdminRepository $adminRepository
  70. * @return mixed
  71. * @author xaboy
  72. * @day 2020/7/2
  73. */
  74. public function create(MerchantValidate $validate)
  75. {
  76. $data = $this->checkParam($validate);
  77. $this->repository->createMerchant($data);
  78. return app('json')->success('添加成功');
  79. }
  80. /**
  81. * @param int $id
  82. * @return mixed
  83. * @throws FormBuilderException
  84. * @throws DataNotFoundException
  85. * @throws DbException
  86. * @throws ModelNotFoundException
  87. * @author xaboy
  88. * @day 2020-04-16
  89. */
  90. public function updateForm($id)
  91. {
  92. if (!$this->repository->exists($id))
  93. return app('json')->fail('数据不存在');
  94. return app('json')->success(formToData($this->repository->updateForm($id)));
  95. }
  96. /**
  97. * @param int $id
  98. * @param MerchantValidate $validate
  99. * @param MerchantCategoryRepository $merchantCategoryRepository
  100. * @return mixed
  101. * @throws DbException
  102. * @author xaboy
  103. * @day 2020-05-06
  104. */
  105. public function update($id, MerchantValidate $validate, MerchantCategoryRepository $merchantCategoryRepository)
  106. {
  107. $data = $this->checkParam($validate, true);
  108. if (!$this->repository->exists($id))
  109. return app('json')->fail('数据不存在');
  110. if ($this->repository->fieldExists('mer_name', $data['mer_name'], $id))
  111. return app('json')->fail('商户名已存在');
  112. if ($data['mer_phone'] && isPhone($data['mer_phone']))
  113. return app('json')->fail('请输入正确的手机号');
  114. if (!$data['category_id'] || !$merchantCategoryRepository->exists($data['category_id']))
  115. return app('json')->fail('商户分类不存在');
  116. unset($data['mer_account'], $data['mer_password']);
  117. $this->repository->update($id, $data);
  118. return app('json')->success('编辑成功');
  119. }
  120. /**
  121. * @param int $id
  122. * @return mixed
  123. * @throws DbException
  124. * @author xaboy
  125. * @day 2020-04-17
  126. */
  127. public function delete($id)
  128. {
  129. if (!$merchant = $this->repository->get(intval($id)))
  130. return app('json')->fail('数据不存在');
  131. if ($merchant->status)
  132. return app('json')->fail('请先关闭该商户');
  133. $this->repository->delete($id);
  134. return app('json')->success('编辑成功');
  135. }
  136. /**
  137. * @param MerchantValidate $validate
  138. * @param bool $isUpdate
  139. * @return array
  140. * @author xaboy
  141. * @day 2020-04-17
  142. */
  143. public function checkParam(MerchantValidate $validate, $isUpdate = false)
  144. {
  145. $data = $this->request->params([['category_id', 0], ['type_id', 0], 'mer_name', 'commission_rate', 'real_name', 'mer_phone', 'mer_keyword', 'mer_address', 'mark', ['sort', 0], ['status', 0], ['is_audit', 0], ['is_best', 0], ['is_bro_goods', 0], ['is_bro_room', 0], ['is_trader', 0]]);
  146. if (!$isUpdate)
  147. $data += $this->request->params(['mer_account', 'mer_password']);
  148. else {
  149. $validate->isUpdate();
  150. unset($data['status']);
  151. }
  152. $validate->check($data);
  153. return $data;
  154. }
  155. /**
  156. * @param int $id
  157. * @return mixed
  158. * @throws DbException
  159. * @author xaboy
  160. * @day 2020-03-31
  161. */
  162. public function switchStatus($id)
  163. {
  164. $is_best = $this->request->param('status', 0) == 1 ? 1 : 0;
  165. if (!$this->repository->exists($id))
  166. return app('json')->fail('数据不存在');
  167. $this->repository->update($id, compact('is_best'));
  168. return app('json')->success('修改成功');
  169. }
  170. /**
  171. * @param int $id
  172. * @return mixed
  173. * @throws DbException
  174. * @author xaboy
  175. * @day 2020-03-31
  176. */
  177. public function switchClose($id)
  178. {
  179. $status = $this->request->param('status', 0) == 1 ? 1 : 0;
  180. if (!$this->repository->exists($id))
  181. return app('json')->fail('数据不存在');
  182. $this->repository->update($id, compact('status'));
  183. Queue::push(ChangeMerchantStatusJob::class, $id);
  184. return app('json')->success('修改成功');
  185. }
  186. /**
  187. * @param $id
  188. * @param MerchantAdminRepository $adminRepository
  189. * @return mixed
  190. * @throws DataNotFoundException
  191. * @throws DbException
  192. * @throws ModelNotFoundException
  193. * @author xaboy
  194. * @day 2020/7/7
  195. */
  196. public function login($id, MerchantAdminRepository $adminRepository)
  197. {
  198. if (!$this->repository->exists($id))
  199. return app('json')->fail('数据不存在');
  200. $adminInfo = $adminRepository->merIdByAdmin($id);
  201. $tokenInfo = $adminRepository->createToken($adminInfo);
  202. $admin = $adminInfo->toArray();
  203. unset($admin['pwd']);
  204. $data = [
  205. 'token' => $tokenInfo['token'],
  206. 'exp' => $tokenInfo['out'],
  207. 'admin' => $admin,
  208. 'url' => '/' . config('admin.merchant_prefix')
  209. ];
  210. return app('json')->success($data);
  211. }
  212. /**
  213. * TODO 修改复制次数表单
  214. * @param $id
  215. * @return mixed
  216. * @author Qinii
  217. * @day 2020-08-06
  218. */
  219. public function changeCopyNumForm($id)
  220. {
  221. return app('json')->success(formToData($this->repository->copyForm($id)));
  222. }
  223. /**
  224. * TODO 修改复制次数
  225. * @param $id
  226. * @return mixed
  227. * @author Qinii
  228. * @day 2020-08-06
  229. */
  230. public function changeCopyNum($id)
  231. {
  232. $data = $this->request->params(['type', 'num']);
  233. $num = $data['num'];
  234. if ($num <= 0) return app('json')->fail('次数必须为正整数');
  235. if ($data['type'] == 2) {
  236. $mer_num = $this->repository->getCopyNum($id);
  237. if (($mer_num - $num) < 0) return app('json')->fail('剩余次数不足');
  238. $num = '-' . $data['num'];
  239. }
  240. $arr = [
  241. 'type' => 'sys',
  242. 'num' => $num,
  243. 'message' => '平台修改「' . $this->request->adminId() . '」',
  244. ];
  245. app()->make(ProductCopyRepository::class)->add($arr, $id);
  246. return app('json')->success('修改成功');
  247. }
  248. /**
  249. * TODO 清理删除的商户内容
  250. * @return \think\response\Json
  251. * @author Qinii
  252. * @day 5/15/21
  253. */
  254. public function clearRedundancy()
  255. {
  256. $this->repository->clearRedundancy();
  257. return app('json')->success('清除完成');
  258. }
  259. }