StoreProduct.php 12 KB

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