dggjimai/application/api/controller/IndexController.php

240 lines
9.6 KiB
PHP
Executable File

<?php
/**
* @author yupoxiong<i@yufuping.com>
* @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 tools\Crypt;
use think\Request;
class IndexController extends Controller
{
protected $authExcept = [
'home','pattern_goods','goods_info','goods_list','category_list'
];
public function home(Request $request,Goods $gmodel,Goodsindex $gimodel)
{
$param = $request->param();
//首页轮播
$carousel = get_ad('carousel',6);
$goodsindex_list=$gimodel->where(array('status'=>1))->order('sort_number ASC')->select();
$goodsindex=array();
foreach ($goodsindex_list as $key => $value) {
$goodsindex[$key]['id'] = Crypt::encrypt($value['id']);
$goodsindex[$key]['name']=$value['name'];
$goodsindex[$key]['type']=$value['type'];
$goods_ids=json_decode($value['goods_ids']);
$where[]=array('g.id','in',$goods_ids);
$goods_list=$gmodel
->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_list as $k1 => $v1) {
$goods_list[$k1]['img'] =$this->img_url.$v1['img'];
$goods_list[$k1]['goods_id'] = Crypt::encrypt($v1['goods_id']);
}
$goodsindex[$key]['goods_list']=$goods_list;
}
$data = [
'carousel' =>$carousel,//首页轮播
'goodsindex' => $goodsindex,//模式板块
];
return success($data);
}
//模式商品
public function pattern_goods(Request $request,Goods $model,Goodsindex $gimodel)
{
$param = $request->param();
$id = $param['id']?Crypt::decrypt($param['id']):'';
if(!$id){
return success(array('list'=>[]));
}
$goodsindex_info=$gimodel->where(array('status'=>1,'id'=>$id))->find();
if(!$goodsindex_info){
return success(array('list'=>[]));
}
$id = $param['id']?Crypt::decrypt($param['id']):'';
$goods_ids=json_decode($goodsindex_info['goods_ids']);
$where[]=array('g.id','in',$goods_ids);
$sort=$param['sort']??'asc';
$order=$param['order']??'g.sort_number';
$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($where)
->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')
->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('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)
->limit($page, $size)
->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_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')
->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);
$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']);
return success(array('info'=>$goods_info,'img_list'=>$img_list,'size_img'=>$size_img,'goodsitem_list'=>$goodsitem_list,'phaseconfig_list'=>$phaseconfig_list));
}
//商品筛选
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);
}
}