StoreProductController.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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\api\controller\v1\store;
  12. use app\Request;
  13. use app\services\order\StoreOrderCartInfoServices;
  14. use app\services\product\product\StoreCategoryServices;
  15. use app\services\product\product\StoreProductReplyServices;
  16. use app\services\product\product\StoreProductServices;
  17. use app\services\user\UserServices;
  18. use think\db\exception\DataNotFoundException;
  19. use think\db\exception\DbException;
  20. use think\db\exception\ModelNotFoundException;
  21. /**
  22. * 商品类
  23. * Class StoreProductController
  24. * @package app\api\controller\store
  25. */
  26. class StoreProductController
  27. {
  28. /**
  29. * 商品services
  30. * @var StoreProductServices
  31. */
  32. protected $services;
  33. public function __construct(StoreProductServices $services)
  34. {
  35. $this->services = $services;
  36. }
  37. /**
  38. * 商品列表
  39. * @param Request $request
  40. * @param StoreCategoryServices $services
  41. * @return mixed
  42. * @throws DataNotFoundException
  43. * @throws DbException
  44. * @throws ModelNotFoundException
  45. */
  46. public function lst(Request $request, StoreCategoryServices $services)
  47. {
  48. $where = $request->getMore([
  49. [['sid', 'd'], 0],
  50. [['cid', 'd'], 0],
  51. ['keyword', '', '', 'store_name'],
  52. ['priceOrder', ''],
  53. ['salesOrder', ''],
  54. [['news', 'd'], 0, '', 'is_new'],
  55. [['type', 0], 0],
  56. ['ids', ''],
  57. ['selectId', ''],
  58. ['productId', ''],
  59. ['coupon_category_id', ''],
  60. ['is_gift',0], //是否礼包商品
  61. ['is_repeat',0], //是否复购商品
  62. ['is_free',0], //是否50元兑换商品
  63. ]);
  64. if ($where['selectId'] && (!$where['sid'] || !$where['cid'])) {
  65. if ($services->value(['id' => $where['selectId']], 'pid')) {
  66. $where['sid'] = $where['selectId'];
  67. } else {
  68. $where['cid'] = $where['selectId'];
  69. }
  70. }
  71. if ($where['ids'] && is_string($where['ids'])) {
  72. $where['ids'] = explode(',', $where['ids']);
  73. foreach ($where['ids'] as $key => &$item) {
  74. $where['ids'][$key] = (int)$item;
  75. if ($where['ids'][$key] == 0) unset($where['ids'][$key]);
  76. }
  77. }
  78. if (!$where['ids']) {
  79. unset($where['ids']);
  80. }
  81. $type = 'big';
  82. $field = ['image', 'recommend_image'];
  83. $list = $this->services->getGoodsList($where, (int)$request->uid());
  84. return app('json')->success(get_thumb_water($list, $type, $field));
  85. }
  86. /**
  87. * 商品分享二维码 推广员
  88. * @param Request $request
  89. * @param $id
  90. * @return mixed
  91. */
  92. public function code(Request $request, $id)
  93. {
  94. $code = $this->services->getCode((int)$id, $request->get('user_type', 'wechat'), $request->user());
  95. return app('json')->success(['code' => $code]);
  96. }
  97. /**
  98. * 商品详情
  99. * @param Request $request
  100. * @param $id
  101. * @param int $type
  102. * @return mixed
  103. * @throws DataNotFoundException
  104. * @throws DbException
  105. * @throws ModelNotFoundException
  106. */
  107. public function detail(Request $request, $id, $type = 0)
  108. {
  109. $data = $this->services->productDetail($request, (int)$id, (int)$type);
  110. return app('json')->success($data);
  111. }
  112. /**
  113. * 为你推荐
  114. * @param Request $request
  115. * @return mixed
  116. * @throws DataNotFoundException
  117. * @throws DbException
  118. * @throws ModelNotFoundException
  119. */
  120. public function product_hot(Request $request)
  121. {
  122. $vip_user = $request->uid() ? app()->make(UserServices::class)->value(['uid' => $request->uid()], 'is_money_level') : 0;
  123. $list = $this->services->getProducts(['is_hot' => 1, 'is_show' => 1, 'is_del' => 0, 'vip_user' => $vip_user]);
  124. return app('json')->success(get_thumb_water($list, 'mid'));
  125. }
  126. /**
  127. * 获取首页推荐不同类型商品的轮播图和商品
  128. * @param Request $request
  129. * @param $type
  130. * @return mixed
  131. * @throws DataNotFoundException
  132. * @throws DbException
  133. * @throws ModelNotFoundException
  134. */
  135. public function groom_list(Request $request, $type)
  136. {
  137. $info['banner'] = [];
  138. $info['list'] = [];
  139. if ($type == 1) {//TODO 精品推荐
  140. $info['banner'] = sys_data('routine_home_bast_banner') ?: [];//TODO 首页精品推荐图片
  141. $info['list'] = $this->services->getRecommendProduct($request->uid(), 'is_best');//TODO 精品推荐个数
  142. } else if ($type == 2) {//TODO 热门榜单
  143. $info['banner'] = sys_data('routine_home_hot_banner') ?: [];//TODO 热门榜单 猜你喜欢推荐图片
  144. $info['list'] = $this->services->getRecommendProduct($request->uid(), 'is_hot');//TODO 热门榜单 猜你喜欢
  145. } else if ($type == 3) {//TODO 首发新品
  146. $info['banner'] = sys_data('routine_home_new_banner') ?: [];//TODO 首发新品推荐图片
  147. $info['list'] = $this->services->getRecommendProduct($request->uid(), 'is_new');//TODO 首发新品
  148. } else if ($type == 4) {//TODO 促销单品
  149. $info['banner'] = sys_data('routine_home_benefit_banner') ?: [];//TODO 促销单品推荐图片
  150. $info['list'] = $this->services->getRecommendProduct($request->uid(), 'is_benefit');//TODO 促销单品
  151. } else if ($type == 5) {//TODO 会员商品
  152. $info['list'] = $this->services->getRecommendProduct($request->uid(), 'is_vip');//TODO 会员商品
  153. }
  154. return app('json')->success($info);
  155. }
  156. /**
  157. * 商品评价数量和好评度
  158. * @param $id
  159. * @return mixed
  160. */
  161. public function reply_config($id)
  162. {
  163. /** @var StoreProductReplyServices $replyService */
  164. $replyService = app()->make(StoreProductReplyServices::class);
  165. $count = $replyService->productReplyCount($id);
  166. return app('json')->success($count);
  167. }
  168. /**
  169. * 获取商品评论
  170. * @param Request $request
  171. * @param $id
  172. * @return mixed
  173. * @throws DataNotFoundException
  174. * @throws DbException
  175. * @throws ModelNotFoundException
  176. */
  177. public function reply_list(Request $request, $id)
  178. {
  179. [$type] = $request->getMore([
  180. [['type', 'd'], 0]
  181. ], true);
  182. /** @var StoreProductReplyServices $replyService */
  183. $replyService = app()->make(StoreProductReplyServices::class);
  184. $list = $replyService->getProductReplyList($id, $type);
  185. return app('json')->success(get_thumb_water($list, 'big', ['pics']));
  186. }
  187. /**
  188. * 获取预售列表
  189. * @param Request $request
  190. * @return mixed
  191. */
  192. public function advanceList(Request $request)
  193. {
  194. $where = $request->getMore([
  195. [['time_type', 'd'], 0]
  196. ]);
  197. return app('json')->success($this->services->getAdvanceList($where));
  198. }
  199. /**
  200. * 分享礼包商品时检测是否购买过该礼包商品
  201. * @param Request $request
  202. * @return mixed
  203. */
  204. public function gift_check(Request $request, $id)
  205. {
  206. if (!$id) {
  207. return app('json')->fail(100100);
  208. }
  209. // 检查该商品是否是礼包商品
  210. $is_gift = $this->services->value(['id' => $id], 'is_gift');
  211. if ($is_gift != 1) {
  212. return app('json')->success('true');
  213. }
  214. // 检查用户是否购买过该礼包商品
  215. /** @var StoreOrderCartInfoServices $cartInfoServices */
  216. $cartInfoServices = app()->make(StoreOrderCartInfoServices::class);
  217. $hasPurchased = $cartInfoServices->hasUserPurchasedProduct($request->uid(), $id);
  218. if (!$hasPurchased) {
  219. return app('json')->fail('购买过该礼包商品后才能分享');
  220. }
  221. return app('json')->success('true');
  222. }
  223. }