quanwei
2025-11-21 2d9362ae6f528f57e6133d5d80f0b633c24e8eb6
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
<?php
 
namespace app\api\controller\product;
 
use app\api\model\product\Product as ProductModel;
use app\api\model\order\Cart as CartModel;
use app\api\controller\Controller;
use app\api\model\settings\Setting as SettingModel;
use app\api\model\shop\FullReduce as FullReduceModel;
use app\api\model\store\Store as StoreModel;
use app\api\model\user\Visit as VisitModel;
use app\api\service\common\RecommendService;
use app\common\library\helper;
use app\common\service\qrcode\ProductService;
use app\api\model\user\Favorite as FavoriteModel;
use app\api\model\plus\coupon\Coupon as CouponModel;
use app\common\model\supplier\Service as ServiceModel;
use app\common\model\supplier\supplier;
 
/**
 * 商品控制器
 */
class Product extends Controller
{
    /**
     * 商品列表
     */
    public function lists()
    {
        // 整理请求的参数
        $param = array_merge($this->postData(), [
            'product_status' => 10,
            'audit_status' => 10
        ]);
 
        // 获取列表数据
        $model = new ProductModel;
        $list = $model->getList($param, $this->getUser(false));
 
        $store = SettingModel::getItem('store');
        return $this->renderSuccess('', compact('list','store'));
    }
 
    /**
     * 推荐产品
     */
    public function recommendProduct($location)
    {
        $recommend = SettingModel::getItem('recommend');
        $store=SettingModel::getItem('store');
        $model = new ProductModel;
        $is_recommend = RecommendService::checkRecommend($recommend, $location);
        $list = [];
        if ($is_recommend) {
            $list = $model->getRecommendProduct($recommend);
        }
        return $this->renderSuccess('', compact('list', 'recommend', 'is_recommend','store'));
    }
 
    /**
     * 获取商品详情
     */
    public function detail($product_id, $url)
    {
        $params = $this->postData();
        // 用户信息
        $user = $this->getUser(false);
        // 商品详情
        $model = new ProductModel;
        $product = $model->getDetails($product_id, $this->getUser(false));
        if ($product === false || $product['audit_status'] != 10 || $product['product_status']['value'] != 10) {
            return $this->renderError($model->getError() ?: '商品信息不存在');
        }
        // 多规格商品sku信息
        $specData = $product['spec_type'] == 20 ? $model->getManySpecData($product['spec_rel'], $product['sku']) : null;
        $isfollow = 0;
        if ($user) {
            if (FavoriteModel::detail($product_id, 20, $user['user_id'])) {
                $isfollow = 1;
            }
        }
        $product['isfollow'] = $isfollow;
        $dataCoupon['shop_supplier_id'] = $product['shop_supplier_id'];
        $model = new CouponModel;
        $couponList = $model->getWaitList($dataCoupon, $this->getUser(false), 1);
        // 访问记录
        (new VisitModel())->addVisit($user, $product['supplier'], $params['visitcode'], $product);
        // 优惠信息
        $discount = [
            // 商品满减
            'product_reduce' => FullReduceModel::getListByProduct($product_id),
            // 赠送积分
            'give_points' => $this->getGivePoints($product),
            // 商品优惠券
            'product_coupon' => $this->getCouponList($product),
        ];
        //是否显示优惠
        $show_discount = false;
        if(count($discount['product_reduce']) > 0
            || $discount['give_points'] > 0
            || count($discount['product_coupon']) > 0 ){
            $show_discount = true;
        }
        //获取商城设置
        $store_setting = SettingModel::getItem("store");
        $store = '';
        //展示门店的信息
        if(!empty($store_setting) &&!empty($store_setting["store_type"]) && $store_setting["store_type"] == 1){
            $store_id = storeModel::getStoreIdBySupplierId($product['shop_supplier_id']);
            if($store_id){
               $store = storeModel::detail($store_id);
            }
        }
 
        return $this->renderSuccess('', [
            // 商品详情
            'detail' => $product,
            // 优惠信息
            'discount' => $discount,
            // 显示优惠
            'show_discount' => $show_discount,
            // 购物车商品总数量
            'cart_total_num' => $user ? (new CartModel())->getProductNum($user) : 0,
            // 多规格商品sku信息
            'specData' => $specData,
            // 微信公众号分享参数
            'share' => $this->getShareParams($url, $product['product_name'], $product['product_name'], '/pages/product/detail/detail', $product['image'][0]['file_path']),
            'couponList' => $couponList,
            //是否显示店铺信息
            'store_open' => SettingModel::getStoreOpen(),
            //显示店铺的类型
            'store' => $store,
            //是否显示下划线价格
            'underline_price_show' => SettingModel::getUnderlinePriceShow(),
            //是否开启客服
            'service_open' => SettingModel::getSysConfig()['service_open'],
            //店铺客服信息
            'mp_service' => ServiceModel::detail($product['shop_supplier_id']),
            //店铺营业状态
            'supplier_status' => (new supplier())->supplierStatus($product['shop_supplier_id']),
        ]);
    }
 
    /**
     * 赠送积分
     */
    private function getGivePoints($product)
    {
        if ($product['is_points_gift'] == 0) {
            return 0;
        }
        // 积分设置
        $setting = SettingModel::getItem('points');
        // 条件:后台开启开启购物送积分
        if (!$setting['is_shopping_gift']) {
            return 0;
        }
        // 积分赠送比例
        $ratio = $setting['gift_ratio'] / 100;
        // 计算赠送积分数量
        return helper::bcmul($product['product_price'], $ratio, 0);
    }
 
    /**
     * 获取商品可用优惠券
     */
    private function getCouponList($product)
    {
        // 可领取优惠券
        $model = new CouponModel;
        $user = $this->getUser(false);
        $couponList = $model->getList($user, null, true, $product['shop_supplier_id']);
        foreach ($couponList as $item){
            // 限制商品
            if($item['apply_range'] == 20){
                $product_ids = explode(',', $item['product_ids']);
                if(!in_array($product['product_id'], $product_ids)){
                    unset($item);
                }
            }
        }
        return $couponList;
    }
 
    /**
     * 预告产品
     */
    public function previewProduct()
    {
        // 整理请求的参数
        $param = array_merge($this->postData(), [
            'type' => 'preview',
        ]);
        // 获取列表数据
        $model = new ProductModel;
        $list = $model->getList($param, $this->getUser(false));
        return $this->renderSuccess('', compact('list'));
    }
 
    /**
     * 生成商品海报
     */
    public function poster($product_id, $source)
    {
        // 商品详情
        $detail = ProductModel::detail($product_id);
        $Qrcode = new ProductService($detail, $this->getUser(false), $source);
        return $this->renderSuccess('', [
            'qrcode' => $Qrcode->getImage(),
        ]);
    }
}