Goods.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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. $goods_sku_price_id = $this->request->param('goods_sku_price_id');
  78. //判断一下活动是否还可用
  79. $mapa = [
  80. 'id' => $activity_id,
  81. 'start_time' => ['lt',time()],
  82. 'end_time' => ['gt',time()],
  83. ];
  84. $activity = Db::name('shopro_activity')->where($mapa)->where('deletetime',NULL)->find();
  85. $activity_id = $activity ? $activity_id : 0;
  86. //砍价活动处理
  87. if($activity && $activity['type'] == 'kan'){
  88. /*$kan_id = $this->request->param('kan_id',0);
  89. if($kan_id && $goods_sku_price_id){
  90. $map_kan = [
  91. 'id' => $kan_id,
  92. 'user_id' => $user['id'],
  93. 'goods_id' => $id,
  94. 'goods_sku_price_id' => $goods_sku_price_id,
  95. 'activity_id' => $activity_id,
  96. 'status' => ['IN',['ing','finish']],
  97. ];
  98. $kan = Db::name('shopro_activity_kan')->where($map_kan)->find();
  99. if(!$kan){
  100. $activity_id = 0;
  101. }
  102. }else{
  103. $activity_id = 0;
  104. }*/
  105. $activity_id = 0;
  106. }
  107. // 存一下,获取器获取指定活动的时候会用到
  108. session('goods-activity_id:' . $id, $activity_id);
  109. $service = new GoodsService(function ($goods, $service) use ($activity_id) {
  110. // $goods->service = $goods->service;
  111. $goods->skus = $goods->skus;
  112. /*if (!$activity_id) {
  113. $goods->activities = $goods->activities;
  114. $goods->promos = $goods->promos;
  115. } else {
  116. $goods->activity = $goods->activity;
  117. $goods->original_goods_price = $goods->original_goods_price;
  118. }*/
  119. $goods->original_goods_price = $goods->original_goods_price;
  120. return $goods;
  121. });
  122. $goods = $service->field('*,price as pricemin')->show()->activity($activity_id)
  123. ->with(
  124. [
  125. /* 'max_sku_price' => function ($query) { // 计算价格区间用(不知道为啥 with 必须再 show 后面)
  126. $query->where('status', 'up');
  127. }, */'favorite'])
  128. ->where('id', $id)->find();
  129. if (!$goods) {
  130. $this->error(__('No Results were found'));
  131. }
  132. // 添加浏览记录
  133. GoodsLog::addView($user, $goods);
  134. // 处理商品规格
  135. $skuPrices = $goods['new_sku_prices'];
  136. $content = $goods['content'];
  137. $goods = $goods->toArray();
  138. $goods['image'] = localpath_to_netpath($goods['image']);//主图
  139. $goods['images'] = array_domain_image($goods['images']);//轮播图
  140. $skuPrices = list_domain_image($skuPrices->toArray(),['image']);//规格图
  141. $goods['sku_prices'] = $skuPrices;
  142. $goods['content'] = $content;
  143. unset($goods['new_sku_prices']);
  144. $this->success('获取成功', $goods);
  145. }
  146. //秒杀列表
  147. public function seckill_list(){
  148. $list = Db::name('shopro_activity')
  149. ->field('id,title,start_time,end_time')
  150. ->where('type','seckill')->where('deletetime',NULL)
  151. ->where('end_time','>',time())
  152. ->order('start_time','asc')
  153. ->autopage()->select();
  154. foreach($list as $key => $val){
  155. $val['current'] = 0;
  156. if($key == 0){
  157. $val['current'] = 1;
  158. }
  159. $val['title'] = '即将开抢';
  160. if(time() > $val['start_time']){
  161. $val['title'] = date('H',$val['start_time']).'点场';
  162. }
  163. $val['seconds'] = $val['end_time'] - time();
  164. $list[$key] = $val;
  165. }
  166. $this->success('获取成功',$list);
  167. }
  168. //某秒杀的商品列表,带分页
  169. public function seckill_productsku_list(){
  170. $activity_id = $this->request->param('activity_id');
  171. $category_id = $this->request->param('category_id');
  172. if (!$activity_id) {
  173. $this->error(__('No Results were found'));
  174. }
  175. $map = [
  176. 'asp.activity_id' => $activity_id,
  177. ];
  178. $list = Db::name('shopro_activity_sku_price')->alias('asp')
  179. ->field([
  180. 'asp.activity_id','asp.goods_sku_price_id','asp.goods_id','asp.stock','asp.sales','asp.price',
  181. 'g.title','g.image',
  182. 'gsp.goods_sku_text','gsp.image as sku_image','gsp.price as old_price']
  183. )
  184. ->join('shopro_activity a','asp.activity_id = a.id','LEFT')
  185. ->join('shopro_goods g' ,'asp.goods_id = g.id','LEFT')
  186. ->join('shopro_goods_sku_price gsp','asp.goods_sku_price_id = gsp.id','LEFT')
  187. ->where('asp.status','up')
  188. ->where('a.deletetime',NULL)->where('a.type','seckill')
  189. ->where('g.deletetime',NULL)->whereIn('g.status', ['up', 'hidden'])
  190. ->where('gsp.status','up')
  191. ->where($map);
  192. if(!empty($category_id)){
  193. $list->where('find_in_set(:category_ids,g.category_ids)', ['category_ids' => $category_id]);
  194. }
  195. $list = $list->autopage()->select();
  196. $list = list_domain_image($list,['image','sku_image']);
  197. foreach($list as $key => $val){
  198. //sku图片代替主图
  199. if(!empty($val['sku_image'])){
  200. $val['image'] = $val['sku_image'];
  201. }
  202. unset($val['sku_image']);
  203. //折扣
  204. $val['zhekou'] = bcdiv($val['price'],$val['old_price'],2);
  205. //已抢百分比
  206. $val['bili'] = bcdiv($val['sales'],$val['stock'],0).'%';
  207. $list[$key] = $val;
  208. }
  209. $this->success('获取成功',$list);
  210. }
  211. //团购商品列表
  212. public function groupon_productsku_list(){
  213. $category_id = $this->request->param('category_id');
  214. $list = Db::name('shopro_activity_sku_price')->alias('asp')
  215. ->field([
  216. 'asp.activity_id','asp.goods_sku_price_id','asp.goods_id','asp.price',
  217. 'a.rules',
  218. 'g.title','g.image',
  219. 'gsp.goods_sku_text','gsp.image as sku_image','gsp.price as old_price']
  220. )
  221. ->join('shopro_activity a','asp.activity_id = a.id','LEFT')
  222. ->join('shopro_goods g' ,'asp.goods_id = g.id','LEFT')
  223. ->join('shopro_goods_sku_price gsp','asp.goods_sku_price_id = gsp.id','LEFT')
  224. ->where('asp.status','up')
  225. ->where('a.deletetime',NULL)->where('a.type','groupon')
  226. ->where('g.deletetime',NULL)->whereIn('g.status', ['up', 'hidden'])
  227. ->where('gsp.status','up');
  228. if(!empty($category_id)){
  229. $list->where('find_in_set(:category_ids,g.category_ids)', ['category_ids' => $category_id]);
  230. }
  231. $list = $list->order('a.end_time asc')->autopage()->select();
  232. $list = list_domain_image($list,['image','sku_image']);
  233. foreach($list as $key => $val){
  234. //sku图片代替主图
  235. if(!empty($val['sku_image'])){
  236. $val['image'] = $val['sku_image'];
  237. }
  238. unset($val['sku_image']);
  239. $val['team_num'] = json_decode($val['rules'],true)['team_num'];
  240. unset($val['rules']);
  241. $list[$key] = $val;
  242. }
  243. $this->success('获取成功',$list);
  244. }
  245. /**
  246. * 获取指定活动相关商品
  247. *
  248. * @return void
  249. */
  250. public function activity()
  251. {
  252. $activity_id = $this->request->param('activity_id');
  253. $need_buyers = $this->request->param('need_buyers', 0); // 需要查询哪些人在参与活动
  254. $activity = Activity::where('id', $activity_id)->find();
  255. if (!$activity) {
  256. $this->error(__('No Results were found'));
  257. }
  258. $goodsIds = $activity->goods_ids ? explode(',', $activity->goods_ids) : [];
  259. // 存一下,获取器获取指定活动的时候会用到
  260. foreach ($goodsIds as $id) {
  261. session('goods-activity_id:' . $id, $activity_id);
  262. }
  263. $service = new GoodsService(function ($goods) use ($need_buyers) {
  264. if ($need_buyers) {
  265. $goods->buyers = $goods->buyers;
  266. }
  267. $goods->activity = $goods->activity;
  268. return $goods;
  269. });
  270. $goods = $service->activity($activity_id)->whereIds($goodsIds)->show()->order('weigh', 'desc')->select();
  271. $goods = collection($goods)->toArray();
  272. foreach ($goods as &$gd) {
  273. unset($gd['new_sku_prices'], $gd['activity']);
  274. }
  275. $this->success('获取成功', $goods);
  276. }
  277. /**
  278. * 获取指定活动相关商品,带分页
  279. *
  280. * @param Request $request
  281. * @return void
  282. */
  283. public function activityList()
  284. {
  285. $activity_id = $this->request->param('activity_id');
  286. $activity = Activity::where('id', $activity_id)->find();
  287. if (!$activity) {
  288. $this->error(__('No Results were found'));
  289. }
  290. $goodsIds = $activity->goods_ids ? explode(',', $activity->goods_ids) : [];
  291. // 存一下,获取器获取指定活动的时候会用到
  292. foreach ($goodsIds as $id) {
  293. session('goods-activity_id:' . $id, $activity_id);
  294. }
  295. $service = new GoodsService(function ($goods) {
  296. $goods->promos = $goods->promos;
  297. return $goods;
  298. });
  299. $goods = $service->activity($activity_id)->whereIds($goodsIds)->show()->order('weigh', 'desc')->paginate();
  300. $this->success('获取成功', $goods);
  301. }
  302. }