StoreProductController.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\controller\api\v1\product;
  12. use app\Request;
  13. use app\services\activity\coupon\StoreCouponIssueServices;
  14. use app\services\activity\discounts\StoreDiscountsServices;
  15. use app\services\activity\promotions\StorePromotionsServices;
  16. use app\services\diy\DiyServices;
  17. use app\services\other\QrcodeServices;
  18. use app\services\product\category\StoreProductCategoryServices;
  19. use app\services\product\product\StoreProductReplyCommentServices;
  20. use app\services\product\product\StoreProductReplyServices;
  21. use app\services\product\product\StoreProductServices;
  22. use app\services\system\attachment\SystemAttachmentServices;
  23. use app\services\user\UserServices;
  24. use think\db\exception\DataNotFoundException;
  25. use think\db\exception\DbException;
  26. use think\db\exception\ModelNotFoundException;
  27. /**
  28. * 商品类
  29. * Class StoreProductController
  30. * @package app\controller\api\store
  31. */
  32. class StoreProductController
  33. {
  34. /**
  35. * 商品services
  36. * @var StoreProductServices
  37. */
  38. protected $services;
  39. public function __construct(StoreProductServices $services)
  40. {
  41. $this->services = $services;
  42. }
  43. /**
  44. * 商品列表
  45. * @param Request $request
  46. * @return mixed
  47. */
  48. public function lst(Request $request, StoreProductCategoryServices $services)
  49. {
  50. $where = $request->getMore([
  51. [['sid', 'd'], 0],
  52. [['cid', 'd'], 0],
  53. ['keyword', '', '', 'store_name'],
  54. ['priceOrder', ''],
  55. ['salesOrder', ''],
  56. [['news', 'd'], 0, '', 'is_new'],
  57. [['type', ''], '', '', 'status'],
  58. ['ids', ''],
  59. [['selectId', 'd'], ''],
  60. ['productId', ''],
  61. ['brand_id', ''],
  62. ['promotions_id', 0],
  63. [['collate_code_id', 'd'], 0], //拼单ID 、桌码ID
  64. ['store_label_id', ''],//商品标签
  65. ]);
  66. if ($where['selectId'] && (!$where['sid'] || !$where['cid'])) {
  67. if ($services->value(['id' => $where['selectId']], 'pid')) {
  68. $where['sid'] = $where['selectId'];
  69. } else {
  70. $where['cid'] = $where['selectId'];
  71. }
  72. }
  73. $where['ids'] = stringToArray($where['ids']);
  74. $where['store_label_id'] = stringToArray($where['store_label_id']);
  75. $where['brand_id'] = stringToArray($where['brand_id']);
  76. $type = 'mid';
  77. $field = ['image', 'recommend_image'];
  78. $where['type'] = [0, 2];
  79. if ($where['store_name']) {//搜索
  80. $field = ['image'];
  81. $where['type'] = [];
  82. $where['pid'] = 0;
  83. }
  84. $where['show_type'] = [0, 1];
  85. $list = $this->services->getGoodsList($where, (int)$request->uid());
  86. return app('json')->successful(get_thumb_water($list, $type, $field));
  87. }
  88. /**
  89. * 搜索获取商品品牌列表
  90. * @param Request $request
  91. * @param StoreProductCategoryServices $services
  92. * @return mixed
  93. */
  94. public function brand(Request $request, StoreProductCategoryServices $services)
  95. {
  96. $where = $request->getMore([
  97. [['sid', 'd'], 0],
  98. [['cid', 'd'], 0],
  99. ['selectId', '']
  100. ]);
  101. if ($where['selectId'] && (!$where['sid'] || !$where['cid'])) {
  102. if ($services->value(['id' => $where['selectId']], 'pid')) {
  103. $where['sid'] = $where['selectId'];
  104. } else {
  105. $where['cid'] = $where['selectId'];
  106. }
  107. }
  108. $cate_id = [];
  109. if ($where['sid']) {
  110. $cate_id = [$where['sid']];
  111. } elseif ($where['cid']) {
  112. $cate_id = array_merge([$where['cid']], $services->getColumn(['pid' => $where['cid'], 'is_show' => 1], 'id'));
  113. }
  114. return app('json')->successful($this->services->getBrandList(['cate_id' => $cate_id]));
  115. }
  116. /**
  117. * 商品分享二维码 推广员
  118. * @param Request $request
  119. * @param $id
  120. * @return mixed
  121. */
  122. public function code(Request $request, $id)
  123. {
  124. $id = (int)$id;
  125. /** @var SystemAttachmentServices $attach */
  126. $attach = app()->make(SystemAttachmentServices::class);
  127. if (!$id || !$this->services->isValidProduct($id)) {
  128. return app('json')->fail('商品不存在或已下架');
  129. }
  130. $userType = $request->get('user_type', 'wechat');
  131. $user = $request->user();
  132. try {
  133. switch ($userType) {
  134. case 'wechat':
  135. //公众号
  136. $name = $id . '_product_detail_' . $user['uid'] . '_is_promoter_' . $user['is_promoter'] . '_wap.jpg';
  137. /** @var QrcodeServices $qrcodeService */
  138. $qrcodeService = app()->make(QrcodeServices::class);
  139. if (sys_config('share_qrcode', 0) && request()->isWechat()) {
  140. $url = $qrcodeService->getTemporaryQrcode('product-' . $id, $user['uid'])->url;
  141. } else {
  142. $url = $qrcodeService->getWechatQrcodePath($name, '/pages/goods_details/index?id=' . $id . '&spread=' . $user['uid']);
  143. }
  144. if ($url === false)
  145. return app('json')->fail('二维码生成失败');
  146. else {
  147. // $codeTmp = $code = $url ? image_to_base64($url) : false;
  148. // if (!$codeTmp) {
  149. // $putCodeUrl = put_image($url);
  150. // $code = $putCodeUrl ? image_to_base64(app()->request->domain(true) . '/' . $putCodeUrl) : false;
  151. // $code ?? unlink(public_path() . $putCodeUrl);
  152. // }
  153. return app('json')->successful(['code' => $url]);
  154. }
  155. break;
  156. case 'routine':
  157. /** @var QrcodeServices $qrcodeService */
  158. $qrcodeService = app()->make(QrcodeServices::class);
  159. $url = $qrcodeService->getRoutineQrcodePath($id, $user['uid'], 0, ['is_promoter' => $user['is_promoter']]);
  160. if ($url === false)
  161. return app('json')->fail('二维码生成失败');
  162. else
  163. return app('json')->successful(['code' => $url]);
  164. }
  165. } catch (\Exception $e) {
  166. return app('json')->fail($e->getMessage(), [
  167. 'code' => $e->getCode(),
  168. 'line' => $e->getLine(),
  169. 'message' => $e->getMessage()
  170. ]);
  171. }
  172. }
  173. /**
  174. * 获取商品的详情内容
  175. * @param $id
  176. * @return mixed
  177. * @throws DataNotFoundException
  178. * @throws DbException
  179. * @throws ModelNotFoundException
  180. * @author 等风来
  181. * @email 136327134@qq.com
  182. * @date 2022/11/28
  183. */
  184. public function detailContent($id)
  185. {
  186. $productInfo = $this->services->getCacheProductInfo($id);
  187. return app('json')->success(['description' => $productInfo['description'] ?? '']);
  188. }
  189. /**
  190. * 商品详情
  191. * @param Request $request
  192. * @param $id
  193. * @param int $type
  194. * @return mixed
  195. */
  196. public function detail(Request $request, $id, $type = 0)
  197. {
  198. [$promotions_type] = $request->getMore([
  199. [['promotions_type', 'd'], 0]
  200. ], true);
  201. $uid = 0;
  202. if ($request->hasMacro('uid')) $uid = (int)$request->uid();
  203. $data = $this->services->productDetail($uid, (int)$id, (int)$type, (int)$promotions_type);
  204. return app('json')->successful($data);
  205. }
  206. /**
  207. * 推荐商品列表
  208. * @param Request $request
  209. * @param DiyServices $diyServices
  210. * @param $id
  211. * @return mixed
  212. * @throws DataNotFoundException
  213. * @throws DbException
  214. * @throws ModelNotFoundException
  215. */
  216. public function recommend(Request $request, DiyServices $diyServices, $id)
  217. {
  218. $list = [];
  219. $diy = $diyServices->getProductDetailDiy();
  220. //推荐开启
  221. if (isset($diy['is_recommend']) && $diy['is_recommend']) {
  222. $num = (int)($diy['recommend_num'] ?? 12);
  223. $storeInfo = [];
  224. if ($id) {
  225. $storeInfo = $this->services->getCacheProductInfo($id);
  226. }
  227. $uid = $request->uid();
  228. $where = [];
  229. $where['is_vip_product'] = 0;
  230. $where['is_verify'] = 1;
  231. $where['pid'] = 0;
  232. if ($uid) {
  233. /** @var UserServices $userServices */
  234. $userServices = app()->make(UserServices::class);
  235. $is_vip = $userServices->value(['uid' => $uid], 'is_money_level');
  236. $where['is_vip_product'] = $is_vip ? -1 : 0;
  237. }
  238. //推荐
  239. if (isset($storeInfo['recommend_list']) && $storeInfo['recommend_list']) {
  240. $recommend_list = explode(',', $storeInfo['recommend_list']);
  241. $list = get_thumb_water($this->services->getProducts(['ids' => $recommend_list, 'is_del' => 0, 'is_show' => 1] + $where, '', $num, ['couponId']));
  242. } else {
  243. $list = get_thumb_water($this->services->getProducts(['store_label_id' => 5, 'is_del' => 0, 'is_show' => 1] + $where, 'rand()', $num, ['couponId']));
  244. }
  245. }
  246. return app('json')->success($list);
  247. }
  248. /**
  249. * 获取商品关联活动信息
  250. * @param Request $request
  251. * @param StoreCouponIssueServices $couponIssueServices
  252. * @param StoreDiscountsServices $storeDiscountsServices
  253. * @param StorePromotionsServices $storePromotionsServices
  254. * @param DiyServices $diyServices
  255. * @param $id
  256. * @return mixed
  257. * @throws DataNotFoundException
  258. * @throws DbException
  259. * @throws ModelNotFoundException
  260. */
  261. public function activity(Request $request, StoreCouponIssueServices $couponIssueServices, StoreDiscountsServices $storeDiscountsServices, StorePromotionsServices $storePromotionsServices, DiyServices $diyServices, $id)
  262. {
  263. [$promotions_type] = $request->getMore([
  264. [['promotions_type', 'd'], 0]
  265. ], true);
  266. $storeInfo = [];
  267. $id = (int)$id;
  268. if ($id) {
  269. $storeInfo = $this->services->getCacheProductInfo($id);
  270. }
  271. $uid = (int)$request->uid();
  272. $data = ['activity' => [], 'coupons' => [], 'discounts_products' => [], 'promotions' => [], 'activity_background' => []];
  273. if ($storeInfo) {
  274. $diy = $diyServices->getProductDetailDiy();
  275. if (isset($diy['is_activity']) && $diy['is_activity'] && !($storeInfo['type'] == 1 && !$storeInfo['pid']))
  276. $data['activity'] = $this->services->getActivityList($storeInfo, false);
  277. if (isset($diy['is_coupon']) && $diy['is_coupon'] && !($storeInfo['type'] == 1 && !$storeInfo['pid']))
  278. $data['coupons'] = $couponIssueServices->getProductCouponList($uid, $id, 'id,coupon_type,coupon_title,coupon_price,use_min_price,start_time,end_time,applicable_type,applicable_store_id', 3);
  279. if (isset($diy['is_discounts']) && $diy['is_discounts']) {
  280. $num = (int)($diy['discounts_num'] ?? 2);
  281. $data['discounts_products'] = $storeDiscountsServices->getDiscounts($id, $uid, $num);
  282. }
  283. if (isset($diy['is_promotions']) && $diy['is_promotions'] && !($storeInfo['type'] == 1 && !$storeInfo['pid'])) {
  284. $promotions_type = $promotions_type ? [(int)$promotions_type] : [1, 2, 3, 4, 6];
  285. $ids = $storeInfo['pid'] ? [$storeInfo['pid']] : [$id];
  286. $store_id = 0;
  287. if ($storeInfo['type'] == 1) {
  288. $store_id = (int)($storeInfo['relation_id'] ?? 0);
  289. }
  290. [$promotions, $productRelation] = $storePromotionsServices->getProductsPromotions($ids, $promotions_type, '*', [], 'promotions_type', $store_id);
  291. $data['promotions'] = $promotions;
  292. if ($data['promotions']) {
  293. foreach ($data['promotions'] as $key => $item) {
  294. if ($item['promotions_type'] == 6) {
  295. $data['activity_background'] = [
  296. 'id' => $item['id'],
  297. 'name' => $item['name'],
  298. 'image' => $item['image'],
  299. ];
  300. unset($data['promotions'][$key]);
  301. }
  302. }
  303. $data['promotions'] = array_merge($data['promotions']);
  304. }
  305. }
  306. }
  307. return app('json')->success($data);
  308. }
  309. /**
  310. * 为你推荐
  311. * @param Request $request
  312. * @param UserServices $userServices
  313. * @return mixed
  314. */
  315. public function product_hot(Request $request, UserServices $userServices)
  316. {
  317. $is_vip = $userServices->value(['uid' => $request->uid()], 'is_money_level');
  318. $vip_user = $is_vip ? -1 : 0;
  319. $list = $this->services->getProducts(['store_label_id' => 1, 'is_show' => 1, 'is_del' => 0, 'is_verify' => 1, 'is_vip_product' => $vip_user, 'type' => [0, 2]], '', 0, ['couponId']);
  320. return app('json')->success(get_thumb_water($list, 'mid'));
  321. }
  322. /**
  323. * 获取首页推荐不同类型商品的轮播图和商品
  324. * @param Request $request
  325. * @param $type
  326. * @return mixed
  327. * @throws DataNotFoundException
  328. * @throws ModelNotFoundException
  329. * @throws \think\exception\DbException
  330. */
  331. public function groom_list(Request $request, $type)
  332. {
  333. $info['banner'] = [];
  334. $info['list'] = [];
  335. $uid = (int)$request->uid();
  336. $where = [];
  337. if ($type == 1) {// 精品推荐
  338. $info['banner'] = sys_data('routine_home_bast_banner') ?: [];// 首页精品推荐图片
  339. $where['is_best'] = 1;
  340. } else if ($type == 2) {// 热门榜单
  341. $info['banner'] = sys_data('routine_home_hot_banner') ?: [];// 热门榜单 猜你喜欢推荐图片
  342. $where['is_hot'] = 1;
  343. } else if ($type == 3) {// 首发新品
  344. $info['banner'] = sys_data('routine_home_new_banner') ?: [];// 首发新品推荐图片
  345. $where['is_new'] = 1;
  346. } else if ($type == 4) {// 促销单品
  347. $info['banner'] = sys_data('routine_home_benefit_banner') ?: [];// 促销单品推荐图片
  348. $where['is_benefit'] = 1;
  349. } else if ($type == 5) {// 会员商品
  350. $where = [
  351. ['vip_price', '>', 0],
  352. ['is_vip', '=', 1],
  353. ];
  354. }
  355. $info['list'] = $this->services->getRecommendProduct($uid, $where);
  356. return app('json')->successful($info);
  357. }
  358. /**
  359. * 商品评价数量和好评度
  360. * @param $id
  361. * @return mixed
  362. */
  363. public function reply_config($id)
  364. {
  365. /** @var StoreProductReplyServices $replyService */
  366. $replyService = app()->make(StoreProductReplyServices::class);
  367. $count = $replyService->productReplyCount((int)$id);
  368. return app('json')->successful($count);
  369. }
  370. /**
  371. * 获取商品评论
  372. * @param Request $request
  373. * @param $id
  374. * @param $type
  375. * @return mixed
  376. */
  377. public function reply_list(Request $request, $id)
  378. {
  379. [$type] = $request->getMore([
  380. [['type', 'd'], 0]
  381. ], true);
  382. /** @var StoreProductReplyServices $replyService */
  383. $replyService = app()->make(StoreProductReplyServices::class);
  384. $list = $replyService->getProductReplyList((int)$id, (int)$type);
  385. return app('json')->successful(get_thumb_water($list, 'small', ['pics']));
  386. }
  387. /**
  388. * 评价详情
  389. * @param StoreProductReplyServices $services
  390. * @param Request $request
  391. * @param $id
  392. * @return mixed
  393. * @throws DataNotFoundException
  394. * @throws DbException
  395. * @throws ModelNotFoundException
  396. */
  397. public function replyInfo(StoreProductReplyServices $services, Request $request, $id)
  398. {
  399. if (!$id) {
  400. return app('json')->fail('缺少参数');
  401. }
  402. return app('json')->success($services->getReplyInfo((int)$id, $request->uid()));
  403. }
  404. /**
  405. * 评论回复列表
  406. * @param StoreProductReplyCommentServices $services
  407. * @param Request $request
  408. * @param $id 评论id
  409. * @return mixed
  410. * @throws DataNotFoundException
  411. * @throws DbException
  412. * @throws ModelNotFoundException
  413. */
  414. public function commentList(StoreProductReplyCommentServices $services, Request $request, $id)
  415. {
  416. if (!$id) {
  417. return app('json')->fail('缺少参数');
  418. }
  419. $data = $services->getReplCommenList((int)$id, '', $request->uid(), false);
  420. return app('json')->success($data['list']);
  421. }
  422. /**
  423. * 保存评论回复
  424. * @param Request $request
  425. * @param StoreProductReplyCommentServices $services
  426. * @param $id
  427. * @return mixed
  428. */
  429. public function replyComment(Request $request, StoreProductReplyCommentServices $services, $id)
  430. {
  431. [$content] = $request->postMore([
  432. ['content', '']
  433. ], true);
  434. if (!$id) {
  435. return app('json')->fail('缺少参数');
  436. }
  437. if (!$content) {
  438. return app('json')->fail('缺少回复内容');
  439. }
  440. $services->saveComment($request->uid(), (int)$id, $content);
  441. return app('json')->success('回复成功');
  442. }
  443. /**
  444. * 回复点赞
  445. * @param Request $request
  446. * @param StoreProductReplyCommentServices $services
  447. * @param $id
  448. * @return mixed
  449. * @throws DataNotFoundException
  450. * @throws DbException
  451. * @throws ModelNotFoundException
  452. */
  453. public function commentPraise(Request $request, StoreProductReplyCommentServices $services, $id)
  454. {
  455. if (!$id) {
  456. return app('json')->fail('缺少参数');
  457. }
  458. if ($services->commentPraise((int)$id, $request->uid())) {
  459. return app('json')->success('点赞成功');
  460. } else {
  461. return app('json')->fail('点赞失败');
  462. }
  463. }
  464. /**
  465. * 取消回复点赞
  466. * @param Request $request
  467. * @param StoreProductReplyCommentServices $services
  468. * @param $id
  469. * @return mixed
  470. * @throws DataNotFoundException
  471. * @throws DbException
  472. * @throws ModelNotFoundException
  473. */
  474. public function unCommentPraise(Request $request, StoreProductReplyCommentServices $services, $id)
  475. {
  476. if (!$id) {
  477. return app('json')->fail('缺少参数');
  478. }
  479. if ($services->unCommentPraise((int)$id, $request->uid())) {
  480. return app('json')->success('取消点赞成功');
  481. } else {
  482. return app('json')->fail('取消点赞失败');
  483. }
  484. }
  485. /**
  486. * 点赞
  487. * @param Request $request
  488. * @param StoreProductReplyServices $services
  489. * @param $id
  490. * @return mixed
  491. * @throws DataNotFoundException
  492. * @throws DbException
  493. * @throws ModelNotFoundException
  494. */
  495. public function replyPraise(Request $request, StoreProductReplyServices $services, $id)
  496. {
  497. if (!$id) {
  498. return app('json')->fail('缺少参数');
  499. }
  500. if ($services->replyPraise((int)$id, $request->uid())) {
  501. return app('json')->success('点赞成功');
  502. } else {
  503. return app('json')->fail('点赞失败');
  504. }
  505. }
  506. /**
  507. * 取消点赞
  508. * @param Request $request
  509. * @param StoreProductReplyServices $services
  510. * @param $id
  511. * @return mixed
  512. * @throws DataNotFoundException
  513. * @throws DbException
  514. * @throws ModelNotFoundException
  515. */
  516. public function unReplyPraise(Request $request, StoreProductReplyServices $services, $id)
  517. {
  518. if (!$id) {
  519. return app('json')->fail('缺少参数');
  520. }
  521. if ($services->unReplyPraise((int)$id, $request->uid())) {
  522. return app('json')->success('取消点赞成功');
  523. } else {
  524. return app('json')->fail('取消点赞失败');
  525. }
  526. }
  527. /**
  528. * 记录评价浏览量
  529. * @param StoreProductReplyServices $services
  530. * @param $id
  531. * @return mixed
  532. */
  533. public function replyViewNum(StoreProductReplyServices $services, $id)
  534. {
  535. if (!$id) {
  536. return app('json')->fail('缺少参数');
  537. }
  538. if ($services->incUpdate($id, 'views_num')) {
  539. return app('json')->success('更新成功');
  540. } else {
  541. return app('json')->fail('更新失败');
  542. }
  543. }
  544. /**
  545. * 获取预售列表
  546. * @param Request $request
  547. * @return mixed
  548. * @throws DataNotFoundException
  549. * @throws DbException
  550. * @throws ModelNotFoundException
  551. */
  552. public function presaleList(Request $request)
  553. {
  554. $where = $request->getMore([
  555. [['time_type', 'd'], 0]
  556. ]);
  557. $uid = (int)$request->uid();
  558. $where['type'] = [0, 2];
  559. return app('json')->successful($this->services->getPresaleList($uid, $where));
  560. }
  561. }