page contents

PHP中封装Redis购物车功能

以下内容希望帮助到大家!

attachments-2020-05-CgW4BcYH5eb101bfa877e.jpg

直接看代码:

<?php
// 服务层

namespace Common\Service;
use Vendor\Func\Red;

class CartService extends CommonService {
    protected $redis;
    protected $pre_key;
    public function __construct()
    {
        parent::__construct();
        $this->redis = Red::create();
        $this->pre_key = C('USER.CART').C('APPID').':';
    }

    /**
     * 加入购物车,移除购物车,但是不会删除
     * @param $openid
     * @param $sku_id
     * @param int $count
     * @return mixed
     */
    public function add($openid, $sku_id, $count = 1)
    {
        $key = $this->pre_key.$openid;
        // 可增可减
        return $this->redis->hIncrBy($key, $sku_id, $count);
    }

    /**
     * 批量添加
     * @param $openid
     * @param array $data
     * @return mixed
     */
    public function addBatch($openid, array $data)
    {
        $key = $this->pre_key.$openid;
        // 批量执行
        $r = $this->redis->multi(\Redis::PIPELINE);
        foreach ($data as $k => $v) {
            $r = $r->hIncrBy($key, $k, $v);
        }
        return $this->redis->exec();
    }

    /**
     * 删除购物车单个商品
     * @param $openid
     * @param $sku_id
     * @return mixed
     */
    public function delete($openid, $sku_id)
    {
        $key = $this->pre_key.$openid;
        return $this->redis->hdel($key, $sku_id);
    }

    /**
     * 删除购物车多个商品
     * @param $openid
     * @param $sku_ids
     * @return bool
     */
    public function deleteBatch($openid, $sku_ids)
    {
        $key = $this->pre_key.$openid;
        foreach ($sku_ids as $k => $v) {
            $this->redis->hdel($key, $v);
        }

        return true;
    }

    /**
     * 检测商品是否已在购物车中
     * @param $openid
     * @param $sku_id
     * @return mixed
     */
    public function exists($openid, $sku_id)
    {
        $key = $this->pre_key.$openid;
        return $this->redis->hExists($key, $sku_id);
    }

    /**
     * 清空购物车
     * @param $openid
     * @return mixed
     */
    public function deleteAll($openid)
    {
        $key = $this->pre_key.$openid;
        return $this->redis->del($key);
    }

    /**
     * 判断购物车中是否有数据,有多少
     * @param $openid
     * @return mixed
     */
    public function hasUserCart($openid)
    {
        $key = $this->pre_key.$openid;
        return $this->redis->hLen($key);
    }

    /**
     * 设置为固定数量
     * @param $openid
     * @param $sku_id
     * @param $count
     * @return bool
     */
    public function setCount($openid, $sku_id, $count)
    {
        $key = $this->pre_key.$openid;
        $status = $this->redis->hset($key, $sku_id, $count);
        if ((int)$status === -1) {
            return false;
        }
        return true;
    }

    /**
     * 获取购物车中单个商品的数量
     * @param $openid
     * @param $sku_id
     * @return mixed
     */
    public function getCount($openid, $sku_id)
    {
        $key = $this->pre_key.$openid;
        return $this->redis->hget($key, $sku_id);
    }

    /**
     * 获取全部数据
     * @param $openid
     * @return mixed
     */
    public function getAll($openid)
    {
        $key = $this->pre_key.$openid;
        return $this->redis->hgetall($key);
    }

    /**
     * 获取全部商品id
     * @param $openid
     * @return mixed
     */
    public function getAllKeys($openid)
    {
        $key = $this->pre_key.$openid;
        return $this->redis->hkeys($key);
    }

    /**
     * 获取全部商品数量
     * @param $openid
     * @return mixed
     */
    public function getAllVal($openid)
    {
        $key = $this->pre_key.$openid;
        return $this->redis->hvals($key);
    }
}

加入购物车,移除购物车,清空购物车,查看购物车数量,查看全部商品等等。


attachments-2020-05-O1eXINOD5eb101b384683.jpg

  • 发表于 2020-05-05 14:04
  • 阅读 ( 417 )
  • 分类:PHP开发

你可能感兴趣的文章

相关问题

0 条评论

请先 登录 后评论
Pack
Pack

1135 篇文章

作家榜 »

  1. 轩辕小不懂 2403 文章
  2. 小柒 1316 文章
  3. Pack 1135 文章
  4. Nen 576 文章
  5. 王昭君 209 文章
  6. 文双 71 文章
  7. 小威 64 文章
  8. Cara 36 文章