You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

530 lines
16 KiB

3 months ago
// pages/evaluate/index.js
//估价页面
const api = require('../../utils/api.js');
3 months ago
Page({
// 分享给朋友/群聊
onShareAppMessage() {
return {
title: '鸡蛋贸易平台 - 智能估价,专业分析',
path: '/pages/evaluate/index',
imageUrl: '/images/你有好蛋.png'
}
},
// 分享到朋友圈
onShareTimeline() {
return {
title: '鸡蛋贸易平台 - 智能估价,专业分析',
query: '',
imageUrl: '/images/你有好蛋.png'
}
},
3 months ago
data: {
evaluateStep: 1,
fromPreviousStep: false, // 用于标记是否从下一步返回
evaluateData: {
region: '',
breed: '',
spec: '',
3 months ago
freshness: '',
size: '',
packaging: '',
quantity: ''
},
// 客户地区列表 - 省市二级联动数据
provinceCities: {
'北京': ['北京'],
'河北': ['石家庄', '唐山', '秦皇岛', '邯郸', '邢台', '保定', '张家口', '承德', '沧州', '廊坊', '衡水'],
'四川': ['成都', '自贡', '攀枝花', '泸州', '德阳', '绵阳', '广元', '遂宁', '内江', '乐山', '南充', '眉山', '宜宾', '广安', '达州', '雅安', '巴中', '资阳'],
'云南': ['昆明', '曲靖', '玉溪', '保山', '昭通', '丽江', '普洱', '临沧', '楚雄', '红河', '文山', '西双版纳', '大理', '德宏', '怒江', '迪庆'],
'贵州': ['贵阳', '六盘水', '遵义', '安顺', '毕节', '铜仁', '黔西南', '黔东南', '黔南']
3 months ago
},
// 省份列表
provinces: [],
// 城市列表
cities: [],
// 当前选中的省份
selectedProvince: '',
// 当前是否显示城市选择
showCities: false,
3 months ago
evaluateResult: {
finalPrice: '0',
totalPrice: '0',
aidate: ''
3 months ago
},
// 鸡蛋品种数据(包含成交单量)
eggBreeds: [
{ name: '罗曼粉', dailyOrders: 2341 },
{ name: '伊莎粉', dailyOrders: 1892 },
{ name: '罗曼灰', dailyOrders: 1567 },
{ name: '海蓝灰', dailyOrders: 1432 },
{ name: '海蓝褐', dailyOrders: 1298 },
{ name: '绿壳', dailyOrders: 1054 },
{ name: '粉一', dailyOrders: 987 },
{ name: '粉二', dailyOrders: 876 },
{ name: '粉八', dailyOrders: 765 },
{ name: '京粉1号', dailyOrders: 654 },
{ name: '京红', dailyOrders: 543 },
{ name: '京粉6号', dailyOrders: 456 },
{ name: '京粉3号', dailyOrders: 389 },
{ name: '农大系列', dailyOrders: 321 },
{ name: '黑鸡土蛋', dailyOrders: 298 },
{ name: '双黄蛋', dailyOrders: 245 },
{ name: '大午金凤', dailyOrders: 198 },
{ name: '黑凤', dailyOrders: 156 }
3 months ago
],
// 鸡蛋规格数据
eggSpecs: [
{ name: '格子装', dailyOrders: 3214 },
{ name: '散托', dailyOrders: 2890 },
{ name: '不限规格', dailyOrders: 2567 },
{ name: '净重47+', dailyOrders: 2345 },
{ name: '净重46-47', dailyOrders: 2109 },
{ name: '净重45-46', dailyOrders: 1876 },
{ name: '净重44-45', dailyOrders: 1654 },
{ name: '净重43-44', dailyOrders: 1432 },
{ name: '净重42-43', dailyOrders: 1234 },
{ name: '净重41-42', dailyOrders: 1056 },
{ name: '净重40-41', dailyOrders: 987 },
{ name: '净重39-40', dailyOrders: 876 },
{ name: '净重38-39', dailyOrders: 765 },
{ name: '净重37-39', dailyOrders: 654 },
{ name: '净重37-38', dailyOrders: 543 },
{ name: '净重36-38', dailyOrders: 456 },
{ name: '净重36-37', dailyOrders: 389 },
{ name: '净重35-36', dailyOrders: 321 },
{ name: '净重34-35', dailyOrders: 289 },
{ name: '净重33-34', dailyOrders: 256 },
{ name: '净重32-33', dailyOrders: 223 },
{ name: '净重32-34', dailyOrders: 198 },
{ name: '净重31-32', dailyOrders: 165 },
{ name: '净重30-35', dailyOrders: 143 },
{ name: '净重30-34', dailyOrders: 121 },
{ name: '净重30-32', dailyOrders: 109 },
{ name: '净重30-31', dailyOrders: 98 },
{ name: '净重29-31', dailyOrders: 87 },
{ name: '净重29-30', dailyOrders: 76 },
{ name: '净重28-29', dailyOrders: 65 },
{ name: '净重28以下', dailyOrders: 54 },
{ name: '毛重52以上', dailyOrders: 132 },
{ name: '毛重50-51', dailyOrders: 121 },
{ name: '毛重48-49', dailyOrders: 109 },
{ name: '毛重47-48', dailyOrders: 98 },
{ name: '毛重46-47', dailyOrders: 87 },
{ name: '毛重45-47', dailyOrders: 76 },
{ name: '毛重45-46', dailyOrders: 65 },
{ name: '毛重44-45', dailyOrders: 54 },
{ name: '毛重43-44', dailyOrders: 43 },
{ name: '毛重42-43', dailyOrders: 32 },
{ name: '毛重41-42', dailyOrders: 21 },
{ name: '毛重40-41', dailyOrders: 19 },
{ name: '毛重38-39', dailyOrders: 17 },
{ name: '毛重36-37', dailyOrders: 15 },
{ name: '毛重34-35', dailyOrders: 13 },
{ name: '毛重32-33', dailyOrders: 11 },
{ name: '毛重30-31', dailyOrders: 9 },
{ name: '毛重30以下', dailyOrders: 7 }
]
3 months ago
},
onLoad() {
console.log('估价页面初始化')
// 页面加载时,对鸡蛋品种按成交单量降序排序并添加排名
const sortedBreeds = [...this.data.eggBreeds].sort((a, b) => b.dailyOrders - a.dailyOrders);
3 months ago
// 添加排名属性
const breedsWithRank = sortedBreeds.map((breed, index) => ({
...breed,
rank: index + 1
}));
// 对鸡蛋规格按成交单量降序排序并添加排名
const sortedSpecs = [...this.data.eggSpecs].sort((a, b) => b.dailyOrders - a.dailyOrders);
const specsWithRank = sortedSpecs.map((spec, index) => ({
...spec,
3 months ago
rank: index + 1
}));
// 初始化省份列表
const provinces = Object.keys(this.data.provinceCities);
3 months ago
this.setData({
eggBreeds: breedsWithRank,
eggSpecs: specsWithRank,
fromPreviousStep: false, // 初始化标志
provinces: provinces
3 months ago
});
},
// 选择省份
selectProvince(e) {
const province = e.currentTarget.dataset.province;
const cities = this.data.provinceCities[province];
3 months ago
this.setData({
selectedProvince: province,
cities: cities,
showCities: true
3 months ago
});
},
// 选择城市
selectCity(e) {
const city = e.currentTarget.dataset.city;
if (!city || !this.data.selectedProvince) {
wx.showToast({
title: '请选择有效的城市',
icon: 'none',
duration: 2000
3 months ago
});
return;
3 months ago
}
const fullRegion = `${this.data.selectedProvince}-${city}`;
3 months ago
this.setData({
'evaluateData.region': fullRegion,
showCities: false
3 months ago
});
// 显示选择成功提示
wx.showToast({
title: `已选择: ${fullRegion}`,
icon: 'success',
duration: 1500
3 months ago
});
// 只有当当前步骤是1且已经从下一步返回时,才自动进入下一步
if (this.data.evaluateStep === 1 && !this.data.fromPreviousStep) {
3 months ago
this.setData({
evaluateStep: 2
3 months ago
});
}
// 重置标志
this.setData({
fromPreviousStep: false
});
},
// 返回选择省份
backToProvince() {
this.setData({
showCities: false
});
},
3 months ago
// 选择鸡蛋品种
selectEggBreed(e) {
const breed = e.currentTarget.dataset.breed;
3 months ago
this.setData({
'evaluateData.breed': breed
3 months ago
});
// 显示选择成功提示
wx.showToast({
title: '已选择: ' + breed,
icon: 'none',
duration: 1500
});
// 只有当当前步骤是2且不是从下一步返回时,才自动进入下一步
if (this.data.evaluateStep === 2 && !this.data.fromPreviousStep) {
this.setData({ evaluateStep: 3 });
3 months ago
}
// 重置标志
this.setData({ fromPreviousStep: false });
3 months ago
},
// 选择鸡蛋规格
selectEggSpec(e) {
const spec = e.currentTarget.dataset.spec;
3 months ago
this.setData({
'evaluateData.spec': spec
3 months ago
});
// 显示选择成功提示
wx.showToast({
title: '已选择: ' + spec,
icon: 'none',
duration: 1500
});
// 只有当当前步骤是3且不是从下一步返回时,才自动进入下一步
if (this.data.evaluateStep === 3 && !this.data.fromPreviousStep) {
this.setData({ evaluateStep: 4 });
3 months ago
}
// 重置标志
this.setData({ fromPreviousStep: false });
3 months ago
},
// 格式化订单数量显示
formatOrderCount(count) {
if (count >= 10000) {
return (count / 10000).toFixed(1) + '万';
} else if (count >= 1000) {
return (count / 1000).toFixed(1) + 'k';
}
return count.toString();
},
// 选择条件
selectCondition(e) {
const { type, value } = e.currentTarget.dataset;
this.setData({
[`evaluateData.${type}`]: value
});
// 只有当当前步骤不是从返回过来的,才自动进入下一步
if (!this.data.fromPreviousStep) {
// 根据当前步骤自动进入下一步
const currentStep = this.data.evaluateStep;
if (currentStep === 4) {
this.setData({ evaluateStep: 5 });
} else if (currentStep === 5) {
3 months ago
this.setData({ evaluateStep: 6 });
} else if (currentStep === 6) {
this.setData({ evaluateStep: 7 });
}
}
// 重置标志
this.setData({ fromPreviousStep: false });
3 months ago
},
// 选择数量
selectQuantity(e) {
const quantity = e.currentTarget.dataset.quantity;
3 months ago
this.setData({
'evaluateData.quantity': quantity
3 months ago
});
},
// 获取报价
getQuote() {
if (this.data.evaluateData.quantity) {
3 months ago
this.calculatePrice();
} else {
wx.showToast({
title: '请选择数量',
3 months ago
icon: 'none',
duration: 2000
});
}
},
// 上一步
prevStep() {
if (this.data.evaluateStep > 1 && this.data.evaluateStep < 9) {
3 months ago
this.setData({
evaluateStep: this.data.evaluateStep - 1,
fromPreviousStep: true // 标记是从下一步返回的
});
} else if (this.data.evaluateStep === 9) {
// 如果在结果页面,返回第七步(数量选择)
this.setData({
evaluateStep: 7,
fromPreviousStep: true
});
3 months ago
} else {
// 如果在第一步,返回上一页
wx.navigateBack();
}
},
// 计算价格
calculatePrice() {
console.log('开始执行calculatePrice函数');
const { region, breed, spec, freshness, size, packaging, quantity } = this.data.evaluateData;
3 months ago
// 校验必填参数
console.log('校验必填参数:', { region, breed, spec, freshness, size, packaging, quantity });
if (!region || !breed || !spec || !freshness || !size || !packaging || !quantity) {
console.error('参数校验失败,缺少必要信息');
3 months ago
wx.showToast({
title: '请完成所有选项',
icon: 'none',
duration: 2000
});
return;
}
// 显示加载中
wx.showLoading({
title: '计算中...',
mask: true
});
// 整合用户输入信息
console.log('准备整合用户输入信息');
const userData = JSON.stringify({
region,
breed,
spec,
freshness,
size,
packaging,
quantity
});
console.log('用户输入信息:', userData);
// 向后端发送请求
console.log('准备发送API请求到 /api/openai/submit');
api.request('/api/openai/submit', 'POST', {
userdate: userData
}).then(res => {
console.log('API请求成功,响应数据:', res);
3 months ago
// 检查响应数据有效性
if (!res) {
throw new Error('后端返回的响应为空');
}
3 months ago
// 解析后端返回的aidate数据
let aiData;
try {
console.log('开始解析aidate数据:', res.aidate);
aiData = typeof res.aidate === 'string' ? JSON.parse(res.aidate) : res.aidate;
console.log('aidate解析结果:', aiData);
} catch (e) {
console.error('解析aidate失败:', e);
console.warn('使用默认价格数据');
// 如果解析失败,使用默认值
aiData = {
finalPrice: '0',
totalPrice: '0'
};
}
3 months ago
// 验证解析后的数据
if (!aiData || typeof aiData !== 'object') {
console.error('解析后的aidata格式错误:', aiData);
aiData = {
finalPrice: '0',
totalPrice: '0'
};
}
3 months ago
// 更新结果
console.log('准备更新页面数据,finalPrice:', aiData.finalPrice || '0', 'totalPrice:', aiData.totalPrice || '0');
3 months ago
this.setData({
evaluateResult: {
finalPrice: aiData.finalPrice || '0',
totalPrice: aiData.totalPrice || '0',
aidate: res.aidate || '' // 保存完整的aidate数据
3 months ago
},
evaluateStep: 9
}, () => {
console.log('页面数据更新完成,加载状态隐藏');
3 months ago
wx.hideLoading();
});
}).catch(err => {
console.error('API请求异常:', err);
wx.hideLoading();
3 months ago
// 根据不同类型的错误提供更详细的提示
let errorMessage = '计算失败,请重试';
if (err && err.message) {
console.error('错误详情:', err.message);
// 可以根据具体错误类型提供更精确的提示
if (err.message.includes('网络')) {
errorMessage = '网络连接异常,请检查网络设置';
} else if (err.message.includes('超时')) {
errorMessage = '请求超时,请稍后再试';
}
}
wx.showToast({
title: errorMessage,
icon: 'none',
duration: 3000
});
});
3 months ago
},
// 重新估价
restartEvaluate() {
this.setData({
evaluateStep: 1,
evaluateData: {
region: '',
breed: '',
spec: '',
3 months ago
freshness: '',
size: '',
packaging: '',
quantity: ''
3 months ago
},
evaluateResult: {
finalPrice: '0',
totalPrice: '0',
aidate: ''
3 months ago
},
fromPreviousStep: false // 重置标志
});
},
// 返回首页
backToHome() {
wx.navigateBack();
},
// 跳转到购物页面
goToBuy() {
console.log('goToBuy 函数被调用');
// 使用与custom-tab-bar相同的跳转逻辑
const url = 'pages/buyer/index';
// 先尝试使用navigateTo
wx.navigateTo({
url: '/' + url,
success: function(res) {
console.log('使用navigateTo成功跳转到购物页面:', res);
},
fail: function(error) {
console.log('navigateTo失败,尝试使用reLaunch:', error);
// 如果navigateTo失败,使用reLaunch
wx.reLaunch({
url: '/' + url,
success: function(res) {
console.log('使用reLaunch成功跳转到购物页面:', res);
},
fail: function(err) {
console.error('reLaunch也失败:', err);
}
});
}
});
},
// 跳转到货源页面
goToSell() {
console.log('goToSell 函数被调用');
// 使用与custom-tab-bar相同的跳转逻辑
const url = 'pages/seller/index';
// 先尝试使用navigateTo
wx.navigateTo({
url: '/' + url,
success: function(res) {
console.log('使用navigateTo成功跳转到货源页面:', res);
},
fail: function(error) {
console.log('navigateTo失败,尝试使用reLaunch:', error);
// 如果navigateTo失败,使用reLaunch
wx.reLaunch({
url: '/' + url,
success: function(res) {
console.log('使用reLaunch成功跳转到货源页面:', res);
},
fail: function(err) {
console.error('reLaunch也失败:', err);
}
});
}
});
}
})