170 lines
4.6 KiB
JavaScript
170 lines
4.6 KiB
JavaScript
|
|
import { config, cdnBase } from '../../config/index';
|
|||
|
|
|
|||
|
|
/** 获取首页数据 */
|
|||
|
|
function mockFetchHome() {
|
|||
|
|
const { delay } = require('../_utils/delay');
|
|||
|
|
const { genSwiperImageList } = require('../../model/swiper');
|
|||
|
|
return delay().then(() => {
|
|||
|
|
return {
|
|||
|
|
swiper: genSwiperImageList(),
|
|||
|
|
tabList: [
|
|||
|
|
{
|
|||
|
|
text: '热门商品',
|
|||
|
|
key: 'hot',
|
|||
|
|
},
|
|||
|
|
{
|
|||
|
|
text: '精选推荐',
|
|||
|
|
key: 0,
|
|||
|
|
},
|
|||
|
|
{
|
|||
|
|
text: '夏日防晒',
|
|||
|
|
key: 1,
|
|||
|
|
},
|
|||
|
|
{
|
|||
|
|
text: '二胎大作战',
|
|||
|
|
key: 2,
|
|||
|
|
},
|
|||
|
|
{
|
|||
|
|
text: '人气榜',
|
|||
|
|
key: 3,
|
|||
|
|
},
|
|||
|
|
{
|
|||
|
|
text: '好评榜',
|
|||
|
|
key: 4,
|
|||
|
|
},
|
|||
|
|
{
|
|||
|
|
text: 'RTX 30',
|
|||
|
|
key: 5,
|
|||
|
|
},
|
|||
|
|
{
|
|||
|
|
text: '手机也疯狂',
|
|||
|
|
key: 6,
|
|||
|
|
},
|
|||
|
|
],
|
|||
|
|
activityImg: `${cdnBase}/activity/banner.png`,
|
|||
|
|
};
|
|||
|
|
});
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/** 获取首页数据 */
|
|||
|
|
export function fetchHome() {
|
|||
|
|
if (config.useMock) {
|
|||
|
|
return mockFetchHome();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return new Promise((resolve, reject) => {
|
|||
|
|
// 并行请求轮播图和分类数据
|
|||
|
|
const bannerRequest = new Promise((resolve, reject) => {
|
|||
|
|
wx.request({
|
|||
|
|
url: `${config.apiBase}/banners`,
|
|||
|
|
method: 'GET',
|
|||
|
|
success: (res) => {
|
|||
|
|
if (res.statusCode === 200 && res.data.code === 200) {
|
|||
|
|
resolve(res.data.data || []);
|
|||
|
|
} else {
|
|||
|
|
console.error('获取轮播图失败:', res.data);
|
|||
|
|
resolve([]); // 失败时返回空数组,不阻断整个流程
|
|||
|
|
}
|
|||
|
|
},
|
|||
|
|
fail: (err) => {
|
|||
|
|
console.error('轮播图请求失败:', err);
|
|||
|
|
resolve([]); // 失败时返回空数组
|
|||
|
|
}
|
|||
|
|
});
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
const categoryRequest = new Promise((resolve, reject) => {
|
|||
|
|
wx.request({
|
|||
|
|
url: `${config.apiBase}/products/categories`,
|
|||
|
|
method: 'GET',
|
|||
|
|
success: (res) => {
|
|||
|
|
if (res.statusCode === 200 && res.data.code === 200) {
|
|||
|
|
resolve(res.data.data || []);
|
|||
|
|
} else {
|
|||
|
|
console.error('获取分类失败:', res.data);
|
|||
|
|
resolve([]); // 失败时返回空数组
|
|||
|
|
}
|
|||
|
|
},
|
|||
|
|
fail: (err) => {
|
|||
|
|
console.error('分类请求失败:', err);
|
|||
|
|
resolve([]); // 失败时返回空数组
|
|||
|
|
}
|
|||
|
|
});
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
// 获取推荐商品
|
|||
|
|
const recommendRequest = new Promise((resolve) => {
|
|||
|
|
wx.request({
|
|||
|
|
url: `${config.apiBase}/products/recommend`,
|
|||
|
|
method: 'GET',
|
|||
|
|
success: (res) => {
|
|||
|
|
if (res.statusCode === 200 && res.data.code === 200) {
|
|||
|
|
console.log('推荐商品数据:', res.data.data);
|
|||
|
|
resolve(res.data.data || []);
|
|||
|
|
} else {
|
|||
|
|
console.error('获取推荐商品失败:', res.data);
|
|||
|
|
resolve([]);
|
|||
|
|
}
|
|||
|
|
},
|
|||
|
|
fail: (err) => {
|
|||
|
|
console.error('推荐商品请求失败:', err);
|
|||
|
|
resolve([]);
|
|||
|
|
}
|
|||
|
|
});
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
// 获取热门商品
|
|||
|
|
const hotRequest = new Promise((resolve) => {
|
|||
|
|
wx.request({
|
|||
|
|
url: `${config.apiBase}/products/hot`,
|
|||
|
|
method: 'GET',
|
|||
|
|
success: (res) => {
|
|||
|
|
if (res.statusCode === 200 && res.data.code === 200) {
|
|||
|
|
console.log('热门商品数据:', res.data.data);
|
|||
|
|
resolve(res.data.data || []);
|
|||
|
|
} else {
|
|||
|
|
console.error('获取热门商品失败:', res.data);
|
|||
|
|
resolve([]);
|
|||
|
|
}
|
|||
|
|
},
|
|||
|
|
fail: (err) => {
|
|||
|
|
console.error('热门商品请求失败:', err);
|
|||
|
|
resolve([]);
|
|||
|
|
}
|
|||
|
|
});
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
// 等待所有请求都完成
|
|||
|
|
Promise.all([bannerRequest, categoryRequest, recommendRequest, hotRequest])
|
|||
|
|
.then(([banners, categories, recommendProducts, hotProducts]) => {
|
|||
|
|
// 转换轮播图数据格式 - TDesign轮播图组件需要字符串数组
|
|||
|
|
const swiper = banners.map(banner => banner.image);
|
|||
|
|
console.log('轮播图数据:', swiper);
|
|||
|
|
|
|||
|
|
// 转换分类数据为tabList格式,在前面添加热门商品tab
|
|||
|
|
const tabList = [
|
|||
|
|
{
|
|||
|
|
text: '热门商品',
|
|||
|
|
key: 'hot',
|
|||
|
|
},
|
|||
|
|
...categories.map((category, index) => ({
|
|||
|
|
text: category.name,
|
|||
|
|
key: category.id || index,
|
|||
|
|
}))
|
|||
|
|
];
|
|||
|
|
|
|||
|
|
resolve({
|
|||
|
|
swiper,
|
|||
|
|
tabList,
|
|||
|
|
recommendProducts,
|
|||
|
|
hotProducts,
|
|||
|
|
activityImg: `${cdnBase}/activity/banner.png`,
|
|||
|
|
});
|
|||
|
|
})
|
|||
|
|
.catch((error) => {
|
|||
|
|
console.error('获取首页数据失败:', error);
|
|||
|
|
reject(error);
|
|||
|
|
});
|
|||
|
|
});
|
|||
|
|
}
|