StoreProductController.php 20 KB

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