StoreProductController.php 8.2 KB

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