public function addCart()
{
//1. 检测如果是get请求 在执行浏览器刷新,则跳回首页
if(request()->isGet()){
$this->redirect('index/index');
}
$params = input();
$validateRes = $this->validate($params,[
'goods_id'=>'require|gt:0',
'spec_goods_id'=>'require|gt:0',
'number'=>'require|gt:0',
]);
if($validateRes !== true){
$this->error('信息错误');
}
//4. 调用加入购物车逻辑层功能
CartLogic::addCart($params['goods_id'],$params['spec_goods_id'],$params['number']);
$goods=Goods::getGoodsWithSpec($params['spec_goods_id'],$params['goods_id']);
return view('cart/success-cart',['goods'=>$goods,'number'=>$params['number']]);
}
前面调用addCart如果登录商品存入购物车表,没登录商品存入cookie
static public function addCart($goods_id,$spec_goods_id,$number,$is_selected=1)
{
//1. 判断 已登录-商品信息添加到数据表中 未登录-商品信息添加到cookie中
if(session('?info')){
$user_id=session('info');
//1.1 已登录- 组装数据,用于查询是否存在相同的商品信息
$goodsData = [
'user_id' => $user_id,
'goods_id' => $goods_id,
'spec_goods_id' => $spec_goods_id,
];
//1.2 查询此信息在购物车表中是否存在
$info = Cart::where($goodsData)->find();
//1.3 判断是否存在同商品
if($info){
//叠加数量
$info->number += $number;
$info->is_selected = $is_selected;
$info->save();
}else{
//添加数量
$goodsData['number'] = $number;
$goodsData['is_selected'] = $is_selected;
Cart::create($goodsData,true);
}
return '已登录,添加数据到表中';
}else{
//2.1 未登录,先从cookie中取出原始数据
$cartInfo = cookie('cart') ?: [];
//2.2 拼接商品ID和sku id的下标组合 类似于 61_804 目的是为了查询此商品是否已经存在 存在叠加数量 不存在正常添加
$key = $goods_id.'_'.$spec_goods_id;
//2.3 判断是否存在商品
if(isset($cartInfo[$key])){
//叠加数量
$cartInfo[$key]['number'] += $number;
$data[$key]['is_selected'] = $is_selected;
}else{
//正常添加
$cartInfo[$key] = [
'id' => $key,
'user_id' => '',
'goods_id' => $goods_id,
'spec_goods_id' => $spec_goods_id,
'number' => $number,
'is_selected'=>$is_selected,
];
}
//2.4 将处理好的数组内容添加到cookie中
cookie('cart',$cartInfo,86400);
return $key.'暂未登录,添加数据到cookie中';
}
}
页面展示,进入购物车详情
购物车页面展示方法 public function index() { //1. 调用逻辑层查询购物车数据方法(结果为各种ID) $data = CartLogic::getAllCart(); //2. 循环查询所有数据 foreach ($data as &$v) { $v['goods']=Goods::getGoodsWithSpec($v['spec_goods_id'],$v['goods_id'])->toArray(); } unset($v); return view('cart/cart',compact('data')); }
调用查询购物车数据方法
static public function getAllCart()
{
//1. 判断是否登录 登录-查数据表 未登录-查cookie
if(session('?info')){
//登陆-查数据表
$uid = session('info');
//查询购物车表中数据
$data = Cart::field('id,user_id,goods_id,spec_goods_id,number')
->where('user_id',$uid)->select();
$data = (new \think\Collection($data))->toArray();
}else{
//未登陆-取cookie值
$data = cookie('cart') ?: [];
//将结果转化为数组格式
$data = array_values($data);
}
return $data;
}
调用商品表数据
static public function getGoodsWithSpec($spec_goods_id,$goods_id)
{
//判断此商品是否存在sku
if($spec_goods_id){
//有sku信息
$where = ['t2.id' => $spec_goods_id];
}else{
//无sku信息
$where = ['t1.id' => $goods_id];
}
//查询一条记录
$data = self::alias('t1') //t1 == goods
->join('pyg_spec_goods t2','t1.id = t2.goods_id','left') //t2 == pyg_spec_goods
->field('t1.*,t2.value_ids,t2.value_names,t2.price,t2.cost_price as cost_price2,t2.store_count')
->where($where)
->find();
//如果存在sku的价格 则替换到原价格上
if($data['price'] > 0){
$data['goods_price'] = $data['price'];
}
if($data['cost_price2']>0){
$data['cost_price'] = $data['cost_price2'];
}
return $data;
}