Goods.php 6.4 KB

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