StoreProduct.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836
  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\admin\v1\product;
  12. use app\controller\admin\AuthController;
  13. use app\jobs\BatchHandleJob;
  14. use app\jobs\product\ProductSyncErp;
  15. use app\services\other\CacheServices;
  16. use app\services\other\queue\QueueServices;
  17. use app\services\product\category\StoreProductCategoryServices;
  18. use app\services\product\product\StoreProductBatchProcessServices;
  19. use app\services\product\product\StoreProductServices;
  20. use app\services\product\sku\StoreProductAttrServices;
  21. use app\services\product\sku\StoreProductAttrValueServices;
  22. use crmeb\services\FileService;
  23. use crmeb\services\SpreadsheetExcelService;
  24. use crmeb\services\UploadService;
  25. use think\facade\App;
  26. /**
  27. * Class StoreProduct
  28. * @package app\controller\admin\v1\product
  29. */
  30. class StoreProduct extends AuthController
  31. {
  32. protected $service;
  33. public function __construct(App $app, StoreProductServices $service)
  34. {
  35. parent::__construct($app);
  36. $this->service = $service;
  37. }
  38. /**
  39. * 显示资源列表头部
  40. * @return mixed
  41. */
  42. public function type_header(StoreProductCategoryServices $storeProductCategoryServices)
  43. {
  44. $where = $this->request->getMore([
  45. ['store_name', ''],
  46. ['cate_id', ''],
  47. ['supplier_id', ''],
  48. ['type', 1, '', 'status'],
  49. ['store_id', ''],
  50. ['store_label_id', ''],
  51. ['brand_id', '']
  52. ]);
  53. $cateId = $where['cate_id'];
  54. if ($cateId) {
  55. $cateId = is_string($cateId) ? [$cateId] : $cateId;
  56. $cateId = array_merge($cateId, $storeProductCategoryServices->getColumn(['pid' => $cateId], 'id'));
  57. $cateId = array_unique(array_diff($cateId, [0]));
  58. }
  59. $where['cate_id'] = $cateId;
  60. $list = $this->service->getHeader(0, $where);
  61. return $this->success(compact('list'));
  62. }
  63. /**
  64. * 获取退出未保存的数据
  65. * @param CacheServices $services
  66. * @return mixed
  67. */
  68. public function getCacheData(CacheServices $services)
  69. {
  70. return $this->success(['info' => $services->getDbCache($this->adminId . '_product_data', [])]);
  71. }
  72. /**
  73. * 1分钟保存一次产品数据
  74. * @param CacheServices $services
  75. * @return mixed
  76. */
  77. public function saveCacheData(CacheServices $services)
  78. {
  79. $data = $this->request->postMore([
  80. ['product_type', 0],//商品类型
  81. ['supplier_id', 0],//供应商ID
  82. ['cate_id', []],
  83. ['store_name', ''],
  84. ['store_info', ''],
  85. ['keyword', ''],
  86. ['unit_name', '件'],
  87. ['recommend_image', ''],
  88. ['slider_image', []],
  89. ['is_sub', []],//佣金是单独还是默认
  90. ['sort', 0],
  91. ['sales', 0],
  92. ['ficti', 0],
  93. ['give_integral', 0],
  94. ['is_show', 0],
  95. ['is_hot', 0],
  96. ['is_benefit', 0],
  97. ['is_best', 0],
  98. ['is_new', 0],
  99. ['mer_use', 0],
  100. ['is_postage', 0],
  101. ['is_good', 0],
  102. ['description', ''],
  103. ['spec_type', 0],
  104. ['video_open', 0],//是否开启视频
  105. ['video_link', ''],//视频链接
  106. ['items', []],
  107. ['attrs', []],
  108. ['activity', []],
  109. ['coupon_ids', []],
  110. ['label_id', []],
  111. ['command_word', ''],
  112. ['tao_words', ''],
  113. ['type', 0],
  114. ['delivery_type', []],//物流设置
  115. ['freight', 1],//运费设置
  116. ['postage', 0],//邮费
  117. ['temp_id', 0],//运费模版
  118. ['recommend_list', []],
  119. ['brand_id', []],
  120. ['soure_link', ''],
  121. ['bar_code', ''],
  122. ['code', ''],
  123. ['is_support_refund', 1],//是否支持退款
  124. ['is_presale_product', 0],//预售商品开关
  125. ['presale_time', []],//预售时间
  126. ['presale_day', 0],//预售发货日
  127. ['is_vip_product', 0],//是否付费会员商品
  128. ['auto_on_time', 0],//自动上架时间
  129. ['auto_off_time', 0],//自动下架时间
  130. ['custom_form', []],//自定义表单
  131. ['system_form_id', 0],//系统表单ID
  132. ['store_label_id', []],//商品标签
  133. ['ensure_id', []],//商品保障服务区
  134. ['specs', []],//商品参数
  135. ['specs_id', 0],//商品参数ID
  136. ['is_limit', 0],//是否限购
  137. ['limit_type', 0],//限购类型
  138. ['limit_num', 0],//限购数量
  139. ['applicable_type', 1],//适用门店类型
  140. ['applicable_store_id', []],//适用门店IDS
  141. ]);
  142. $services->setDbCache($this->adminId . '_product_data', $data, 68400);
  143. return $this->success();
  144. }
  145. /**
  146. * 删除数据缓存
  147. * @param CacheServices $services
  148. * @return mixed
  149. */
  150. public function deleteCacheData(CacheServices $services)
  151. {
  152. $services->delectDbCache($this->adminId . '_product_data');
  153. return $this->success();
  154. }
  155. /**
  156. * 显示资源列表
  157. * @return mixed
  158. */
  159. public function index(StoreProductCategoryServices $storeProductCategoryServices)
  160. {
  161. $where = $this->request->getMore([
  162. ['store_name', ''],
  163. ['cate_id', ''],
  164. ['supplier_id', 0],
  165. ['type', 1, '', 'status'],
  166. ['sales', 'normal'],
  167. ['store_id', 0],
  168. ['store_label_id', ''],
  169. ['brand_id', '']
  170. ]);
  171. if ($where['supplier_id']) {
  172. $where['relation_id'] = $where['supplier_id'];
  173. $where['type'] = 2;
  174. } elseif ($where['store_id']) {
  175. $where['relation_id'] = $where['store_id'];
  176. $where['type'] = 1;
  177. } else {
  178. $where['pid'] = 0;
  179. }
  180. $cateId = $where['cate_id'];
  181. if ($cateId) {
  182. $cateId = is_string($cateId) ? [$cateId] : $cateId;
  183. $cateId = array_merge($cateId, $storeProductCategoryServices->getColumn(['pid' => $cateId], 'id'));
  184. $cateId = array_unique(array_diff($cateId, [0]));
  185. }
  186. $where['cate_id'] = $cateId;
  187. unset($where['supplier_id'], $where['store_id']);
  188. $data = $this->service->getList($where);
  189. return $this->success($data);
  190. }
  191. /**
  192. * 审核商品表单
  193. * @param $id
  194. * @return mixed
  195. */
  196. public function verifyForm($id)
  197. {
  198. if (!$id) {
  199. return $this->fail('缺少参数');
  200. }
  201. return $this->success($this->service->verifyForm($id));
  202. }
  203. /**
  204. * 强制下架表单
  205. * @param $id
  206. * @return mixed
  207. */
  208. public function removeForm($id)
  209. {
  210. if (!$id) {
  211. return $this->fail('缺少参数');
  212. }
  213. return $this->success($this->service->verifyForm($id, 2));
  214. }
  215. /**
  216. * 审核商品
  217. * @param string $is_show
  218. * @param string $id
  219. * @return mixed
  220. */
  221. public function setVerify($id = '')
  222. {
  223. if (!$id) {
  224. return $this->fail('缺少参数');
  225. }
  226. $data = $this->request->getMore([
  227. ['is_verify', 1],
  228. ['refusal', '']
  229. ]);
  230. if (in_array($data['is_verify'], [-1, -2]) && !$data['refusal']) {
  231. return $this->fail('请输入原因');
  232. }
  233. $this->service->verify((int)$id, $data);
  234. return $this->success('审核成功');
  235. }
  236. /**
  237. * 修改状态
  238. * @param string $is_show
  239. * @param string $id
  240. * @return mixed
  241. */
  242. public function set_show($is_show = '', $id = '')
  243. {
  244. $this->service->setShow([$id], $is_show);
  245. return $this->success($is_show == 1 ? '上架成功' : '下架成功');
  246. }
  247. /**
  248. * 设置批量商品上架
  249. * @return mixed
  250. */
  251. public function product_show()
  252. {
  253. [$ids, $all, $where] = $this->request->postMore([
  254. ['ids', []],
  255. ['all', 0],
  256. ['where', []],
  257. ], true);
  258. if ($all == 0) {//单页不走队列
  259. if (empty($ids)) return $this->fail('请选择需要上架的商品');
  260. $this->service->setShow($ids, 1);
  261. return $this->success('上架成功');
  262. } else {//全部
  263. $ids = [];
  264. if (isset($where['type'])) {
  265. $where['status'] = $where['type'];
  266. unset($where['type']);
  267. }
  268. $type = 4;//商品上架
  269. /** @var QueueServices $queueService */
  270. $queueService = app()->make(QueueServices::class);
  271. $queueService->setQueueData($where, 'id', $ids, $type);
  272. //加入队列
  273. BatchHandleJob::dispatch(['up', $type]);
  274. return $this->success('后台程序已执商品上架任务!');
  275. }
  276. }
  277. /**
  278. * 设置批量商品下架
  279. * @return mixed
  280. */
  281. public function product_unshow()
  282. {
  283. [$ids, $all, $where] = $this->request->postMore([
  284. ['ids', []],
  285. ['all', 0],
  286. ['where', []],
  287. ], true);
  288. if ($all == 0) {//单页不走队列
  289. if (empty($ids)) return $this->fail('请选择需要下架的商品');
  290. $this->service->setShow($ids, 0);
  291. return $this->success('下架成功');
  292. } else {//全部
  293. $ids = [];
  294. if (isset($where['type'])) {
  295. $where['status'] = $where['type'];
  296. unset($where['type']);
  297. }
  298. $type = 4;//商品下架
  299. /** @var QueueServices $queueService */
  300. $queueService = app()->make(QueueServices::class);
  301. $queueService->setQueueData($where, 'id', $ids, $type);
  302. //加入队列
  303. BatchHandleJob::dispatch(['down', $type]);
  304. return $this->success('后台程序已执商品下架任务!');
  305. }
  306. }
  307. /**
  308. * 批量设置商品配送方式
  309. * @return mixed
  310. */
  311. public function setProductDeliveryType()
  312. {
  313. [$ids, $all, $deliveryType] = $this->request->postMore([
  314. ['ids', []],
  315. ['all', 0],
  316. ['delivery_type', []],
  317. ], true);
  318. if (!$deliveryType) {
  319. return $this->fail('请选择配送方式');
  320. }
  321. if ($all == 0 && empty($ids)) {
  322. return $this->fail('请选择需要设置的商品');
  323. }
  324. if (count($ids) == 1) {
  325. $product = $this->service->get($ids[0]);
  326. if ($product && in_array($product['product_type'], [1, 2, 3])) {
  327. return $this->fail('虚拟、卡密商品不需要设置配送方式');
  328. }
  329. }
  330. if ($all == 1) {
  331. $update_where = ['is_show' => 1, 'is_del' => 0];
  332. } else {
  333. $update_where = [['id', 'in', $ids]];
  334. }
  335. if (!$this->service->update($update_where, ['delivery_type' => $deliveryType])) {
  336. return $this->fail('设置失败');
  337. }
  338. $this->service->cacheTag()->clear();
  339. return $this->success('设置成功');
  340. }
  341. /**
  342. * 批量设置商品标签
  343. * @return mixed
  344. */
  345. public function setProductlabel()
  346. {
  347. [$ids, $all, $store_label_id] = $this->request->postMore([
  348. ['ids', []],
  349. ['all', 0],
  350. ['store_label_id', []],
  351. ], true);
  352. if (!$store_label_id) {
  353. return $this->fail('请选择商品标签');
  354. }
  355. if ($all == 0 && empty($ids)) {
  356. return $this->fail('请选择需要设置的商品');
  357. }
  358. if ($all == 1) {
  359. $update_where = ['is_show' => 1, 'is_del' => 0];
  360. } else {
  361. $update_where = [['id', 'in', $ids]];
  362. }
  363. if (!$this->service->update($update_where, ['store_label_id' => implode(',', $store_label_id)])) {
  364. return $this->fail('设置失败');
  365. }
  366. $this->service->cacheTag()->clear();
  367. return $this->success('设置成功');
  368. }
  369. /**
  370. * 批量设置商品保障服务
  371. * @return mixed
  372. */
  373. public function setProductEnsure()
  374. {
  375. [$ids, $all, $ensure_id] = $this->request->postMore([
  376. ['ids', []],
  377. ['all', 0],
  378. ['store_label_id', []],
  379. ], true);
  380. if (!$ensure_id) {
  381. return $this->fail('请选择商品保障服务');
  382. }
  383. if ($all == 0 && empty($ids)) {
  384. return $this->fail('请选择需要设置的商品');
  385. }
  386. if ($all == 1) {
  387. $update_where = ['is_show' => 1, 'is_del' => 0];
  388. } else {
  389. $update_where = [['id', 'in', $ids]];
  390. }
  391. if (!$this->service->update($update_where, ['ensure_id' => implode(',', $ensure_id)])) {
  392. return $this->fail('设置失败');
  393. }
  394. $this->service->cacheTag()->clear();
  395. return $this->success('设置成功');
  396. }
  397. /**
  398. * 批量设置商品参数
  399. * @return mixed
  400. */
  401. public function setProductSpecs()
  402. {
  403. [$ids, $all, $specs_id, $specs] = $this->request->postMore([
  404. ['ids', []],
  405. ['all', 0],
  406. ['specs_id', 0],
  407. ['specs', []],
  408. ], true);
  409. if (!$specs_id) {
  410. return $this->fail('请选择商品参数模版');
  411. }
  412. if (!$specs) {
  413. return $this->fail('请添加商品参数');
  414. }
  415. if ($all == 0 && empty($ids)) {
  416. return $this->fail('请选择需要设置的商品');
  417. }
  418. if ($all == 1) {
  419. $update_where = ['is_show' => 1, 'is_del' => 0];
  420. } else {
  421. $update_where = [['id', 'in', $ids]];
  422. }
  423. if (!$this->service->update($update_where, ['specs_id' => $specs_id, 'specs' => json_encode($specs)])) {
  424. return $this->fail('设置失败');
  425. }
  426. $this->service->cacheTag()->clear();
  427. return $this->success('设置成功');
  428. }
  429. /**
  430. * 获取规格模板
  431. * @return mixed
  432. */
  433. public function get_rule()
  434. {
  435. return $this->success($this->service->getRule());
  436. }
  437. /**
  438. * 获取商品详细信息
  439. * @param int $id
  440. * @throws \think\db\exception\DataNotFoundException
  441. * @throws \think\db\exception\DbException
  442. * @throws \think\db\exception\ModelNotFoundException
  443. */
  444. public function get_product_info($id = 0)
  445. {
  446. return $this->success($this->service->getInfo((int)$id));
  447. }
  448. /**
  449. * 保存新建或编辑
  450. * @param $id
  451. * @return mixed
  452. * @throws \Exception
  453. */
  454. public function save(StoreProductAttrServices $attrServices, $id)
  455. {
  456. $data = $this->request->postMore([
  457. ['product_type', 0],//商品类型
  458. ['supplier_id', 0],//供应商ID
  459. ['cate_id', []],
  460. ['store_name', ''],
  461. ['store_info', ''],
  462. ['keyword', ''],
  463. ['unit_name', '件'],
  464. ['recommend_image', ''],
  465. ['slider_image', []],
  466. ['is_sub', []],//佣金是单独还是默认
  467. ['sort', 0],
  468. // ['sales', 0],
  469. ['ficti', 0],
  470. ['give_integral', 0],
  471. ['is_show', 0],
  472. ['is_hot', 0],
  473. ['is_benefit', 0],
  474. ['is_best', 0],
  475. ['is_new', 0],
  476. ['mer_use', 0],
  477. ['is_postage', 0],
  478. ['is_good', 0],
  479. ['description', ''],
  480. ['spec_type', 0],
  481. ['video_open', 0],
  482. ['video_link', ''],
  483. ['items', []],
  484. ['attrs', []],
  485. ['recommend', []],//商品推荐
  486. ['activity', []],
  487. ['coupon_ids', []],
  488. ['label_id', []],
  489. ['command_word', ''],
  490. ['tao_words', ''],
  491. ['type', 0, '', 'is_copy'],
  492. ['delivery_type', []],//物流设置
  493. ['freight', 1],//运费设置
  494. ['postage', 0],//邮费
  495. ['temp_id', 0],//运费模版
  496. ['recommend_list', []],
  497. ['brand_id', []],
  498. ['soure_link', ''],
  499. ['bar_code', ''],
  500. ['code', ''],
  501. ['is_support_refund', 1],//是否支持退款
  502. ['is_presale_product', 0],//预售商品开关
  503. ['presale_time', []],//预售时间
  504. ['presale_day', 0],//预售发货日
  505. ['is_vip_product', 0],//是否付费会员商品
  506. ['auto_on_time', 0],//自动上架时间
  507. ['auto_off_time', 0],//自动下架时间
  508. ['custom_form', []],//自定义表单
  509. ['system_form_id', 0],//系统表单ID
  510. ['store_label_id', []],//商品标签
  511. ['ensure_id', []],//商品保障服务区
  512. ['specs', []],//商品参数
  513. ['specs_id', 0],//商品参数ID
  514. ['is_limit', 0],//是否限购
  515. ['limit_type', 0],//限购类型
  516. ['limit_num', 0],//限购数量
  517. ['applicable_type', 1],//适用门店类型
  518. ['applicable_store_id', []],//适用门店IDS
  519. ]);
  520. if ($data['applicable_type'] == 1) {
  521. $data['applicable_store_id'] = [];
  522. } elseif ($data['applicable_type'] == 2) {
  523. if (!$data['applicable_store_id']) {
  524. return $this->fail('请选择要适用门店');
  525. }
  526. }
  527. $this->service->save((int)$id, $data);
  528. $this->service->cacheTag()->clear();
  529. $attrServices->cacheTag()->clear();
  530. return $this->success($id ? '保存商品信息成功' : '添加商品成功!');
  531. }
  532. /**
  533. * 删除指定资源
  534. *
  535. * @param int $id
  536. * @return \think\Response
  537. */
  538. public function delete(StoreProductAttrServices $attrServices, $id)
  539. {
  540. //删除商品检测是否有参与活动
  541. $this->service->checkActivity($id);
  542. $res = $this->service->del($id);
  543. event('product.delete', [$id]);
  544. $this->service->cacheTag()->clear();
  545. $attrServices->cacheTag()->clear();
  546. return $this->success($res);
  547. }
  548. /**
  549. * 生成规格列表
  550. * @param int $id
  551. * @param int $type
  552. * @return mixed
  553. */
  554. public function is_format_attr($id = 0, $type = 0)
  555. {
  556. $data = $this->request->postMore([
  557. ['attrs', []],
  558. ['items', []],
  559. ['product_type', 0]
  560. ]);
  561. if ($id > 0 && $type == 1) $this->service->checkActivity($id);
  562. $plat_type = 0;
  563. $relation_id = 0;
  564. if ((int)$id) {//编辑 需要查看商品来源
  565. $productInfo = $this->service->get((int)$id, ['id', 'type', 'relation_id']);
  566. if (!$productInfo) {
  567. return $this->fail('商品不存在');
  568. }
  569. $plat_type = (int)$productInfo['type'];
  570. $relation_id = (int)$productInfo['relation_id'];
  571. }
  572. $info = $this->service->getAttr($data, $id, $type, $plat_type, $relation_id);
  573. return $this->success(compact('info'));
  574. }
  575. /**
  576. * 获取选择的商品列表
  577. * @return mixed
  578. */
  579. public function search_list()
  580. {
  581. $where = $this->request->getMore([
  582. ['ids', ''],
  583. ['cate_id', ''],
  584. ['store_name', ''],
  585. ['is_integral', ''],
  586. ['type', 1, '', 'status'],
  587. ['is_live', 0],
  588. ['is_new', ''],
  589. ['is_vip_product', ''],
  590. ['is_presale_product', ''],
  591. ['store_label_id', ''],
  592. ['is_supplier', -1],//供应商商品0:不是1:是
  593. ]);
  594. $where['is_show'] = 1;
  595. $where['is_del'] = 0;
  596. $where['is_verify'] = 1;
  597. /** @var StoreProductCategoryServices $storeCategoryServices */
  598. $storeCategoryServices = app()->make(StoreProductCategoryServices::class);
  599. if ($where['cate_id'] !== '') {
  600. if ($storeCategoryServices->value(['id' => $where['cate_id']], 'pid')) {
  601. $where['sid'] = $where['cate_id'];
  602. } else {
  603. $where['cid'] = $where['cate_id'];
  604. }
  605. }
  606. $where['type'] = [0, 2];
  607. if ($where['is_supplier'] == 1) {
  608. $where['type'] = [2];
  609. } elseif($where['is_supplier'] == 0) {
  610. $where['type'] = [0];
  611. }
  612. unset($where['cate_id'], $where['is_supplier']);
  613. $list = $this->service->searchList($where);
  614. return $this->success($list);
  615. }
  616. /**
  617. * 获取某个商品规格
  618. * @return mixed
  619. */
  620. public function get_attrs()
  621. {
  622. [$id, $type] = $this->request->getMore([
  623. [['id', 'd'], 0],
  624. [['type', 'd'], 0],
  625. ], true);
  626. $info = $this->service->getProductRules($id, $type);
  627. return $this->success(compact('info'));
  628. }
  629. /**
  630. * 获取运费模板列表
  631. * @return mixed
  632. */
  633. public function get_template()
  634. {
  635. [$id] = $this->request->getMore([
  636. [['id', 'd'], 0],
  637. ], true);
  638. $type = $relation_id = 0;
  639. if ((int)$id) {
  640. $info = $this->service->get((int)$id, ['id', 'type', 'relation_id']);
  641. if ($info) {
  642. $type = (int)$info['type'];
  643. $relation_id = (int)$info['relation_id'];
  644. }
  645. }
  646. return $this->success($this->service->getTemp($type, $relation_id));
  647. }
  648. /**
  649. * 获取视频上传token
  650. * @return mixed
  651. * @throws \Exception
  652. */
  653. public function getTempKeys()
  654. {
  655. $upload = UploadService::init();
  656. $type = (int)sys_config('upload_type', 1);
  657. $key = $this->request->get('key', '');
  658. $path = $this->request->get('path', '');
  659. $contentType = $this->request->get('contentType', '');
  660. if ($type === 5) {
  661. if (!$key || !$contentType) {
  662. return app('json')->fail('缺少参数');
  663. }
  664. $re = $upload->getTempKeys($key, $path, $contentType);
  665. } else {
  666. $re = $upload->getTempKeys();
  667. }
  668. return $re ? $this->success($re) : $this->fail($upload->getError());
  669. }
  670. /**
  671. * 检测商品是否开活动
  672. * @param $id
  673. * @throws \think\db\exception\DataNotFoundException
  674. * @throws \think\db\exception\DbException
  675. * @throws \think\db\exception\ModelNotFoundException
  676. */
  677. public function check_activity($id)
  678. {
  679. $this->service->checkActivity($id);
  680. return $this->success('删除成功');
  681. }
  682. /**
  683. * 获取商品所有规格数据
  684. * @param StoreProductAttrValueServices $services
  685. * @param $id
  686. * @return mixed
  687. */
  688. public function getAttrs(StoreProductAttrValueServices $services, $id)
  689. {
  690. if (!$id) {
  691. return $this->fail('缺少商品ID');
  692. }
  693. return $this->success($services->getProductAttrValue(['product_id' => $id, 'type' => 0]));
  694. }
  695. /**
  696. * 快速修改商品规格库存
  697. * @param StoreProductAttrValueServices $services
  698. * @param $id
  699. * @return mixed
  700. */
  701. public function saveProductAttrsStock(StoreProductAttrValueServices $services, $id)
  702. {
  703. if (!$id) {
  704. return $this->fail('缺少商品ID');
  705. }
  706. [$attrs] = $this->request->getMore([
  707. ['attrs', []]
  708. ], true);
  709. if (!$attrs) {
  710. return $this->fail('请重新修改规格库存');
  711. }
  712. foreach ($attrs as $attr) {
  713. if (!isset($attr['unique']) || !isset($attr['pm']) || !isset($attr['stock'])) {
  714. return $this->fail('请重新修改规格库存');
  715. }
  716. }
  717. return $this->success(['stock' => $services->saveProductAttrsStock((int)$id, $attrs)]);
  718. }
  719. /**
  720. * 导入卡密
  721. * @return mixed
  722. * @throws \PhpOffice\PhpSpreadsheet\Reader\Exception
  723. */
  724. public function import_card()
  725. {
  726. $data = $this->request->getMore([
  727. ['file', ""]
  728. ]);
  729. if (!$data['file']) return app('json')->fail('请上传文件');
  730. $file = public_path() . substr($data['file'], 1);
  731. /** @var FileService $readExcelService */
  732. $readExcelService = app()->make(FileService::class);
  733. $cardData = $readExcelService->readProductCardExcel($file, 2);
  734. return app('json')->success($cardData);
  735. }
  736. /**
  737. * 导入erp商品
  738. * @return mixed
  739. * @throws \PhpOffice\PhpSpreadsheet\Exception
  740. * @throws \PhpOffice\PhpSpreadsheet\Reader\Exception
  741. */
  742. public function import_erp_product()
  743. {
  744. [$path] = $this->request->postMore([
  745. ['path', ""]
  746. ], true);
  747. if (!$path) {
  748. return $this->fail('请上传表格');
  749. }
  750. $path = public_path() . substr($path, 1);
  751. $data = SpreadsheetExcelService::getExcelData($path, ['spu' => 'A']);
  752. $spus = [];
  753. foreach ($data as $item) {
  754. $spus[] = $item['spu'];
  755. }
  756. $spus = array_unique(array_filter($spus));
  757. foreach ($spus as $spu) {
  758. ProductSyncErp::dispatchDo('productFromErp', [$spu]);
  759. }
  760. unlink($path);
  761. return $this->success('已加入消息队列进行同步');
  762. }
  763. /**
  764. * 商品批量操作
  765. * @param StoreProductBatchProcessServices $batchProcessServices
  766. * @return mixed
  767. */
  768. public function batchProcess(StoreProductBatchProcessServices $batchProcessServices)
  769. {
  770. [$type, $ids, $all, $where, $data] = $this->request->postMore([
  771. ['type', 1],
  772. ['ids', ''],
  773. ['all', 0],
  774. ['where', ""],
  775. ['data', []]
  776. ], true);
  777. if (!$ids && $all == 0) return $this->fail('请选择批处理商品');
  778. if (!$data) {
  779. return $this->fail('请选择批处理数据');
  780. }
  781. if (isset($where['type'])) {
  782. $where['status'] = $where['type'];
  783. unset($where['type']);
  784. }
  785. //批量操作
  786. $batchProcessServices->batchProcess((int)$type, $ids, $data, !!$all, $where);
  787. return app('json')->success('已加入消息队列,请稍后查看');
  788. }
  789. }