fix: 修复社区帖子详情页营养统计数据为空的问题
- 修复数据解包逻辑,正确处理 CommonResult 响应 - 新增对 data.nutrition 对象的支持 - 扩展字段映射,支持下划线命名(energy_kcal, protein_g 等) - 使用 Vue. 保证营养数据响应式更新 - 改进营养 AI 回填的稳定性 由 Cursor CLI 检测并修复
This commit is contained in:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user