Merchant.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2024 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\coupon\StoreCouponRepository;
  13. use app\common\repositories\store\product\ProductCopyRepository;
  14. use app\common\repositories\store\service\StoreServiceRepository;
  15. use app\common\repositories\system\operate\OperateLogRepository;
  16. use crmeb\basic\BaseController;
  17. use app\common\repositories\system\merchant\MerchantAdminRepository;
  18. use app\common\repositories\system\merchant\MerchantCategoryRepository;
  19. use app\common\repositories\system\merchant\MerchantRepository;
  20. use app\validate\admin\MerchantValidate;
  21. use crmeb\jobs\ChangeMerchantStatusJob;
  22. use FormBuilder\Exception\FormBuilderException;
  23. use think\App;
  24. use think\db\exception\DataNotFoundException;
  25. use think\db\exception\DbException;
  26. use think\db\exception\ModelNotFoundException;
  27. use think\facade\Queue;
  28. /**
  29. * 商户
  30. */
  31. class Merchant extends BaseController
  32. {
  33. /**
  34. * @var MerchantRepository
  35. */
  36. protected $repository;
  37. /**
  38. * Merchant constructor.
  39. * @param App $app
  40. * @param MerchantRepository $repository
  41. */
  42. public function __construct(App $app, MerchantRepository $repository)
  43. {
  44. parent::__construct($app);
  45. $this->repository = $repository;
  46. }
  47. /**
  48. * 商户列表头部统计
  49. * @return \think\response\Json
  50. * @return \think\response\Json
  51. * @author Qinii
  52. */
  53. public function count()
  54. {
  55. $where = $this->request->params(['keyword', 'date', 'status', 'statusTag', 'is_trader', 'category_id', 'type_id']);
  56. return app('json')->success($this->repository->count($where));
  57. }
  58. /**
  59. * 列表
  60. * @author xaboy
  61. * @day 2020-04-16
  62. */
  63. public function lst()
  64. {
  65. [$page, $limit] = $this->getPage();
  66. $where = $this->request->params(['keyword', 'date', 'status', 'statusTag', 'is_trader', 'category_id', 'type_id', ['order', 'create_time'], 'is_best','offline_switch','region_id']);
  67. return app('json')->success($this->repository->lst($where, $page, $limit));
  68. }
  69. /**
  70. * 统计表单 - 弃用
  71. * @return mixed
  72. * @throws FormBuilderException
  73. * @author xaboy
  74. * @day 2020-04-16
  75. */
  76. public function createForm()
  77. {
  78. return app('json')->success(formToData($this->repository->form()));
  79. }
  80. /**
  81. * 创建
  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. $data['admin_info'] = $this->request->adminInfo();
  90. $this->repository->createMerchant($data);
  91. return app('json')->success('添加成功');
  92. }
  93. /**
  94. * 编辑表单
  95. * @param int $id
  96. * @author xaboy
  97. * @day 2020-04-16
  98. */
  99. public function updateForm($id)
  100. {
  101. if (!$this->repository->exists($id))
  102. return app('json')->fail('数据不存在');
  103. return app('json')->success(formToData($this->repository->updateForm($id)));
  104. }
  105. /**
  106. * 编辑
  107. * @param int $id
  108. * @return mixed
  109. * @throws DbException
  110. * @author xaboy
  111. * @day 2020-05-06
  112. */
  113. public function update($id, MerchantValidate $validate, MerchantCategoryRepository $merchantCategoryRepository)
  114. {
  115. $data = $this->checkParam($validate, true);
  116. if (!$merchant = $this->repository->get($id))
  117. return app('json')->fail('数据不存在');
  118. if ($this->repository->fieldExists('mer_name', $data['mer_name'], $id))
  119. return app('json')->fail('商户名已存在');
  120. if ($data['mer_phone'] && isPhone($data['mer_phone']))
  121. return app('json')->fail('请输入正确的手机号');
  122. if (!$data['category_id'] || !$merchantCategoryRepository->exists($data['category_id']))
  123. return app('json')->fail('商户分类不存在');
  124. unset($data['mer_account'], $data['mer_password']);
  125. $margin = $this->repository->checkMargin($id, $data['type_id']);
  126. $data['margin'] = $margin['margin'];
  127. $data['is_margin'] = $margin['is_margin'];
  128. $data['ot_margin'] = $margin['ot_margin'];
  129. // 商户编辑记录日志
  130. event('create_operate_log', [
  131. 'category' => OperateLogRepository::PLATFORM_EDIT_MERCHANT,
  132. 'data' => [
  133. 'merchant' => $merchant,
  134. 'admin_info' => $this->request->adminInfo(),
  135. 'update_infos' => $data
  136. ],
  137. ]);
  138. $this->repository->update($id, $data);
  139. return app('json')->success('编辑成功');
  140. }
  141. /**
  142. * 删除表单
  143. * @param $id
  144. * @return \think\response\Json
  145. * @author Qinii
  146. * @day 2023/5/9
  147. */
  148. public function deleteForm($id)
  149. {
  150. return app('json')->success(formToData($this->repository->deleteForm($id)));
  151. }
  152. /**
  153. * 删除
  154. * @param int $id
  155. * @return mixed
  156. * @throws DbException
  157. * @author xaboy
  158. * @day 2020-04-17
  159. */
  160. public function delete($id)
  161. {
  162. $type = $this->request->param('type', 0);
  163. if (!$merchant = $this->repository->get(intval($id)))
  164. return app('json')->fail('数据不存在');
  165. if ($merchant->status)
  166. return app('json')->fail('请先关闭该商户');
  167. $this->repository->delete($id);
  168. if ($type) $this->repository->clearAttachment($id);
  169. return app('json')->success('删除成功');
  170. }
  171. /**
  172. * @param MerchantValidate $validate
  173. * @param bool $isUpdate
  174. * @return array
  175. * @author xaboy
  176. * @day 2020-04-17
  177. */
  178. public function checkParam(MerchantValidate $validate, $isUpdate = false)
  179. {
  180. $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', ['commission_switch', 0],['offline_switch',0],['region_id', 0]]);
  181. if (!$isUpdate) {
  182. $data += $this->request->params(['mer_account', 'mer_password']);
  183. } else {
  184. $validate->isUpdate();
  185. }
  186. $validate->check($data);
  187. return $data;
  188. }
  189. /**
  190. * 修改商户状态
  191. * @param int $id
  192. * @return mixed
  193. * @throws DbException
  194. * @author xaboy
  195. * @day 2020-03-31
  196. */
  197. public function switchStatus($id)
  198. {
  199. $is_best = $this->request->param('status', 0) == 1 ? 1 : 0;
  200. if (!$this->repository->exists($id))
  201. return app('json')->fail('数据不存在');
  202. $this->repository->update($id, compact('is_best'));
  203. return app('json')->success('修改成功');
  204. }
  205. /**
  206. * 关闭商户
  207. * @param int $id
  208. * @return mixed
  209. * @throws DbException
  210. * @author xaboy
  211. * @day 2020-03-31
  212. */
  213. public function switchClose($id)
  214. {
  215. $status = $this->request->param('status', 0) == 1 ? 1 : 0;
  216. if (!$merchant = $this->repository->get($id))
  217. return app('json')->fail('数据不存在');
  218. $this->repository->update($id, compact('status'));
  219. app()->make(StoreCouponRepository::class)->getSearch([])->where('mer_id', $id)->update(['status' => $status]);
  220. app()->make(StoreServiceRepository::class)->close($id, 'mer_id');
  221. Queue::push(ChangeMerchantStatusJob::class, $id);
  222. // 商户编辑记录日志
  223. event('create_operate_log', [
  224. 'category' => OperateLogRepository::PLATFORM_EDIT_MERCHANT_AUDIT_STATUS,
  225. 'data' => [
  226. 'merchant' => $merchant,
  227. 'admin_info' => $this->request->adminInfo(),
  228. 'update_infos' => ['status' => $status]
  229. ],
  230. ]);
  231. return app('json')->success('修改成功');
  232. }
  233. /**
  234. * 从平台登陆到商户后台
  235. * @param $id
  236. * @author xaboy
  237. * @day 2020/7/7
  238. */
  239. public function login($id, MerchantAdminRepository $adminRepository)
  240. {
  241. if (!$this->repository->exists($id))
  242. return app('json')->fail('数据不存在');
  243. $adminInfo = $adminRepository->merIdByAdmin($id);
  244. $tokenInfo = $adminRepository->createToken($adminInfo);
  245. $admin = $adminInfo->toArray();
  246. unset($admin['pwd']);
  247. $data = [
  248. 'token' => $tokenInfo['token'],
  249. 'exp' => $tokenInfo['out'],
  250. 'admin' => $admin,
  251. 'url' => '/' . config('admin.merchant_prefix')
  252. ];
  253. return app('json')->success($data);
  254. }
  255. /**
  256. * 修改复制次数表单
  257. * @param $id
  258. * @return mixed
  259. * @author Qinii
  260. * @day 2020-08-06
  261. */
  262. public function changeCopyNumForm($id)
  263. {
  264. return app('json')->success(formToData($this->repository->copyForm($id)));
  265. }
  266. /**
  267. * 修改复制次数
  268. * @param $id
  269. * @return mixed
  270. * @author Qinii
  271. * @day 2020-08-06
  272. */
  273. public function changeCopyNum($id)
  274. {
  275. $data = $this->request->params(['type', 'num']);
  276. $num = $data['num'];
  277. if ($num <= 0) return app('json')->fail('次数必须为正整数');
  278. if ($data['type'] == 2) {
  279. $mer_num = $this->repository->getCopyNum($id);
  280. if (($mer_num - $num) < 0) return app('json')->fail('剩余次数不足');
  281. $num = '-' . $data['num'];
  282. }
  283. $arr = [
  284. 'type' => 'sys',
  285. 'num' => $num,
  286. 'message' => '平台修改「' . $this->request->adminId() . '」',
  287. ];
  288. app()->make(ProductCopyRepository::class)->add($arr, $id);
  289. return app('json')->success('修改成功');
  290. }
  291. /**
  292. * 清理删除的商户内容
  293. * @return \think\response\Json
  294. * @author Qinii
  295. * @day 5/15/21
  296. */
  297. public function clearRedundancy()
  298. {
  299. $this->repository->clearRedundancy();
  300. return app('json')->success('清除完成');
  301. }
  302. /**
  303. * 需补缴保证金商户列表
  304. * @return \think\response\Json
  305. * @author Qinii
  306. * @day 5/15/21
  307. */
  308. public function makeUpMarginLst()
  309. {
  310. [$page, $limit] = $this->getPage();
  311. $where['margin'] = 0;
  312. $data = $this->repository->lst($where, $page, $limit);
  313. return app('json')->success($data);
  314. }
  315. /**
  316. * 平台后台商户详情
  317. * @param $id
  318. * @return \think\response\Json
  319. * @author Qinii
  320. * @day 2023/7/1
  321. */
  322. public function detail($id)
  323. {
  324. $data = $this->repository->adminDetail($id);
  325. return app('json')->success($data);
  326. }
  327. /**
  328. * 商户操作记录
  329. * @param $product_id
  330. * @return \think\response\Json
  331. * @throws DataNotFoundException
  332. * @throws DbException
  333. * @throws ModelNotFoundException
  334. *
  335. * @date 2023/10/19
  336. * @author yyw
  337. */
  338. public function getOperateList($merchant_id)
  339. {
  340. $where = $this->request->params([
  341. ['type', ''],
  342. ['date', '']
  343. ]);
  344. $where['relevance_id'] = $merchant_id;
  345. $where['relevance_type'] = OperateLogRepository::RELEVANCE_MERCHANT;
  346. [$page, $limit] = $this->getPage();
  347. return app('json')->success(app()->make(OperateLogRepository::class)->lst($where, $page, $limit));
  348. }
  349. /**
  350. * 商户下拉筛选功能接口
  351. * @return \think\response\Json
  352. * @author Qinii
  353. * @day 2024/5/15
  354. */
  355. public function mer_select()
  356. {
  357. $keyword = $this->request->param('keyword','');//'mer_id,mer_name'
  358. $data = $this->repository->mer_select($keyword);
  359. return app('json')->success($data);
  360. }
  361. /**
  362. * 商户虚拟关注量表单
  363. * @param $id
  364. * @return \think\response\Json
  365. * @author Qinii
  366. */
  367. public function careFictiForm($id)
  368. {
  369. $form = $this->repository->careFictiForm($id);
  370. return app('json')->success(formToData($form));
  371. }
  372. /**
  373. * 商户虚拟关注量
  374. * @param $id
  375. * @return \think\response\Json
  376. * @author Qinii
  377. */
  378. public function careFicti($id)
  379. {
  380. $data = $this->request->params(['type','num']);
  381. $this->repository->careFicti($id,$data);
  382. return app('json')->success('修改成功');
  383. }
  384. }