StoreProduct.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\adminapi\controller\v1\product;
  12. use app\adminapi\controller\AuthController;
  13. use app\adminapi\controller\v1\system\SystemClearData;
  14. use app\services\order\StoreCartServices;
  15. use app\services\other\CacheServices;
  16. use app\services\product\product\StoreCategoryServices;
  17. use app\services\product\product\StoreProductServices;
  18. use crmeb\services\FileService;
  19. use app\services\other\UploadService;
  20. use think\facade\App;
  21. use think\Request;
  22. /**
  23. * Class StoreProduct
  24. * @package app\adminapi\controller\v1\product
  25. */
  26. class StoreProduct extends AuthController
  27. {
  28. protected $service;
  29. public function __construct(App $app, StoreProductServices $service)
  30. {
  31. parent::__construct($app);
  32. $this->service = $service;
  33. }
  34. /**
  35. * 显示资源列表头部
  36. * @return mixed
  37. */
  38. public function type_header()
  39. {
  40. $where = $this->request->getMore([
  41. ['store_name', ''],
  42. ['cate_id', ''],
  43. ['spec_type', ''],
  44. ['is_gift', ''],
  45. ['vip_product', ''],
  46. ['price_s', []],
  47. ['stock_s', []],
  48. ['sales_s', []],
  49. ]);
  50. $list = $this->service->getHeader($where);
  51. return app('json')->success(compact('list'));
  52. }
  53. /**
  54. * 获取退出未保存的数据
  55. * @param CacheServices $services
  56. * @return mixed
  57. */
  58. public function getCacheData(CacheServices $services)
  59. {
  60. return app('json')->success(['info' => $services->getDbCache($this->adminId . '_product_data', [])]);
  61. }
  62. /**
  63. * 1分钟保存一次产品数据
  64. * @param CacheServices $services
  65. * @return mixed
  66. */
  67. public function saveCacheData(CacheServices $services)
  68. {
  69. $data = $this->request->postMore([
  70. ['cate_id', []],
  71. ['store_name', ''],
  72. ['store_info', ''],
  73. ['keyword', ''],
  74. ['unit_name', '件'],
  75. ['image', []],
  76. ['recommend_image', ''],
  77. ['slider_image', []],
  78. ['postage', 0],
  79. ['is_sub', []],//佣金是单独还是默认
  80. ['sort', 0],
  81. ['sales', 0],
  82. ['ficti', 100],
  83. ['give_integral', 0],
  84. ['is_show', 0],
  85. ['temp_id', 0],
  86. ['is_hot', 0],
  87. ['is_benefit', 0],
  88. ['is_best', 0],
  89. ['is_new', 0],
  90. ['mer_use', 0],
  91. ['is_postage', 0],
  92. ['is_good', 0],
  93. ['description', ''],
  94. ['spec_type', 0],
  95. ['video_link', ''],
  96. ['items', []],
  97. ['attrs', []],
  98. ['activity', []],
  99. ['coupon_ids', []],
  100. ['label_id', []],
  101. ['command_word', ''],
  102. ['tao_words', ''],
  103. ['type', 0]
  104. ]);
  105. $services->setDbCache($this->adminId . '_product_data', $data, 68400);
  106. return app('json')->success(100000);
  107. }
  108. /**
  109. * 删除数据缓存
  110. * @param CacheServices $services
  111. * @return mixed
  112. */
  113. public function deleteCacheData(CacheServices $services)
  114. {
  115. $services->delectDbCache($this->adminId . '_product_data');
  116. return app('json')->success(100002);
  117. }
  118. /**
  119. * 显示资源列表
  120. * @return mixed
  121. */
  122. public function index()
  123. {
  124. $where = $this->request->getMore([
  125. ['store_name', ''],
  126. ['cate_id', ''],
  127. ['type', 1],
  128. ['sales', 'normal'],
  129. ['spec_type', ''],
  130. ['is_gift', ''],
  131. ['vip_product', ''],
  132. ['price_s', []],
  133. ['stock_s', []],
  134. ['sales_s', []],
  135. ['store_label_id', []],
  136. ['logistics', ''],
  137. ['time', ''],
  138. ['virtual_type', ''],
  139. ]);
  140. $data = $this->service->getList($where);
  141. return app('json')->success($data);
  142. }
  143. /**
  144. * 修改状态
  145. * @param string $is_show
  146. * @param string $id
  147. * @return mixed
  148. */
  149. public function set_show($is_show = '', $id = '')
  150. {
  151. $del = $this->service->value(['id' => $id], 'is_del');
  152. if ($del == 1) return app('json')->fail('商品已删除,请先恢复商品');
  153. $this->service->setShow([$id], $is_show);
  154. return app('json')->success(100014);
  155. }
  156. /**
  157. * 设置批量商品上架
  158. * @return mixed
  159. */
  160. public function product_show()
  161. {
  162. [$ids] = $this->request->postMore([
  163. ['ids', []]
  164. ], true);
  165. $this->service->setShow($ids, 1);
  166. return app('json')->success(100014);
  167. }
  168. /**
  169. * 设置批量商品下架
  170. * @return mixed
  171. */
  172. public function product_unshow()
  173. {
  174. [$ids] = $this->request->postMore([
  175. ['ids', []]
  176. ], true);
  177. $this->service->setShow($ids, 0);
  178. return app('json')->success(100014);
  179. }
  180. /**
  181. * 获取规格模板
  182. * @return mixed
  183. */
  184. public function get_rule()
  185. {
  186. $list = $this->service->getRule();
  187. return app('json')->success($list);
  188. }
  189. /**
  190. * 获取商品详细信息
  191. * @param int $id
  192. * @throws \think\db\exception\DataNotFoundException
  193. * @throws \think\db\exception\DbException
  194. * @throws \think\db\exception\ModelNotFoundException
  195. */
  196. public function get_product_info($id = 0)
  197. {
  198. return app('json')->success($this->service->getInfo((int)$id));
  199. }
  200. /**
  201. * 保存新建或编辑
  202. * @param $id
  203. * @return mixed
  204. * @throws \Exception
  205. */
  206. public function save($id)
  207. {
  208. $data = $this->request->postMore([
  209. ['virtual_type', 0],// 商品类型
  210. ['cate_id', []],//分类id
  211. ['store_name', ''],//商品名称
  212. ['keyword', ''],//关键字
  213. ['unit_name', '件'],//单位
  214. ['store_info', ''],//商品简介
  215. ['slider_image', []],//轮播图
  216. ['video_open', 0],//是否开启视频
  217. ['video_link', ''],//视频链接
  218. ['spec_type', 0],//单多规格
  219. ['items', []],//规格
  220. ['attrs', []],//规格
  221. ['description', ''],//商品详情
  222. ['description_images', []],//商品详情
  223. ['logistics', []],//物流方式
  224. ['freight', 1],//运费设置
  225. ['postage', 0],//邮费
  226. ['temp_id', 0],//运费模版id
  227. ['give_integral', 0],//赠送积分
  228. ['presale', 0],//预售商品开关
  229. ['presale_time', 0],//预售时间
  230. ['presale_day', 0],//预售发货日
  231. ['vip_product', 0],//是否付费会员商品
  232. ['is_sub', []],//佣金是单独还是默认
  233. ['recommend', []],//商品推荐
  234. ['activity', []],//活动优先级
  235. ['recommend_list', []],//优品推荐商品
  236. ['coupon_ids', []],//优惠券
  237. ['label_id', []],//用户标签
  238. ['command_word', ''],//商品口令
  239. ['is_show', 0],//是否上架
  240. ['ficti', 0],//虚拟销量
  241. ['sort', 0],//排序
  242. ['recommend_image', ''],//商品推荐图
  243. ['sales', 0],//销量
  244. ['custom_form', []],//自定义表单
  245. ['type', 0],
  246. ['is_copy', 0],//是否是复制商品
  247. ['is_limit', 0],//是否限购
  248. ['limit_type', 0],//限购类型
  249. ['limit_num', 0],//限购数量
  250. ['min_qty', 1],//起购数量
  251. ['params_list', []],//商品参数
  252. ['label_list', []],//商品标签
  253. ['protection_list', []],//商品保障
  254. ['is_gift', 0],//是否礼品
  255. ['gift_price', 0],//礼品附加费
  256. ]);
  257. $this->service->save((int)$id, $data);
  258. return app('json')->success(100000);
  259. }
  260. /**
  261. * 删除指定资源
  262. *
  263. * @param int $id
  264. * @return \think\Response
  265. */
  266. public function delete($id)
  267. {
  268. //删除商品检测是否有参与活动
  269. $this->service->checkActivity($id);
  270. $res = $this->service->del($id);
  271. /** @var StoreCartServices $cartService */
  272. $cartService = app()->make(StoreCartServices::class);
  273. $cartService->changeStatus($id, 0);
  274. return app('json')->success($res);
  275. }
  276. /**
  277. * 批量移动到回收站
  278. * @return \think\Response
  279. */
  280. public function batchDelete()
  281. {
  282. [$ids] = $this->request->postMore([
  283. ['ids', []],
  284. ], true);
  285. return app('json')->success($this->service->batchDelete($ids));
  286. }
  287. /**
  288. * 批量从回收站恢复
  289. * @return \think\Response
  290. */
  291. public function batchRecover()
  292. {
  293. [$ids] = $this->request->postMore([
  294. ['ids', []],
  295. ], true);
  296. $this->service->batchRecover($ids);
  297. return app('json')->success('恢复成功');
  298. }
  299. /**
  300. * 生成规格列表
  301. * @param int $id
  302. * @param int $type
  303. * @return mixed
  304. */
  305. public function is_format_attr($id = 0, $type = 0)
  306. {
  307. $data = $this->request->postMore([
  308. ['attrs', []],
  309. ['items', []],
  310. ['is_virtual', 0],
  311. ['virtual_type', 0]
  312. ]);
  313. $info = $this->service->getAttr($data, $id, $type);
  314. return app('json')->success(compact('info'));
  315. }
  316. /**
  317. * 获取选择的商品列表
  318. * @return mixed
  319. */
  320. public function search_list()
  321. {
  322. $where = $this->request->getMore([
  323. ['cate_id', ''],
  324. ['store_name', ''],
  325. ['type', 1],
  326. ['is_live', 0],
  327. ['is_new', ''],
  328. ['is_virtual', -1],
  329. ['is_presale', -1],
  330. ['is_show', 1],
  331. ]);
  332. $where['is_del'] = 0;
  333. $where['cate_id'] = toIntArray($where['cate_id']);
  334. /** @var StoreCategoryServices $storeCategoryServices */
  335. $storeCategoryServices = app()->make(StoreCategoryServices::class);
  336. if ($where['cate_id'] !== '') {
  337. if ($storeCategoryServices->value(['id' => $where['cate_id']], 'pid')) {
  338. $where['sid'] = $where['cate_id'];
  339. } else {
  340. $where['cid'] = $where['cate_id'];
  341. }
  342. }
  343. unset($where['cate_id']);
  344. $list = $this->service->searchList($where);
  345. return app('json')->success($list);
  346. }
  347. /**
  348. * 获取某个商品规格
  349. * @return mixed
  350. */
  351. public function get_attrs()
  352. {
  353. [$id, $type] = $this->request->getMore([
  354. [['id', 'd'], 0],
  355. [['type', 'd'], 0],
  356. ], true);
  357. $info = $this->service->getProductRules($id, $type);
  358. return app('json')->success(compact('info'));
  359. }
  360. /**
  361. * 获取运费模板列表
  362. * @return mixed
  363. */
  364. public function get_template()
  365. {
  366. return app('json')->success($this->service->getTemp());
  367. }
  368. /**
  369. * 获取视频上传token
  370. * @return mixed
  371. * @throws \Exception
  372. */
  373. public function getTempKeys(Request $request)
  374. {
  375. $upload = UploadService::init();
  376. $type = (int)sys_config('upload_type', 1);
  377. $key = $request->get('key', '');
  378. $path = $request->get('path', '');
  379. $contentType = $request->get('contentType', '');
  380. if ($type === 5) {
  381. if (!$key || !$contentType) {
  382. return app('json')->fail('缺少参数');
  383. }
  384. $re = $upload->getTempKeys($key, $path, $contentType);
  385. } else {
  386. $re = $upload->getTempKeys();
  387. }
  388. return $re ? app('json')->success($re) : app('json')->fail(100016);
  389. }
  390. /**
  391. * 检测商品是否开活动
  392. * @param $id
  393. * @throws \think\db\exception\DataNotFoundException
  394. * @throws \think\db\exception\DbException
  395. * @throws \think\db\exception\ModelNotFoundException
  396. */
  397. public function check_activity($id)
  398. {
  399. $this->service->checkActivity($id);
  400. return app('json')->success(100002);
  401. }
  402. /**
  403. * 导入卡密
  404. * @return mixed
  405. * @throws \PhpOffice\PhpSpreadsheet\Reader\Exception
  406. */
  407. public function import_card()
  408. {
  409. $data = $this->request->getMore([
  410. ['file', ""]
  411. ]);
  412. if (!$data['file']) return app('json')->fail(400168);
  413. $file = public_path() . substr($data['file'], 1);
  414. // 获取文件后缀
  415. $suffix = strtolower(pathinfo($file, PATHINFO_EXTENSION));
  416. if (!in_array($suffix, ['xls', 'xlsx'])) {
  417. return app('json')->fail('文件格式不正确,请上传xls或xlsx格式的文件!');
  418. }
  419. /** @var FileService $readExcelService */
  420. $readExcelService = app()->make(FileService::class);
  421. $cardData = $readExcelService->readExcel($file, 'card', 1, ucfirst($suffix));
  422. return app('json')->success($cardData);
  423. }
  424. /**
  425. * 商品批量设置
  426. * @return mixed
  427. */
  428. public function batchSetting()
  429. {
  430. $data = $this->request->postMore([
  431. ['ids', []],
  432. ['cate_id', []],
  433. ['logistics', []],
  434. ['freight', 2],
  435. ['postage', 0],
  436. ['temp_id', 1],
  437. ['give_integral', 0],
  438. ['coupon_ids', []],
  439. ['label_id', []],
  440. ['label_list', []],
  441. ['recommend', []],
  442. ['type', 0],
  443. ['is_gift', 0],
  444. ['gift_price', 0],
  445. ]);
  446. $this->service->batchSetting($data);
  447. return app('json')->success(100014);
  448. }
  449. /**
  450. * 商品类型接口
  451. * @return \think\Response
  452. * @author wuhaotian
  453. * @email 442384644@qq.com
  454. * @date 2024/9/29
  455. */
  456. public function productTypeConfig()
  457. {
  458. $productTypeConfig = sys_config('product_type_config');
  459. foreach ($productTypeConfig as $key => $value) {
  460. $productTypeConfig[$key] = intval($value);
  461. }
  462. return app('json')->success(sys_config('product_type_config'));
  463. }
  464. /**
  465. * 商品迁移导出
  466. * @return \think\Response
  467. * @author wuhaotian
  468. * @email 442384644@qq.com
  469. * @date 2024/10/9
  470. */
  471. public function productExport()
  472. {
  473. $where = $this->request->getMore([
  474. ['store_name', ''],
  475. ['cate_id', ''],
  476. ['type', 1],
  477. ['sales', 'normal']
  478. ]);
  479. $where['virtual_type'] = 0;
  480. return app('json')->success($this->service->productExportList($where));
  481. }
  482. /**
  483. * 商品迁移导入
  484. * @return \think\Response
  485. * @throws \PhpOffice\PhpSpreadsheet\Reader\Exception
  486. * @author wuhaotian
  487. * @email 442384644@qq.com
  488. * @date 2024/10/9
  489. */
  490. public function productImport()
  491. {
  492. [$file] = $this->request->getMore([
  493. ['file', ""]
  494. ], true);
  495. if (!$file) return app('json')->fail(400168);
  496. $res = $this->service->productImport($file);
  497. return app('json')->success('导入成功', $res);
  498. }
  499. /**
  500. * 回收站商品彻底删除
  501. * @param $id
  502. * @return \think\Response
  503. * @author wuhaotian
  504. * @email 442384644@qq.com
  505. * @date 2024/10/9
  506. */
  507. public function fullDel($id)
  508. {
  509. app()->make(SystemClearData::class)->recycleProduct($id);
  510. return app('json')->success('删除成功');
  511. }
  512. public function otherInfo($id, $type)
  513. {
  514. if (!$id) return app('json')->fail('参数错误');
  515. return app('json')->success($this->service->otherInfo($id, $type));
  516. }
  517. public function otherSave($id, $type)
  518. {
  519. $data = $this->request->postMore([
  520. ['is_sub', 0],
  521. ['is_vip', 0],
  522. ['vip_product', 0],
  523. ['attr_value', []],
  524. ]);
  525. $this->service->otherSave($id, $type, $data);
  526. return app('json')->success('保存成功');
  527. }
  528. }