Goods.php 5.9 KB

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