# 概况

img

# 命令

  • hgetall key 全选
  • hget key field 单个商品数量
  • hset key field value 加入购物车
  • hlen key 购物车数量
  • hIncrBy key field num +-数量
  • hdel key field 删除商品
  • del key 清空购物车

# 代码

<?php
/***
 * 购物车服务
 * Date: 2021/3/18
 * Time: 9:10 下午
 * Author gan
 */

namespace App\Service;

use \App\Consts\Redis as RedisConst;
use EasySwoole\RedisPool\RedisPool;

class CartService
{

    /**
     * 加入购物车
     *
     * @param int $userId  用户ID
     * @param int $goodsId 商品ID
     * @param int $num     数量
     * @return array
     * @date   2021/3/18
     * @time   9:14 下午
     * @author gan
     */
    public static function addCart($userId, $goodsId, $num)
    {
        try {
            $result = RedisPool::invoke(function (\EasySwoole\Redis\Redis $redis) use ($userId, $goodsId, $num) {
                $key     = RedisConst::CART . $userId;
                $lastNum = $redis->hIncrBy($key, $goodsId, $num);
                if (empty($lastNum)) {
                    $redis->hDel($key, $goodsId);
                }
                return $lastNum;
            }, RedisConst::NAME);
            if ($result === false || $result === null) {
                throw new \Swoole\Exception("加入购物车失败");
            }
            return [0, "OK", $result];
        } catch (\Exception $e) {
            return [1, $e->getMessage(), []];
        }
    }


    /**
     * 购物车删除单商品,删除和数量为0,调用此方法
     *
     * @param int $userId  用户ID
     * @param int $goodsId 商品ID
     * @return array
     * @date   2021/3/18
     * @time   9:17 下午
     * @author gan
     */
    public static function delCartGoods($userId, $goodsId)
    {
        try {
            $result = RedisPool::invoke(function (\EasySwoole\Redis\Redis $redis) use ($userId, $goodsId) {
                $key = RedisConst::CART . $userId;
                return $redis->hDel($key, $goodsId);
            }, RedisConst::NAME);
            if ($result === false || $result === null) {
                throw new \Swoole\Exception("商品删除失败");
            }
            return [0, "OK", $result];
        } catch (\Exception $e) {
            return [1, $e->getMessage(), []];
        }
    }

    /**清空购物车
     *
     * @param int $userId 用户ID
     * @return mixed
     * @date   2021/3/18
     * @time   9:19 下午
     * @author gan
     */
    public static function cleanCart($userId)
    {
        try {
            $result = RedisPool::invoke(function (\EasySwoole\Redis\Redis $redis) use ($userId) {
                $key = RedisConst::CART . $userId;
                return $redis->del($key);
            }, RedisConst::NAME);
            if ($result === false || $result === null) {
                throw new \Swoole\Exception("清空失败");
            }
            return [0, "OK", $result];
        } catch (\Exception $e) {
            return [1, $e->getMessage(), []];
        }
    }

    /**
     * 购物车列表
     *
     * @param int $userId 用户ID
     * @return array
     * @date   2021/3/18
     * @time   9:22 下午
     * @author gan
     */
    public static function getCartList($userId)
    {
        try {
            $result = RedisPool::invoke(function (\EasySwoole\Redis\Redis $redis) use ($userId) {
                $key  = RedisConst::CART . $userId;
                $len  = $redis->hLen($key);
                $list = $redis->hGetAll($key);
                return ["len" => $len, "list" => $list];
            }, RedisConst::NAME);
            if ($result === false || $result === null) {
                throw new \Swoole\Exception("获取购物车数据失败");
            }
            return [0, "OK", $result];
        } catch (\Exception $e) {
            return [1, $e->getMessage(), []];
        }
    }
}
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125