移动系统liao
2025-02-17 557c2711a3e103ebc3d0492344eca9730d5e92b2
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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
<template>
    <view class="u-wrap">
        <u-toast ref="uToast" /><u-no-network></u-no-network>
        <u-navbar safeAreaInsetTop fixed placeholder>
            <view slot="left">
                <u-search :show-action="true" shape="round" v-model="searchKey" actionText="搜索" placeholder="请输入搜索内容" @custom="goSearch" @search="goSearch"></u-search>
            </view>
            <u-icon name="scan" color="#fff" size="16" slot="right"></u-icon>
        </u-navbar>
 
        <view class="coreshop-padding-10" v-if="CateStyle==1">
            <view v-for="(item,index) in tabbar" :key="index" @click="goClass(item.id)">
                <u--image width="100%" height="195px" :src="item.imageUrl" :showLoading="false"></u--image>
                <view class="coreshop-text-center coreshop-padding-top-10 coreshop-padding-bottom-10">
                    {{item.name}}
                </view>
            </view>
        </view>
        <view class="coreshop-padding-10" v-if="CateStyle==2">
            <div class="col3Box" v-for="(item,index) in tabbar" :key="index" @click="goClass(item.id)">
                <u--image width="100%" height="80px" :src="item.imageUrl" :showLoading="false"></u--image>
                <view class="coreshop-text-center coreshop-padding-top-15 coreshop-padding-bottom-15 coreshop-font-sm">
                    {{item.name}}
                </view>
            </div>
        </view>
        <view class="u-menu-wrap coreshop-flex-direction-row" v-if="CateStyle==3">
            <scroll-view scroll-y scroll-with-animation class="u-tab-view menu-scroll-view" :scroll-top="scrollTop">
                <view v-for="(item,index) in tabbar" :key="index" class="u-tab-item" :class="[current==index ? 'u-tab-item-active' : '']"
                      :data-current="index" @tap.stop="swichMenu(index)">
                    <text class="u-line-1">{{item.name}}</text>
                </view>
            </scroll-view>
            <block v-for="(item,index) in tabbar" :key="index">
                <scroll-view scroll-y class="right-box" v-if="current==index">
                    <view class="coreshop-padding-10">
                        <coreshop-advert code="TplIndexBanner1"></coreshop-advert>
                        <view class="class-item">
                            <view class="item-title">
                                <text>{{item.name}}</text>
                            </view>
                            <view class="item-container coreshop-flex-direction-row">
                                <view class="thumb-box" v-for="(item1, index1) in item.child" :key="index1" @click="goClass(item1.id)">
                                    <u--image width="60px" height="60px" :src="item1.imageUrl" :showLoading="false"></u--image>
                                    <view class="item-menu-name">{{item1.name}}</view>
                                </view>
                            </view>
                        </view>
                    </view>
                </scroll-view>
            </block>
        </view>
        <view class="u-menu-wrap coreshop-flex-direction-row" v-if="CateStyle==4">
            <view class="main" :style="'height:' + scorllH+'px;'">
                <view class="content" :style="'height:' + scorllH+'px;'">
                    <scroll-view class="menus" scroll-with-animation scroll-y :scroll-top="headerH" :style="'height:' + scorllH+'px;'">
                        <view class="wrapper">
                            <view class="menu" :id="`menu-${item.id}`" :class="{'current': item.id === currentCateId}" v-for="(item, index) in menus" :key="index" @tap="handleMenuTap(item.id,index)">
                                <text>{{ item.name }}</text>
                            </view>
                        </view>
                    </scroll-view>
                    <scroll-view class="goods" scroll-with-animation scroll-y :scroll-top="headerH" :style="'height:' + scorllH+'px;'" @scrolltolower="scrolltolower">
                        <u-sticky v-if="menuChilds">
                            <view class="coreshop-bg-white coreshop-padding-left-10">
                                <u-tabs :list="menuChilds" :current="childCurrent" :activeStyle="{color: '#303133',fontWeight: 'bold',transform: 'scale(1.05)',fontSize:'14px'}" :inactiveStyle="{color: '#606266',transform: 'scale(1)',fontSize:'14px'}" @change="clickChange"></u-tabs>
                            </view>
                        </u-sticky>
 
                        <view class="coreshop-padding-10">
                            <coreshop-advert code="TplIndexBanner1" showTitle="true"></coreshop-advert>
                        </view>
 
                        <view class="wrapper">
 
                            <view class="list">
                                <view class="category" v-for="(good, key) in goodsList" :key="key" :id="`cate-${good.id}`">
                                    <view class="items">
                                        <view class="good">
                                            <view class="image">
                                                <u--image :showLoading="true" :src="good.image" width="83px" height="83px" radius="4px" @click="goGoodsDetail(good.id)"></u--image>
                                            </view>
                                            <view class="right">
                                                <text class="u-line-2" @tap="goGoodsDetail(good.id)">{{ good.name }}</text>
                                                <u--text type="info" size="12px" lines="1" :text="good.brief"></u--text>
                                                <!--<text class="tips u-line-1" @tap="goGoodsDetail(good.id)">{{ good.brief }}</text>-->
                                                <view class="price_and_action">
                                                    <text class="price">¥{{ good.price }}</text>
                                                    <view class="btn-group">
                                                        <button type="primary" class="btn property_btn" hover-class="none" size="mini" @tap="showGoodSkuModal(good)">
                                                            选规格
                                                        </button>
                                                    </view>
                                                </view>
                                            </view>
                                        </view>
                                    </view>
                                </view>
                                <u-loadmore :status="loadStatus" :icon-type="iconType" :load-text="loadText" margin-top="20" margin-bottom="20" />
                            </view>
                        </view>
                    </scroll-view>
                </view>
                <!-- 购物车栏 begin -->
                <view class="cart-box">
                    <view class="mark"  @tap="redirectCart()">
                        <image src="/static/images/common/cart.png" class="cart-img"></image>
                        <view class="tag">{{cartNums}}</view>
                    </view>
                    <view class="price">¥{{cartMoney}}</view>
                    <button type="primary" class="pay-btn" @tap="redirectCart()" :disabled="cartNums<=0">去结算</button>
                </view>
                <!-- 购物车栏 end -->
            </view>
 
            <vk-data-goods-sku-popup ref="skuPopup"
                                     v-model="skuKey"
                                     border-radius="20"
                                     :amount-type="0"
                                     :localdata="goodsSkuInfo"
                                     :mode="skuMode"
                                     @open="onOpenSkuPopup"
                                     @close="onCloseSkuPopup"
                                     @add-cart="addCart"
                                     @buy-now="buyNow"></vk-data-goods-sku-popup>
        </view>
        
        <!-- 登录提示 -->
        <coreshop-login-modal></coreshop-login-modal>
    </view>
</template>
<script>
    import { mapState } from 'vuex';
    export default {
 
        data() {
            return {
                tabbar: [],
                scrollTop: 0, //tab标题的滚动条位置
                current: 0, // 预设当前项的值
                menuHeight: 0, // 左边菜单的高度
                menuItemHeight: 0, // 左边菜单item的高度,
                beans: [],
                isChild: false,
                searchKey: '',
 
                headerH: 0,
                scorllH: 0, //滚动区域全屏
                cartNums: 0, // 购物车数量
                cartMoney: 0, // 购物车数量
                menus: [],//所有分类
                menuChilds: [], //当前分类下子集
                needCurrentCateId: 0, //仿点餐模式下应该获取大类还是小类数据
                childCurrent: 0, //子集默认选中下标
 
                goodsList: [], //所有商品
                menuScrollIntoView: '',
                good: {},
                currentCateId: 0,//默认分类
                scrollTop: 0,
                loadStatus: 'loadmore',
                loadIconType: 'flower',
                loadText: {
                    loadmore: '轻轻上拉',
                    loading: '努力加载中',
                    nomore: '实在没有了'
                },
                type: 2, // 1加入购物车 2购买
                cartType: this.$globalConstVars.paymentType.common,
                page: 1,
                limit: 10,
                // 是否打开SKU弹窗
                skuKey: false,
                // SKU弹窗模式
                skuMode: 1,
                // 后端返回的商品信息
                goodsSkuInfo: {},
 
            }
        },
        computed: {
            ...mapState({
                hasLogin: state => state.hasLogin,
                userInfo: state => state.userInfo,
            }),
            hasLogin: {
                get() {
                    return this.$store.state.hasLogin;
                },
                set(val) {
                    this.$store.commit('hasLogin', val);
                }
            },
            userInfo: {
                get() {
                    return this.$store.state.userInfo;
                },
                set(val) {
                    this.$store.commit('userInfo', val);
                }
            },
            CateStyle() {
                return this.$store.state.config.cateStyle ? this.$store.state.config.cateStyle : 3;
            }
        },
        onLoad() {
            uni.getSystemInfo({
                success: (e) => {
                    // #ifndef MP
                    this.StatusBar = e.statusBarHeight;
                    if (e.platform == 'android') {
                        this.headerH = e.statusBarHeight + 50;
                    } else {
                        this.headerH = e.statusBarHeight + 45;
                    };
                    // #endif
                    this.scorllH = e.windowHeight - e.statusBarHeight - 50;
                }
            })
            // 获取购物车数量
            if (this.$store.state.config.cateStyle == 4) {
                this.goodsList = [];
                this.page = 1;
                this.limit = 10;
                this.categories();
                //this.getCartNums();
            } else {
                this.categories();
            }
        },
        onShow() {
            // 获取购物车数量
            if (this.$store.state.config.cateStyle == 4) {
                this.getCartNums();
            }
        },
        methods: {
            // 点击左边的栏目切换
            async swichMenu(index) {
                if (index == this.current) return;
                this.current = index;
                // 如果为0,意味着尚未初始化
                if (this.menuHeight == 0 || this.menuItemHeight == 0) {
                    await this.getElRect('menu-scroll-view', 'menuHeight');
                    await this.getElRect('u-tab-item', 'menuItemHeight');
                }
                // 将菜单菜单活动item垂直居中
                this.scrollTop = index * this.menuItemHeight + this.menuItemHeight / 2 - this.menuHeight / 2;
            },
            // 获取一个目标元素的高度
            getElRect(elClass, dataVal) {
                new Promise((resolve, reject) => {
                    const query = uni.createSelectorQuery().in(this);
                    query.select('.' + elClass).fields({ size: true }, res => {
                        // 如果节点尚未生成,res值为null,循环调用执行
                        if (!res) {
                            setTimeout(() => {
                                this.getElRect(elClass);
                            }, 10);
                            return;
                        }
                        this[dataVal] = res.height;
                    }).exec();
                })
            },
            goClass(catId) {
                uni.navigateTo({
                    url: '/pages/category/list/list?id=' + catId
                });
            },
            goSearch() {
                if (this.searchKey != '') {
                    this.$u.route('/pages/category/list/list?key=' + this.searchKey);
                } else {
                    this.$refs.uToast.show({ message: '请输入查询关键字', type: 'warning', })
                }
            },
            categories() {
                this.$u.api.categories().then(res => {
                    if (res.status) {
                        this.tabbar = res.data;
                        if (this.$store.state.config.cateStyle == 4) {
                            this.menus = res.data;
                            if (res.data.length > 0) {
                                for (var i = 0; i < res.data.length; i++) {
                                    if (res.data[i].child) {
                                        let item = {
                                            id: 0,
                                            imageUrl: "",
                                            name: "全部",
                                            sort: 0,
                                        }
                                        res.data[i].child.unshift(...[item])
                                    }
                                }
                                this.currentCateId = res.data[0].id;
                                this.needCurrentCateId = res.data[0].id;
                                this.menuChilds = res.data[0].child;
                                this.getGoods();
                            }
 
                        }
                    }
                });
            },
            // 打开sku弹出
            openSkuPopup() {
                this.skuKey = true;
            },
            closeSkuPopup() {
                this.skuKey = false;
            },
            // sku组件监听
            onOpenSkuPopup() {
                //console.log("监听 - 打开sku组件");
            },
            onCloseSkuPopup() {
                //console.log("监听 - 关闭sku组件");
            },
            showGoodSkuModal(item) {
                if (item.id) {
                    this.getGoodsDetail(item.id);
                }
            },
            // 获取商品详情
            getGoodsDetail(goodsId) {
                uni.showLoading({
                    title: '加载中'
                });
                let _this = this;
                let data = {
                    id: goodsId,
                    data: true
                }
                // 如果用户已经登录 要传用户token
                let userToken = this.$db.get("userToken");
                if (userToken) {
                    this.$u.api.goodsDetailByToken(data).then(res => {
                        if (res.status == true) {
                            _this.goodsSkuInfo = res.data.skuList;
                            _this.openSkuPopup();
                            setTimeout(function () {
                                uni.hideLoading();
                            }, 1000);
                        } else {
                            _this.$refs.uToast.show({
                                message: res.msg, type: 'error', complete: function () {
                                    uni.navigateBack({
                                        delta: 1
                                    });
                                }
                            })
                            uni.hideLoading();
                        }
                    })
                } else {
                    this.$u.api.goodsDetail(data).then(res => {
                        if (res.status == true) {
                            _this.goodsSkuInfo = res.data.skuList;
                            _this.openSkuPopup();
                            setTimeout(function () {
                                uni.hideLoading();
                            }, 1000);
                        } else {
                            _this.$refs.uToast.show({
                                message: res.msg, type: 'error', complete: function () {
                                    uni.navigateBack({
                                        delta: 1
                                    });
                                }
                            })
                            uni.hideLoading();
                        }
                    })
                }
            },
            // 加入购物车按钮
            addCart(selectShop) {
                var that = this;
                uni.showLoading({
                    title: '加载中'
                });
                if (!this.hasLogin) {
                    uni.hideLoading();
                    this.$store.commit('showLoginTip', true);
                    return false;
                }
                if (selectShop.buy_num > 0) {
                    let data = {
                        productId: selectShop._id,
                        nums: selectShop.buy_num,
                        type: 1,
                    }
                    this.$u.api.addCart(data).then(res => {
                        if (res.status) {
                            uni.hideLoading();
                            that.getCartNums(); // 获取购物车数量
                            that.$refs.uToast.show({ message: res.msg, type: 'success' });
                        } else {
                            that.$u.toast(res.msg);
                            uni.hideLoading();
                        }
                    });
                }
                that.closeSkuPopup();
            },
            // 立即购买
            buyNow(selectShop) {
                var that = this;
                uni.showLoading({
                    title: '加载中'
                });
                if (!this.hasLogin) {
                    uni.hideLoading();
                    this.$store.commit('showLoginTip', true);
                    return false;
                }
                if (selectShop.buy_num > 0) {
                    let data = {
                        productId: selectShop._id,
                        nums: selectShop.buy_num,
                        type: 2,
                        cartType: this.cartType
                    }
                    this.$u.api.addCart(data).then(res => {
                        if (res.status) {
                            let cartIds = res.data;
                            that.$u.route('/pages/placeOrder/index/index?cartIds=' + JSON.stringify(cartIds));
                            uni.hideLoading();
                        } else {
                            this.$u.toast(res.msg);
                            uni.hideLoading();
                        }
                    });
                }
                that.closeSkuPopup();
            },
 
            handleMenuTap(id, index) {
 
                var _this = this;
                this.currentCateId = id;
                this.needCurrentCateId = id;
 
                this.menuChilds = [];
                this.menuChilds = _this.menus[index].child;
 
                this.childCurrent = 0;
                this.$forceUpdate()
 
                this.page = 1;
                this.goodsList = [];
                this.getGoods(0);
 
            },
            clickChange(e) {
                this.childCurrent = e.index;
                if (e.id != 0) {
                    this.needCurrentCateId = e.id;
                } else {
                    this.needCurrentCateId = this.currentCateId;
                }
                this.page = 1;
                this.goodsList = [];
                this.getGoods();
            },
            scrolltolower() {
                console.log("触底了");
                if (this.loadStatus != 'nomore') {
                    this.getGoods();
                }
            },
            //取得商品数据
            getGoods: function () {
                uni.showLoading({
                    title: '加载中'
                });
                var _this = this;
                let data = {
                    page: this.page,
                    limit: this.limit,
                };
                var where = {
                    catId: this.needCurrentCateId,
                };
                data.where = JSON.stringify(where);
                _this.$u.api.goodsList(data).then(res => {
                    if (res.status) {
                        const _list = res.data.list;
                        _this.goodsList = [..._this.goodsList, ..._list];
                        if (res.data.totalPages >= _this.page) {
                            _this.loadStatus = 'loadmore';
                            _this.page++;
                        } else {
                            // 数据已加载完毕
                            _this.loadStatus = 'nomore';
                        }
                        uni.hideLoading();
                    } else {
                        // 接口请求出错了
                        uni.hideLoading();
                        this.$u.toast(res.msg);
                    }
                });
            },
            // 获取购物车数量
            getCartNums() {
                let userToken = this.$db.get("userToken");
                if (userToken && userToken != '') {
                    // 获取购物车数量
                    this.$u.api.getCartNumAndMoney().then(res => {
                        if (res.status) {
                            this.cartNums = res.data.count;
                            this.cartMoney = res.data.money;
                            console.log(this.cartNums);
                        }
                    })
                }
            },
        }
    }
</script>
<style lang="scss" scoped>
    @import "index.scss";
</style>