deming/components/shop/list/index.vue
2020-08-04 21:50:12 +08:00

148 lines
4.0 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<template>
<view class="list">
<view class="top">商品推荐</view>
<view>
<u-tabs-swiper ref="uTabs" :list="classifyList" name="gc_name" :is-scroll="true" active-color="#FF780F" :current="current" font-size="24" :show-bar="false" @change="tabsChange" height="60" ></u-tabs-swiper>
</view>
<swiper class="swiper-box" :current="swiperCurrent" @transition="transition" @animationfinish="animationfinish" :style="{height: swiperHeight}">
<swiper-item class="swiper-item" v-for="(_, index) in classifyList" :key="index">
<view class="goods-item">
<item v-for="item in goodsList" :key="item.goods_id" :info="item"></item>
</view>
<u-empty text="暂无商品" mode="list" color="#000" v-if="!goodsList.length"></u-empty>
</swiper-item>
</swiper>
<!-- 加载更多 -->
<u-loadmore :status="loadStatus" bgColor="#FFF" margin-top="20" margin-bottom="20" @loadmore="loadMore" v-if="goodsList.length>=pageSize"></u-loadmore>
</view>
</template>
<script>
import item from "./item";
export default {
name:"list",
data() {
return {
pageSize: 12,
current: -1,
swiperCurrent: 0,
goodsList: [],
classifyList: [],
swiperHeight: '',
page: 1, // 从 1 开始
loadStatus: 'loadmore',
timer: true,
}
},
components: {
item,
},
watch: {
current(index) {
this.page = 1;
const id = this.classifyList[index].gc_id;
this.getGoodsRecommend({gc_id: id});
}
},
created() {
this.getGoodsClassRecommend();
},
methods: {
loadMore(page) {
if(!this.timer) return false;
this.loadStatus = "loading";
this.page++;
this.getGoodsRecommend({
gc_id: this.classifyList[this.current].gc_id,
page: this.page,
reload: false,
}).then(length => {
// console.log(length);
if(length == 0) {
this.page--;
this.loadStatus = 'nomore';
} else {
this.loadStatus = 'loadmore';
}
}).catch(() => {
this.loadStatus = "nomore";
this.page--;
})
},
getGoodsClassRecommend(gc_id) {
this.$u.api.getGoodsClassRecommend().then(res => {
if (res.errCode == 0) {
// 初始化 current
this.current = 0;
this.classifyList = res.data.gc_recommend;
}
})
},
async getGoodsRecommend({ page = this.page, gc_id, reload = true } = {}) {
const res = await this.$u.api.getGoodsRecommend({
page: page,
gc_id: gc_id,
})
if (res.errCode == 0) {
this.timer = true;
if(reload) this.goodsList = res.data.goodsList;
else this.goodsList.push(...res.data.goodsList);
// console.log(this.goodsList);
this.setSwiperHeight();
}
return res.data.goodsList.length;
},
setSwiperHeight() {
// height: 230px, margin-bottom: 13
const height = Math.ceil(this.goodsList.length / 2) * (510 + 26);
this.swiperHeight = height == 0 ? '230rpx' : height + 'rpx';
},
// tabs通知swiper切换
tabsChange(index) {
this.swiperCurrent = index;
},
// swiper-item左右移动通知tabs的滑块跟随移动
transition(e) {
let dx = e.detail.dx;
this.$refs.uTabs.setDx(dx);
},
// 由于swiper的内部机制问题快速切换swiper不会触发dx的连续变化需要在结束时重置状态
// swiper滑动结束分别设置tabs和swiper的状态
animationfinish(e) {
let current = e.detail.current;
this.$refs.uTabs.setFinishCurrent(current);
this.swiperCurrent = current;
this.current = current;
},
}
}
</script>
<style lang="scss" scoped>
.list{
background-color: #ffffff;
padding: 30rpx {
top: 0;
};
.top {
font-size: 30rpx;
height: 90rpx;
line-height: 90rpx;
text-align: center;
color: #333;
}
.swiper-box {
height: 100%;
margin-bottom: 10rpx;
.swiper-item {
height: 100%;
.goods-item {
display: flex;
flex-wrap: wrap;
justify-content: space-between;
width: 100%;
height: 100%;
overflow-y: scroll;
}
}
}
}
</style>