Files
2025-11-17 14:11:46 +08:00

244 lines
6.4 KiB
JavaScript
Raw Permalink 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.

import { fetchHome } from '../../services/home/home';
import { fetchGoodsList } from '../../services/good/fetchGoods';
import Toast from 'tdesign-miniprogram/toast/index';
Page({
data: {
imgSrcs: [],
tabList: [],
goodsList: [],
recommendProducts: [],
hotProducts: [],
goodsListLoadStatus: 0,
pageLoading: false,
current: 1,
autoplay: true,
duration: '500',
interval: 5000,
navigation: { type: 'dots' },
swiperImageProps: { mode: 'aspectFill' },
currentTabValue: 'hot', // 当前选中的tab值
emptyStateConfig: {
title: '暂无商品',
description: '当前分类下暂时没有商品,试试其他分类吧',
icon: 'shop',
showAction: true,
actionText: '刷新'
},
},
goodListPagination: {
index: 0,
num: 20,
},
privateData: {
tabIndex: 0,
currentCategoryId: null,
isInitialized: false,
},
onShow() {
this.getTabBar().init();
},
onLoad() {
this.init();
},
onReachBottom() {
if (this.data.goodsListLoadStatus === 0) {
this.loadGoodsList();
}
},
onPullDownRefresh() {
this.init();
},
init() {
this.loadHomePage();
},
loadHomePage() {
wx.stopPullDownRefresh();
this.setData({
pageLoading: true,
});
fetchHome().then(({ swiper, tabList, recommendProducts, hotProducts }) => {
// 转换热门商品数据格式为商品列表格式
const hotGoodsList = (hotProducts || []).map(item => ({
spuId: item.id,
thumb: item.main_image,
title: item.name,
price: item.price,
originPrice: item.orig_price || item.price,
tags: [],
stock: item.stock || 0,
isStock: item.stock > 0,
}));
this.setData({
tabList,
imgSrcs: swiper,
recommendProducts: recommendProducts || [],
hotProducts: hotProducts || [],
goodsList: hotGoodsList, // 默认显示热门商品
currentTabValue: 'hot', // 重置为热门商品tab
pageLoading: false,
});
// 标记为已初始化,确保默认选中热门商品
this.privateData.isInitialized = true;
this.privateData.currentCategoryId = null; // 热门商品不需要分类ID
this.privateData.tabIndex = 0; // 重置tab索引
});
},
tabChangeHandle(e) {
const selectedValue = e.detail.value;
this.privateData.tabIndex = e.detail.index;
// 更新当前选中的tab值
this.setData({
currentTabValue: selectedValue
});
// 如果选择的是热门商品tab显示热门商品数据
if (selectedValue === 'hot') {
// 转换热门商品数据格式为商品列表格式
const hotGoodsList = (this.data.hotProducts || []).map(item => ({
spuId: item.id,
thumb: item.main_image,
title: item.name,
price: item.price,
originPrice: item.orig_price || item.price,
tags: [],
stock: item.stock || 0,
isStock: item.stock > 0,
}));
this.setData({
goodsList: hotGoodsList,
goodsListLoadStatus: 0,
emptyStateConfig: {
title: '暂无热门商品',
description: '当前没有热门商品,稍后再来看看吧',
icon: 'heart',
showAction: true,
actionText: '刷新'
}
});
this.privateData.currentCategoryId = null;
} else {
// 否则加载对应分类的商品列表
const currentCategory = (this.data.tabList || []).find(tab => tab.key === selectedValue);
const categoryName = currentCategory ? currentCategory.text : '该分类';
this.setData({
emptyStateConfig: {
title: '暂无商品',
description: `${categoryName}下暂时没有商品,试试其他分类吧`,
icon: 'shop',
showAction: true,
actionText: '刷新'
}
});
this.privateData.currentCategoryId = selectedValue;
this.loadGoodsList(true);
}
},
onReTry() {
this.loadGoodsList();
},
async loadGoodsList(fresh = false) {
if (fresh) {
wx.pageScrollTo({
scrollTop: 0,
});
// 重置分页信息
this.goodListPagination.index = 0;
}
this.setData({ goodsListLoadStatus: 1 });
const pageSize = this.goodListPagination.num;
let pageIndex = fresh ? 1 : this.goodListPagination.index + 1;
const categoryId = this.privateData.currentCategoryId;
try {
const nextList = await fetchGoodsList(pageIndex, pageSize, categoryId);
this.setData({
goodsList: fresh ? nextList : this.data.goodsList.concat(nextList),
goodsListLoadStatus: 0,
});
this.goodListPagination.index = pageIndex;
this.goodListPagination.num = pageSize;
} catch (err) {
console.error('加载商品列表失败:', err);
this.setData({ goodsListLoadStatus: 3 });
}
},
goodListClickHandle(e) {
const { index } = e.detail;
const goodsList = this.data.goodsList || [];
if (index >= 0 && index < goodsList.length) {
const { spuId } = goodsList[index];
wx.navigateTo({
url: `/pages/goods/details/index?spuId=${spuId}`,
});
}
},
goodListAddCartHandle() {
Toast({
context: this,
selector: '#t-toast',
message: '点击加入购物车',
});
},
navToSearchPage() {
wx.navigateTo({ url: '/pages/goods/search/index' });
},
navToActivityDetail({ detail }) {
const { index: promotionID = 0 } = detail || {};
wx.navigateTo({
url: `/pages/promotion/index?promotion_id=${promotionID}`,
});
},
onEmptyAction() {
// 空状态操作:刷新当前分类的商品列表
if (this.privateData.currentCategoryId === null) {
// 如果是热门商品,重新加载首页数据
this.init();
} else {
// 如果是分类商品,重新加载商品列表
this.loadGoodsList(true);
}
},
// 分享功能
onShareAppMessage() {
return {
title: '精选好物 - 优质商品一站购齐',
path: '/pages/home/home',
imageUrl: this.data.imgSrcs && this.data.imgSrcs.length > 0 ? this.data.imgSrcs[0].img : ''
};
},
// 分享到朋友圈
onShareTimeline() {
return {
title: '精选好物 - 优质商品一站购齐',
imageUrl: this.data.imgSrcs && this.data.imgSrcs.length > 0 ? this.data.imgSrcs[0].img : ''
};
}
});