PHP tp6 购物车的流程
2021/12/30 14:08:34
本文主要是介绍PHP tp6 购物车的流程,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
控制层 /加入购物车 表单提交 public function addcart() { // if(request()->isGet()){ // //如果是get请求 跳转到首页 // $this->redirect('home/index/index'); // } //接收数据 $params = input(); // var_dump($params);die; //参数检测 $validate = $this->validate($params, [ 'goods_id' => 'require|integer|gt:0', 'number' => 'require|integer|gt:0', 'spec_goods_id' => 'integer|gt:0', ]); if($validate !== true){ $this->error($validate); } //判断登录状态:已登录,添加到数据表;未登录,添加到cookie //处理数据 调用封装好的方法 \app\home\logic\CartLogic::addCart($params['goods_id'], $params['spec_goods_id'], $params['number']); //结果页面展示 //查询商品相关信息以及SKU信息 $goods = \app\common\model\Goods::getGoodsWithSpec($params['spec_goods_id'], $params['goods_id']); return view('addcart', ['goods' => $goods, 'number' => $params['number']]); } public function index() { //查询所有的购物记录 $list = \app\home\logic\CartLogic::getAllCart(); // var_dump($list);die; //对每一条购物记录 查询商品相关信息(商品信息和SKU信息) foreach($list as &$v){ //$v['goods_id'] $v['spec_goods_id'] $v['goods'] = \app\common\model\Goods::getGoodsWithSpec($v['spec_goods_id'], $v['goods_id'])->toArray(); } unset($v); return view('index', ['list' => $list]); } /** * ajax修改购买数量 */ public function changenum() { //接收参数 id number $params = input(); //参数检测 $validate = $this->validate($params, [ 'id' => 'require', 'number' => 'require|integer|gt:0' ]); if($validate !== true){ $res = ['code' => 400, 'msg' => '参数错误']; echo json_encode($res);die; } //处理数据 \app\home\logic\CartLogic::changeNum($params['id'], $params['number']); //返回数据 $res = ['code' => 200, 'msg' => 'success']; echo json_encode($res);die; } /** * ajax删除购物记录 */ public function delcart() { //接收参数 $params = input(); //参数检测 /*$validate = $this->validate($params, [ 'id' => 'require' ]); if($validate !== true){ $res = ['code' => 400, 'msg' => $validate]; echo json_encode($res);die; }*/ if(!isset($params['id']) || empty($params['id'])){ $res = ['code' => 400, 'msg' => '参数错误']; echo json_encode($res);die; } //处理数据 \app\home\logic\CartLogic::delCart($params['id']); //返回数据 $res = ['code' => 200, 'msg' => 'success']; echo json_encode($res);die; } /** * ajax修改选中状态 */ public function changestatus() { //接收参数 $params = input(); //参数检测 $validate = $this->validate($params, [ 'id' => 'require', 'status' => 'require|in:0,1' ]); if($validate !== true){ $res = ['code' => 400, 'msg' => $validate]; echo json_encode($res);die; } //处理数据 \app\home\logic\CartLogic::changeStatus($params['id'], $params['status']); //返回数据 $res = ['code' => 200, 'msg' => 'success']; echo json_encode($res);die; } } //逻辑层
public static function getAllCart() { //判登录断用户是否 if (session('?uid')) { $user_id = session('uid.id'); $data = \app\home\model\Cart::where('user_id', $user_id)->select();//将列表展示 return $data->toArray(); } else { $cart = \cookie('cart')?:[]; // dd($cart); if (empty($cart)) { return []; } $cart_data = unserialize($cart); // dd($cart_data); return array_values($cart_data); } } public static function addCart($goods_id, $spec_goods_id, $number, $is_selected) { if (session('?uid.id')) { $user_id = session('uid.id'); // 如果登录 $data = \app\home\model\Cart::where('goods_id', $goods_id) ->where('spec_goods_id', $spec_goods_id) ->where('user_id', $user_id) ->find(); if (!$data) { // 找到相同的数据 $cart_arr = [ 'number' => $number, 'is_selected' => $is_selected, 'spec_goods_id' => $spec_goods_id, 'goods_id' => $goods_id, 'user_id' => $user_id, ]; $goods = \app\home\model\Cart::create($cart_arr); return $goods; } else { $cart_data['number'] = $data['number'] + $number; \app\home\model\Cart::update($cart_data, ['id' => $data['id']]); return $data; } } else { // 未登录 // 取出cookie 如果是第一次在浏览器上加购商品是数组; //如果不是取出cookie值进行反序列hua; $cart = \cookie('cart') ?: []; // 拼接$key $key = $goods_id . '_' . $spec_goods_id; if (is_array($cart)) { $cart[$key]= [ 'number' => $number, 'is_selected' => $is_selected, 'spec_goods_id' => $spec_goods_id, 'goods_id' => $goods_id, ]; \cookie('cart', serialize($cart)); } $cart_arr = unserialize($cart); // dd($cart_arr); if (isset($cart_arr[$key])) { // 如果存在键 数量加一 $cart_arr[$key]['number']+= $number; \cookie('cart', serialize($cart_arr)); // dd($cart_arr); } else { $cart_arr[$key] = [ 'number' => $number, 'is_selected' => $is_selected, 'spec_goods_id' => $spec_goods_id, 'goods_id' => $goods_id, ]; } \cookie('cart', serialize($cart_arr)); return array_values($cart_arr); } } // cookie中取出数据 static function getCookie() { //如果不是取出cookie值进行反序列hua; $cart = \cookie('cart') ?: []; if(empty($cart)){ return []; } $cart_arr = unserialize($cart); foreach ($cart_arr as &$v){ self::addCart($v['goods_id'],$v['spec_goods_id'],$v['number'],$v['is_selected']); } unset($val); \cookie('cart',null); }
//model层 //获取商品以及sku信息 public static function getGoodsWithSpec($spec_goods_id, $goods_id) { //如果有sku的id, 就以之作为查询条件 if($spec_goods_id){ //有sku,且有多个,需要根据sku的id取指定的一个 $where = ['t2.id'=>$spec_goods_id]; }else{ //没有sku, 就根据商品id查询,sku相关的字段都是null $where = ['t1.id' => $goods_id]; } //如果没有,就以商品id作为查询条件 $goods = self::alias('t1') ->join('pyg_spec_goods t2', 't1.id=t2.goods_id', 'left') ->field('t1.*, t2.value_ids, t2.value_names, t2.price, t2.cost_price as cost_price2, t2.store_count') ->where($where) ->find(); // //如果sku信息中,price cost_price 大于0,则覆盖商品信息中的对应字段 if($goods['price'] > 0){ $goods['goods_price'] = $goods['price']; } if($goods['cost_price2'] > 0){ $goods['cost_price'] = $goods['cost_price2']; } return $goods; }
这篇关于PHP tp6 购物车的流程的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-11开源 PHP 商城项目 CRMEB 二次开发和部署教程
- 2024-11-09怎么使用php在kaufland平台刊登商品?-icode9专业技术文章分享
- 2024-11-05PHP的抽象类和接口是什么,有什么区别-icode9专业技术文章分享
- 2024-11-01开源 PHP 商城项目 CRMEB 安装和使用教程
- 2024-11-01用php和mysql写无限分类,有哪几种方法-icode9专业技术文章分享
- 2024-10-31php数据分表导出时部分数据无法导出什么原因-icode9专业技术文章分享
- 2024-10-30有经验的 PHP 开发者学习一门新的编程语言,有哪些推荐的有前景的语言-icode9专业技术文章分享
- 2024-10-21php 检测图片是否篡改过-icode9专业技术文章分享
- 2024-10-20fruitcake/php-cors 该怎么使用-icode9专业技术文章分享
- 2024-10-18PHP7.1可以使用哪个版本的swoole-icode9专业技术文章分享