Page({ data: { productNames: [], loading: false, error: '', category: '', priceRange: { // 价格范围 min: 0, max: 0, hasPrice: false } }, // 解析规格,提取类型(净重/毛重)和数值范围 parseSpecification(spec) { const weightMatch = spec.match(/(净重|毛重)(\d+)-(\d+)/); if (weightMatch) { const type = weightMatch[1]; // 净重或毛重 const min = parseFloat(weightMatch[2]); const max = parseFloat(weightMatch[3]); const avg = (min + max) / 2; return { type: type, min: min, max: max, avg: avg }; } return null; }, onLoad(options) { // 对分类参数进行 URL 解码 const category = options.category ? decodeURIComponent(options.category) : ''; this.setData({ category: category }); console.log('解码后的分类:', category); this.loadProductNames(); }, loadProductNames() { this.setData({ loading: true, error: '', productNames: [] // 清空商品名称数据,确保加载时只显示加载状态 }); console.log('开始加载商品列表,当前分类:', this.data.category); // 尝试从本地存储中获取分类商品映射数据 const categoryProductsMap = wx.getStorageSync('evaluate2CategoryProductsMap') || {}; if (categoryProductsMap && Object.keys(categoryProductsMap).length > 0) { console.log('从本地存储获取分类商品映射数据'); // 获取当前分类下的商品名称列表 let productNames = []; if (this.data.category) { productNames = categoryProductsMap[this.data.category] || []; console.log(`分类"${this.data.category}"下的商品数量:`, productNames.length); console.log('商品名称列表:', productNames); } else { // 如果没有分类参数,获取所有商品名称 const allProductNames = []; Object.values(categoryProductsMap).forEach(names => { allProductNames.push(...names); }); // 去重 productNames = [...new Set(allProductNames)]; console.log('所有商品数量:', productNames.length); } // 计算价格范围 let priceRange = { min: 0, max: 0, hasPrice: false }; // 从原始商品数据中计算价格范围 const allProducts = wx.getStorageSync('allProducts') || []; console.log('本地存储中的商品总数:', allProducts.length); console.log('当前分类:', this.data.category); if (allProducts.length > 0) { // 过滤出当前分类下的商品 const categoryProducts = allProducts.filter(product => { if (!product.category) return false; const productCategory = String(product.category).trim(); return productCategory === this.data.category; }); console.log('当前分类下的商品数量:', categoryProducts.length); console.log('当前分类下的商品详情:', categoryProducts); // 按规格分组计算平均价格 const specPriceMap = {}; categoryProducts.forEach(product => { console.log('处理商品:', product.productName || product.name, '价格:', product.price, '规格:', product.specification || product.spec); if (product.price && (product.specification || product.spec)) { const priceStr = String(product.price).trim(); const specStr = String(product.specification || product.spec).trim(); console.log('商品价格字符串:', priceStr, '规格字符串:', specStr); // 处理逗号分隔的多个价格 const priceArray = priceStr.split(',').map(p => p.trim()).filter(p => p && p.trim() !== ''); console.log('处理后的价格数组:', priceArray); // 处理逗号分隔的多个规格 let specs = specStr.split(',').map(spec => spec.trim()).filter(spec => spec.length > 0); console.log('处理后的规格数组:', specs); // 进一步处理规格,确保每个规格都是独立的 const processedSpecs = []; specs.forEach(spec => { if (spec.includes(',')) { // 按中文逗号分割 const subSpecs = spec.split(',').map(s => s.trim()).filter(s => s.length > 0); processedSpecs.push(...subSpecs); } else { processedSpecs.push(spec); } }); specs = processedSpecs; console.log('最终规格数组:', specs); // 将规格和价格配对 specs.forEach((spec, index) => { console.log('处理规格:', spec, '对应价格索引:', index); if (spec.length > 0 && index < priceArray.length) { const price = priceArray[index]; if (price && price.trim() !== '') { const priceValue = parseFloat(price); if (!isNaN(priceValue)) { // 解析规格 const specInfo = this.parseSpecification(spec); console.log('解析规格结果:', specInfo); // 价格<10的需要按照公式计算 let finalPrice = priceValue; if (priceValue < 10 && specInfo) { if (specInfo.type === '净重') { // 净重:规格平均值 × 价格 finalPrice = specInfo.avg * priceValue; } else if (specInfo.type === '毛重') { // 毛重:(规格平均值 - 5) × 价格 finalPrice = (specInfo.avg - 5) * priceValue; } console.log('价格计算:', priceValue, '->', finalPrice); } // 按规格分组存储价格 if (!specPriceMap[spec]) { specPriceMap[spec] = []; } specPriceMap[spec].push(finalPrice); console.log('存储价格:', finalPrice, '到规格:', spec); } else { console.log('价格解析失败:', price); } } else { console.log('价格为空或无效:', price); } } else { console.log('规格长度为0或价格索引超出范围:', spec.length, index, priceArray.length); } }); } else { console.log('商品缺少价格或规格:', !product.price ? '无价格' : '', !product.specification && !product.spec ? '无规格' : ''); } }); // 计算每个规格的平均价格,然后找出最低和最高 const specAvgPrices = []; Object.keys(specPriceMap).forEach(spec => { const prices = specPriceMap[spec]; if (prices.length > 0) { const avgPrice = prices.reduce((sum, price) => sum + price, 0) / prices.length; specAvgPrices.push(avgPrice); } }); if (specAvgPrices.length > 0) { priceRange.min = Math.round(Math.min(...specAvgPrices) * 100) / 100; priceRange.max = Math.round(Math.max(...specAvgPrices) * 100) / 100; priceRange.hasPrice = true; } } this.setData({ productNames: productNames, priceRange: priceRange, loading: false }); // 结束下拉刷新 wx.stopPullDownRefresh(); } else { // 如果本地存储中没有数据,尝试从本地存储获取原始商品数据 const allProducts = wx.getStorageSync('allProducts') || []; if (allProducts.length > 0) { console.log('从本地存储获取原始商品数据'); // 过滤出有有效category字段的商品 const productsWithCategory = allProducts.filter(product => { if (!product.category) return false; const categoryStr = String(product.category).trim(); return categoryStr !== ''; }); // 移除价格过滤,获取所有商品 let filteredProducts = productsWithCategory; // 如果有分类参数,过滤出该分类下的商品 if (this.data.category) { console.log('当前分类:', this.data.category); const targetCategory = String(this.data.category).trim(); filteredProducts = filteredProducts.filter(product => { if (!product.category) return false; const productCategory = String(product.category).trim(); return productCategory === targetCategory; }); } // 提取商品名称,支持多种字段 const productNames = filteredProducts.map(product => { // 尝试从多个字段获取商品名称 const nameFields = [product.productName, product.name, product.product]; for (const field of nameFields) { if (field) { const trimmedName = String(field).trim(); if (trimmedName !== '') { return trimmedName; } } } return ''; }).filter(name => name !== ''); // 去重 const uniqueProductNames = [...new Set(productNames)]; console.log('最终商品名称列表:', uniqueProductNames); // 计算价格范围 let priceRange = { min: 0, max: 0, hasPrice: false }; // 从过滤后的商品中计算价格范围,按规格分组计算平均价格 const specPriceMap = {}; filteredProducts.forEach(product => { if (product.price && (product.specification || product.spec)) { const priceStr = String(product.price).trim(); const specStr = String(product.specification || product.spec).trim(); // 处理逗号分隔的多个价格 const priceArray = priceStr.split(',').map(p => p.trim()).filter(p => p && p.trim() !== ''); // 处理逗号分隔的多个规格 let specs = specStr.split(',').map(spec => spec.trim()).filter(spec => spec.length > 0); // 进一步处理规格,确保每个规格都是独立的 const processedSpecs = []; specs.forEach(spec => { if (spec.includes(',')) { // 按中文逗号分割 const subSpecs = spec.split(',').map(s => s.trim()).filter(s => s.length > 0); processedSpecs.push(...subSpecs); } else { processedSpecs.push(spec); } }); specs = processedSpecs; // 将规格和价格配对 specs.forEach((spec, index) => { if (spec.length > 0 && index < priceArray.length) { const price = priceArray[index]; if (price && price.trim() !== '') { const priceValue = parseFloat(price); if (!isNaN(priceValue)) { // 解析规格 const specInfo = this.parseSpecification(spec); // 价格<10的需要按照公式计算 let finalPrice = priceValue; if (priceValue < 10 && specInfo) { if (specInfo.type === '净重') { // 净重:规格平均值 × 价格 finalPrice = specInfo.avg * priceValue; } else if (specInfo.type === '毛重') { // 毛重:(规格平均值 - 5) × 价格 finalPrice = (specInfo.avg - 5) * priceValue; } } // 按规格分组存储价格 if (!specPriceMap[spec]) { specPriceMap[spec] = []; } specPriceMap[spec].push(finalPrice); } } } }); } }); // 计算每个规格的平均价格,然后找出最低和最高 const specAvgPrices = []; Object.keys(specPriceMap).forEach(spec => { const prices = specPriceMap[spec]; if (prices.length > 0) { const avgPrice = prices.reduce((sum, price) => sum + price, 0) / prices.length; specAvgPrices.push(avgPrice); } }); if (specAvgPrices.length > 0) { priceRange.min = Math.round(Math.min(...specAvgPrices) * 100) / 100; priceRange.max = Math.round(Math.max(...specAvgPrices) * 100) / 100; priceRange.hasPrice = true; } this.setData({ productNames: uniqueProductNames, priceRange: priceRange, loading: false }); // 结束下拉刷新 wx.stopPullDownRefresh(); } else { // 如果本地存储中也没有原始商品数据,调用API获取数据 console.log('本地存储中没有数据,调用API获取商品数据...'); const api = require('../../utils/api'); // 使用正确的参数调用getProducts方法 api.getProducts(1, 1000, 'all', '').then(result => { console.log('API返回结果:', result); // 从返回对象中提取products数组,如果不存在则使用空数组 const products = result.products || []; // 过滤出有有效category字段的商品 const productsWithCategory = products.filter(product => { if (!product.category) return false; const categoryStr = String(product.category).trim(); return categoryStr !== ''; }); // 移除价格过滤,获取所有商品 let filteredProducts = productsWithCategory; // 如果有分类参数,过滤出该分类下的商品 if (this.data.category) { console.log('当前分类:', this.data.category); const targetCategory = String(this.data.category).trim(); filteredProducts = filteredProducts.filter(product => { if (!product.category) return false; const productCategory = String(product.category).trim(); return productCategory === targetCategory; }); } // 提取商品名称,支持多种字段 const productNames = filteredProducts.map(product => { // 尝试从多个字段获取商品名称 const nameFields = [product.productName, product.name, product.product]; for (const field of nameFields) { if (field) { const trimmedName = String(field).trim(); if (trimmedName !== '') { return trimmedName; } } } return ''; }).filter(name => name !== ''); // 去重 const uniqueProductNames = [...new Set(productNames)]; console.log('最终商品名称列表:', uniqueProductNames); // 计算价格范围 let priceRange = { min: 0, max: 0, hasPrice: false }; // 从过滤后的商品中计算价格范围,按规格分组计算平均价格 const specPriceMap = {}; filteredProducts.forEach(product => { if (product.price && (product.specification || product.spec)) { const priceStr = String(product.price).trim(); const specStr = String(product.specification || product.spec).trim(); // 处理逗号分隔的多个价格 const priceArray = priceStr.split(',').map(p => p.trim()).filter(p => p && p.trim() !== ''); // 处理逗号分隔的多个规格 let specs = specStr.split(',').map(spec => spec.trim()).filter(spec => spec.length > 0); // 进一步处理规格,确保每个规格都是独立的 const processedSpecs = []; specs.forEach(spec => { if (spec.includes(',')) { // 按中文逗号分割 const subSpecs = spec.split(',').map(s => s.trim()).filter(s => s.length > 0); processedSpecs.push(...subSpecs); } else { processedSpecs.push(spec); } }); specs = processedSpecs; // 将规格和价格配对 specs.forEach((spec, index) => { if (spec.length > 0 && index < priceArray.length) { const price = priceArray[index]; if (price && price.trim() !== '') { const priceValue = parseFloat(price); if (!isNaN(priceValue)) { // 解析规格 const specInfo = this.parseSpecification(spec); // 价格<10的需要按照公式计算 let finalPrice = priceValue; if (priceValue < 10 && specInfo) { if (specInfo.type === '净重') { // 净重:规格平均值 × 价格 finalPrice = specInfo.avg * priceValue; } else if (specInfo.type === '毛重') { // 毛重:(规格平均值 - 5) × 价格 finalPrice = (specInfo.avg - 5) * priceValue; } } // 按规格分组存储价格 if (!specPriceMap[spec]) { specPriceMap[spec] = []; } specPriceMap[spec].push(finalPrice); } } } }); } }); // 计算每个规格的平均价格,然后找出最低和最高 const specAvgPrices = []; Object.keys(specPriceMap).forEach(spec => { const prices = specPriceMap[spec]; if (prices.length > 0) { const avgPrice = prices.reduce((sum, price) => sum + price, 0) / prices.length; specAvgPrices.push(avgPrice); } }); if (specAvgPrices.length > 0) { priceRange.min = Math.round(Math.min(...specAvgPrices) * 100) / 100; priceRange.max = Math.round(Math.max(...specAvgPrices) * 100) / 100; priceRange.hasPrice = true; } this.setData({ productNames: uniqueProductNames, priceRange: priceRange, loading: false }); // 结束下拉刷新 wx.stopPullDownRefresh(); }).catch(err => { console.error('获取商品列表失败:', err); this.setData({ error: '获取商品列表失败,请稍后重试', loading: false }); // 结束下拉刷新 wx.stopPullDownRefresh(); }); } } }, selectProduct(e) { const productName = e.currentTarget.dataset.product; console.log('选择商品:', productName); // 将商品名称和当前分类存储到本地存储 wx.setStorageSync('selectedProductName', productName); wx.setStorageSync('selectedCategory', this.data.category); // 存储当前分类 // 使用wx.switchTab导航到tabBar页面 wx.switchTab({ url: '/pages/evaluate2/index', success: function(res) { console.log('跳转成功:', res); }, fail: function(err) { console.error('跳转失败:', err); } }); }, // 返回分类选择页面 goBackToCategories() { console.log('返回分类选择页面'); wx.redirectTo({ url: '/pages/evaluate2/one' }); }, // 下拉刷新 onPullDownRefresh() { console.log('开始下拉刷新'); this.loadProductNames(); } });