StoreProduct.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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. ['type',0]
  126. ]);
  127. if ($where['type'] == 1){
  128. $where['is_gift'] = 1;
  129. }else if ($where['type'] == 2){
  130. $where['is_repeat'] = 1;
  131. }else if ($where['type'] == 3){
  132. $where['is_free'] = 1;
  133. }
  134. unset($where['type']);
  135. $data = $this->service->getList($where);
  136. return app('json')->success($data);
  137. }
  138. /**
  139. * 修改状态
  140. * @param string $is_show
  141. * @param string $id
  142. * @return mixed
  143. */
  144. public function set_show($is_show = '', $id = '')
  145. {
  146. $this->service->setShow([$id], $is_show);
  147. return app('json')->success(100014);
  148. }
  149. /**
  150. * 设置批量商品上架
  151. * @return mixed
  152. */
  153. public function product_show()
  154. {
  155. [$ids] = $this->request->postMore([
  156. ['ids', []]
  157. ], true);
  158. $this->service->setShow($ids, 1);
  159. return app('json')->success(100014);
  160. }
  161. /**
  162. * 设置批量商品下架
  163. * @return mixed
  164. */
  165. public function product_unshow()
  166. {
  167. [$ids] = $this->request->postMore([
  168. ['ids', []]
  169. ], true);
  170. $this->service->setShow($ids, 0);
  171. return app('json')->success(100014);
  172. }
  173. /**
  174. * 获取规格模板
  175. * @return mixed
  176. */
  177. public function get_rule()
  178. {
  179. $list = $this->service->getRule();
  180. return app('json')->success($list);
  181. }
  182. /**
  183. * 获取商品详细信息
  184. * @param int $id
  185. * @throws \think\db\exception\DataNotFoundException
  186. * @throws \think\db\exception\DbException
  187. * @throws \think\db\exception\ModelNotFoundException
  188. */
  189. public function get_product_info($id = 0)
  190. {
  191. return app('json')->success($this->service->getInfo((int)$id));
  192. }
  193. /**
  194. * 保存新建或编辑
  195. * @param $id
  196. * @return mixed
  197. * @throws \Exception
  198. */
  199. public function save($id)
  200. {
  201. $data = $this->request->postMore([
  202. ['virtual_type', 0],// 商品类型
  203. ['cate_id', []],//分类id
  204. ['store_name', ''],//商品名称
  205. ['keyword', ''],//关键字
  206. ['unit_name', '件'],//单位
  207. ['store_info', ''],//商品简介
  208. ['slider_image', []],//轮播图
  209. ['video_open', 0],//是否开启视频
  210. ['video_link', ''],//视频链接
  211. ['spec_type', 0],//单多规格
  212. ['items', []],//规格
  213. ['attrs', []],//规格
  214. ['description', ''],//商品详情
  215. ['description_images', []],//商品详情
  216. ['logistics', []],//物流方式
  217. ['freight', 1],//运费设置
  218. ['postage', 0],//邮费
  219. ['temp_id', 0],//运费模版id
  220. ['give_integral', 0],//赠送积分
  221. ['presale', 0],//预售商品开关
  222. ['presale_time', 0],//预售时间
  223. ['presale_day', 0],//预售发货日
  224. ['vip_product', 0],//是否付费会员商品
  225. ['is_sub', []],//佣金是单独还是默认
  226. ['recommend', []],//商品推荐
  227. ['activity', []],//活动优先级
  228. ['recommend_list', []],//优品推荐商品
  229. ['coupon_ids', []],//优惠券
  230. ['label_id', []],//用户标签
  231. ['command_word', ''],//商品口令
  232. ['is_show', 0],//是否上架
  233. ['ficti', 0],//虚拟销量
  234. ['sort', 0],//排序
  235. ['recommend_image', ''],//商品推荐图
  236. ['sales', 0],//销量
  237. ['custom_form', []],//自定义表单
  238. ['type', 0],
  239. ['is_copy', 0],//是否是复制商品
  240. ['is_limit', 0],//是否限购
  241. ['limit_type', 0],//限购类型
  242. ['limit_num', 0],//限购数量
  243. ['min_qty', 1],//起购数量
  244. ['is_gift', 0],//是否礼包商品
  245. ['is_repeat', 0],//是否复购商品
  246. ['is_free', 0],//是否复购商品
  247. ]);
  248. $this->service->save((int)$id, $data);
  249. return app('json')->success(100000);
  250. }
  251. /**
  252. * 删除指定资源
  253. *
  254. * @param int $id
  255. * @return \think\Response
  256. */
  257. public function delete($id)
  258. {
  259. //删除商品检测是否有参与活动
  260. $this->service->checkActivity($id);
  261. $res = $this->service->del($id);
  262. /** @var StoreCartServices $cartService */
  263. $cartService = app()->make(StoreCartServices::class);
  264. $cartService->changeStatus($id, 0);
  265. return app('json')->success($res);
  266. }
  267. /**
  268. * 生成规格列表
  269. * @param int $id
  270. * @param int $type
  271. * @return mixed
  272. */
  273. public function is_format_attr($id = 0, $type = 0)
  274. {
  275. $data = $this->request->postMore([
  276. ['attrs', []],
  277. ['items', []],
  278. ['is_virtual', 0],
  279. ['virtual_type', 0]
  280. ]);
  281. if ($id > 0 && $type == 1) $this->service->checkActivity($id);
  282. $info = $this->service->getAttr($data, $id, $type);
  283. return app('json')->success(compact('info'));
  284. }
  285. /**
  286. * 获取选择的商品列表
  287. * @return mixed
  288. */
  289. public function search_list()
  290. {
  291. $where = $this->request->getMore([
  292. ['cate_id', ''],
  293. ['store_name', ''],
  294. ['type', 1],
  295. ['is_live', 0],
  296. ['is_new', ''],
  297. ['is_virtual', -1],
  298. ['is_presale', -1],
  299. ['is_gift',0],
  300. ['is_repeat',0],
  301. ['is_free',0]
  302. ]);
  303. $where['is_show'] = 1;
  304. $where['is_del'] = 0;
  305. /** @var StoreCategoryServices $storeCategoryServices */
  306. $storeCategoryServices = app()->make(StoreCategoryServices::class);
  307. if ($where['cate_id'] !== '') {
  308. if ($storeCategoryServices->value(['id' => $where['cate_id']], 'pid')) {
  309. $where['sid'] = $where['cate_id'];
  310. } else {
  311. $where['cid'] = $where['cate_id'];
  312. }
  313. }
  314. unset($where['cate_id']);
  315. $list = $this->service->searchList($where);
  316. return app('json')->success($list);
  317. }
  318. /**
  319. * 获取某个商品规格
  320. * @return mixed
  321. */
  322. public function get_attrs()
  323. {
  324. [$id, $type] = $this->request->getMore([
  325. [['id', 'd'], 0],
  326. [['type', 'd'], 0],
  327. ], true);
  328. $info = $this->service->getProductRules($id, $type);
  329. return app('json')->success(compact('info'));
  330. }
  331. /**
  332. * 获取运费模板列表
  333. * @return mixed
  334. */
  335. public function get_template()
  336. {
  337. return app('json')->success($this->service->getTemp());
  338. }
  339. /**
  340. * 获取视频上传token
  341. * @return mixed
  342. * @throws \Exception
  343. */
  344. public function getTempKeys(Request $request)
  345. {
  346. $upload = UploadService::init();
  347. $type = (int)sys_config('upload_type', 1);
  348. $key = $request->get('key', '');
  349. $path = $request->get('path', '');
  350. $contentType = $request->get('contentType', '');
  351. if ($type === 5) {
  352. if (!$key || !$contentType) {
  353. return app('json')->fail('缺少参数');
  354. }
  355. $re = $upload->getTempKeys($key, $path, $contentType);
  356. } else {
  357. $re = $upload->getTempKeys();
  358. }
  359. return $re ? app('json')->success($re) : app('json')->fail(100016);
  360. }
  361. /**
  362. * 检测商品是否开活动
  363. * @param $id
  364. * @throws \think\db\exception\DataNotFoundException
  365. * @throws \think\db\exception\DbException
  366. * @throws \think\db\exception\ModelNotFoundException
  367. */
  368. public function check_activity($id)
  369. {
  370. $this->service->checkActivity($id);
  371. return app('json')->success(100002);
  372. }
  373. /**
  374. * 导入卡密
  375. * @return mixed
  376. * @throws \PhpOffice\PhpSpreadsheet\Reader\Exception
  377. */
  378. public function import_card()
  379. {
  380. $data = $this->request->getMore([
  381. ['file', ""]
  382. ]);
  383. if (!$data['file']) return app('json')->fail(400168);
  384. $file = public_path() . substr($data['file'], 1);
  385. /** @var FileService $readExcelService */
  386. $readExcelService = app()->make(FileService::class);
  387. $cardData = $readExcelService->readExcel($file, 'card');
  388. return app('json')->success($cardData);
  389. }
  390. /**
  391. * 商品批量设置
  392. * @return mixed
  393. */
  394. public function batchSetting()
  395. {
  396. $data = $this->request->postMore([
  397. ['ids', []],
  398. ['cate_id', []],
  399. ['logistics', []],
  400. ['freight', 2],
  401. ['postage', 0],
  402. ['temp_id', 1],
  403. ['give_integral', 0],
  404. ['coupon_ids', []],
  405. ['label_id', []],
  406. ['recommend', []],
  407. ['type', 0]
  408. ]);
  409. $this->service->batchSetting($data);
  410. return app('json')->success(100014);
  411. }
  412. }