const app = getApp(); const math = require('../../utils/math.js'); Page({ data: { age: '', height: '', gender: 'male', showResult: false, sdsValue: null, percentile: null, growthEvaluation: '', chartData: null }, // 输入年龄 inputAge(e) { this.setData({ age: Number(e.detail.value) // 确保age是数字 }); }, // 输入身高 inputHeight(e) { this.setData({ height: Number(e.detail.value) // 确保height是数字 }); }, // 选择性别 selectGender(e) { const gender = e.currentTarget.dataset.gender; this.setData({ gender }); }, // 重置表单 reset() { this.setData({ age: '', height: '', gender: 'male', showResult: false }); }, // 计算SDS calculate() { const { age, height, gender } = this.data; // 输入验证 if (!age || !height) { wx.showToast({ title: '请填写完整信息', icon: 'none' }); return; } // 直接使用本地生长数据 this.useLocalGrowthData(); }, // 处理生长数据 processGrowthData(growthData) { try { const { age, height, gender } = this.data; // 计算SDS值并获取标准数据 const { sds, standard } = this.calculateSDS(height, age, gender, growthData); console.log('SDS计算详情:', { height, age, gender, mean: standard.mean, sd: standard.sd, sds }); // 显示计算公式 console.log(`计算公式:SDS = (${height} - ${standard.mean}) / ${standard.sd} = ${sds.toFixed(2)}`); // 计算百分位数 const percentile = this.calculatePercentile(sds); // 获取生长评价 const evaluation = this.getGrowthEvaluation(sds); // 更新结果 this.setData({ showResult: true, sdsValue: Number(sds.toFixed(2)), // 确保sdsValue是数字类型 percentile: percentile, growthEvaluation: evaluation }); // 绘制生长曲线 this.drawGrowthChart(growthData); } catch (error) { wx.showToast({ title: error.message || '计算失败', icon: 'none' }); } }, // 使用本地生长数据 useLocalGrowthData() { try { const localData = require('./growthData.js'); // 确保数据类型一致 const processedData = localData.map(item => ({ ...item, age: Number(item.age) })); this.processGrowthData(processedData); } catch (error) { wx.showToast({ title: '本地数据加载失败', icon: 'none' }); } }, // 计算SDS值 calculateSDS(height, age, gender, growthData) { // 根据年龄和性别获取标准值 const standard = growthData.find(d => Number(d.age) === Number(age) && d.gender === gender ); if (!standard) { throw new Error('未找到对应年龄和性别的标准数据'); } // SDS = (实际值 - 平均值) / 标准差 const sds = (height - standard.mean) / standard.sd; return { sds, standard }; }, // 计算百分位数 calculatePercentile(sds) { // 使用标准正态分布计算百分位数 const percentile = 100 * (1 + math.erf(sds / Math.sqrt(2))) / 2; return percentile.toFixed(1); }, // 获取生长评价 getGrowthEvaluation(sds) { if (sds < -2) { return '生长迟缓'; } else if (sds >= -2 && sds <= 2) { return '正常范围'; } else { return '生长过快'; } }, // 绘制生长曲线 drawGrowthChart(growthData) { const ctx = wx.createCanvasContext('growthChart'); // 绘制逻辑... ctx.draw(); }, // 返回 onClickLeft() { wx.navigateBack(); } });