/* eslint-disable no-param-reassign */ import { config } from '../../config/index'; /** 获取搜索结果 - Mock数据 */ function mockSearchResult(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; if (item.spuTagList) { item.tags = item.spuTagList.map((tag) => ({ title: tag.title })); } else { item.tags = []; } // 添加库存状态字段 - Mock数据 item.isStock = item.spuStockQuantity > 0; item.stock = item.spuStockQuantity || 0; }); } return delay().then(() => { return data; }); } /** 调用真实的搜索API */ function realSearchResult(params) { return new Promise((resolve, reject) => { // 构建请求参数 const requestParams = { keyword: params.keyword || '', page: params.pageNum || 1, page_size: params.pageSize || 30, }; // 添加排序参数 if (params.sort === 1) { requestParams.sort_by = 'price'; requestParams.sort_order = params.sortType === 1 ? 'desc' : 'asc'; } // 添加价格筛选 if (params.minPrice && params.minPrice > 0) { requestParams.min_price = params.minPrice; } if (params.maxPrice && params.maxPrice > 0) { requestParams.max_price = params.maxPrice; } // 构建查询字符串 const queryString = Object.keys(requestParams) .filter(key => requestParams[key] !== undefined && requestParams[key] !== '') .map(key => `${encodeURIComponent(key)}=${encodeURIComponent(requestParams[key])}`) .join('&'); const url = `${config.apiBase}/products/search?${queryString}`; console.log('🔍 搜索API请求:', { url, params: requestParams, originalParams: params }); wx.request({ url, method: 'GET', header: { 'Content-Type': 'application/json', }, success: (res) => { console.log('🔍 搜索API响应:', res); if (res.statusCode === 200 && res.data && res.data.code === 200) { const { data: responseData } = res.data; console.log('🔍 后端响应数据结构:', responseData); // 转换后端数据格式为前端期望的格式 const result = { saasId: null, storeId: null, pageNum: responseData.page || params.pageNum || 1, pageSize: responseData.page_size || params.pageSize || 30, totalCount: responseData.total || 0, spuList: (responseData.list || []).map(item => ({ spuId: item.id, title: item.name, primaryImage: item.main_image || item.image || '', thumb: item.main_image || item.image || '', minSalePrice: item.price || 0, maxLinePrice: item.original_price || item.price || 0, price: item.price || 0, originPrice: item.original_price || item.price || 0, spuTagList: (item.tags || []).map(tag => ({ title: tag })), tags: (item.tags || []).map(tag => ({ title: tag })), // 添加库存状态字段 isStock: item.stock > 0, stock: item.stock || 0, // 保留原始数据以备后用 _originalData: item })), algId: 0, }; console.log('🔍 转换后的搜索结果:', result); resolve(result); } else { console.error('🔍 搜索API错误响应:', res); reject(new Error(res.data?.message || '搜索请求失败')); } }, fail: (error) => { console.error('🔍 搜索API请求失败:', error); reject(new Error('网络请求失败,请检查网络连接')); } }); }); } /** 获取搜索结果 */ export function getSearchResult(params) { if (config.useMock) { return mockSearchResult(params); } return realSearchResult(params); }