StoreProduct.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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\services\order\StoreCartServices;
  14. use app\services\other\CacheServices;
  15. use app\services\product\product\StoreCategoryServices;
  16. use app\services\product\product\StoreProductServices;
  17. use crmeb\services\FileService;
  18. use app\services\other\UploadService;
  19. use think\facade\App;
  20. use think\Request;
  21. /**
  22. * Class StoreProduct
  23. * @package app\adminapi\controller\v1\product
  24. */
  25. class StoreProduct extends AuthController
  26. {
  27. protected $service;
  28. public function __construct(App $app, StoreProductServices $service)
  29. {
  30. parent::__construct($app);
  31. $this->service = $service;
  32. }
  33. /**
  34. * 显示资源列表头部
  35. * @return mixed
  36. */
  37. public function type_header()
  38. {
  39. $where = $this->request->getMore([
  40. ['store_name', ''],
  41. ['cate_id', ''],
  42. ]);
  43. $list = $this->service->getHeader($where);
  44. return app('json')->success(compact('list'));
  45. }
  46. /**
  47. * 获取退出未保存的数据
  48. * @param CacheServices $services
  49. * @return mixed
  50. */
  51. public function getCacheData(CacheServices $services)
  52. {
  53. return app('json')->success(['info' => $services->getDbCache($this->adminId . '_product_data', [])]);
  54. }
  55. /**
  56. * 1分钟保存一次产品数据
  57. * @param CacheServices $services
  58. * @return mixed
  59. */
  60. public function saveCacheData(CacheServices $services)
  61. {
  62. $data = $this->request->postMore([
  63. ['cate_id', []],
  64. ['store_name', ''],
  65. ['store_info', ''],
  66. ['keyword', ''],
  67. ['unit_name', '件'],
  68. ['image', []],
  69. ['recommend_image', ''],
  70. ['slider_image', []],
  71. ['postage', 0],
  72. ['is_sub', []],//佣金是单独还是默认
  73. ['sort', 0],
  74. ['sales', 0],
  75. ['ficti', 100],
  76. ['give_integral', 0],
  77. ['is_show', 0],
  78. ['temp_id', 0],
  79. ['is_hot', 0],
  80. ['is_benefit', 0],
  81. ['is_best', 0],
  82. ['is_new', 0],
  83. ['mer_use', 0],
  84. ['is_postage', 0],
  85. ['is_good', 0],
  86. ['description', ''],
  87. ['spec_type', 0],
  88. ['video_link', ''],
  89. ['items', []],
  90. ['attrs', []],
  91. ['activity', []],
  92. ['coupon_ids', []],
  93. ['label_id', []],
  94. ['command_word', ''],
  95. ['tao_words', ''],
  96. ['type', 0]
  97. ]);
  98. $services->setDbCache($this->adminId . '_product_data', $data, 68400);
  99. return app('json')->success(100000);
  100. }
  101. /**
  102. * 删除数据缓存
  103. * @param CacheServices $services
  104. * @return mixed
  105. */
  106. public function deleteCacheData(CacheServices $services)
  107. {
  108. $services->delectDbCache($this->adminId . '_product_data');
  109. return app('json')->success(100002);
  110. }
  111. /**
  112. * 显示资源列表
  113. * @return mixed
  114. */
  115. public function index()
  116. {
  117. $where = $this->request->getMore([
  118. ['store_name', ''],
  119. ['cate_id', ''],
  120. ['type', 1],
  121. ['sales', 'normal'],
  122. ['is_gift',0],
  123. ['is_repeat',0],
  124. ['is_free',0]
  125. ]);
  126. $data = $this->service->getList($where);
  127. return app('json')->success($data);
  128. }
  129. /**
  130. * 修改状态
  131. * @param string $is_show
  132. * @param string $id
  133. * @return mixed
  134. */
  135. public function set_show($is_show = '', $id = '')
  136. {
  137. $this->service->setShow([$id], $is_show);
  138. return app('json')->success(100014);
  139. }
  140. /**
  141. * 设置批量商品上架
  142. * @return mixed
  143. */
  144. public function product_show()
  145. {
  146. [$ids] = $this->request->postMore([
  147. ['ids', []]
  148. ], true);
  149. $this->service->setShow($ids, 1);
  150. return app('json')->success(100014);
  151. }
  152. /**
  153. * 设置批量商品下架
  154. * @return mixed
  155. */
  156. public function product_unshow()
  157. {
  158. [$ids] = $this->request->postMore([
  159. ['ids', []]
  160. ], true);
  161. $this->service->setShow($ids, 0);
  162. return app('json')->success(100014);
  163. }
  164. /**
  165. * 获取规格模板
  166. * @return mixed
  167. */
  168. public function get_rule()
  169. {
  170. $list = $this->service->getRule();
  171. return app('json')->success($list);
  172. }
  173. /**
  174. * 获取商品详细信息
  175. * @param int $id
  176. * @throws \think\db\exception\DataNotFoundException
  177. * @throws \think\db\exception\DbException
  178. * @throws \think\db\exception\ModelNotFoundException
  179. */
  180. public function get_product_info($id = 0)
  181. {
  182. return app('json')->success($this->service->getInfo((int)$id));
  183. }
  184. /**
  185. * 保存新建或编辑
  186. * @param $id
  187. * @return mixed
  188. * @throws \Exception
  189. */
  190. public function save($id)
  191. {
  192. $data = $this->request->postMore([
  193. ['virtual_type', 0],// 商品类型
  194. ['cate_id', []],//分类id
  195. ['store_name', ''],//商品名称
  196. ['keyword', ''],//关键字
  197. ['unit_name', '件'],//单位
  198. ['store_info', ''],//商品简介
  199. ['slider_image', []],//轮播图
  200. ['video_open', 0],//是否开启视频
  201. ['video_link', ''],//视频链接
  202. ['spec_type', 0],//单多规格
  203. ['items', []],//规格
  204. ['attrs', []],//规格
  205. ['description', ''],//商品详情
  206. ['description_images', []],//商品详情
  207. ['logistics', []],//物流方式
  208. ['freight', 1],//运费设置
  209. ['postage', 0],//邮费
  210. ['temp_id', 0],//运费模版id
  211. ['give_integral', 0],//赠送积分
  212. ['presale', 0],//预售商品开关
  213. ['presale_time', 0],//预售时间
  214. ['presale_day', 0],//预售发货日
  215. ['vip_product', 0],//是否付费会员商品
  216. ['is_sub', []],//佣金是单独还是默认
  217. ['recommend', []],//商品推荐
  218. ['activity', []],//活动优先级
  219. ['recommend_list', []],//优品推荐商品
  220. ['coupon_ids', []],//优惠券
  221. ['label_id', []],//用户标签
  222. ['command_word', ''],//商品口令
  223. ['is_show', 0],//是否上架
  224. ['ficti', 0],//虚拟销量
  225. ['sort', 0],//排序
  226. ['recommend_image', ''],//商品推荐图
  227. ['sales', 0],//销量
  228. ['custom_form', []],//自定义表单
  229. ['type', 0],
  230. ['is_copy', 0],//是否是复制商品
  231. ['is_limit', 0],//是否限购
  232. ['limit_type', 0],//限购类型
  233. ['limit_num', 0],//限购数量
  234. ['min_qty', 1],//起购数量
  235. ['is_gift', 0],//是否礼包商品
  236. ['is_repeat', 0],//是否复购商品
  237. ['is_free', 0],//是否复购商品
  238. ]);
  239. $this->service->save((int)$id, $data);
  240. return app('json')->success(100000);
  241. }
  242. /**
  243. * 删除指定资源
  244. *
  245. * @param int $id
  246. * @return \think\Response
  247. */
  248. public function delete($id)
  249. {
  250. //删除商品检测是否有参与活动
  251. $this->service->checkActivity($id);
  252. $res = $this->service->del($id);
  253. /** @var StoreCartServices $cartService */
  254. $cartService = app()->make(StoreCartServices::class);
  255. $cartService->changeStatus($id, 0);
  256. return app('json')->success($res);
  257. }
  258. /**
  259. * 生成规格列表
  260. * @param int $id
  261. * @param int $type
  262. * @return mixed
  263. */
  264. public function is_format_attr($id = 0, $type = 0)
  265. {
  266. $data = $this->request->postMore([
  267. ['attrs', []],
  268. ['items', []],
  269. ['is_virtual', 0],
  270. ['virtual_type', 0]
  271. ]);
  272. if ($id > 0 && $type == 1) $this->service->checkActivity($id);
  273. $info = $this->service->getAttr($data, $id, $type);
  274. return app('json')->success(compact('info'));
  275. }
  276. /**
  277. * 获取选择的商品列表
  278. * @return mixed
  279. */
  280. public function search_list()
  281. {
  282. $where = $this->request->getMore([
  283. ['cate_id', ''],
  284. ['store_name', ''],
  285. ['type', 1],
  286. ['is_live', 0],
  287. ['is_new', ''],
  288. ['is_virtual', -1],
  289. ['is_presale', -1],
  290. ['is_gift',0],
  291. ['is_repeat',0],
  292. ['is_free',0]
  293. ]);
  294. $where['is_show'] = 1;
  295. $where['is_del'] = 0;
  296. /** @var StoreCategoryServices $storeCategoryServices */
  297. $storeCategoryServices = app()->make(StoreCategoryServices::class);
  298. if ($where['cate_id'] !== '') {
  299. if ($storeCategoryServices->value(['id' => $where['cate_id']], 'pid')) {
  300. $where['sid'] = $where['cate_id'];
  301. } else {
  302. $where['cid'] = $where['cate_id'];
  303. }
  304. }
  305. unset($where['cate_id']);
  306. $list = $this->service->searchList($where);
  307. return app('json')->success($list);
  308. }
  309. /**
  310. * 获取某个商品规格
  311. * @return mixed
  312. */
  313. public function get_attrs()
  314. {
  315. [$id, $type] = $this->request->getMore([
  316. [['id', 'd'], 0],
  317. [['type', 'd'], 0],
  318. ], true);
  319. $info = $this->service->getProductRules($id, $type);
  320. return app('json')->success(compact('info'));
  321. }
  322. /**
  323. * 获取运费模板列表
  324. * @return mixed
  325. */
  326. public function get_template()
  327. {
  328. return app('json')->success($this->service->getTemp());
  329. }
  330. /**
  331. * 获取视频上传token
  332. * @return mixed
  333. * @throws \Exception
  334. */
  335. public function getTempKeys(Request $request)
  336. {
  337. $upload = UploadService::init();
  338. $type = (int)sys_config('upload_type', 1);
  339. $key = $request->get('key', '');
  340. $path = $request->get('path', '');
  341. $contentType = $request->get('contentType', '');
  342. if ($type === 5) {
  343. if (!$key || !$contentType) {
  344. return app('json')->fail('缺少参数');
  345. }
  346. $re = $upload->getTempKeys($key, $path, $contentType);
  347. } else {
  348. $re = $upload->getTempKeys();
  349. }
  350. return $re ? app('json')->success($re) : app('json')->fail(100016);
  351. }
  352. /**
  353. * 检测商品是否开活动
  354. * @param $id
  355. * @throws \think\db\exception\DataNotFoundException
  356. * @throws \think\db\exception\DbException
  357. * @throws \think\db\exception\ModelNotFoundException
  358. */
  359. public function check_activity($id)
  360. {
  361. $this->service->checkActivity($id);
  362. return app('json')->success(100002);
  363. }
  364. /**
  365. * 导入卡密
  366. * @return mixed
  367. * @throws \PhpOffice\PhpSpreadsheet\Reader\Exception
  368. */
  369. public function import_card()
  370. {
  371. $data = $this->request->getMore([
  372. ['file', ""]
  373. ]);
  374. if (!$data['file']) return app('json')->fail(400168);
  375. $file = public_path() . substr($data['file'], 1);
  376. /** @var FileService $readExcelService */
  377. $readExcelService = app()->make(FileService::class);
  378. $cardData = $readExcelService->readExcel($file, 'card');
  379. return app('json')->success($cardData);
  380. }
  381. /**
  382. * 商品批量设置
  383. * @return mixed
  384. */
  385. public function batchSetting()
  386. {
  387. $data = $this->request->postMore([
  388. ['ids', []],
  389. ['cate_id', []],
  390. ['logistics', []],
  391. ['freight', 2],
  392. ['postage', 0],
  393. ['temp_id', 1],
  394. ['give_integral', 0],
  395. ['coupon_ids', []],
  396. ['label_id', []],
  397. ['recommend', []],
  398. ['type', 0]
  399. ]);
  400. $this->service->batchSetting($data);
  401. return app('json')->success(100014);
  402. }
  403. }