94 lines
4.1 KiB
JavaScript
94 lines
4.1 KiB
JavaScript
import { config } from '../../config/index';
|
||
|
||
/** 获取商品列表 */
|
||
function mockFetchGood(ID = 0) {
|
||
const { delay } = require('../_utils/delay');
|
||
const { genGood } = require('../../model/good');
|
||
return delay().then(() => genGood(ID));
|
||
}
|
||
|
||
/** 获取商品详情 */
|
||
export function fetchGood(ID = 0) {
|
||
if (config.useMock) {
|
||
return mockFetchGood(ID);
|
||
}
|
||
|
||
return new Promise((resolve, reject) => {
|
||
wx.request({
|
||
url: `${config.apiBase}/frontend/products/${ID}/detail`,
|
||
method: 'GET',
|
||
success: (res) => {
|
||
if (res.statusCode === 200 && res.data.code === 200) {
|
||
// 转换后端数据格式为前端期望的格式
|
||
const product = res.data.data;
|
||
|
||
// 计算价格范围
|
||
let minPrice = parseInt(product.minSalePrice) || 0;
|
||
let maxPrice = parseInt(product.maxSalePrice) || 0;
|
||
let minOriginPrice = parseInt(product.minLinePrice) || 0;
|
||
let maxOriginPrice = parseInt(product.maxLinePrice) || 0;
|
||
|
||
if (product.skuList && product.skuList.length > 0) {
|
||
const prices = product.skuList.map(sku => {
|
||
const priceInfo = sku.priceInfo && sku.priceInfo.length > 0 ? sku.priceInfo[0] : null;
|
||
return priceInfo ? parseInt(priceInfo.price) : 0; // 直接使用分为单位
|
||
});
|
||
const originPrices = product.skuList.map(sku => {
|
||
const priceInfo = sku.priceInfo && sku.priceInfo.length > 0 ? sku.priceInfo[0] : null;
|
||
return priceInfo ? parseInt(priceInfo.originPrice || priceInfo.price) : 0; // 直接使用分为单位
|
||
});
|
||
minPrice = Math.min(...prices);
|
||
maxPrice = Math.max(...prices);
|
||
minOriginPrice = Math.min(...originPrices);
|
||
maxOriginPrice = Math.max(...originPrices);
|
||
}
|
||
|
||
const result = {
|
||
// 基础信息:优先读取后端前端化字段,其次回退旧字段
|
||
spuId: product.spuId || product.id,
|
||
title: product.title || product.name,
|
||
price: minPrice,
|
||
originPrice: minOriginPrice,
|
||
minSalePrice: minPrice,
|
||
maxSalePrice: maxPrice,
|
||
maxLinePrice: maxOriginPrice,
|
||
primaryImage: product.primaryImage || product.main_image,
|
||
images: (product.images && product.images.length ? product.images : (product.primaryImage || product.main_image ? [product.primaryImage || product.main_image] : [])),
|
||
video: product.video || null,
|
||
// 详情描述:仅返回图片列表,支持后端返回字符串单图的情况
|
||
desc: (Array.isArray(product.desc) && product.desc.length)
|
||
? product.desc
|
||
: ((typeof product.desc === 'string' && product.desc.trim())
|
||
? [product.desc.trim()]
|
||
: ((Array.isArray(product.detail_images) && product.detail_images.length)
|
||
? product.detail_images
|
||
: [])),
|
||
// 文本描述:单独提供文本字段作为兜底
|
||
descriptionText: product.description || '',
|
||
details: product.details,
|
||
brand: product.brand,
|
||
category: product.category,
|
||
isPutOnSale: product.isPutOnSale ?? 1, // 1表示上架,0表示下架
|
||
isStock: (product.spuStockQuantity ?? product.stock ?? 0) > 0,
|
||
stockNum: product.spuStockQuantity ?? product.stock ?? 0,
|
||
spuStockQuantity: product.spuStockQuantity ?? product.stock ?? 0,
|
||
soldNum: product.soldNum ?? product.sold_count ?? 0,
|
||
spuTagList: product.spuTagList ?? product.tags ?? [],
|
||
limitInfo: product.limitInfo ?? [],
|
||
specList: product.specList || [],
|
||
skuList: product.skuList || [],
|
||
etitle: product.etitle || product.name,
|
||
available: product.available ?? ((product.spuStockQuantity ?? product.stock ?? 0) > 0 ? 1 : 0)
|
||
};
|
||
resolve(result);
|
||
} else {
|
||
reject(new Error(res.data.message || '获取商品详情失败'));
|
||
}
|
||
},
|
||
fail: (err) => {
|
||
reject(err);
|
||
}
|
||
});
|
||
});
|
||
}
|