绿满眶商城微信小程序-uniapp
peng
2025-08-08 8a2d6949a4525f90e0e75f2f3f83c13b8830a758
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
526
527
<template>
    <view class="container" :style="{paddingTop:buttonHeight+'px'}">
        <input type="text" value="flushDom" v-if="false" />
        <top-bar selectedTitleIndex="shop" textColor="black" @changeTab="topBarChange" class="topBar"></top-bar>
        <view class="search">
            <u-search class="nav-search" @change='searchGoods' v-model="searchInfo" placeholder="搜索商品"
                :show-action="false"></u-search>
        </view>
        <view class="banner" style="margin-top: 14rpx;" v-if="bannerList.length>0">
            <swiper :indicator-dots="false" :autoplay="true" :interval="3000" :duration="1000" :circular="true"
                style="width: 100%;height: 160rpx;">
                <swiper-item v-for="item in bannerList" :key="item.id"
                    @click="navigateGoodsInfo(item.skuId,item.goodsId)">
                    <view style="width: 100%; display: flex; align-items: center;justify-content: center;">
                        <image :src="item.showBannerUrl" mode="aspectFill"
                            style="width: 690rpx;height: 150rpx;border-radius: 24rpx;overflow: hidden;"></image>
                    </view>
                </swiper-item>
            </swiper>
        </view>
        <view style="display: flex;padding: 32rpx;box-sizing: border-box;">
            <view class="navigatorMenue" style="height: 1050rpx;">
                <scroll-view class="typeNavigation" :scroll-y="true" :show-scrollbar="false">
                    <view class="typeNavigationItem" :class="{typeNavigationItemCheck:currentCategort ==item.id}"
                        @click="chooseCategory(item.id)" v-for="item in categoryList" :key="item.id">
                        {{item.name}}
                    </view>
                </scroll-view>
 
            </view>
            <view class="goodsInfos" style="position: relative;overflow: hidden;box-sizing: border-box;">
                <scroll-view :scroll-y="true" :show-scrollbar="false" style="height: 1050rpx;"
                    @scrolltolower="loadMoreData()">
 
                    <view class="goodsItem" v-for="item in goodsList" :key="item.id"
                        @click.prevent="goToGoodsInfo(item.id)" style="display: flex;align-items: flex-start;justify-content: flex-start;margin: 10rpx 0;
                        box-sizing: border-box; border-bottom: 1rpx solid lightgray;">
                        <view class="pic" style="position: relative;">
                            <image :src="item.thumbnail" mode="aspectFill"
                                style="height: 200rpx;width: 200rpx;border-radius: 24rpx;">
                            </image>
                            <text style="background-color: #f92e20;color: #fff;
                                font-weight: bold;border-radius: 12rpx;padding: 5rpx;
                                margin-right: 15rpx;position: absolute;top: 0;left: 0;" v-if="item.salesModel==='PRESALE'">预售</text>
                        </view>
                        <view style="display: flex;align-items: flex-start;justify-content: space-between;
                            margin-left: 10rpx;flex-direction: column;width: 100%;">
                            <view class="goodsName">
                                <text>{{item.goodsName}}</text>
                            </view>
                            <view class="goodsContent">
                                {{item.sellingPoint}}
                            </view>
                            <view
                                style="display: flex; justify-content: space-between;align-items:center; 100%;width: 100%;">
                                <view class="goodsPrice">¥{{item.price}}</view>
                                <view class="addCard" style="display: flex; align-items: center;">
                                    <view style="padding: 20rpx 0 20rpx 20rpx;" v-show="item.cardNum && item.cardNum>0"
                                        @click.stop="miousCardNum($event,item.id)">
                                        <uni-icons type="minus" size="30" color="#96a6bc"></uni-icons>
                                    </view>
                                    <input v-show="item.cardNum&&item.cardNum>0" @click.stop="" :maxlength="2"
                                        type="number" confirm-type="numeric" style="width: 50rpx;text-align: center;"
                                        v-model="item.cardNum" :disabled="true" />
                                    <view style="padding: 20rpx 20rpx 20rpx 0;"
                                        @click.stop="addCardNum($event,item.id)">
                                        <uni-icons type="plus" size="30" color="#e06c75"></uni-icons>
                                        <!-- <text class="iconfont" style="size: 1.5em;">&#xe7b1;</text> -->
                                    </view>
                                </view>
                            </view>
                        </view>
                    </view>
                </scroll-view>
            </view>
            <!--         <view @click="gotoCardList()" style="position: absolute;bottom: 120rpx; right: 32rpx;">
                <view class="settlement">去购物车结算</view>
            </view> -->
        </view>
        <view class="squareFotter" style="width: 750rpx; display: flex;align-items: center; 
            justify-content: space-between;padding: 0 32rpx;box-sizing: border-box;">
            <view style="display: flex;align-items: center;justify-content: center;">
                <view class="icon" style="position: relative;">
                    <text class="iconfont">&#xe761;</text>
                    <view style=" height: 36rpx;width:36rpx;display: flex;
                         align-items: center;
                         justify-content: center;
                         background-color: #f9583b;border-radius: 100%; position: absolute; 
                         top: -25rpx; right: -20rpx;color: #fff;font-size: 24rpx;">
                        {{priceInfo.num > 99?'···':priceInfo.num}}
                    </view>
                </view>
                <view style="margin-left: 10rpx;color: #e06c75;font-size: 48rpx;font-weight: bold;">
                    ¥{{priceInfo.price}}</view>
            </view>
            <view class="settlement" @click="gotoCardList()">去结算</view>
        </view>
    </view>
</template>
 
<script>
    import TopBar from "@/components/TopBar.vue";
    import {
        getCategoryList,
        getGoodsList,
        getBannerList
    } from "@/api/goods.js";
    import {
        addCard,
        getCarts,
        deleteSkuItem
    } from '@/api/trade.js'
    import {
        getSTSToken
    } from '@/api/common.js'
    import {
        data
    } from "../../components/uview-components/uview-ui/libs/mixin/mixin";
    export default {
        components: {
            TopBar
        },
        data() {
            return {
                priceInfo: {
                    price: 0,
                    num: 0
                },
                bannerList: [],
                canGoToCarList: false,
                nums: 10,
                searchInfo: '',
                //商品列表
                goodsList: [],
                buttonHeight: 0,
                //记录两列高度
                //商品导航分类
                categoryList: [],
                //当前选中的分类
                currentCategort: '',
                //显示没有数据
                canLoadMore: false,
                //最大页数
                maxPages: 0,
                //刷新dom使用
                flushDom: '',
                //查询商品需要的参数
                getGoodsParam: {
                    keyword: '',
                    pageSize: 12,
                    pageNumber: 1,
                    categoryId: null,
                    canFilter: true,
                    needFilterPre: true,
                    order: 'asc',
                    sort: 'goodsSort'
                },
            }
        },
        methods: {
            navigateGoodsInfo(skuId, goodsId) {
                uni.navigateTo({
                    url: `/pages/product/goods?id=${skuId}&goodsId=${goodsId}`
                });
            },
            gotoCardList() {
                uni.navigateTo({
                    url: '/pages/cusbar/cart/cartList'
                })
            },
            async addCardNum(e, id) {
                // this.goodsList.forEach(item => {
                //     if (item.id === id) {
                //         if (item.cardNum) {
                //             item.cardNum++;
                //         } else {
                //             item.cardNum = 1;
                //         }
                //         this.flushDom = new Date().getMilliseconds();
                //         const data = {
                //             skuId: item.id,
                //             num: item.cardNum
                //         }
                //         await addCard(data)
                //         await this.getCartList();
                //         return;
                //     }
                // })
                for (let i = 0; i < this.goodsList.length; i++) {
                    const item = this.goodsList[i];
                    if (item.id === id) {
                        if (item.cardNum) {
                            item.cardNum++;
                        } else {
                            item.cardNum = 1;
                        }
                        this.flushDom = new Date().getMilliseconds();
                        const data = {
                            skuId: item.id,
                            num: item.cardNum
                        }
                        await addCard(data)
                        await this.getCartList();
                        return;
                    }
                }
            },
            async miousCardNum(e, id) {
                for (let i = 0; i < this.goodsList.length; i++) {
                    const item = this.goodsList[i];
                    if (item.id === id) {
                        if (item.cardNum && item.cardNum > 1) {
                            item.cardNum--;
                        } else {
                            await deleteSkuItem([id]);
                            item.cardNum = 0;
                            this.flushDom = new Date().getMilliseconds();
                            await this.getCartList();
                            break; // 退出整个循环
                        }
                        this.flushDom = new Date().getMilliseconds();
                        const data = {
                            skuId: item.id,
                            num: item.cardNum
                        };
                        await addCard(data);
                        await this.getCartList();
                        break; // 退出整个循环
                    }
                }
                // this.goodsList.forEach(item => {
                //     console.log('测试为什么不进入循环1',id,JSON.stringify(item.cardNum))
                //     if (item.id === id) {
                //         console.log('测试为什么不进入循环2',id,JSON.stringify(item.cardNum))
                //         if (item.cardNum && item.cardNum > 1) {
                //             console.log('测试为什么不进入循环3',id,JSON.stringify(item.cardNum))
                //             item.cardNum--;
                //         } else {
                //             console.log('测试为什么不进入循环4',id,JSON.stringify(item.cardNum))
                //             deleteSkuItem([id])
                //             item.cardNum = 0;
                //             this.flushDom = new Date().getMilliseconds();
                //             return;
                //         }
                //         this.flushDom = new Date().getMilliseconds();
                //         console.log("选中的商品为------------------->", JSON.stringify(item))
                //         console.log(item.cardNum, item.id)
                //         const data = {
                //             skuId: item.id,
                //             num: item.cardNum
                //         }
                //         addCard(data)
                //         return;
                //     }
                // })
 
            },
            topBarChange(titleObj) {
                if (titleObj.index === 'home') {
                    uni.switchTab({
                        url: titleObj.pagePath
                    });
                } else {
                    uni.redirectTo({
                        url: titleObj.pagePath
                    });
                }
            },
            async searchGoods(keyWard) {
                this.getGoodsParam.keyword = keyWard
                this.getGoodsParam.pageNumber = 1
                this.getGoodsParam.categoryId = ''
                this.goodsList = [];
                await this.getgoodsData();
            },
            confirm() {},
            // 打开窗口
            showDrawer(e) {
                this.$refs[e].open()
            },
            // 关闭窗口
            closeDrawer(e) {
                this.$refs[e].close()
            },
            // 抽屉状态发生变化触发
            change(e, type) {
                this[type] = e
            },
            getvideoInfo(e, arrName, index) {
                const wight = e.detail.width;
                const height = e.detail.height;
                this[arrName][index].width = 310 + 'rpx';
                //计算宽高比
                const videoHeight = Math.floor(310 / (wight / height))
                this[arrName][index].height = videoHeight + 'rpx';
                this[arrName][index].show = true;
                //计算集合高度
                if (arrName === 'goodsList2') {
                    if (videoHeight > 300) {
                        this.listHeight2 += videoHeight;
                    } else if (videoHeight < 300) {
                        this.listHeight2 -= videoHeight;
                    }
                }
                if (arrName === 'goodsList1') {
                    if (videoHeight > 300) {
                        this.listHeight1 += videoHeight;
                    } else if (videoHeight < 300) {
                        this.listHeight1 -= videoHeight;
                    }
                }
                // console.log(this[arrName][index].width, this[arrName][index].height)
                this.flushDom = new Date();
            },
            goToGoodsInfo(id) {
                const item = this.goodsList.find(item => id === item.id);
                // console.log(item)
                uni.navigateTo({
                    url: `/pages/product/goods?id=${item.id}&goodsId=${item.goodsId}`
                });
            },
            //触底获取数据
            async loadMoreData() {
                // console.log("触底加载------------》", this.maxPages)
                if (this.getGoodsParam.pageNumber < this.maxPages) {
                    this.getGoodsParam.pageNumber++;
                    await this.getgoodsData()
                } else {
                    this.canLoadMore = true;
                }
 
            },
            //获取分类导航栏
            async loadCategoryList() {
                let list = await getCategoryList(0);
                list.data.result.unshift({
                    id: 'ALL',
                    name: '全部'
                })
                this.categoryList = list.data.result
            },
            async getBannerList() {
                const bannerList = await getBannerList();
                this.bannerList = bannerList.data.data
                // console.log(this.bannerList)
            },
            async getCartList() {
                const cardInfos = await getCarts();
                const cardList = cardInfos.data.result.cartList.flatMap(item => {
                    return item.skuList.map(sku => {
                        return (sku.goodsSku.id, {
                            skuId: sku.goodsSku.id,
                            num: sku.num,
                            price: sku.priceDetailDTO.flowPrice
                        })
                    })
 
                })
 
                this.priceInfo = cardList.reduce((newVal, current) => {
                    return {
                        num: newVal.num + current.num,
                        price: newVal.price + current.price
                    }
                }, {
                    num: 0,
                    price: 0
                })
            },
            async getgoodsData() {
                const cardInfos = await getCarts();
                // console.log('--------------------->', cardInfos)
                // 处理购物车信息
                let cardMap = new Map();
                this.canGoToCarList = cardInfos.data.result.cartList.length > 0
                cardInfos.data.result.cartList.forEach(item => {
 
                    item.skuList.forEach(sku => {
                        cardMap.set(sku.goodsSku.id, {
                            skuId: sku.goodsSku.id,
                            num: sku.num
                        })
                    })
 
                })
                //回显购物车数据
                const goodsList = await getGoodsList(this.getGoodsParam);
                goodsList.data.result.records.forEach(item => {
                    const cardItem = cardMap.get(item.id);
                    if (cardItem) {
                        item.cardNum = cardItem.num;
                    }
                })
                this.goodsList = [...this.goodsList, ...goodsList.data.result.records];
                this.maxPages = goodsList.data.result.pages
                // console.log('this.goodsList----------------------->', JSON.stringify(goodsList.data.result))
 
            },
            async chooseCategory(id) {
                this.searchInfo = '';
                this.canLoadMore = false;
                this.currentCategort = id
                this.getGoodsParam.categoryId = id
                this.getGoodsParam.keyword = ''
                this.getGoodsParam.pageNumber = 1
                this.goodsList = []
                if (id === 'ALL') {
                    this.getGoodsParam.categoryId = '';
                } else {
                    this.getGoodsParam.categoryId = id
                }
                await this.getgoodsData();
                console.log(this.maxPages)
            }
        },
 
        async onLoad() {
            await this.getCartList();
            // 获取状态栏高度
            const systemInfo = uni.getSystemInfoSync();
 
            this.buttonHeight = systemInfo.statusBarHeight + 40;
            console.log('-------------------------------->', JSON.stringify(systemInfo))
            await this.loadCategoryList();
            this.getGoodsParam.pageNumber = 1
            this.getGoodsParam.categoryId = ""
            this.getGoodsParam.keyword = ""
            await this.getgoodsData()
            await this.getBannerList()
        }
    }
</script>
 
<style lang="scss" scoped>
    .container {
        box-sizing: border-box;
        width: 750rpx;
        padding-bottom: 64rpx;
        height: 100vh;
        overflow: hidden;
 
    }
 
    .settlement {
        width: 180rpx;
        height: 70rpx;
        line-height: 70rpx;
        background: linear-gradient(91deg, $light-color 1%, $aider-light-color 99%);
        border-radius: 900rpx;
        text-align: center;
        color: #fff;
        margin-right: 10rpx;
    }
 
    .navigatorMenue {
        margin-left: -32rpx;
        background-color: #f4f4f4;
        height: 100%;
        width: 200rpx;
        border-radius: 0 24rpx 24rpx 0;
    }
 
    .goodsInfos {
        box-sizing: border-box;
        width: 500rpx;
        height: 1050rpx;
        margin-left: 10rpx;
    }
 
    .topBar {
        position: fixed;
        top: 20rpx;
        left: 20rpx;
        z-index: 1000;
        background-color: #ffffff;
    }
 
    .typeNavigation {
        box-sizing: border-box;
        padding: 10rpx;
        height: 100%;
    }
 
    .typeNavigationItem {
        padding: 24rpx;
        font-size: 28rpx;
        color: black;
        margin-top: 10rpx;
    }
 
    .typeNavigationItemCheck {
        color: #ef321e;
        font-size: 32rpx;
        font-weight: bold;
        border: 0;
    }
 
    .goodsName {
        // padding: 10rpx;
        box-sizing: border-box;
        // display: -webkit-box;
        // -webkit-box-orient: vertical;
        // -webkit-line-clamp: 1;
        // height: 52rpx;
        font-weight: bold;
        // overflow: hidden;
        // text-overflow: ellipsis;
        // text-align: start;
        // vertical-align: middle;
        text-wrap: wrap;
    }
 
    .goodsContent {
        text-indent: 2em;
        display: -webkit-box;
        -webkit-box-orient: vertical;
        -webkit-line-clamp: 2;
        overflow: hidden;
        text-overflow: ellipsis;
        vertical-align: middle;
    }
 
    .goodsPrice {
        color: red;
        font-weight: bold;
    }
</style>