当前位置:   article > 正文

tp5 用 Cache缓存实现购物车功能(仿照美团、饿了么)_tp cache

tp cache

定义变量有点乱,坏习惯真的需要改一下,希望大家遵循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'];
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83

代码如上,需要什么进行进行改动即可,我是临漂实习生

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/201021
推荐阅读
  

闽ICP备14008679号