移动系统liao
2024-05-09 5d6cb15ac86d9174393cb9d1538d69b567e2c26c
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
<template>
    <view class="wrap">
        <u-toast ref="uToast" /><u-no-network></u-no-network>
        <u-navbar leftIcon="search" title="购物车" safeAreaInsetTop fixed placeholder @leftClick="goSearch"></u-navbar>
 
        <!-- 购物车主结构  -->
        <view v-if="shoppingCard.list && shoppingCard.list.length > 0">
            <u-checkbox-group placement="column" v-model="cartIds" @change="itemChange">
                <view class="cart-shoppingcard" v-for="(item, index) in shoppingCard.list" :key="index">
                    <view class="coreshop-flex coreshop-flex-direction-row coreshop-justify-between coreshop-align-center">
                        <view class="cart-shopp-name">
                            <text class="coreshop-font-15 coreshop-text-bold">{{item.products.name}}</text>
                        </view>
                        <view class="coreshop-flex coreshop-flex-direction-row coreshop-justify-between">
                            <u-button :plain="true" size="mini" @click="goGoodsDetail(item.products.goodsId)">浏览</u-button>
                        </view>
                    </view>
                    <view style="height: 12.5px;"></view>
                    <view class="cart-shoppingcard-goods">
                        <view class="cart-shoppingcard-goods-checkbtn">
                            <u-checkbox active-color="red" :name="item.id"></u-checkbox>
                        </view>
                        <image class="cart-shoppingcard-goods-image" :src="item.products.images" mode="widthFix"></image>
                        <view class="cart-shoppingcard-goods-body">
                            <view class="cart-shoppingcard-goods-title" v-if="item.products.spesDesc">{{ item.products.spesDesc }}</view>
                            <view class="cart-shoppingcard-goods-title" v-if="item.products.promotionList">
                                <text class="cart-badge cart-bg-gray" v-for="(v, k) in item.products.promotionList" :key="k" :class="v.type !== 2 ? 'bg-gray' : ''"> {{ v.name }}</text>
                            </view>
 
                            <view class="coreshop-flex coreshop-flex-direction-row coreshop-justify-between coreshop-margin-top-10">
                                <view class="coreshop-flex coreshop-flex-direction-row coreshop-align-center">
                                    <text class="cart-shoppingcard-goods-price">¥{{item.products.price}}</text>
                                    <text class="coreshop-font-10 coreshop-margin-left-5" v-if="pointSwitch==1 && pointGetModel==2 && pointShowPoint==1 && item.products.points > 0">
                                        (送 {{item.products.points}}{{ pointShowName}})
                                    </text>
                                </view>
                                <view class="cart-shoppingcard-goods-number">
                                    <u-number-box buttonSize="24" integer :name="index" :value="item.nums" ref="index" @change="toNumberChange" :step="1" :min="1" :max="item.products.stock"></u-number-box>
                                </view>
                            </view>
 
                            <view class="coreshop-flex coreshop-flex-direction-row coreshop-justify-between coreshop-margin-top-10">
                                <text class="cart-shoppingcard-remove  coreshop-text-green" v-if="item.stockNo">库存不足</text>
                                <text class="cart-shoppingcard-remove  coreshop-text-green" v-else-if="item.stockTension">库存紧张</text>
                                <text class="cart-shoppingcard-remove " v-else=""></text>
                            </view>
 
                        </view>
 
                    </view>
 
                    <view class="coreshop-flex coreshop-flex-direction-row coreshop-justify-between coreshop-margin-top-10">
                        <view class="coreshop-flex coreshop-flex-direction-row coreshop-font-13"  v-if="pointSwitch==1 && pointExchangeModel==2 && pointShowExchangePrice==1 && item.products.pointsDeduction > 0">
                            <view>
                                {{ pointShowName}}兑换价:
                            </view>
                            <view class="coreshop-text-red">
                                {{pointDiscountedProportion * item.products.pointsDeduction }}{{ pointShowName}}+{{ parseFloat(item.products.price - item.products.pointsDeduction).toFixed(2)}}元
                            </view>
                        </view>
                        <view v-else></view>
                        <u-icon name="trash" size="14" @click="removeGoods" :index="index" label="删除" labelSize="12"></u-icon>
                    </view>
 
                </view>
            </u-checkbox-group>
 
        </view>
 
        <!-- 购物车为空 -->
        <view v-else>
            <view class="coreshop-emptybox">
                <u-empty :icon="$globalConstVars.apiFilesUrl+'/static/images/empty/cart.png'" icon-size="150" text="您的购物车空空如也" mode="list"></u-empty>
                <navigator class="coreshop-btn" url="/pages/category/index/index" open-type="switchTab">随便逛逛</navigator>
            </view>
        </view>
 
        <view class="coreshop-flex coreshop-flex-direction-row coreshop-justify-between coreshop-align-center cart-footer" v-if="shoppingCard.list && shoppingCard.list.length >= 1">
            <view class="cart-shoppingcard-checkbtn coreshop-flex coreshop-flex-direction-row coreshop-justify-between coreshop-align-center">
                <u-checkbox-group v-model="selectAll" @change="itemChangeAll">
                    <u-checkbox name="全选" label="全选" active-color="red"></u-checkbox>
                </u-checkbox-group>
            </view>
            <view class="cart-shoppingcard-count coreshop-flex coreshop-flex-direction-column coreshop-justify-between coreshop-align-left coreshop-margin-right-5"  v-if="pointSwitch==1 && pointExchangeModel==2 && pointShowExchangePrice==1 && pointShowMoney>0">
                <view>
                    <text class="cart-text">合计 :</text>
                    <text class="coreshop-font-18 coreshop-text-red">¥{{totalprice}}</text>
                </view>
                <view>
                    <text class="coreshop-font-12">{{ pointShowName}}兑换价:</text>
                    <text class="coreshop-font-12 coreshop-text-red">{{pointShowText}}</text>
                </view>
            </view>
            <view class="cart-shoppingcard-count coreshop-flex coreshop-flex-direction-row coreshop-justify-between coreshop-align-center coreshop-margin-right-5" v-else>
                <text class="cart-text">合计 :</text>
                <text class="coreshop-font-18 coreshop-text-red">¥{{totalprice}}</text>
            </view>
            <view class="cart-shoppingcard-checkout coreshop-bg-red" @tap="checkout">立即结算</view>
        </view>
 
        <!-- 登录提示 -->
        <coreshop-login-modal></coreshop-login-modal>
    </view>
 
</template>
<script>
    export default {
        data() {
            return {
                // 总价
                totalprice: '0.00',
                // 选择文本
                // 购物车数据 可以来自 api 请求或本地数据
                shoppingCard: {},
                cartIds: [], //选中ids
                goSettlement: false, //去结算按钮
                selectAll: ['全选'],
                pointShowText: "",
                pointShowMoney: 0,
            }
        },
        //页面加载
        onShow() {
            let userToken = this.$db.get('userToken');
            if (userToken) {
                this.cartIds = [];
                this.getCartData(); //获取购物车数据
                // 初始化时候计算总价,如果使用api 获取购物车项目在 api 请求完成后执行此函数 
                //this.countTotoal();
            }
        },
        computed: {
            // 从vuex中获取店铺名称
            shopName() {
                return this.$store.state.config.shopName;
            },
            GoodsStocksWarn() {
                return this.$store.state.config.goodsStocksWarn;
            },
            pointSwitch() { return this.$store.state.config.pointSwitch },
            pointShowExchangePrice() { return this.$store.state.config.pointShowExchangePrice },
            pointDiscountedProportion() { return this.$store.state.config.pointDiscountedProportion },
            pointExchangeModel() { return this.$store.state.config.pointExchangeModel },
            pointShowName() { return this.$store.state.config.pointShowName },
            pointGetModel() { return this.$store.state.config.pointGetModel },
            pointShowPoint() { return this.$store.state.config.pointShowPoint },
        },
        methods: {
            //数组转字符串
            arrayToStr: function (array) {
                return array.toString();
            },
            //获取购物车数据
            getCartData: function () {
                let _this = this;
                let cartIds = _this.arrayToStr(_this.cartIds);
                let data = {
                    ids: cartIds,
                    display: 'all'
                };
                this.$u.api.cartList(data).then(res => {
                    if (res.status) {
                        _this.shoppingCard = res.data;
                        _this.showHandle(_this.shoppingCard); //数量设置
                        //console.log(_this.shoppingCard);
                    }
                });
            },
            //渲染前配置数据
            showHandle: function (data, flag = true) {
                let _this = this;
                let goSettlement = false;
                for (let i in data.list) {
                    //不可能购买0件
                    if (data.list[i].nums < 1) {
                        data.list[i].nums = 1;
                    }
                    //不能买大于库存的数量(库存不足)
                    let stockNo = false;
                    let maxStock = data.list[i].products.stock;
                    if (data.list[i].nums > data.list[i].products.stock) {
                        stockNo = true;
                        maxStock = data.list[i].nums;
                    }
                    data.list[i].maxStock = maxStock;
                    data.list[i].stockNo = stockNo;
 
                    //库存紧张
                    let stockTension = false;
                    if (_this.GoodsStocksWarn >= data.list[i].products.stock) {
                        stockTension = true;
                    }
                    data.list[i].stockTension = stockTension;
 
                    //设置样式
                    data.list[i].minStatus = 'normal';
                    data.list[i].maxStatus = 'normal';
                    if (data.list[i].nums == 1) {
                        data.list[i].minStatus = 'disabled';
                    }
                    if (data.list[i].nums == data.list[i].products.stock) {
                        data.list[i].maxStatus = 'disabled';
                    }
 
                    //设置规格参数
                    data.list[i].spes = [];
                    if (data.list[i].products.spesDesc != null) {
                        let spesArray = data.list[i].products.spesDesc.split(',');
                        for (let key in spesArray) {
                            let spesOne = spesArray[key].split(':');
                            data.list[i].spes.push(spesOne[1]);
                        }
                    }
                    //添加左滑效果
                    data.list[i].isTouchMove = false;
                    //是否可以去支付
                    if (data.list[i].isSelect) {
                        goSettlement = true;
                    }
                    //id转换为字符串
                    data.list[i].id = _this.arrayToStr(data.list[i].id);
                    //选中状态
                    if (flag) {
                        data.list[i].isSelect = true;
                        if (data.list[i].isSelect) {
                            if (_this.cartIds.indexOf(data.list[i].id) < 0) {
                                _this.cartIds.push(data.list[i].id);
                            }
                        }
                    }
                }
                data.goodsPromotionMoney = _this.$common.formatMoney(data.goodsPromotionMoney, 2, '');
                data.orderPromotionMoney = _this.$common.formatMoney(data.orderPromotionMoney, 2, '');
                data.amount = _this.$common.formatMoney(data.amount, 2, '');
                let isLoad = false;
                if (data.list.length < 1) {
                    isLoad = true;
                }
                let n = 0;
                for (let i in data.promotionList) {
                    n++;
                }
 
                _this.goSettlement = goSettlement;
                _this.isLoad = isLoad;
                _this.cartNums = n;
 
                if (flag) {
                    _this.shoppingCard = data;
                } else {
                    _this.getCartData();
                }
                _this.countTotoal();
            },
            //计算总计函数
            countTotoal: function () {
                let _that = this;
                var total = 0;
                var pointsMoneySum = 0;
                var pointsSum = 0;
 
                for (let i in _that.shoppingCard.list) {
                    if (_that.shoppingCard.list[i].isSelect) {
                        total += Number(_that.shoppingCard.list[i].products.price) * Number(_that.shoppingCard.list[i].nums);
                        _that.pointShowMoney += Number(_that.shoppingCard.list[i].products.pointsDeduction) * Number(_that.shoppingCard.list[i].nums);
                        pointsSum += _that.pointDiscountedProportion * Number(_that.shoppingCard.list[i].products.pointsDeduction) * Number(_that.shoppingCard.list[i].nums);
                        pointsMoneySum += Number(_that.shoppingCard.list[i].products.price - _that.shoppingCard.list[i].products.pointsDeduction) * Number(_that.shoppingCard.list[i].nums);
                    }
                }
                _that.pointShowText = pointsSum + _that.pointShowName + '+' + _that.$common.formatMoney(pointsMoneySum, 2, '');
                _that.totalprice = _that.$common.formatMoney(total, 2, '');
            },
            toNumberChange: function (e) {
                this.$u.throttle(this.numberChange(e), 500)
            },
            numberChange(e) {
                uni.showLoading({
                    title: '加载中',
                    mask: true
                });
                let _this = this;
                let data = {
                    id: this.shoppingCard.list[e.name].productId,
                    nums: e.value
                };
                _this.$u.api.setCartNum(data).then(res => {
                    if (res.status) {
                        _this.shoppingCard.list[e.name].nums = e.value;
                        //重新计算总价
                        this.countTotoal();
                        setTimeout(function () {
                            uni.hideLoading();
                        }, 300);
                    } else {
                        _this.$u.toast(res.msg);
                        setTimeout(function () {
                            uni.hideLoading();
                        }, 300);
                    }
                });
 
            },
            removeGoods: function (index) {
                let _this = this;
                var index = index;
                uni.showModal({
                    title: '确认提醒',
                    content: '您确定要移除此商品吗?',
                    success: (e) => {
                        if (e.confirm) {
                            //移除数据库
                            let data = {
                                id: _this.shoppingCard.list[index].id
                            };
                            _this.$u.api.removeCart(data).then(res => {
                                if (res.status) {
                                    //清除已经勾选的商品和购物车数据
                                    var idIndex = _this.cartIds.indexOf(_this.shoppingCard.list[index].id);
                                    if (idIndex > -1) {
                                        _this.cartIds.splice(index, 1);
                                    }
                                    _this.shoppingCard.list.splice(index, 1);
                                    //计算总价
                                    _this.countTotoal();
                                    _this.$refs.uToast.show({ message: res.msg, type: 'success', back: false });
                                }
                            });
 
                        }
                    }
                })
            },
            checkout: function () {
                let _this = this;
                let cartData = _this.shoppingCard.list;
                let newData = '';
                for (let key in cartData) {
                    if (cartData[key].isSelect == true) {
                        newData += ',' + cartData[key].id;
                        _this.goSettlement = true;
                    }
                }
                if (newData.substr(0, 1) == ',') {
                    newData = newData.substr(1);
                }
                if (newData.length > 0) {
                    _this.$u.route('/pages/placeOrder/index/index?orderType=1&cartIds=' + JSON.stringify(newData));
                    return true;
                } else {
                    //没有选择不跳转
                    _this.$refs.uToast.show({ message: "请选择要下单的商品", type: 'error', back: false });
                }
 
            },
            // 商品选中
            itemChange: function (e) {
                var _this = this;
                _this.cartIds = e;
                var list = _this.shoppingCard.list;
                for (var i = 0; i < list.length; i++) {
                    var bl = _this.cartIds.indexOf(list[i].id) != -1;
                    if (bl) {
                        list[i].isSelect = true;
                    } else {
                        list[i].isSelect = false;
                    }
                }
                _this.shoppingCard.list = list;
                if (_this.cartIds.length == _this.shoppingCard.list.length) {
                    this.selectAll = ['全选'];
                } else {
                    this.selectAll = [];
                }
                this.countTotoal();
            },
            itemChangeAll: function (e) {
                let _this = this;
                if (e.length > 0) {
                    var ids = [];
                    for (var i = 0; i < _this.shoppingCard.list.length; i++) {
                        _this.shoppingCard.list[i].isSelect = true;
                        ids.push(_this.shoppingCard.list[i].id);
                    }
                    _this.cartIds = ids;
                } else {
                    for (var i = 0; i < _this.shoppingCard.list.length; i++) {
                        _this.shoppingCard.list[i].isSelect = false;
                    }
                    _this.cartIds = [];
                }
                this.countTotoal();
            },
        }
    }
</script>
<style lang="scss" scoped>
    @import "cart.scss";
</style>