deming/components/shop/list/index.vue

150 lines
4.3 KiB
Vue
Raw Normal View History

2020-06-11 16:12:04 +08:00
<template>
<view class="list">
2020-07-24 19:48:57 +08:00
<view class="top">商品推荐</view>
2020-07-17 17:34:42 +08:00
<view>
2020-08-08 12:07:50 +08:00
<!-- <u-tabs-swiper ref="uTabs" :list="classifyList" name="gc_name" :is-scroll="true" active-color="#FF780F" :current="current" font-size="26" :show-bar="false" @change="tabsChange" height="60"></u-tabs-swiper> -->
<u-tabs :list="classifyList" name="gc_name" :is-scroll="true" :current="current" @change="tabsChange" active-color="#FF780F" :show-bar="false" height="60" font-size="24" inactive-color="#333333"></u-tabs>
2020-07-16 17:39:06 +08:00
</view>
2020-08-08 12:07:50 +08:00
<swiper class="swiper-box" :current="swiperCurrent" @animationfinish="animationfinish" :style="{height: swiperHeight}">
2020-07-17 17:34:42 +08:00
<swiper-item class="swiper-item" v-for="(_, index) in classifyList" :key="index">
2020-08-08 12:07:50 +08:00
<view class="goods-item">
<item v-for="item in goodsList[index]" :key="item.goods_id" :info="item"></item>
2020-07-24 19:48:57 +08:00
</view>
2020-08-08 12:07:50 +08:00
<u-empty text="暂无商品" mode="list" color="#000" margin-top="20" v-if="!goodsList[index] || !goodsList[index].length"></u-empty>
2020-07-16 17:39:06 +08:00
</swiper-item>
2020-07-17 17:34:42 +08:00
</swiper>
2020-07-23 20:58:56 +08:00
<!-- 加载更多 -->
2020-08-07 11:59:08 +08:00
<u-loadmore :status="loadStatus" bgColor="#FFF" margin-top="20" margin-bottom="20" @loadmore="loadMore"></u-loadmore>
2020-07-24 19:48:57 +08:00
</view>
2020-06-11 16:12:04 +08:00
</template>
<script>
2020-07-23 20:58:56 +08:00
import item from "./item";
2020-06-11 16:12:04 +08:00
export default {
2020-07-16 17:39:06 +08:00
name:"list",
data() {
return {
2020-08-07 11:59:08 +08:00
// pageSize: 12,
2020-07-23 14:56:20 +08:00
current: -1,
2020-07-16 17:39:06 +08:00
swiperCurrent: 0,
2020-07-17 17:34:42 +08:00
goodsList: [],
2020-07-23 14:56:20 +08:00
classifyList: [],
swiperHeight: '',
2020-07-23 20:58:56 +08:00
page: 1, // 从 1 开始
loadStatus: 'loadmore',
timer: true,
2020-07-16 17:39:06 +08:00
}
},
2020-07-23 20:58:56 +08:00
components: {
item,
2020-06-18 14:57:26 +08:00
},
2020-07-17 17:34:42 +08:00
watch: {
current(index) {
2020-07-27 17:59:47 +08:00
this.page = 1;
2020-08-08 12:07:50 +08:00
// this.goodsList = [];
2020-07-17 17:34:42 +08:00
const id = this.classifyList[index].gc_id;
2020-07-23 20:58:56 +08:00
this.getGoodsRecommend({gc_id: id});
2020-07-17 17:34:42 +08:00
}
},
created() {
2020-07-23 14:56:20 +08:00
this.getGoodsClassRecommend();
2020-07-16 17:39:06 +08:00
},
methods: {
2020-07-23 20:58:56 +08:00
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 => {
2020-07-24 20:35:28 +08:00
// console.log(length);
2020-07-23 20:58:56 +08:00
if(length == 0) {
this.page--;
this.loadStatus = 'nomore';
} else {
2020-07-30 20:33:16 +08:00
this.loadStatus = 'loadmore';
2020-07-23 20:58:56 +08:00
}
}).catch(() => {
this.loadStatus = "nomore";
this.page--;
})
},
2020-08-05 17:38:11 +08:00
getGoodsClassRecommend() {
2020-07-23 14:56:20 +08:00
this.$u.api.getGoodsClassRecommend().then(res => {
if (res.errCode == 0) {
// 初始化 current
this.current = 0;
this.classifyList = res.data.gc_recommend;
}
})
},
2020-07-23 20:58:56 +08:00
async getGoodsRecommend({ page = this.page, gc_id, reload = true } = {}) {
const res = await this.$u.api.getGoodsRecommend({
page: page,
2020-07-17 17:34:42 +08:00
gc_id: gc_id,
})
2020-08-05 17:38:11 +08:00
// this.swiperCurrent = this.current;
2020-07-23 20:58:56 +08:00
if (res.errCode == 0) {
2020-07-30 20:33:16 +08:00
this.timer = true;
2020-08-08 12:07:50 +08:00
if(reload) this.goodsList[this.current] = res.data.goodsList;
else this.goodsList[this.current].push(...res.data.goodsList);
2020-07-23 20:58:56 +08:00
// console.log(this.goodsList);
this.setSwiperHeight();
}
2020-08-08 12:07:50 +08:00
this.$forceUpdate();
2020-07-23 20:58:56 +08:00
return res.data.goodsList.length;
2020-07-17 17:34:42 +08:00
},
2020-07-23 14:56:20 +08:00
setSwiperHeight() {
2020-08-08 12:07:50 +08:00
// height: 480rpx, margin-bottom: 26rpx
const height = Math.ceil(this.goodsList[this.current].length / 2) * (480 + 26);
2020-08-05 17:38:11 +08:00
this.swiperHeight = height == 0 ? '230rpx' : height + 'rpx';
2020-08-08 12:07:50 +08:00
// console.log(this.swiperHeight);
2020-07-23 14:56:20 +08:00
},
2020-07-16 17:39:06 +08:00
// tabs通知swiper切换
tabsChange(index) {
2020-08-05 17:38:11 +08:00
this.current = index;
2020-08-08 12:07:50 +08:00
this.swiperCurrent = this.current;
2020-08-05 17:38:11 +08:00
// this.getGoodsRecommend({ gc_id: this.classifyList[this.current].gc_id });
2020-07-16 17:39:06 +08:00
},
// 由于swiper的内部机制问题快速切换swiper不会触发dx的连续变化需要在结束时重置状态
// swiper滑动结束分别设置tabs和swiper的状态
animationfinish(e) {
let current = e.detail.current;
this.current = current;
2020-08-05 17:38:11 +08:00
this.swiperCurrent = current;
// this.getGoodsRecommend({ gc_id: this.classifyList[this.current].gc_id });
2020-07-16 17:39:06 +08:00
},
2020-06-18 14:57:26 +08:00
}
2020-06-11 16:12:04 +08:00
}
</script>
<style lang="scss" scoped>
.list{
2020-07-24 19:48:57 +08:00
background-color: #ffffff;
padding: 30rpx {
top: 0;
};
.top {
2020-06-11 16:37:52 +08:00
font-size: 30rpx;
2020-08-05 22:06:09 +08:00
height: 74rpx;
line-height: 88rpx;
2020-06-11 16:37:52 +08:00
text-align: center;
color: #333;
}
2020-08-04 21:46:52 +08:00
.swiper-box {
height: 100%;
2020-08-08 12:07:50 +08:00
padding-top: 20rpx;
2020-08-04 21:46:52 +08:00
.swiper-item {
2020-08-06 10:48:21 +08:00
// height: 100%;
2020-08-04 21:46:52 +08:00
.goods-item {
display: flex;
flex-wrap: wrap;
justify-content: space-between;
width: 100%;
2020-08-06 10:48:21 +08:00
// height: 100%;
2020-08-04 21:46:52 +08:00
overflow-y: scroll;
}
}
}
2020-06-11 16:12:04 +08:00
}
</style>