quanwei
2025-11-15 6f12eadd25c8f5335fd9eccf373bf9835bf3e4c6
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
<?php
 
namespace app\api\controller\branch;
 
use app\api\controller\Controller;
use app\api\model\branch\Activity as ActivityModel;
use app\api\model\branch\ActivityCart as CartModel;
use app\api\model\settings\Setting as SettingModel;
use app\common\model\branch\ActivityProductPrice as ProductPriceModel;
 
/**
 * 购物车控制器
 */
class ActivityCart extends Controller
{
    private $user;
 
    // $model
    private $model;
 
    /**
     * 构造方法
     */
    public function initialize()
    {
        $this->user = $this->getUser(false);
        $this->model = new CartModel();
    }
 
 
    /**
     * 购物车列表
     */
    public function lists()
    {
        $model = new CartModel();
        $post_data = $this->postData();
        $activity_id = $post_data['activity_id'];
        // 购物车商品列表
        $productList = $model->getList($this->user,[],$activity_id);
        //是否显示店铺信息
        $store_open = SettingModel::getStoreOpen();
        return $this->renderSuccess('', compact('productList', 'store_open'));
    }
 
    /**
     * 加入购物车
     * @param int $product_id 商品id
     * @param int $product_num 商品数量
     * @param string $product_sku_id 商品sku索引
     */
    public function add()
    {
        $data = $this->request->param();
        $product_id = $data['product_id'];
        $product_num = $data['total_num'];
        $spec_sku_id = $data['spec_sku_id'];
        $activity_id = $data['activity_id'];
 
        /*验证权限*/
        $activity_model = new ActivityModel();
        $detail = $activity_model->detail($activity_id);
        if (!$detail) {
            return $this->renderError('活动不存在');
        }
        if ($detail['status_text']['status'] != 1) {
            return $this->renderError('活动未开始');
        }
        $model = new CartModel();
        if (!$model->add($this->user, $product_id, $product_num, $spec_sku_id, $activity_id)) {
            return $this->renderError($model->getError() ?: '加入购物车失败');
        }
        // 购物车商品总数量
        $totalNum = $model->getProductNum($this->user,$activity_id);
        return $this->renderSuccess('加入购物车成功', ['cart_total_num' => $totalNum]);
    }
 
    /**
     * 减少购物车商品数量
     * @param $product_id
     * @param $product_sku_id
     * @return array
     */
    public function sub($product_id, $spec_sku_id, $activity_id = 0)
    {
        $model = new CartModel();
 
        /*验证权限*/
        $activity_model = new ActivityModel();
        $detail = $activity_model->detail($activity_id);
        if (!$detail) {
            return $this->renderError('活动不存在');
        }
        if ($detail['status_text']['status'] != 1) {
            return $this->renderError('活动未开始');
        }
        $model->sub($this->user, $product_id, $spec_sku_id, $activity_id);
        return $this->renderSuccess('');
    }
 
    /**
     * 删除购物车中指定商品
     * @param $product_sku_id (支持字符串ID集)
     * @return array
     */
    public function delete($cart_id, $activity_id = 0)
    {
        /*验证权限*/
        $activity_model = new ActivityModel();
        $detail = $activity_model->detail($activity_id);
        if (!$detail) {
            return $this->renderError('活动不存在');
        }
        if ($detail['status_text']['status'] != 1) {
            return $this->renderError('活动未开始');
        }
        $this->model->setDelete($this->user, $cart_id);
        return $this->renderSuccess('删除成功');
    }
}