* @title 首页 */ namespace app\api\controller; use app\common\model\User; use app\common\model\AccountLog; use app\common\model\Goods; use app\common\model\Order; use app\common\model\MallImage; use app\common\model\GoodsCategory; use app\common\model\ShopConfig; use tools\Crypt; use think\Request; class IndexController extends Controller { protected $authExcept = [ 'home','recommend_goods','goods_info','goods_list','category_list' ]; public function home(Request $request,AccountLog $almodel,Goods $gmodel,Order $omodel,User $umodel) { //echo $token = $this->getToken(1).'|'; //echo $uid=Crypt::encrypt(1); $param = $request->param(); $uid = $param['uid']?Crypt::decrypt($param['uid']):''; //首页轮播 $carousel = get_ad('carousel',6); //精品推荐 $merchant = get_ad('merchant',6); //每日收益 $profit_num=ShopConfig::get('shouyi', 'num'); //弹窗配置 $goods_config=array(); $amount_today='0.00'; $amount_yesterday='0.00'; $amount_tuiguang='0.00'; if($uid){ $where[]=array('source_type','in','100,110,120'); $where[]=array('user_id','eq',$uid); //今日收益 $amount_today = $almodel ->where($where) ->whereTime('create_time', 'today') ->sum('change_amount'); //昨天收益 $amount_yesterday = $almodel ->where($where) ->whereTime('create_time', 'yesterday') ->sum('change_amount'); //推广收益 $amount_tuiguang = $almodel ->where(['source_type' =>100,'user_id'=>$uid]) ->sum('change_amount'); $goods_config['is_open']=ShopConfig::get('goods', 'is_open'); $is_first=ShopConfig::get('goods', 'is_first'); $goods_id=ShopConfig::get('goods', 'goods_id'); if($is_first==1){ $user_count=$umodel->where(array('user_level_id'=>1))->count(); if($user_count==0){ $goods_config['is_open']=0; } } $ty_count=$omodel->where(array('order_type'=>2,'goods_id'=>$goods_id))->count(); if($ty_count>0){ $goods_config['is_open']=0; } $goods_config['day']=ShopConfig::get('goods', 'day'); if($goods_id){ $goods_info= $gmodel ->alias('g') ->field("mi.thumb_image as img, g.name") ->join('mall_image mi', 'mi.union_id= g.id and is_cover=1 and mi.type=1', 'LEFT') ->where(array('g.id'=>$goods_id)) ->find(); $goods_info['img']=$this->img_url.$goods_info['img']; $goods_config['goods_info'] =$goods_info; } } $data = [ 'carousel' =>$carousel,//首页轮播 'merchant' => $merchant,//精品推荐 'profit_num'=>$profit_num,//每日收益 'amount_today'=>$amount_today,//今日收益 'amount_yesterday'=>$amount_yesterday,//昨天收益 'amount_tuiguang'=>$amount_tuiguang,//推广收益 'goods_config'=>$goods_config,//弹窗配置 ]; return success($data); } //精品推荐 public function recommend_goods(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); $goods_count = $model ->alias('g') ->where(array('is_recommend'=>1)) ->join('mall_image mi', 'mi.union_id= g.id and is_cover=1 and mi.type=1', 'LEFT') ->count(); $totalPages = ceil($goods_count / $size); $goods_list = $model ->alias('g') ->field("mi.thumb_image as img, g.name,g.attr,g.price,g.order_num,g.id as goods_id") ->join('mall_image mi', 'mi.union_id= g.id and is_cover=1 and mi.type=1', 'LEFT') ->where(array('is_recommend'=>1)) ->limit($page, $size) ->order('g.sort_number ASC') ->select(); if (!$goods_list) { return client_error('暂无数据'); } 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_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); $cid = $param['cid']?Crypt::decrypt($param['cid']):''; if($cid){ $where[]=array('goods_category_id','=',$cid); } $goods_count = $model ->alias('g') ->where($where) ->join('mall_image mi', 'mi.union_id= g.id and is_cover=1 and mi.type=1', 'LEFT') ->count(); $totalPages = ceil($goods_count / $size); $goods_list = $model ->alias('g') ->field("mi.thumb_image as img,g.name,g.attr,g.price,g.order_num,g.id as goods_id") ->join('mall_image mi', 'mi.union_id= g.id and is_cover=1 and mi.type=1', 'LEFT') ->where($where) ->limit($page, $size) ->order('g.sort_number ASC') ->select(); if (!$goods_list) { return client_error('暂无数据'); } 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) { $param = $request->param(); $gid = $param['gid']?Crypt::decrypt($param['gid']):''; if(!$gid){ return client_error('暂无数据'); } $goods_info=$model->field('name,attr,price,order_num,id as goods_id,detail')->where(array('id'=>$gid,'status'=>1))->find(); $goods_info['goods_id'] = Crypt::encrypt($goods_info['goods_id']); $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']; } return success(array('info'=>$goods_info,'img_list'=>$img_list)); } //商品分类 public function category_list(Request $request,GoodsCategory $gcmodel) { $category_list=$gcmodel->field('name,id as cid')->select(); foreach ($category_list as $k1 => $v1) { $category_list[$k1]['cid'] = Crypt::encrypt($v1['cid']); } //广告 $ad = get_ad('goods',1); return success(array('list'=>$category_list,'ad'=>$ad)); } }