赞
踩
定义变量有点乱,坏习惯真的需要改一下,希望大家遵循PHP PSR-2 代码风格规范 PHP PSR-2
namespace app\api\controller; use app\common\controller\Api; use think\Cache; use think\Db; class Cart { //购物车的显示 public function index() { $UID = input('user_id'); $UID == is_numeric(strval($UID))&&!empty($UID)?$UID:die('请求出错'); $sel = Cache::get($UID); if ($sel){ return json(['code'=>'200','data'=>$sel]); }else{ return json(['code'=>'200','data'=>'','msg'=>'请去添加商品']); } } //加购物车 public function toAdd(){ $bin = 0; $data = array_filter(input()); $arr = Cache::get($data['user_id']); if (!$arr){ $b[] = $data; $dat = Cache::set($data['user_id'],$b); if ($dat){ return json(['code'=>'200','msg'=>'ok1']); } } foreach ($arr as $key=>&$val){ if ($val['goods_id']==$data['goods_id']){ $val['buy_num'] = $val['buy_num']+1; $val['goods_price'] = round($val['buy_num']*$data['goods_price'],2); $bin++; } } if ($bin==0){ $arr[] = $data; $da = Cache::set($data['user_id'],$arr); if ($da){ return json(['code'=>'200','msg'=>'ok2']); } } $ins = Cache::set($data['user_id'],$arr); if ($ins){ return json(['code'=>'200','msg'=>'ok3']); } } //减购物车 public function toRde(){ $data = array_filter(input()); $arr = Cache::get($data['user_id']); foreach ($arr as $key => &$val){ if($val['goods_id']==$data['goods_id']){ if ($val['buy_num']>1){ $val['buy_num'] = $val['buy_num']-1; $val['goods_price'] = $val['goods_price']-$data['goods_price']; }else{ unset($arr[$key]); } } } $rde = Cache::set($data['user_id'],$arr); if ($rde){ return json(['code'=>'200','msg'=>'ok']); } } //清空购物车 public function delCar() { $uid = input('user_id'); $uid == is_numeric(strval($uid))&&!empty($uid)?$uid:die('请求出错'); $sel = Cache::get($uid); $sel = count($sel)!==0?:die('购物车是空的,快去选择商品吧'); $todel = Cache::set($uid,[]); return json($this->delmsg($todel)); } public function delmsg($todel){ return $todel==true?['code'=>'200','msg'=>'success']:['code'=>'100','msg'=>'is fail']; } }
代码如上,需要什么进行进行改动即可,我是临漂实习生
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。