Merchant.php 9.4 KB

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