Goods.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <?php
  2. namespace addons\shopro\controller\goods;
  3. use addons\shopro\controller\Common;
  4. use addons\shopro\service\goods\GoodsService;
  5. use app\admin\model\shopro\user\GoodsLog;
  6. use app\admin\model\shopro\activity\Activity;
  7. class Goods extends Common
  8. {
  9. protected $noNeedLogin = ['*'];
  10. protected $noNeedRight = ['*'];
  11. public function index()
  12. {
  13. $params = $this->request->param();
  14. $keyword = $params['keyword'] ?? '';
  15. $ids = $params['ids'] ?? '';
  16. $category_id = $params['category_id'] ?? '';
  17. $is_category_deep = $params['is_category_deep'] ?? true;
  18. $sort = $params['sort'] ?? 'weigh';
  19. $order = $params['order'] ?? 'desc';
  20. $service = new GoodsService(function ($goods) {
  21. $goods->activities = $goods->activities;
  22. $goods->promos = $goods->promos;
  23. return $goods;
  24. });
  25. /*$service->up()->with(['max_sku_price' => function ($query) { // 计算价格区间用(不知道为啥 with 必须再 show 后面)
  26. $query->where('status', 'up');
  27. }]);*/
  28. $service->up();
  29. if ($keyword) {
  30. $service->search($keyword);
  31. }
  32. if ($ids) {
  33. $service->whereIds($ids);
  34. }
  35. if ($category_id) {
  36. $service->category($category_id, $is_category_deep);
  37. }
  38. if ($sort) {
  39. $service->order($sort, $order);
  40. }
  41. // $goods = $service->paginate();
  42. $goods = $service->select_autopage();
  43. $this->success('获取成功', $goods);
  44. }
  45. /**
  46. * 通过 ids 获取商品(不分页)
  47. *
  48. * @return void
  49. */
  50. public function ids()
  51. {
  52. $params = $this->request->param();
  53. $ids = $params['ids'] ?? '';
  54. $service = new GoodsService(function ($goods) {
  55. $goods->activities = $goods->activities;
  56. $goods->promos = $goods->promos;
  57. return $goods;
  58. });
  59. $service->show()->with(['max_sku_price' => function ($query) {
  60. $query->where('status', 'up');
  61. }]);
  62. if ($ids) {
  63. $service->whereIds($ids);
  64. }
  65. $goods = $service->select();
  66. $this->success('获取成功', $goods);
  67. }
  68. public function detail()
  69. {
  70. $user = auth_user();
  71. $id = $this->request->param('id');
  72. $activity_id = $this->request->param('activity_id');
  73. // 存一下,获取器获取指定活动的时候会用到
  74. session('goods-activity_id:' . $id, $activity_id);
  75. $service = new GoodsService(function ($goods, $service) use ($activity_id) {
  76. $goods->service = $goods->service;
  77. $goods->skus = $goods->skus;
  78. if (!$activity_id) {
  79. $goods->activities = $goods->activities;
  80. $goods->promos = $goods->promos;
  81. } else {
  82. $goods->activity = $goods->activity;
  83. $goods->original_goods_price = $goods->original_goods_price;
  84. }
  85. return $goods;
  86. });
  87. $goods = $service->show()->activity($activity_id)->with(['max_sku_price' => function ($query) { // 计算价格区间用(不知道为啥 with 必须再 show 后面)
  88. $query->where('status', 'up');
  89. }, 'favorite'])->where('id', $id)->find();
  90. if (!$goods) {
  91. $this->error(__('No Results were found'));
  92. }
  93. // 添加浏览记录
  94. GoodsLog::addView($user, $goods);
  95. // 处理商品规格
  96. $skuPrices = $goods['new_sku_prices'];
  97. $content = $goods['content'];
  98. $goods = $goods->toArray();
  99. $goods['images'] = array_domain_image($goods['images']);
  100. $goods['sku_prices'] = $skuPrices;
  101. $goods['content'] = $content;
  102. unset($goods['new_sku_prices']);
  103. $this->success('获取成功', $goods);
  104. }
  105. /**
  106. * 获取指定活动相关商品
  107. *
  108. * @return void
  109. */
  110. public function activity()
  111. {
  112. $activity_id = $this->request->param('activity_id');
  113. $need_buyers = $this->request->param('need_buyers', 0); // 需要查询哪些人在参与活动
  114. $activity = Activity::where('id', $activity_id)->find();
  115. if (!$activity) {
  116. $this->error(__('No Results were found'));
  117. }
  118. $goodsIds = $activity->goods_ids ? explode(',', $activity->goods_ids) : [];
  119. // 存一下,获取器获取指定活动的时候会用到
  120. foreach ($goodsIds as $id) {
  121. session('goods-activity_id:' . $id, $activity_id);
  122. }
  123. $service = new GoodsService(function ($goods) use ($need_buyers) {
  124. if ($need_buyers) {
  125. $goods->buyers = $goods->buyers;
  126. }
  127. $goods->activity = $goods->activity;
  128. return $goods;
  129. });
  130. $goods = $service->activity($activity_id)->whereIds($goodsIds)->show()->order('weigh', 'desc')->select();
  131. $goods = collection($goods)->toArray();
  132. foreach ($goods as &$gd) {
  133. unset($gd['new_sku_prices'], $gd['activity']);
  134. }
  135. $this->success('获取成功', $goods);
  136. }
  137. /**
  138. * 获取指定活动相关商品,带分页
  139. *
  140. * @param Request $request
  141. * @return void
  142. */
  143. public function activityList()
  144. {
  145. $activity_id = $this->request->param('activity_id');
  146. $activity = Activity::where('id', $activity_id)->find();
  147. if (!$activity) {
  148. $this->error(__('No Results were found'));
  149. }
  150. $goodsIds = $activity->goods_ids ? explode(',', $activity->goods_ids) : [];
  151. // 存一下,获取器获取指定活动的时候会用到
  152. foreach ($goodsIds as $id) {
  153. session('goods-activity_id:' . $id, $activity_id);
  154. }
  155. $service = new GoodsService(function ($goods) {
  156. $goods->promos = $goods->promos;
  157. return $goods;
  158. });
  159. $goods = $service->activity($activity_id)->whereIds($goodsIds)->show()->order('weigh', 'desc')->paginate();
  160. $this->success('获取成功', $goods);
  161. }
  162. }