文件
xionggeshop/xgsc.gynhc.com_wx/pages/index/components/sh-hot-goods.vue
2026-09-14 17:24:53 +08:00

586 行
7.5 KiB
Vue

<template>
<!-- 为你推荐 -->
<view class="hot-goods" :class="{ 'hot-goods--compact': compactMode }">
<view class="home-goods-grid" v-if="compactMode">
<view class="home-goods-item" v-for="item in goodsList" :key="item.id">
<shopro-goods-card
layout="grid"
:detail="item"
:type="item.activity_type || ''"
:image="item.image"
:title="item.title"
:subtitle="item.subtitle"
:price="item.price"
:originPrice="item.original_price"
:tagTextList="item.activity_discounts_tags"
@click="$Router.push({ path: '/pages/goods/detail', query: { id: item.id } })"
></shopro-goods-card>
</view>
</view>
<view class="u-waterfall" v-else-if="goodsType === 1">
<view id="u-left-column" class="u-column">
<view class="goods-item u-m-b-16 u-flex u-row-center u-col-center" v-for="leftGoods in leftList" :key="leftGoods.id">
<shopro-goods-card
:detail="leftGoods"
:type="leftGoods.activity_type"
:image="leftGoods.image"
:title="leftGoods.title"
:subtitle="leftGoods.subtitle"
:price="leftGoods.price"
:originPrice="leftGoods.original_price"
:sales="leftGoods.sales"
:tagTextList="leftGoods.activity_discounts_tags"
@click="$Router.push({ path: '/pages/goods/detail', query: { id: leftGoods.id } })"
></shopro-goods-card>
</view>
</view>
<view id="u-right-column" class="u-column">
<view class="goods-item u-m-b-16 u-flex u-row-center u-col-center" v-for="rightGoods in rightList" :key="rightGoods.id">
<shopro-goods-card
:detail="rightGoods"
:type="rightGoods.activity_type"
:image="rightGoods.image"
:title="rightGoods.title"
:subtitle="rightGoods.subtitle"
:price="rightGoods.price"
:originPrice="rightGoods.original_price"
:sales="rightGoods.sales"
:tagTextList="rightGoods.activity_discounts_tags"
@click="$Router.push({ path: '/pages/goods/detail', query: { id: rightGoods.id } })"
></shopro-goods-card>
</view>
</view>
</view>
<!-- m -->
<view class="big-card-wrap u-p-10" v-else-if="goodsType === 2">
<block v-for="item in goodsList" :key="item.id">
<sh-goods-card
:detail="item"
:type="item.activity_type"
:image="item.image"
:title="item.title"
:subtitle="item.subtitle"
:price="item.price"
:originPrice="item.original_price"
:sales="item.sales"
:tagTextList="item.activity_discounts_tags"
@click="$Router.push({ path: '/pages/goods/detail', query: { id: item.id } })"
></sh-goods-card>
</block>
</view>
<button v-show="!compactMode && total > perPage" class="u-reset-button refresh-btn u-m-y-20 u-flex u-col-center u-row-center" @tap.stop="loadMore">
<text class="u-m-r-6 u-iconfont uicon-reload" style="font-size: 28rpx;color: #999" :class="{ 'refresh-active': isRefresh }"></text>
{{ listParams.page >= lastPage ? '收起' : '加载更多' }}
</button>
<view class="x-c" v-if="compactMode && goodsList.length">
<u-loadmore height="80rpx" :status="loadStatus" icon-type="flower" color="#ccc" />
</view>
<shopro-empty
v-if="compactMode && !goodsList.length && loadStatus !== 'loading'"
marginTop="80rpx"
:image="$IMG_URL + '/imgs/empty/empty_goods.png'"
tipText="暂无商品"
></shopro-empty>
</view>
</template>
<script>
/**
* 自定义之为你推荐
* @property {Object} detail - 推荐商品信息
*/
import shGoodsCard from './sh-goods-card.vue';
export default {
components: {
shGoodsCard
},
data() {
return {
listParams: {
page: 1
},
lastPage: 1,
total: 0,
perPage: 0,
goodsList: [],
isRefresh: false,
loadStatus: 'loadmore',
addTime: 100,
leftHeight: 0,
rightHeight: 0,
leftList: [],
rightList: [],
tempList: [],
goodsType: this.detail?.style || 1
};
},
props: {
detail: {
type: Object,
default: () => ({})
},
compactMode: {
type: Boolean,
default: false
}
},
created() {
this.initGoodsList();
},
methods: {
initGoodsList() {
if (this.detail?.id) {
this.listParams.category_id = this.detail.id;
this.getGoodsList();
return;
}
if (this.detail?.ids) {
this.listParams.goods_ids = this.detail.ids;
this.getGoodsList();
return;
}
if (this.compactMode) {
this.getGoodsList();
}
},
async splitData() {
if (!this.tempList.length) return;
let item = this.tempList[0];
if (!item) return;
if (this.leftHeight < this.rightHeight) {
this.leftHeight += item.activity_discounts_tags?.length ? 350 : 330;
this.leftList.push(item);
} else if (this.leftHeight > this.rightHeight) {
this.rightHeight += item.activity_discounts_tags?.length ? 350 : 330;
this.rightList.push(item);
} else {
this.leftHeight += item.activity_discounts_tags?.length ? 350 : 330;
this.leftList.push(item);
}
this.tempList.splice(0, 1);
if (this.tempList.length) {
setTimeout(() => {
this.splitData();
}, this.addTime);
}
},
clear() {
this.leftList = [];
this.rightList = [];
this.leftHeight = 0;
this.rightHeight = 0;
this.tempList = [];
},
getGoodsList() {
let that = this;
if (this.compactMode) {
this.loadStatus = 'loading';
}
that.$http('goods.lists', this.listParams).then(res => {
if (res.code === 1) {
this.lastPage = res.data.last_page;
this.total = res.data.total;
this.perPage = res.data.per_page;
this.isRefresh = false;
that.goodsList = [...that.goodsList, ...res.data.data];
if (this.compactMode) {
this.loadStatus = this.listParams.page < res.data.last_page ? 'loadmore' : 'nomore';
return;
}
that.tempList = res.data.data;
that.goodsList.length && that.splitData();
} else if (this.compactMode) {
this.loadStatus = 'nomore';
}
}).catch(() => {
if (this.compactMode) {
this.loadStatus = 'nomore';
}
});
},
loadMore() {
if (!this.isRefresh) {
if (this.listParams.page < this.lastPage) {
this.isRefresh = true;
this.listParams.page += 1;
this.getGoodsList();
} else {
this.listParams.page = 1;
this.lastPage = 1;
this.goodsList = [];
this.clear();
this.getGoodsList();
}
}
}
}
};
</script>
<style lang="scss">
@mixin vue-flex($direction: row) {
/* #ifndef APP-NVUE */
display: flex;
flex-direction: $direction;
/* #endif */
}
.u-waterfall {
@include vue-flex;
flex-direction: row;
align-items: flex-start;
}
.u-column {
@include vue-flex;
flex: 1;
flex-direction: column;
height: auto;
min-width: 0;
& + & {
margin-left: $brand-page-gutter;
}
}
.hot-goods {
background: none;
padding: 0 $brand-page-gutter;
box-sizing: border-box;
.goods-item {
width: 100%;
}
.refresh-btn {
margin-left: 50%;
transform: translateX(-50%);
width: 156rpx;
line-height: 50rpx;
background: #ffffff;
border-radius: 25rpx;
font-size: 22rpx;
font-weight: 500;
color: #999999;
white-space: nowrap;
}
.refresh-active {
transform: rotate(360deg);
transition: all linear 0.5s;
}
}
.hot-goods--compact {
padding: 0;
}
.home-goods-grid {
display: flex;
flex-direction: row;
flex-wrap: wrap;
justify-content: space-between;
padding: 0 $brand-page-gutter 24rpx;
box-sizing: border-box;
}
.home-goods-item {
width: calc(50% - 10rpx);
margin-bottom: 20rpx;
}
</style>