109 lines
3.8 KiB
PHP
Executable File
109 lines
3.8 KiB
PHP
Executable File
<?php
|
|
/**
|
|
* @author yupoxiong<i@yufuping.com>
|
|
* @title 首页
|
|
*/
|
|
|
|
namespace app\api\controller;
|
|
use app\common\model\Goods;
|
|
use app\common\model\GoodsItem;
|
|
use app\common\model\GoodsCategory;
|
|
use app\common\model\MallImage;
|
|
use app\common\model\ShopConfig;
|
|
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,GoodsCategory $gcmodel)
|
|
{
|
|
$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);
|
|
$keyword = $param['kw']??'';
|
|
if($keyword){
|
|
$where[]=array('g.name','LIKE',$keyword);
|
|
}
|
|
$cid = $param['cid'];
|
|
$category_list=$gcmodel->field('name,id')->where(array('parent_id'=>0))->select();
|
|
foreach ($category_list as $k1 => $v1){
|
|
$category_list[$k1]=$v1['id'];
|
|
}
|
|
$where[]=array('goods_category_id','=',$category_list[$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')
|
|
->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")
|
|
->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,GoodsItem $gimodel)
|
|
{
|
|
$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')
|
|
->where(array('g.id'=>$gid,'status'=>1))->find();
|
|
$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'];
|
|
}
|
|
//商品规格
|
|
$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']);
|
|
}
|
|
$goods_info['goods_id'] = Crypt::encrypt($goods_info['goods_id']);
|
|
|
|
return success(array('info'=>$goods_info,'img_list'=>$img_list,'goodsitem_list'=>$goodsitem_list));
|
|
}
|
|
|
|
//商品分类
|
|
public function category_list(Request $request,GoodsCategory $gcmodel)
|
|
{
|
|
$category_list=$gcmodel->field('name')->where(array('parent_id'=>0))->select();
|
|
$data=array(
|
|
'category_list'=>$category_list
|
|
);
|
|
return success($data);
|
|
}
|
|
} |