277 lines
11 KiB
PHP
Executable File
277 lines
11 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 app\common\model\GoodsCollect;
|
|
use app\common\model\DevRegion;
|
|
use tools\Crypt;
|
|
use think\Request;
|
|
use think\facade\Cache;
|
|
class GoodsController extends Controller
|
|
{
|
|
|
|
protected $authExcept = [
|
|
'goods_info','goods_list','category_list','region_list'
|
|
];
|
|
|
|
//商品列表
|
|
public function goods_list(Request $request,Goods $model)
|
|
{
|
|
$param = $request->param();
|
|
$size=$param['size']>0?$param['size']:9;
|
|
$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']);
|
|
}
|
|
if($goods_info['goods_size']){
|
|
$goods_info['goods_size']=explode("\r\n",$goods_info['goods_size']);
|
|
}
|
|
$goods_fm_img='';
|
|
$img_list=$mimodel->field('thumb_image,ori_image,is_cover')->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'];
|
|
if($value['is_cover']==1){
|
|
$goods_fm_img=$value['thumb_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.id as goods_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();
|
|
$category_goods_list[]=array('img'=>$goods_fm_img,'goods_id'=>$goods_info['goods_id']);
|
|
foreach ($category_goods as $k1 => $v1) {
|
|
$v1['img'] =$this->img_url.$v1['img'];
|
|
$v1['goods_id'] = Crypt::encrypt($v1['goods_id']);
|
|
$category_goods_list[]=$v1;
|
|
}
|
|
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_list));
|
|
}
|
|
|
|
//商品筛选
|
|
public function category_list(Request $request,GoodsCategory $gcmodel,GoodsBrand $gbmodel)
|
|
{
|
|
$brand_list=$gbmodel->field('name,initial,img')->where(array('is_show'=>1))->select();
|
|
foreach ($brand_list as $k1 => $v1) {
|
|
$brand_list[$k1]['img'] =$this->img_url.$v1['img'];
|
|
}
|
|
$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 region_list(Request $request,DevRegion $drmodel)
|
|
{
|
|
$region=Cache::get('region_list');
|
|
if(!$region){
|
|
$region_list=$drmodel->field('name,id as cid,parent_id')->select();
|
|
$region=getSubs($region_list,100000);
|
|
Cache::set('region_list',$region);
|
|
}
|
|
return success($region);
|
|
}
|
|
|
|
|
|
//愿望清单列表
|
|
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']?$param['goods_id']:'';
|
|
if(!$goods_id){
|
|
return client_error('该商品不存在');
|
|
}
|
|
if($param['is_collect']==1){
|
|
$goods_id = Crypt::decrypt($param['goods_id']);
|
|
$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{
|
|
$goods_ids=array();
|
|
foreach ($goods_id as $k1 => $v1) {
|
|
$goods_ids[] = Crypt::decrypt($v1);
|
|
}
|
|
if(count($goods_ids)==0){
|
|
return client_error('该商品不存在');
|
|
}
|
|
$res =$gcmodel->whereIn('goods_id',$goods_ids)->where(['user_id'=>$user_id])->delete();
|
|
}
|
|
return $res ? success('','操作成功') : client_error('操作失败');
|
|
}
|
|
} |