绿满眶商城微信小程序-uniapp
zxl
7 天以前 b303b6945a139153688e86635346a621fe0c29b3
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
<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" @blur='searchGoods' v-model="searchInfo" placeholder="搜索商品"
                :show-action="false"></u-search>
        </view>
        <view style="display: flex;padding: 32rpx;box-sizing: border-box;height: 100%;">
            <view class="navigatorMenue">
                <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">
                <scroll-view :scroll-y="true" :show-scrollbar="false">
 
                    <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; ">
                        <view class="pic" style="position: relative;">
                            <image :src="item.thumbnail" mode="aspectFill"
                                style="height: 150rpx;width: 150rpx;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 class="goodsContent" 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
                                style="display: flex; justify-content: space-between;align-items:center; 100%;margin-top: 32rpx;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>
 
    </view>
</template>
 
<script>
    import TopBar from "@/components/TopBar.vue";
    import {
        getCategoryList,
        getGoodsList
    } 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 {
                searchInfo: '',
                //商品列表
                goodsList: [],
                buttonHeight: 0,
                //记录两列高度
                //商品导航分类
                categoryList: [],
                //当前选中的分类
                currentCategort: '',
                //显示没有数据
                canLoadMore: false,
                //最大页数
                maxPages: 0,
                //刷新dom使用
                flushDom: '',
                //查询商品需要的参数
                getGoodsParam: {
                    keyword: '',
                    pageSize: 12,
                    pageNumber: 1,
                    categoryId: null,
                    canFilter: true,
                    needFilterPre:true
                },
            }
        },
        methods: {
            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();
                        console.log("选中的商品为------------------->", JSON.stringify(item))
                        const data = {
                            skuId: item.id,
                            num: item.cardNum
                        }
                        addCard(data)
                        return;
                    }
                })
 
            },
            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 {
                            deleteSkuItem([id]);
                            item.cardNum = 0;
                            this.flushDom = new Date().getMilliseconds();
                            break; // 退出整个循环
                        }
                        this.flushDom = new Date().getMilliseconds();
                        const data = {
                            skuId: item.id,
                            num: item.cardNum
                        };
                        addCard(data);
                        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 = ''
                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);
                uni.navigateTo({
                    url: `/pages/product/goods?id=${item.id}&goodsId=${item.goodsId}`
                });
            },
            //触底获取数据
            async loadMoreData() {
                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 getgoodsData() {
                const cardInfos = await getCarts();
                // 处理购物车信息
                let cardMap = new Map();
                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 = goodsList.data.result.records;
                console.log('this.goodsList----------------------->',JSON.stringify(this.goodsList))
 
            },
            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() {
            // 获取状态栏高度
            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()
        }
    }
</script>
 
<style lang="scss" scoped>
    .container {
        width: 750rpx;
        padding-bottom: 64rpx;
        height: 100vh;
        overflow: hidden;
 
    }
 
 
    .navigatorMenue {
        margin-left: -32rpx;
        background-color: #f4f4f4;
        height: 100%;
        width: 200rpx;
        border-radius: 0 24rpx 24rpx 0;
    }
 
    .goodsInfos {
        width: 500rpx;
        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;
    }
 
    .goodsPrice {
        color: red;
        font-weight: bold;
    }
</style>