Goods.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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. use think\Db;
  8. class Goods extends Common
  9. {
  10. protected $noNeedLogin = ['*'];
  11. protected $noNeedRight = ['*'];
  12. public function index()
  13. {
  14. $params = $this->request->param();
  15. $keyword = $params['keyword'] ?? '';
  16. $ids = $params['ids'] ?? '';
  17. $category_id = $params['category_id'] ?? '';
  18. $is_category_deep = $params['is_category_deep'] ?? true;
  19. $sort = $params['sort'] ?? 'weigh';
  20. $order = $params['order'] ?? 'desc';
  21. $service = new GoodsService(function ($goods) {
  22. $goods->activities = $goods->activities;
  23. $goods->promos = $goods->promos;
  24. return $goods;
  25. });
  26. /*$service->up()->with(['max_sku_price' => function ($query) { // 计算价格区间用(不知道为啥 with 必须再 show 后面)
  27. $query->where('status', 'up');
  28. }]);*/
  29. $service->up();
  30. if ($keyword) {
  31. $service->search($keyword);
  32. }
  33. if ($ids) {
  34. $service->whereIds($ids);
  35. }
  36. if ($category_id) {
  37. $service->category($category_id, $is_category_deep);
  38. }
  39. if ($sort) {
  40. $service->order($sort, $order);
  41. }
  42. // $goods = $service->paginate();
  43. $goods = $service->select_autopage();
  44. $goods = collection($goods)->toArray();
  45. $goods = list_domain_image($goods,['image']);//主图
  46. $this->success('获取成功', $goods);
  47. }
  48. /**
  49. * 通过 ids 获取商品(不分页)
  50. *
  51. * @return void
  52. */
  53. public function ids()
  54. {
  55. $params = $this->request->param();
  56. $ids = $params['ids'] ?? '';
  57. $service = new GoodsService(function ($goods) {
  58. $goods->activities = $goods->activities;
  59. $goods->promos = $goods->promos;
  60. return $goods;
  61. });
  62. $service->show()->with(['max_sku_price' => function ($query) {
  63. $query->where('status', 'up');
  64. }]);
  65. if ($ids) {
  66. $service->whereIds($ids);
  67. }
  68. $goods = $service->select();
  69. $this->success('获取成功', $goods);
  70. }
  71. //商品详情
  72. public function detail()
  73. {
  74. $user = auth_user();
  75. $id = $this->request->param('id');
  76. $activity_id = $this->request->param('activity_id');
  77. //判断一下活动是否还可用
  78. $mapa = [
  79. 'id' => $activity_id,
  80. 'start_time' => ['lt',time()],
  81. 'end_time' => ['gt',time()],
  82. ];
  83. $activity_id = Db::name('shopro_activity')->where($mapa)->where('deletetime',NULL)->find() ? $activity_id : 0;
  84. // 存一下,获取器获取指定活动的时候会用到
  85. session('goods-activity_id:' . $id, $activity_id);
  86. $service = new GoodsService(function ($goods, $service) use ($activity_id) {
  87. $goods->service = $goods->service;
  88. $goods->skus = $goods->skus;
  89. if (!$activity_id) {
  90. $goods->activities = $goods->activities;
  91. $goods->promos = $goods->promos;
  92. } else {
  93. $goods->activity = $goods->activity;
  94. $goods->original_goods_price = $goods->original_goods_price;
  95. }
  96. return $goods;
  97. });
  98. $goods = $service->field('*,price as pricemin')->show()->activity($activity_id)
  99. ->with(
  100. [
  101. /* 'max_sku_price' => function ($query) { // 计算价格区间用(不知道为啥 with 必须再 show 后面)
  102. $query->where('status', 'up');
  103. }, */'favorite'])
  104. ->where('id', $id)->find();
  105. if (!$goods) {
  106. $this->error(__('No Results were found'));
  107. }
  108. // 添加浏览记录
  109. GoodsLog::addView($user, $goods);
  110. // 处理商品规格
  111. $skuPrices = $goods['new_sku_prices'];
  112. $content = $goods['content'];
  113. $goods = $goods->toArray();
  114. $goods['image'] = localpath_to_netpath($goods['image']);//主图
  115. $goods['images'] = array_domain_image($goods['images']);//轮播图
  116. $skuPrices = list_domain_image($skuPrices->toArray(),['image']);//规格图
  117. $goods['sku_prices'] = $skuPrices;
  118. $goods['content'] = $content;
  119. unset($goods['new_sku_prices']);
  120. $this->success('获取成功', $goods);
  121. }
  122. //秒杀列表
  123. public function seckill_list(){
  124. $list = Db::name('shopro_activity')
  125. ->field('id,title,start_time,end_time')
  126. ->where('type','seckill')->where('deletetime',NULL)
  127. ->where('end_time','>',time())
  128. ->order('start_time','asc')
  129. ->autopage()->select();
  130. foreach($list as $key => $val){
  131. $val['current'] = 0;
  132. if($key == 0){
  133. $val['current'] = 1;
  134. }
  135. $val['title'] = '即将开抢';
  136. if(time() > $val['start_time']){
  137. $val['title'] = date('H',$val['start_time']).'点场';
  138. }
  139. $val['seconds'] = $val['end_time'] - time();
  140. $list[$key] = $val;
  141. }
  142. $this->success('获取成功',$list);
  143. }
  144. //某秒杀的商品列表,带分页
  145. public function seckill_productsku_list(){
  146. $activity_id = $this->request->param('activity_id');
  147. $category_id = $this->request->param('category_id');
  148. if (!$activity_id) {
  149. $this->error(__('No Results were found'));
  150. }
  151. $map = [
  152. 'asp.activity_id' => $activity_id,
  153. ];
  154. $list = Db::name('shopro_activity_sku_price')->alias('asp')
  155. ->field([
  156. 'asp.activity_id','asp.goods_sku_price_id','asp.goods_id','asp.stock','asp.sales','asp.price',
  157. 'g.title','g.image',
  158. 'gsp.goods_sku_text','gsp.image as sku_image','gsp.price as old_price']
  159. )
  160. ->join('shopro_activity a','asp.activity_id = a.id','LEFT')
  161. ->join('shopro_goods g' ,'asp.goods_id = g.id','LEFT')
  162. ->join('shopro_goods_sku_price gsp','asp.goods_sku_price_id = gsp.id','LEFT')
  163. ->where('asp.status','up')
  164. ->where('a.deletetime',NULL)->where('a.type','seckill')
  165. ->where('g.deletetime',NULL)->whereIn('g.status', ['up', 'hidden'])
  166. ->where('gsp.status','up')
  167. ->where($map);
  168. if(!empty($category_id)){
  169. $list->where('find_in_set(:category_ids,g.category_ids)', ['category_ids' => $category_id]);
  170. }
  171. $list = $list->autopage()->select();
  172. $list = list_domain_image($list,['image','sku_image']);
  173. foreach($list as $key => $val){
  174. //sku图片代替主图
  175. if(!empty($val['sku_image'])){
  176. $val['image'] = $val['sku_image'];
  177. }
  178. unset($val['sku_image']);
  179. //折扣
  180. $val['zhekou'] = bcdiv($val['price'],$val['old_price'],2);
  181. //已抢百分比
  182. $val['bili'] = bcdiv($val['sales'],$val['stock'],0).'%';
  183. $list[$key] = $val;
  184. }
  185. $this->success('获取成功',$list);
  186. }
  187. /**
  188. * 获取指定活动相关商品
  189. *
  190. * @return void
  191. */
  192. public function activity()
  193. {
  194. $activity_id = $this->request->param('activity_id');
  195. $need_buyers = $this->request->param('need_buyers', 0); // 需要查询哪些人在参与活动
  196. $activity = Activity::where('id', $activity_id)->find();
  197. if (!$activity) {
  198. $this->error(__('No Results were found'));
  199. }
  200. $goodsIds = $activity->goods_ids ? explode(',', $activity->goods_ids) : [];
  201. // 存一下,获取器获取指定活动的时候会用到
  202. foreach ($goodsIds as $id) {
  203. session('goods-activity_id:' . $id, $activity_id);
  204. }
  205. $service = new GoodsService(function ($goods) use ($need_buyers) {
  206. if ($need_buyers) {
  207. $goods->buyers = $goods->buyers;
  208. }
  209. $goods->activity = $goods->activity;
  210. return $goods;
  211. });
  212. $goods = $service->activity($activity_id)->whereIds($goodsIds)->show()->order('weigh', 'desc')->select();
  213. $goods = collection($goods)->toArray();
  214. foreach ($goods as &$gd) {
  215. unset($gd['new_sku_prices'], $gd['activity']);
  216. }
  217. $this->success('获取成功', $goods);
  218. }
  219. /**
  220. * 获取指定活动相关商品,带分页
  221. *
  222. * @param Request $request
  223. * @return void
  224. */
  225. public function activityList()
  226. {
  227. $activity_id = $this->request->param('activity_id');
  228. $activity = Activity::where('id', $activity_id)->find();
  229. if (!$activity) {
  230. $this->error(__('No Results were found'));
  231. }
  232. $goodsIds = $activity->goods_ids ? explode(',', $activity->goods_ids) : [];
  233. // 存一下,获取器获取指定活动的时候会用到
  234. foreach ($goodsIds as $id) {
  235. session('goods-activity_id:' . $id, $activity_id);
  236. }
  237. $service = new GoodsService(function ($goods) {
  238. $goods->promos = $goods->promos;
  239. return $goods;
  240. });
  241. $goods = $service->activity($activity_id)->whereIds($goodsIds)->show()->order('weigh', 'desc')->paginate();
  242. $this->success('获取成功', $goods);
  243. }
  244. }