From 9dcb58f056bb432c596c055ec25061d370791714 Mon Sep 17 00:00:00 2001 From: scottpan <43121650@qq.com> Date: Thu, 5 Mar 2026 11:19:03 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E7=A4=BE=E5=8C=BA?= =?UTF-8?q?=E5=B8=96=E5=AD=90=E8=AF=A6=E6=83=85=E9=A1=B5=E8=90=A5=E5=85=BB?= =?UTF-8?q?=E7=BB=9F=E8=AE=A1=E6=95=B0=E6=8D=AE=E4=B8=BA=E7=A9=BA=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 修复数据解包逻辑,正确处理 CommonResult 响应 - 新增对 data.nutrition 对象的支持 - 扩展字段映射,支持下划线命名(energy_kcal, protein_g 等) - 使用 Vue. 保证营养数据响应式更新 - 改进营养 AI 回填的稳定性 由 Cursor CLI 检测并修复 --- msh_single_uniapp/pages/tool/post-detail.vue | 31 +++++++++++++------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/msh_single_uniapp/pages/tool/post-detail.vue b/msh_single_uniapp/pages/tool/post-detail.vue index 83a988f..c6e532b 100644 --- a/msh_single_uniapp/pages/tool/post-detail.vue +++ b/msh_single_uniapp/pages/tool/post-detail.vue @@ -328,7 +328,8 @@ export default { try { const res = await getCommunityDetail(id) - const data = res.data || res + // 兼容 CommonResult:res = { code, data: 详情对象 },取 res.data 作为详情 + const data = (res && res.data !== undefined && res.data !== null) ? res.data : res // 格式化帖子数据 this.formatPostData(data) @@ -417,16 +418,23 @@ export default { if (statsFromData.length > 0) return statsFromData } + // 5) nutrition 对象(部分接口返回的 data.nutrition) + const nutritionObj = data.nutrition + if (nutritionObj && typeof nutritionObj === 'object' && !Array.isArray(nutritionObj)) { + const statsFromNut = this.buildNutritionStatsFromNutritionObject(nutritionObj) + if (statsFromNut.length > 0) return statsFromNut + } + return [] }, - /** 从单一营养对象构建 [{label, value}, ...],统一兼容 energyKcal/proteinG/potassiumMg/phosphorusMg 及 calories/protein 等命名 */ + /** 从单一营养对象构建 [{label, value}, ...],统一兼容 energyKcal/proteinG/potassiumMg/phosphorusMg 及 calories/protein、下划线命名 */ buildNutritionStatsFromNutritionObject(obj) { if (!obj || typeof obj !== 'object') return [] - const cal = obj.energyKcal ?? obj.calories ?? obj.energy ?? obj.calorie ?? obj.actualEnergy - const pro = obj.proteinG ?? obj.protein ?? obj.proteins ?? obj.actualProtein - const pot = obj.potassiumMg ?? obj.potassium ?? obj.k - const pho = obj.phosphorusMg ?? obj.phosphorus ?? obj.p + const cal = obj.energyKcal ?? obj.energy_kcal ?? obj.calories ?? obj.energy ?? obj.calorie ?? obj.actualEnergy + const pro = obj.proteinG ?? obj.protein_g ?? obj.protein ?? obj.proteins ?? obj.actualProtein + const pot = obj.potassiumMg ?? obj.potassium_mg ?? obj.potassium ?? obj.k + const pho = obj.phosphorusMg ?? obj.phosphorus_mg ?? obj.phosphorus ?? obj.p return [ { label: '热量(kcal)', value: cal != null ? String(cal) : '-' }, { label: '蛋白质', value: this.formatNutritionValue(pro, 'g') }, @@ -466,7 +474,7 @@ export default { const detail = res.data || res const stats = this.buildNutritionStatsFromCheckinDetail(detail) if (stats.length > 0) { - this.postData.nutritionStats = stats + this.$set(this.postData, 'nutritionStats', stats) } } catch (e) { console.warn('拉取打卡详情补充营养数据失败:', e) @@ -480,11 +488,12 @@ export default { if (!this.postId) return try { const res = await fillPostNutrition(this.postId) - const data = res.data || res + // fill-nutrition 返回 CommonResult.success(nutrition),营养对象在 res.data + const data = (res && res.data !== undefined && res.data !== null) ? res.data : res if (!data) return const stats = this.buildNutritionStatsFromDetailData(data) if (stats.length > 0) { - this.postData.nutritionStats = stats + this.$set(this.postData, 'nutritionStats', stats) } } catch (e) { console.warn('服务端填充帖子营养失败:', e) @@ -512,12 +521,12 @@ export default { const protein = this.formatNutritionValue(data.proteinG, 'g') const potassium = data.potassiumMg != null ? String(data.potassiumMg) + 'mg' : '-' const phosphorus = data.phosphorusMg != null ? String(data.phosphorusMg) + 'mg' : '-' - this.postData.nutritionStats = [ + this.$set(this.postData, 'nutritionStats', [ { label: '热量(kcal)', value: energy }, { label: '蛋白质', value: protein }, { label: '钾', value: potassium }, { label: '磷', value: phosphorus } - ] + ]) uni.showToast({ title: '已补充营养估算', icon: 'success' }) } catch (e) { console.warn('AI 营养估算失败:', e)