* @title 首页 */ namespace app\api\controller; use app\common\model\Goodsindex; use app\common\model\Goods; use app\common\model\GoodsBrand; use app\common\model\GoodsItem; use app\common\model\GoodsCategory; use app\common\model\MallImage; use app\common\model\ShopConfig; use app\common\model\PhaseConfig; use app\common\model\GoodsCollect; use tools\Crypt; use think\Request; class GoodsController extends Controller { protected $authExcept = [ 'goods_info','goods_list','category_list' ]; //商品列表 public function goods_list(Request $request,Goods $model) { $param = $request->param(); $size=$param['size']>0?$param['size']:12; $page_l=$param['page']>1?$param['page']:1; $page=$size * ($page_l - 1); $where[]=array('status','=',1); $where[]=array('gi.stock','>',0); $keyword = $param['keyword']??''; $where1=''; if($keyword){ foreach ($keyword as $v1) { $where1.="( `spec_value` LIKE '%".$v1."%' OR `gb`.`name` LIKE '%".$v1."%' OR `gc`.`name` LIKE '%".$v1."%') or"; } $where1=rtrim($where1,'or'); } $sort=$param['sort']??'asc'; $order=$param['order']??'g.sort_number'; $goods_count = $model ->alias('g') ->where($where) ->where($where1) ->join('mall_image mi', 'mi.union_id= g.id and is_cover=1 and mi.type=1', 'LEFT') ->join('goods_brand gb', 'gb.id = g.brand_id') ->join('goods_item gi', 'gi.goods_id = g.id') ->join('goods_category gc', 'gc.id = g.goods_category_id') ->group('g.id') ->count(); $totalPages = ceil($goods_count / $size); $goods_list = $model ->alias('g') ->field("mi.thumb_image as img, g.name,g.price,g.id as goods_id,gb.name as brand_name") ->join('mall_image mi', 'mi.union_id= g.id and is_cover=1 and mi.type=1', 'LEFT') ->join('goods_brand gb', 'gb.id = g.brand_id') ->join('goods_item gi', 'gi.goods_id = g.id') ->join('goods_category gc', 'gc.id = g.goods_category_id') ->where($where) ->where($where1) ->limit($page, $size) ->group('g.id') ->order($order.' '.$sort) ->select(); if (!$goods_list) { return success(array('list'=>[])); } foreach ($goods_list as $k1 => $v1) { $goods_list[$k1]['img'] =$this->img_url.$v1['img']; $goods_list[$k1]['goods_id'] = Crypt::encrypt($v1['goods_id']); } $page=array( 'count'=>$goods_count, 'num'=>$page_l, 'size'=>$size, 'page'=>$totalPages ); return success(array('list'=>$goods_list,'page'=>$page)); } //商品详情 public function goods_info(Request $request,Goods $model,MallImage $mimodel,GoodsItem $gimodel,PhaseConfig $pcmodel) { $param = $request->param(); $gid = $param['gid']?Crypt::decrypt($param['gid']):''; if(!$gid){ return client_error('暂无数据'); } //商品信息 $goods_info=$model ->alias('g') ->field('g.name as goods_name,price,g.id as goods_id,detail,gb.name as brand_name,model,collocation_goods,original_price,goods_size,pc.name as phase_name,goods_category_id') ->join('goods_brand gb', 'gb.id = g.brand_id') ->join('phase_config pc', 'pc.id= g.phase_id', 'LEFT') ->where(array('g.id'=>$gid,'status'=>1))->find(); //精选配置 $collocation_goods=json_decode($goods_info['collocation_goods']); $where[]=array('g.id','in',$collocation_goods); $where[]=array('g.status','=',1); $goods_info['collocation_goods'] =$model ->alias('g') ->field("mi.thumb_image as img, g.name,g.price,g.id as goods_id,gb.name as brand_name") ->join('goods_brand gb', 'gb.id= g.brand_id') ->join('mall_image mi', 'mi.union_id= g.id and is_cover=1 and mi.type=1', 'LEFT') ->where($where) ->select(); foreach ($goods_info['collocation_goods'] as $k1 => $v1) { $goods_info['collocation_goods'][$k1]['img'] =$this->img_url.$v1['img']; $goods_info['collocation_goods'][$k1]['goods_id'] = Crypt::encrypt($v1['goods_id']); } $goods_info['goods_size']=explode("\r\n",$goods_info['goods_size']); $img_list=$mimodel->field('thumb_image,ori_image')->where(array('union_id'=>$gid,'type'=>1))->select(); foreach ($img_list as $key => $value) { $img_list[$key]['thumb_image'] =$this->img_url.$value['thumb_image']; $img_list[$key]['ori_image'] =$this->img_url.$value['ori_image']; } //尺寸图片 $size_img=$this->img_url.ShopConfig::get('website', 'size_img'); //商品尺码 $goodsitem_list=$gimodel->field("id,spec_value,stock")->where(array('goods_id'=>$goods_info['goods_id']))->select(); foreach ($goodsitem_list as $key => $value) { $goodsitem_list[$key]['id']=Crypt::encrypt($value['id']); } //品相列表 $phaseconfig_list=$pcmodel->field("name,describe")->all(); $goods_info['goods_id'] = Crypt::encrypt($goods_info['goods_id']); $category_goods =$model ->alias('g') ->field("mi.thumb_image as img, g.name,g.price,g.id as goods_id,gb.name as brand_name") ->join('goods_brand gb', 'gb.id= g.brand_id') ->join('mall_image mi', 'mi.union_id= g.id and is_cover=1 and mi.type=1', 'LEFT') ->where(array('g.goods_category_id'=>$goods_info['goods_category_id'],'g.status'=>1)) ->select(); foreach ($category_goods as $k1 => $v1) { $category_goods[$k1]['img'] =$this->img_url.$v1['img']; $category_goods[$k1]['goods_id'] = Crypt::encrypt($v1['goods_id']); } return success(array('info'=>$goods_info,'img_list'=>$img_list,'size_img'=>$size_img,'goodsitem_list'=>$goodsitem_list,'phaseconfig_list'=>$phaseconfig_list,'category_goods'=>$category_goods)); } //商品筛选 public function category_list(Request $request,GoodsCategory $gcmodel,GoodsBrand $gbmodel) { $brand_list=$gbmodel->field('name,initial')->where(array('is_show'=>1))->select(); $category_list=$gcmodel->field('name,id as cid,parent_id')->select(); $category=getSubs($category_list,0); $size_list=ShopConfig::get('website', 'size'); $size_list=explode(',',$size_list); $data=array( 'brand_list'=>$brand_list, 'category_list'=>$category, 'size_list'=>$size_list, ); return success($data); } //愿望清单列表 public function collectgoods_list(Request $request,GoodsCollect $gcmodel) { $param = $request->param(); $user_id = $param['uid']?Crypt::decrypt($param['uid']):''; if(!$user_id){ return unauthorized('请先授权'); } $size=$param['size']>0?$param['size']:12; $page_l=$param['page']>1?$param['page']:1; $page=$size * ($page_l - 1); $where[]=array('g.status','=',1); $where[]=array('user_id','=',$user_id); $goods_count = $gcmodel ->alias('gc') ->where($where) ->join('goods g', 'g.id = gc.goods_id') ->join('mall_image mi', 'mi.union_id= gc.goods_id and is_cover=1 and mi.type=1', 'LEFT') ->join('goods_brand gb', 'gb.id = g.brand_id') ->count(); $totalPages = ceil($goods_count / $size); $goods_list = $gcmodel ->alias('gc') ->field("mi.thumb_image as img, g.name,g.price,g.id as goods_id,gb.name as brand_name") ->join('goods g', 'g.id = gc.goods_id') ->join('mall_image mi', 'mi.union_id= gc.goods_id and is_cover=1 and mi.type=1', 'LEFT') ->join('goods_brand gb', 'gb.id = g.brand_id') ->where($where) ->limit($page, $size) ->order('g.sort_number asc') ->select(); if (!$goods_list) { return success(array('list'=>[])); } foreach ($goods_list as $k1 => $v1) { $goods_list[$k1]['img'] =$this->img_url.$v1['img']; $goods_list[$k1]['goods_id'] = Crypt::encrypt($v1['goods_id']); } $page=array( 'count'=>$goods_count, 'num'=>$page_l, 'size'=>$size, 'page'=>$totalPages ); return success(array('list'=>$goods_list,'page'=>$page)); } public function handlecollectgoods(Request $request,GoodsCollect $gcmodel){ $param = $request->param(); $user_id = $param['uid']?Crypt::decrypt($param['uid']):''; if(!$user_id){ return unauthorized('请先授权'); } $goods_id = $param['goods_id']?Crypt::decrypt($param['goods_id']):''; if(!$goods_id){ return client_error('该商品不存在'); } if($param['is_collect']==1){ $where = [ 'user_id' => $user_id, 'goods_id' => $goods_id ]; $info = GoodsCollect::where($where)->find(); if(!$info){ $data =[ 'user_id'=>$user_id, 'goods_id'=>$goods_id, 'create_time'=>time(), ]; $res =$gcmodel->insert($data); }else{ return success('','操作成功'); } }else{ $res =$gcmodel->where(['goods_id'=>$goods_id,'user_id'=>$user_id])->delete(); } return $res ? success('','操作成功') : client_error('操作失败'); } }