147 lines
4.3 KiB
JavaScript
147 lines
4.3 KiB
JavaScript
/* eslint-disable no-param-reassign */
|
|
import { config } from '../../config/index';
|
|
|
|
/** 获取商品列表 */
|
|
function mockFetchGoodsList(params) {
|
|
const { delay } = require('../_utils/delay');
|
|
const { getSearchResult } = require('../../model/search');
|
|
|
|
const data = getSearchResult(params);
|
|
|
|
if (data.spuList.length) {
|
|
data.spuList.forEach((item) => {
|
|
item.spuId = item.spuId;
|
|
item.thumb = item.primaryImage;
|
|
item.title = item.title;
|
|
item.price = item.minSalePrice;
|
|
item.originPrice = item.maxLinePrice;
|
|
item.desc = '';
|
|
if (item.spuTagList) {
|
|
item.tags = item.spuTagList.map((tag) => tag.title);
|
|
} else {
|
|
item.tags = [];
|
|
}
|
|
});
|
|
}
|
|
return delay().then(() => {
|
|
return data;
|
|
});
|
|
}
|
|
|
|
/** 获取商品列表 */
|
|
export function fetchGoodsList(params) {
|
|
console.log('[API] fetchGoodsList 开始调用:', {
|
|
inputParams: params,
|
|
useMock: config.useMock,
|
|
timestamp: new Date().toLocaleString()
|
|
});
|
|
|
|
if (config.useMock) {
|
|
console.log('[API] 使用Mock数据');
|
|
return mockFetchGoodsList(params);
|
|
}
|
|
|
|
const requestData = {
|
|
page: params.pageNum || 1,
|
|
page_size: params.pageSize || 20,
|
|
category_id: params.category_id,
|
|
keyword: params.keyword,
|
|
min_price: params.minPrice,
|
|
max_price: params.maxPrice,
|
|
sort: params.sort,
|
|
sortType: params.sortType
|
|
};
|
|
|
|
// 过滤无效参数,避免传递 'undefined' 等值
|
|
Object.keys(requestData).forEach((key) => {
|
|
const val = requestData[key];
|
|
if (
|
|
val === undefined ||
|
|
val === null ||
|
|
(typeof val === 'string' && (val.trim() === '' || val.trim().toLowerCase() === 'undefined'))
|
|
) {
|
|
delete requestData[key];
|
|
}
|
|
});
|
|
|
|
console.log('[API] 发送请求到后端:', {
|
|
url: `${config.apiBase}/products`,
|
|
method: 'GET',
|
|
requestData: requestData,
|
|
sortInfo: {
|
|
hasSort: !!params.sort,
|
|
sortType: params.sortType,
|
|
sortText: params.sortType === 1 ? '价格从高到低' : params.sortType === 0 ? '价格从低到高' : '默认排序'
|
|
},
|
|
timestamp: new Date().toLocaleString()
|
|
});
|
|
|
|
return new Promise((resolve, reject) => {
|
|
wx.request({
|
|
url: `${config.apiBase}/products`,
|
|
method: 'GET',
|
|
data: requestData,
|
|
success: (res) => {
|
|
console.log('[API] 收到后端响应:', {
|
|
statusCode: res.statusCode,
|
|
dataCode: res.data?.code,
|
|
dataMessage: res.data?.message,
|
|
totalCount: res.data?.data?.total,
|
|
listLength: res.data?.data?.list?.length,
|
|
timestamp: new Date().toLocaleString()
|
|
});
|
|
|
|
if (res.statusCode === 200 && res.data.code === 200) {
|
|
// 转换后端数据格式为前端期望的格式
|
|
const products = res.data.data.list.map(item => ({
|
|
spuId: item.id.toString(),
|
|
thumb: item.main_image,
|
|
title: item.name,
|
|
price: item.price,
|
|
originPrice: item.orig_price,
|
|
desc: item.description,
|
|
tags: item.tags || [],
|
|
isStock: item.stock > 0
|
|
}));
|
|
|
|
const result = {
|
|
spuList: products,
|
|
totalCount: res.data.data.total,
|
|
pageNum: res.data.data.page,
|
|
pageSize: res.data.data.page_size
|
|
};
|
|
|
|
console.log('[API] 数据转换完成:', {
|
|
productsCount: products.length,
|
|
totalCount: result.totalCount,
|
|
pageNum: result.pageNum,
|
|
pageSize: result.pageSize,
|
|
firstProduct: products[0] ? {
|
|
title: products[0].title,
|
|
price: products[0].price
|
|
} : null,
|
|
timestamp: new Date().toLocaleString()
|
|
});
|
|
|
|
resolve(result);
|
|
} else {
|
|
console.error('[API] 请求失败:', {
|
|
statusCode: res.statusCode,
|
|
dataCode: res.data?.code,
|
|
message: res.data?.message || '获取商品列表失败',
|
|
timestamp: new Date().toLocaleString()
|
|
});
|
|
reject(new Error(res.data.message || '获取商品列表失败'));
|
|
}
|
|
},
|
|
fail: (err) => {
|
|
console.error('[API] 网络请求失败:', {
|
|
error: err,
|
|
timestamp: new Date().toLocaleString()
|
|
});
|
|
reject(err);
|
|
}
|
|
});
|
|
});
|
|
}
|