绿满眶商城微信小程序-uniapp
peng
12 小时以前 a6c950ba797dffec842cf7e923fc439868645766
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
<template>
    <view class="container">
        <input type="text" v-show="false" v-model="flushDom" />
        <view class="search">
            <u-search class="nav-search" @blur='searchGoods' placeholder="搜索商品" :show-action="false"></u-search>
        </view>
        <view class="commoditySquare">
            <view class="left" style="width: 310rpx;">
                <view class="commoditySquareItem" v-for="(item,index) in goodsList1"
                    @click="goToGoodsInfo('goodsList1',item.id)">
                    <video :src="item.goodsVideo" v-if="item.goodsVideo " v-show="item.show" :initial-time="0"
                        :controls="false" object-fit="contain" :show-play-btn="false" :show-center-play-btn="false"
                        @loadedmetadata="getvideoInfo($event,'goodsList1',index)" :ref="'video'+item.id"
                        :style="{width:item.width,height:item.height}"></video>
                    <image :src="item.thumbnail" v-if="item.goodsVideo ==null  || item.goodsVideo == ''"
                        mode="aspectFill" class="goodsImg">
                    </image>
                    <view class="goodsInfo">
                        <view class="">{{item.goodsName}}</view>
                        <view class="priceInfo">
                            <view class="">¥{{item.price}}</view>
                            <view class="">已售: {{item.buyCount}}</view>
                        </view>
                    </view>
                </view>
            </view>
            <view class="right" style="width: 310rpx;">
                <view class="commoditySquareItem" v-for="(item,index) in goodsList2"
                    @click="goToGoodsInfo('goodsList2',item.id)">
                    <video :src="item.goodsVideo" v-if="item.goodsVideo " v-show="item.show" :initial-time="0"
                        :controls="false" object-fit="contain" :show-play-btn="false" :show-center-play-btn="false"
                        @loadedmetadata="getvideoInfo($event,'goodsList2',index)" :ref="'video'+item.id"
                        :style="{width:item.width,height:item.height}"></video>
                    <image :src="item.thumbnail" v-if="item.goodsVideo ==null || item.goodsVideo == ''"
                        mode="aspectFill" class="goodsImg">
                    </image>
                    <view class="goodsInfo">
                        <view class="">{{item.goodsName}}</view>
                        <view class="priceInfo">
                            <view class="">¥{{item.price}}</view>
                            <view class="">已售: {{item.buyCount}}</view>
                        </view>
                    </view>
                </view>
            </view>
 
 
            <view class="openShowLeft" @click="showDrawer('showLeft')" v-if="!showLeft">
                <uni-icons type="right" size="30"></uni-icons>
            </view>
            <uni-drawer ref="showLeft" mode="left" width="120" height="94vh" @change="change($event,'showLeft')"
                :cus-style="true" class="navigationLeft">
                <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>
            </uni-drawer>
        </view>
        <view style="display: flex;align-items: center;justify-content: center;margin-top: 20rpx;" v-if="canLoadMore">
            没有更多数据了.................</view>
        <custom-tabbar bgColor="#ffffff" selected="shop"></custom-tabbar>
    </view>
</template>
 
<script>
    import {
        getCategoryList,
        getGoodsList
    } from "@/api/goods.js";
    import {
        getSTSToken
    } from '@/api/common.js'
    export default {
        data() {
            return {
                //记录两列高度
                listHeight1: 0,
                listHeight2: 0,
                //是否显示打开左侧弹窗
                showLeft: false,
                //商品导航分类
                categoryList: [],
                //当前选中的分类
                currentCategort: '',
                //显示没有数据
                canLoadMore: false,
                //最大页数
                maxPages: 0,
                //刷新dom使用
                flushDom: '',
                //查询商品需要的参数
                getGoodsParam: {
                    keyword: '',
                    pageSize: 12,
                    pageNumber: 1,
                    categoryId: null,
                },
                //商品双列显示
                goodsList1: [],
                goodsList2: [],
            }
        },
        methods: {
            async searchGoods(keyWard) {
                this.getGoodsParam.keyword = keyWard
                this.getGoodsParam.pageNumber = 1
                this.getGoodsParam.categoryId = ''
                this.goodsList1 = []
                this.goodsList2 = []
                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(arrName, id) {
                const item = this[arrName].find(item => id === item.id);
                uni.navigateTo({
                    url: `/pages/product/goods?id=${item.id}&goodsId=${item.goodsId}`
                });
            },
            //获取分类导航栏
            async loadCategoryList() {
                let list = await getCategoryList(0);
                this.categoryList = list.data.result
            },
            async getgoodsData() {
                const goodsList = await getGoodsList(this.getGoodsParam);
                const sts = await getSTSToken();
                const stsUrl = sts.data.data.endpoint
                // 处理数据
                goodsList.data.result.records.forEach(item => {
                    if (item.thumbnail !== '' && item.thumbnail !== null && item.thumbnail.indexOf('http') ===
                        -1) {
                        item.thumbnail = stsUrl + '/' + item.thumbnail
                    }
                    if (item.goodsVideo !== '' && item.goodsVideo !== null && item.goodsVideo.indexOf(
                            'http') === -1) {
                        item.goodsVideo = stsUrl + '/' + item.goodsVideo
                    }
                })
                //计算两个数组的长度 用于解决瀑布流两边高度不一致问题
                //平分给两个数组
                const goodsSize = goodsList.data.result.records.length;
                let middle = Math.ceil(goodsSize / 2);
                //判断那个集合长度高 重新分配集合数据
                const diffHeight = this.listHeight1 - this.listHeight2;
                //list1长度大于list2
                if (diffHeight > 0) {
                    const needCount = Math.floor(diffHeight / 300)
                    if (needCount > 6) {
                        middle = 0;
                    } else {
                        middle -= needCount;
                    }
                } else if(diffHeight < 0) {
                    const needCount = Math.abs(Math.floor(diffHeight / 300))
                    if (needCount > 6) {
                        middle += 6
                    } else {
                        middle = needCount;
                    }
                }
                this.goodsList1 = [...this.goodsList1, ...goodsList.data.result.records.slice(0, middle)];
                this.goodsList2 = [...this.goodsList2, ...goodsList.data.result.records.slice(middle)];
                this.maxPages = goodsList.data.result.pages
 
                // if(this.listHeight1===0||this.listHeight2===0)}
                this.listHeight1 += middle * 300;
                this.listHeight2 += (goodsSize - middle) * 300
            },
            async chooseCategory(id) {
                this.canLoadMore = false;
                this.currentCategort = id
                this.getGoodsParam.categoryId = id
                this.getGoodsParam.keyword = ''
                this.getGoodsParam.pageNumber = 1
                this.goodsList1 = [];
                this.goodsList2 = [];
                await this.getgoodsData();
                console.log(this.maxPages)
            }
        },
        onShow() {
            this.showDrawer('showLeft')
        },
        async onLoad() {
            await this.loadCategoryList();
            this.getGoodsParam.pageNumber = 1
            this.getGoodsParam.categoryId = ""
            this.getGoodsParam.keyword = ""
            await this.getgoodsData()
        },
        async onReachBottom() {
            if (this.getGoodsParam.pageNumber < this.maxPages) {
                this.getGoodsParam.pageNumber++;
                await this.getgoodsData()
            } else {
                this.canLoadMore = true;
            }
 
        }
    }
</script>
 
<style lang="scss" scoped>
    .container {
        width: 750rpx;
        padding-bottom: 64rpx;
    }
 
    .commoditySquare {
        width: 750rpx;
        position: relative;
        box-sizing: border-box;
        display: flex;
        justify-content: space-around;
    }
 
    .left {
        box-sizing: border-box;
    }
 
    .right {
        box-sizing: border-box;
    }
 
    .goodsImg {
        width: 300rpx;
        height: 300rpx;
        padding-bottom: 10rpx;
    }
 
    .commoditySquareItem {
        // border: 1rpx solid #b6dbba;
        border-radius: 12rpx;
        box-sizing: border-box;
        padding: 0 32rpx;
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
    }
 
    .commoditySquareItem:nth-child(n+1) {
        margin-top: 32rpx;
    }
 
    .openShowLeft {
        position: fixed;
        top: 120rpx;
        left: 0;
        align-items: center;
        justify-content: flex-start;
        background-color: #b6b6b6;
        opacity: 0.8;
        z-index: 999;
        height: 70rpx;
        width: 50rpx;
        border-radius: 0 50% 50% 0;
    }
 
    .navigationLeft {
        position: relative;
        background-color: #fcfcfc;
        // opacity: 0.5;
        box-sizing: border-box;
    }
 
 
    .scroll-view {
        /* #ifndef APP-NVUE */
        width: 100%;
        height: 100%;
        /* #endif */
        flex: 1
    }
 
    .priceInfo {
        margin-top: 10rpx;
        display: flex;
        justify-content: space-around;
    }
 
    // 处理抽屉内容滚动
    .scroll-view-box {
        flex: 1;
        position: absolute;
        top: 0;
        right: 0;
        bottom: 0;
        left: 0;
    }
 
 
    .typeNavigation {
        box-sizing: border-box;
        padding: 10rpx;
        height: 100%;
        // position: relative;
    }
 
    .typeNavigationItem {
        padding: 24rpx;
        font-size: 28rpx;
        color: black;
        margin-top: 10rpx;
        border-radius: 12rpx;
        border: 1rpx solid gray;
    }
 
    .typeNavigationItemCheck {
        background-color: #42b993;
        color: #fff;
        border: 0;
    }
 
    .closeShowLeft {
        position: absolute;
        top: 20rpx;
        right: 0;
    }
 
    .nav-search {
        padding-left: 30rpx !important;
        padding-right: 20rpx !important;
        // position: fixed;
        // top: 0;
        // width: 500rpx;
        // z-index: 9999;
    }
</style>