Goods.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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. //商品列表
  13. public function index()
  14. {
  15. $params = $this->request->param();
  16. $keyword = $params['keyword'] ?? '';
  17. $ids = $params['ids'] ?? '';
  18. $category_id = $params['category_id'] ?? '';
  19. $is_category_deep = $params['is_category_deep'] ?? true;
  20. $sort = $params['sort'] ?? 'weigh';
  21. $order = $params['order'] ?? 'desc';
  22. $service = new GoodsService();
  23. /*$service->up()->with(['max_sku_price' => function ($query) { // 计算价格区间用(不知道为啥 with 必须再 show 后面)
  24. $query->where('status', 'up');
  25. }]);*/
  26. $service->up();
  27. if ($keyword) {
  28. $service->search($keyword);
  29. }
  30. if ($ids) {
  31. $service->whereIds($ids);
  32. }
  33. if ($category_id) {
  34. $service->category($category_id, $is_category_deep);
  35. }
  36. if ($sort) {
  37. $service->order($sort, $order);
  38. }
  39. // $goods = $service->paginate();
  40. $goods = $service->select_autopage();
  41. $goods = collection($goods)->toArray();
  42. $goods = list_domain_image($goods,['image']);//主图
  43. $this->success('获取成功', $goods);
  44. }
  45. //直播间商品列表
  46. public function liveroom_index(){
  47. $room_no = input('room_no','','trim');
  48. $keyword = input('keyword','','trim');
  49. $goods_list = Db::name('live_room_goods')->field('goods_id,goods_no,is_top')->where('room_no',$room_no)->where('status','up')->order('is_top desc,goods_no asc,id desc')->select();
  50. if(empty($goods_list))
  51. {
  52. $this->success(1,null);
  53. }
  54. $goods_ids = array_column($goods_list,'goods_id');
  55. $service = new GoodsService();
  56. $service->up();
  57. if ($keyword) {
  58. $service->search($keyword);
  59. }
  60. $service->whereIds($goods_ids);
  61. $goods = $service->select_autopage();
  62. $goods = collection($goods)->toArray();
  63. $goods = list_domain_image($goods,['image']);//主图
  64. foreach($goods as $key => $good){
  65. foreach($goods_list as $k => $v){
  66. if($good['id'] == $v['goods_id']){
  67. $good['is_top'] = $v['is_top'];
  68. $good['goods_no'] = $v['goods_no'];
  69. }
  70. }
  71. $goods[$key] = $good;
  72. }
  73. $this->success('获取成功', $goods);
  74. }
  75. /**
  76. * 通过 ids 获取商品(不分页)
  77. *
  78. * @return void
  79. */
  80. public function ids()
  81. {
  82. $params = $this->request->param();
  83. $ids = $params['ids'] ?? '';
  84. $service = new GoodsService(function ($goods) {
  85. $goods->activities = $goods->activities;
  86. $goods->promos = $goods->promos;
  87. return $goods;
  88. });
  89. $service->show()->with(['max_sku_price' => function ($query) {
  90. $query->where('status', 'up');
  91. }]);
  92. if ($ids) {
  93. $service->whereIds($ids);
  94. }
  95. $goods = $service->select();
  96. $this->success('获取成功', $goods);
  97. }
  98. //商品详情
  99. public function detail()
  100. {
  101. $user = auth_user();
  102. $id = $this->request->param('id');
  103. $activity_id = $this->request->param('activity_id');
  104. $goods_sku_price_id = $this->request->param('goods_sku_price_id');
  105. //判断一下活动是否还可用
  106. $mapa = [
  107. 'id' => $activity_id,
  108. 'start_time' => ['lt',time()],
  109. 'end_time' => ['gt',time()],
  110. ];
  111. $activity = Db::name('shopro_activity')->where($mapa)->where('deletetime',NULL)->find();
  112. $activity_id = $activity ? $activity_id : 0;
  113. //砍价活动处理
  114. if($activity && $activity['type'] == 'kan'){
  115. /*$kan_id = $this->request->param('kan_id',0);
  116. if($kan_id && $goods_sku_price_id){
  117. $map_kan = [
  118. 'id' => $kan_id,
  119. 'user_id' => $user['id'],
  120. 'goods_id' => $id,
  121. 'goods_sku_price_id' => $goods_sku_price_id,
  122. 'activity_id' => $activity_id,
  123. 'status' => ['IN',['ing','finish']],
  124. 'expire_time' => ['gt',time()],
  125. ];
  126. $kan = Db::name('shopro_activity_kan')->where($map_kan)->find();
  127. if(!$kan){
  128. $activity_id = 0;
  129. }
  130. }else{
  131. $activity_id = 0;
  132. }*/
  133. // $activity_id = 0;
  134. }
  135. // 存一下,获取器获取指定活动的时候会用到
  136. session('goods-activity_id:' . $id, $activity_id);
  137. $service = new GoodsService(function ($goods, $service) use ($activity_id) {
  138. // $goods->service = $goods->service;
  139. $goods->skus = $goods->skus; //第一次调用 model/goods 里的 getSkusAttr
  140. /*if (!$activity_id) {
  141. $goods->activities = $goods->activities;
  142. $goods->promos = $goods->promos;
  143. } else {
  144. $goods->activity = $goods->activity;
  145. $goods->original_goods_price = $goods->original_goods_price;
  146. }*/
  147. $old_goods_price = $goods->original_goods_price; //getOriginalGoodsPriceAttr
  148. $goods->old_goods_price = $old_goods_price ? $old_goods_price : $goods->price;
  149. return $goods;
  150. });
  151. $goods = $service->show()->activity($activity_id)
  152. ->with(
  153. [
  154. /* 'max_sku_price' => function ($query) { // 计算价格区间用(不知道为啥 with 必须再 show 后面)
  155. $query->where('status', 'up');
  156. }, */'favorite'])
  157. ->where('id', $id)->find();
  158. if (!$goods) {
  159. $this->error(__('No Results were found'));
  160. }
  161. // 添加浏览记录
  162. GoodsLog::addView($user, $goods);
  163. // 处理商品规格
  164. $skuPrices = $goods['new_sku_prices'];
  165. $content = $goods['content'];
  166. $goods = $goods->toArray();
  167. $goods['image'] = localpath_to_netpath($goods['image']);//主图
  168. $goods['images'] = array_domain_image($goods['images']);//轮播图
  169. $skuPrices = list_domain_image($skuPrices->toArray(),['image']);//规格图
  170. //$goods['current_sku'] = (object)[];
  171. foreach ($skuPrices as $key => $skuPrice) {
  172. //如果传过来规格id,那主图,price的第零个,original_goods_price的第零个 都需要跟着变
  173. if($goods_sku_price_id){
  174. if($goods_sku_price_id == $skuPrice['id']){
  175. $goods['image'] = $skuPrice['image'] ? $skuPrice['image'] : $goods['image'];
  176. $goods['price'][0] = $skuPrice['price'];
  177. $goods['old_goods_price'][0] = $skuPrice['old_price'];
  178. /*$goods['current_sku'] = [
  179. 'id' => $skuPrice['id'],
  180. 'goods_sku_ids' => $skuPrice['goods_sku_ids'],
  181. 'goods_sku_text' => $skuPrice['goods_sku_text'],
  182. ];*/
  183. }
  184. }
  185. }
  186. $goods['sku_prices'] = $skuPrices;
  187. $goods['content'] = $content;
  188. unset($goods['new_sku_prices']);
  189. //评价数量
  190. $goods['comment_number'] = Db::name('shopro_goods_comment')->where('goods_id',$id)->where('status','normal')->where('deletetime',NULL)->count('id');
  191. $this->success('获取成功', $goods);
  192. }
  193. //秒杀列表
  194. public function seckill_list(){
  195. $list = Db::name('shopro_activity')
  196. ->field('id,title,start_time,end_time')
  197. ->where('type','seckill')->where('deletetime',NULL)
  198. ->where('end_time','>',time())
  199. ->order('start_time','asc')
  200. ->autopage()->select();
  201. foreach($list as $key => $val){
  202. $val['current'] = 0;
  203. if($key == 0){
  204. $val['current'] = 1;
  205. }
  206. $val['title'] = '即将开抢';
  207. if(time() > $val['start_time']){
  208. $val['title'] = date('H',$val['start_time']).'点场';
  209. }
  210. $val['seconds'] = $val['end_time'] - time();
  211. $list[$key] = $val;
  212. }
  213. $this->success('获取成功',$list);
  214. }
  215. //某秒杀的商品列表,带分页
  216. public function seckill_productsku_list(){
  217. $activity_id = $this->request->param('activity_id');
  218. $category_id = $this->request->param('category_id');
  219. if (!$activity_id) {
  220. $this->error(__('No Results were found'));
  221. }
  222. $map = [
  223. 'asp.activity_id' => $activity_id,
  224. ];
  225. $list = Db::name('shopro_activity_sku_price')->alias('asp')
  226. ->field([
  227. 'asp.activity_id','asp.goods_sku_price_id','asp.goods_id','asp.stock','asp.sales','asp.price',
  228. 'g.title','g.image',
  229. 'gsp.goods_sku_text','gsp.image as sku_image','gsp.price as old_price']
  230. )
  231. ->join('shopro_activity a','asp.activity_id = a.id','LEFT')
  232. ->join('shopro_goods g' ,'asp.goods_id = g.id','LEFT')
  233. ->join('shopro_goods_sku_price gsp','asp.goods_sku_price_id = gsp.id','LEFT')
  234. ->where('asp.status','up')
  235. ->where('a.deletetime',NULL)->where('a.type','seckill')
  236. ->where('g.deletetime',NULL)->whereIn('g.status', ['up', 'hidden'])
  237. ->where('gsp.status','up')
  238. ->where($map);
  239. if(!empty($category_id)){
  240. $list->where('find_in_set(:category_ids,g.category_ids)', ['category_ids' => $category_id]);
  241. }
  242. $list = $list->autopage()->select();
  243. $list = list_domain_image($list,['image','sku_image']);
  244. foreach($list as $key => $val){
  245. //sku图片代替主图
  246. if(!empty($val['sku_image'])){
  247. $val['image'] = $val['sku_image'];
  248. }
  249. unset($val['sku_image']);
  250. //折扣
  251. $val['zhekou'] = bcdiv($val['price'],$val['old_price'],2);
  252. //已抢百分比
  253. $val['bili'] = bcdiv($val['sales'],$val['stock'],2);
  254. $list[$key] = $val;
  255. }
  256. $this->success('获取成功',$list);
  257. }
  258. //团购商品列表
  259. public function groupon_productsku_list(){
  260. $category_id = $this->request->param('category_id');
  261. $list = Db::name('shopro_activity_sku_price')->alias('asp')
  262. ->field([
  263. 'asp.activity_id','asp.goods_sku_price_id','asp.goods_id','asp.price',
  264. 'a.rules',
  265. 'g.title','g.image',
  266. 'gsp.goods_sku_text','gsp.image as sku_image','gsp.price as old_price']
  267. )
  268. ->join('shopro_activity a','asp.activity_id = a.id','LEFT')
  269. ->join('shopro_goods g' ,'asp.goods_id = g.id','LEFT')
  270. ->join('shopro_goods_sku_price gsp','asp.goods_sku_price_id = gsp.id','LEFT')
  271. ->where('asp.status','up')
  272. ->where('a.deletetime',NULL)->where('a.type','groupon')
  273. ->where('g.deletetime',NULL)->whereIn('g.status', ['up', 'hidden'])
  274. ->where('gsp.status','up');
  275. if(!empty($category_id)){
  276. $list->where('find_in_set(:category_ids,g.category_ids)', ['category_ids' => $category_id]);
  277. }
  278. $list = $list->order('a.end_time asc')->autopage()->select();
  279. $list = list_domain_image($list,['image','sku_image']);
  280. foreach($list as $key => $val){
  281. //sku图片代替主图
  282. if(!empty($val['sku_image'])){
  283. $val['image'] = $val['sku_image'];
  284. }
  285. unset($val['sku_image']);
  286. $val['team_num'] = json_decode($val['rules'],true)['team_num'];
  287. unset($val['rules']);
  288. $list[$key] = $val;
  289. }
  290. $this->success('获取成功',$list);
  291. }
  292. /**
  293. * 获取指定活动相关商品
  294. *
  295. * @return void
  296. */
  297. public function activity()
  298. {
  299. $activity_id = $this->request->param('activity_id');
  300. $need_buyers = $this->request->param('need_buyers', 0); // 需要查询哪些人在参与活动
  301. $activity = Activity::where('id', $activity_id)->find();
  302. if (!$activity) {
  303. $this->error(__('No Results were found'));
  304. }
  305. $goodsIds = $activity->goods_ids ? explode(',', $activity->goods_ids) : [];
  306. // 存一下,获取器获取指定活动的时候会用到
  307. foreach ($goodsIds as $id) {
  308. session('goods-activity_id:' . $id, $activity_id);
  309. }
  310. $service = new GoodsService(function ($goods) use ($need_buyers) {
  311. if ($need_buyers) {
  312. $goods->buyers = $goods->buyers;
  313. }
  314. $goods->activity = $goods->activity;
  315. return $goods;
  316. });
  317. $goods = $service->activity($activity_id)->whereIds($goodsIds)->show()->order('weigh', 'desc')->select();
  318. $goods = collection($goods)->toArray();
  319. foreach ($goods as &$gd) {
  320. unset($gd['new_sku_prices'], $gd['activity']);
  321. }
  322. $this->success('获取成功', $goods);
  323. }
  324. /**
  325. * 获取指定活动相关商品,带分页
  326. *
  327. * @param Request $request
  328. * @return void
  329. */
  330. public function activityList()
  331. {
  332. $activity_id = $this->request->param('activity_id');
  333. $activity = Activity::where('id', $activity_id)->find();
  334. if (!$activity) {
  335. $this->error(__('No Results were found'));
  336. }
  337. $goodsIds = $activity->goods_ids ? explode(',', $activity->goods_ids) : [];
  338. // 存一下,获取器获取指定活动的时候会用到
  339. foreach ($goodsIds as $id) {
  340. session('goods-activity_id:' . $id, $activity_id);
  341. }
  342. $service = new GoodsService(function ($goods) {
  343. $goods->promos = $goods->promos;
  344. return $goods;
  345. });
  346. $goods = $service->activity($activity_id)->whereIds($goodsIds)->show()->order('weigh', 'desc')->paginate();
  347. $this->success('获取成功', $goods);
  348. }
  349. }