123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437 |
- <?php
- namespace addons\shopro\controller\goods;
- use addons\shopro\controller\Common;
- use addons\shopro\service\goods\GoodsService;
- use app\admin\model\shopro\user\GoodsLog;
- use app\admin\model\shopro\activity\Activity;
- use think\Db;
- class Goods extends Common
- {
- protected $noNeedLogin = ['*'];
- protected $noNeedRight = ['*'];
- //商品列表
- public function index()
- {
- $params = $this->request->param();
- $keyword = $params['keyword'] ?? '';
- $ids = $params['ids'] ?? '';
- $category_id = $params['category_id'] ?? '';
- $is_category_deep = $params['is_category_deep'] ?? true;
- $sort = $params['sort'] ?? 'weigh';
- $order = $params['order'] ?? 'desc';
- $service = new GoodsService();
- /*$service->up()->with(['max_sku_price' => function ($query) { // 计算价格区间用(不知道为啥 with 必须再 show 后面)
- $query->where('status', 'up');
- }]);*/
- $service->up();
- if ($keyword) {
- $service->search($keyword);
- }
- if ($ids) {
- $service->whereIds($ids);
- }
- if ($category_id) {
- $service->category($category_id, $is_category_deep);
- }
- if ($sort) {
- $service->order($sort, $order);
- }
- // $goods = $service->paginate();
- $goods = $service->select_autopage();
- $goods = collection($goods)->toArray();
- $goods = list_domain_image($goods,['image']);//主图
- $this->success('获取成功', $goods);
- }
- //直播间商品列表
- public function liveroom_index(){
- $room_no = input('room_no','','trim');
- $keyword = input('keyword','','trim');
- $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();
- if(empty($goods_list))
- {
- $this->success(1,null);
- }
- $goods_ids = array_column($goods_list,'goods_id');
- $service = new GoodsService();
- $service->up();
- if ($keyword) {
- $service->search($keyword);
- }
- $service->whereIds($goods_ids);
- $goods = $service->select_autopage();
- $goods = collection($goods)->toArray();
- $goods = list_domain_image($goods,['image']);//主图
- foreach($goods as $key => $good){
- foreach($goods_list as $k => $v){
- if($good['id'] == $v['goods_id']){
- $good['is_top'] = $v['is_top'];
- $good['goods_no'] = $v['goods_no'];
- }
- }
- $goods[$key] = $good;
- }
- $this->success('获取成功', $goods);
- }
- /**
- * 通过 ids 获取商品(不分页)
- *
- * @return void
- */
- public function ids()
- {
- $params = $this->request->param();
- $ids = $params['ids'] ?? '';
- $service = new GoodsService(function ($goods) {
- $goods->activities = $goods->activities;
- $goods->promos = $goods->promos;
- return $goods;
- });
- $service->show()->with(['max_sku_price' => function ($query) {
- $query->where('status', 'up');
- }]);
- if ($ids) {
- $service->whereIds($ids);
- }
- $goods = $service->select();
- $this->success('获取成功', $goods);
- }
- //商品详情
- public function detail()
- {
- $user = auth_user();
- $id = $this->request->param('id');
- $activity_id = $this->request->param('activity_id');
- $goods_sku_price_id = $this->request->param('goods_sku_price_id');
- //判断一下活动是否还可用
- $mapa = [
- 'id' => $activity_id,
- 'start_time' => ['lt',time()],
- 'end_time' => ['gt',time()],
- ];
- $activity = Db::name('shopro_activity')->where($mapa)->where('deletetime',NULL)->find();
- $activity_id = $activity ? $activity_id : 0;
- //砍价活动处理
- if($activity && $activity['type'] == 'kan'){
- /*$kan_id = $this->request->param('kan_id',0);
- if($kan_id && $goods_sku_price_id){
- $map_kan = [
- 'id' => $kan_id,
- 'user_id' => $user['id'],
- 'goods_id' => $id,
- 'goods_sku_price_id' => $goods_sku_price_id,
- 'activity_id' => $activity_id,
- 'status' => ['IN',['ing','finish']],
- 'expire_time' => ['gt',time()],
- ];
- $kan = Db::name('shopro_activity_kan')->where($map_kan)->find();
- if(!$kan){
- $activity_id = 0;
- }
- }else{
- $activity_id = 0;
- }*/
- // $activity_id = 0;
- }
- // 存一下,获取器获取指定活动的时候会用到
- session('goods-activity_id:' . $id, $activity_id);
- $service = new GoodsService(function ($goods, $service) use ($activity_id) {
- // $goods->service = $goods->service;
- $goods->skus = $goods->skus; //第一次调用 model/goods 里的 getSkusAttr
-
- /*if (!$activity_id) {
- $goods->activities = $goods->activities;
- $goods->promos = $goods->promos;
- } else {
- $goods->activity = $goods->activity;
- $goods->original_goods_price = $goods->original_goods_price;
- }*/
- $old_goods_price = $goods->original_goods_price; //getOriginalGoodsPriceAttr
- $goods->old_goods_price = $old_goods_price ? $old_goods_price : $goods->price;
- return $goods;
- });
- $goods = $service->show()->activity($activity_id)
- ->with(
- [
- /* 'max_sku_price' => function ($query) { // 计算价格区间用(不知道为啥 with 必须再 show 后面)
- $query->where('status', 'up');
- }, */'favorite'])
- ->where('id', $id)->find();
- if (!$goods) {
- $this->error(__('No Results were found'));
- }
- // 添加浏览记录
- GoodsLog::addView($user, $goods);
- // 处理商品规格
- $skuPrices = $goods['new_sku_prices'];
- $content = $goods['content'];
- $goods = $goods->toArray();
- $goods['image'] = localpath_to_netpath($goods['image']);//主图
- $goods['images'] = array_domain_image($goods['images']);//轮播图
- $skuPrices = list_domain_image($skuPrices->toArray(),['image']);//规格图
- //$goods['current_sku'] = (object)[];
- foreach ($skuPrices as $key => $skuPrice) {
- //如果传过来规格id,那主图,price的第零个,original_goods_price的第零个 都需要跟着变
- if($goods_sku_price_id){
- if($goods_sku_price_id == $skuPrice['id']){
- $goods['image'] = $skuPrice['image'] ? $skuPrice['image'] : $goods['image'];
- $goods['price'][0] = $skuPrice['price'];
- $goods['old_goods_price'][0] = $skuPrice['old_price'];
- /*$goods['current_sku'] = [
- 'id' => $skuPrice['id'],
- 'goods_sku_ids' => $skuPrice['goods_sku_ids'],
- 'goods_sku_text' => $skuPrice['goods_sku_text'],
- ];*/
- }
- }
- }
- $goods['sku_prices'] = $skuPrices;
- $goods['content'] = $content;
- unset($goods['new_sku_prices']);
- //评价数量
- $goods['comment_number'] = Db::name('shopro_goods_comment')->where('goods_id',$id)->where('status','normal')->where('deletetime',NULL)->count('id');
- $this->success('获取成功', $goods);
- }
- //秒杀列表
- public function seckill_list(){
- $list = Db::name('shopro_activity')
- ->field('id,title,start_time,end_time')
- ->where('type','seckill')->where('deletetime',NULL)
- ->where('end_time','>',time())
- ->order('start_time','asc')
- ->autopage()->select();
- foreach($list as $key => $val){
- $val['current'] = 0;
- if($key == 0){
- $val['current'] = 1;
- }
- $val['title'] = '即将开抢';
- if(time() > $val['start_time']){
- $val['title'] = date('H',$val['start_time']).'点场';
- }
- $val['seconds'] = $val['end_time'] - time();
- $list[$key] = $val;
- }
- $this->success('获取成功',$list);
- }
- //某秒杀的商品列表,带分页
- public function seckill_productsku_list(){
- $activity_id = $this->request->param('activity_id');
- $category_id = $this->request->param('category_id');
- if (!$activity_id) {
- $this->error(__('No Results were found'));
- }
- $map = [
- 'asp.activity_id' => $activity_id,
- ];
- $list = Db::name('shopro_activity_sku_price')->alias('asp')
- ->field([
- 'asp.activity_id','asp.goods_sku_price_id','asp.goods_id','asp.stock','asp.sales','asp.price',
- 'g.title','g.image',
- 'gsp.goods_sku_text','gsp.image as sku_image','gsp.price as old_price']
- )
- ->join('shopro_activity a','asp.activity_id = a.id','LEFT')
- ->join('shopro_goods g' ,'asp.goods_id = g.id','LEFT')
- ->join('shopro_goods_sku_price gsp','asp.goods_sku_price_id = gsp.id','LEFT')
- ->where('asp.status','up')
- ->where('a.deletetime',NULL)->where('a.type','seckill')
- ->where('g.deletetime',NULL)->whereIn('g.status', ['up', 'hidden'])
- ->where('gsp.status','up')
- ->where($map);
- if(!empty($category_id)){
- $list->where('find_in_set(:category_ids,g.category_ids)', ['category_ids' => $category_id]);
- }
- $list = $list->autopage()->select();
- $list = list_domain_image($list,['image','sku_image']);
- foreach($list as $key => $val){
- //sku图片代替主图
- if(!empty($val['sku_image'])){
- $val['image'] = $val['sku_image'];
- }
- unset($val['sku_image']);
- //折扣
- $val['zhekou'] = bcdiv($val['price'],$val['old_price'],2);
- //已抢百分比
- $val['bili'] = bcdiv($val['sales'],$val['stock'],2);
- $list[$key] = $val;
- }
- $this->success('获取成功',$list);
- }
- //团购商品列表
- public function groupon_productsku_list(){
- $category_id = $this->request->param('category_id');
- $list = Db::name('shopro_activity_sku_price')->alias('asp')
- ->field([
- 'asp.activity_id','asp.goods_sku_price_id','asp.goods_id','asp.price',
- 'a.rules',
- 'g.title','g.image',
- 'gsp.goods_sku_text','gsp.image as sku_image','gsp.price as old_price']
- )
- ->join('shopro_activity a','asp.activity_id = a.id','LEFT')
- ->join('shopro_goods g' ,'asp.goods_id = g.id','LEFT')
- ->join('shopro_goods_sku_price gsp','asp.goods_sku_price_id = gsp.id','LEFT')
- ->where('asp.status','up')
- ->where('a.deletetime',NULL)->where('a.type','groupon')
- ->where('g.deletetime',NULL)->whereIn('g.status', ['up', 'hidden'])
- ->where('gsp.status','up');
- if(!empty($category_id)){
- $list->where('find_in_set(:category_ids,g.category_ids)', ['category_ids' => $category_id]);
- }
- $list = $list->order('a.end_time asc')->autopage()->select();
- $list = list_domain_image($list,['image','sku_image']);
- foreach($list as $key => $val){
- //sku图片代替主图
- if(!empty($val['sku_image'])){
- $val['image'] = $val['sku_image'];
- }
- unset($val['sku_image']);
- $val['team_num'] = json_decode($val['rules'],true)['team_num'];
- unset($val['rules']);
- $list[$key] = $val;
- }
- $this->success('获取成功',$list);
- }
- /**
- * 获取指定活动相关商品
- *
- * @return void
- */
- public function activity()
- {
- $activity_id = $this->request->param('activity_id');
- $need_buyers = $this->request->param('need_buyers', 0); // 需要查询哪些人在参与活动
- $activity = Activity::where('id', $activity_id)->find();
- if (!$activity) {
- $this->error(__('No Results were found'));
- }
- $goodsIds = $activity->goods_ids ? explode(',', $activity->goods_ids) : [];
- // 存一下,获取器获取指定活动的时候会用到
- foreach ($goodsIds as $id) {
- session('goods-activity_id:' . $id, $activity_id);
- }
- $service = new GoodsService(function ($goods) use ($need_buyers) {
- if ($need_buyers) {
- $goods->buyers = $goods->buyers;
- }
- $goods->activity = $goods->activity;
- return $goods;
- });
- $goods = $service->activity($activity_id)->whereIds($goodsIds)->show()->order('weigh', 'desc')->select();
- $goods = collection($goods)->toArray();
- foreach ($goods as &$gd) {
- unset($gd['new_sku_prices'], $gd['activity']);
- }
- $this->success('获取成功', $goods);
- }
- /**
- * 获取指定活动相关商品,带分页
- *
- * @param Request $request
- * @return void
- */
- public function activityList()
- {
- $activity_id = $this->request->param('activity_id');
- $activity = Activity::where('id', $activity_id)->find();
- if (!$activity) {
- $this->error(__('No Results were found'));
- }
- $goodsIds = $activity->goods_ids ? explode(',', $activity->goods_ids) : [];
- // 存一下,获取器获取指定活动的时候会用到
- foreach ($goodsIds as $id) {
- session('goods-activity_id:' . $id, $activity_id);
- }
- $service = new GoodsService(function ($goods) {
- $goods->promos = $goods->promos;
- return $goods;
- });
- $goods = $service->activity($activity_id)->whereIds($goodsIds)->show()->order('weigh', 'desc')->paginate();
- $this->success('获取成功', $goods);
- }
- }
|