StoreProduct.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  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. ['is_lb', 0],//是否礼品
  256. ['gift_price', 0],//礼品附加费
  257. ['rebate_radio', 0],//贡献值返利比例
  258. ]);
  259. $this->service->save((int)$id, $data);
  260. return app('json')->success(100000);
  261. }
  262. /**
  263. * 删除指定资源
  264. *
  265. * @param int $id
  266. * @return \think\Response
  267. */
  268. public function delete($id)
  269. {
  270. //删除商品检测是否有参与活动
  271. $this->service->checkActivity($id);
  272. $res = $this->service->del($id);
  273. /** @var StoreCartServices $cartService */
  274. $cartService = app()->make(StoreCartServices::class);
  275. $cartService->changeStatus($id, 0);
  276. return app('json')->success($res);
  277. }
  278. /**
  279. * 批量移动到回收站
  280. * @return \think\Response
  281. */
  282. public function batchDelete()
  283. {
  284. [$ids] = $this->request->postMore([
  285. ['ids', []],
  286. ], true);
  287. return app('json')->success($this->service->batchDelete($ids));
  288. }
  289. /**
  290. * 批量从回收站恢复
  291. * @return \think\Response
  292. */
  293. public function batchRecover()
  294. {
  295. [$ids] = $this->request->postMore([
  296. ['ids', []],
  297. ], true);
  298. $this->service->batchRecover($ids);
  299. return app('json')->success('恢复成功');
  300. }
  301. /**
  302. * 生成规格列表
  303. * @param int $id
  304. * @param int $type
  305. * @return mixed
  306. */
  307. public function is_format_attr($id = 0, $type = 0)
  308. {
  309. $data = $this->request->postMore([
  310. ['attrs', []],
  311. ['items', []],
  312. ['is_virtual', 0],
  313. ['virtual_type', 0]
  314. ]);
  315. $info = $this->service->getAttr($data, $id, $type);
  316. return app('json')->success(compact('info'));
  317. }
  318. /**
  319. * 获取选择的商品列表
  320. * @return mixed
  321. */
  322. public function search_list()
  323. {
  324. $where = $this->request->getMore([
  325. ['cate_id', ''],
  326. ['store_name', ''],
  327. ['type', 1],
  328. ['is_live', 0],
  329. ['is_new', ''],
  330. ['is_virtual', -1],
  331. ['is_presale', -1],
  332. ['is_show', 1],
  333. ]);
  334. $where['is_del'] = 0;
  335. $where['cate_id'] = toIntArray($where['cate_id']);
  336. /** @var StoreCategoryServices $storeCategoryServices */
  337. $storeCategoryServices = app()->make(StoreCategoryServices::class);
  338. if ($where['cate_id'] !== '') {
  339. if ($storeCategoryServices->value(['id' => $where['cate_id']], 'pid')) {
  340. $where['sid'] = $where['cate_id'];
  341. } else {
  342. $where['cid'] = $where['cate_id'];
  343. }
  344. }
  345. unset($where['cate_id']);
  346. $list = $this->service->searchList($where);
  347. return app('json')->success($list);
  348. }
  349. /**
  350. * 获取某个商品规格
  351. * @return mixed
  352. */
  353. public function get_attrs()
  354. {
  355. [$id, $type] = $this->request->getMore([
  356. [['id', 'd'], 0],
  357. [['type', 'd'], 0],
  358. ], true);
  359. $info = $this->service->getProductRules($id, $type);
  360. return app('json')->success(compact('info'));
  361. }
  362. /**
  363. * 获取运费模板列表
  364. * @return mixed
  365. */
  366. public function get_template()
  367. {
  368. return app('json')->success($this->service->getTemp());
  369. }
  370. /**
  371. * 获取视频上传token
  372. * @return mixed
  373. * @throws \Exception
  374. */
  375. public function getTempKeys(Request $request)
  376. {
  377. $upload = UploadService::init();
  378. $type = (int)sys_config('upload_type', 1);
  379. $key = $request->get('key', '');
  380. $path = $request->get('path', '');
  381. $contentType = $request->get('contentType', '');
  382. if ($type === 5) {
  383. if (!$key || !$contentType) {
  384. return app('json')->fail('缺少参数');
  385. }
  386. $re = $upload->getTempKeys($key, $path, $contentType);
  387. } else {
  388. $re = $upload->getTempKeys();
  389. }
  390. return $re ? app('json')->success($re) : app('json')->fail(100016);
  391. }
  392. /**
  393. * 检测商品是否开活动
  394. * @param $id
  395. * @throws \think\db\exception\DataNotFoundException
  396. * @throws \think\db\exception\DbException
  397. * @throws \think\db\exception\ModelNotFoundException
  398. */
  399. public function check_activity($id)
  400. {
  401. $this->service->checkActivity($id);
  402. return app('json')->success(100002);
  403. }
  404. /**
  405. * 导入卡密
  406. * @return mixed
  407. * @throws \PhpOffice\PhpSpreadsheet\Reader\Exception
  408. */
  409. public function import_card()
  410. {
  411. $data = $this->request->getMore([
  412. ['file', ""]
  413. ]);
  414. if (!$data['file']) return app('json')->fail(400168);
  415. $file = public_path() . substr($data['file'], 1);
  416. // 获取文件后缀
  417. $suffix = strtolower(pathinfo($file, PATHINFO_EXTENSION));
  418. if (!in_array($suffix, ['xls', 'xlsx'])) {
  419. return app('json')->fail('文件格式不正确,请上传xls或xlsx格式的文件!');
  420. }
  421. /** @var FileService $readExcelService */
  422. $readExcelService = app()->make(FileService::class);
  423. $cardData = $readExcelService->readExcel($file, 'card', 1, ucfirst($suffix));
  424. return app('json')->success($cardData);
  425. }
  426. /**
  427. * 商品批量设置
  428. * @return mixed
  429. */
  430. public function batchSetting()
  431. {
  432. $data = $this->request->postMore([
  433. ['ids', []],
  434. ['cate_id', []],
  435. ['logistics', []],
  436. ['freight', 2],
  437. ['postage', 0],
  438. ['temp_id', 1],
  439. ['give_integral', 0],
  440. ['coupon_ids', []],
  441. ['label_id', []],
  442. ['label_list', []],
  443. ['recommend', []],
  444. ['type', 0],
  445. ['is_gift', 0],
  446. ['gift_price', 0],
  447. ]);
  448. $this->service->batchSetting($data);
  449. return app('json')->success(100014);
  450. }
  451. /**
  452. * 商品类型接口
  453. * @return \think\Response
  454. * @author wuhaotian
  455. * @email 442384644@qq.com
  456. * @date 2024/9/29
  457. */
  458. public function productTypeConfig()
  459. {
  460. $productTypeConfig = sys_config('product_type_config');
  461. foreach ($productTypeConfig as $key => $value) {
  462. $productTypeConfig[$key] = intval($value);
  463. }
  464. return app('json')->success(sys_config('product_type_config'));
  465. }
  466. /**
  467. * 商品迁移导出
  468. * @return \think\Response
  469. * @author wuhaotian
  470. * @email 442384644@qq.com
  471. * @date 2024/10/9
  472. */
  473. public function productExport()
  474. {
  475. $where = $this->request->getMore([
  476. ['store_name', ''],
  477. ['cate_id', ''],
  478. ['type', 1],
  479. ['sales', 'normal']
  480. ]);
  481. $where['virtual_type'] = 0;
  482. return app('json')->success($this->service->productExportList($where));
  483. }
  484. /**
  485. * 商品迁移导入
  486. * @return \think\Response
  487. * @throws \PhpOffice\PhpSpreadsheet\Reader\Exception
  488. * @author wuhaotian
  489. * @email 442384644@qq.com
  490. * @date 2024/10/9
  491. */
  492. public function productImport()
  493. {
  494. [$file] = $this->request->getMore([
  495. ['file', ""]
  496. ], true);
  497. if (!$file) return app('json')->fail(400168);
  498. $res = $this->service->productImport($file);
  499. return app('json')->success('导入成功', $res);
  500. }
  501. /**
  502. * 回收站商品彻底删除
  503. * @param $id
  504. * @return \think\Response
  505. * @author wuhaotian
  506. * @email 442384644@qq.com
  507. * @date 2024/10/9
  508. */
  509. public function fullDel($id)
  510. {
  511. app()->make(SystemClearData::class)->recycleProduct($id);
  512. return app('json')->success('删除成功');
  513. }
  514. public function otherInfo($id, $type)
  515. {
  516. if (!$id) return app('json')->fail('参数错误');
  517. return app('json')->success($this->service->otherInfo($id, $type));
  518. }
  519. public function otherSave($id, $type)
  520. {
  521. $data = $this->request->postMore([
  522. ['is_sub', 0],
  523. ['is_vip', 0],
  524. ['vip_product', 0],
  525. ['attr_value', []],
  526. ]);
  527. $this->service->otherSave($id, $type, $data);
  528. return app('json')->success('保存成功');
  529. }
  530. }