| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419 |
- <template>
- <view class="page">
- <view class="warning-banner">
- <text class="banner-icon">⚠️</text>
- <text class="banner-text">以下食材推荐指数低于 0,建议谨慎食用或避免</text>
- </view>
- <view class="member-bar" v-if="memberList.length > 1">
- <view class="member-tab" :class="{'active': currentMemberId === (item.id || item.userId)}"
- v-for="(item, mi) in memberList" :key="mi"
- @tap="switchMember(item)">
- <text class="member-name">{{ item.nickname || item.roleLabel || '全部' }}</text>
- </view>
- </view>
- <scroll-view class="content" scroll-y :refresher-enabled="true" :refresher-triggered="refreshing" @refresherrefresh="onRefresh">
- <view class="food-card" v-for="(item, idx) in foods" :key="idx">
- <view class="food-header">
- <text class="food-name">{{ item.foodName }}</text>
- <view class="score-badge-group">
- <text class="score-value danger">{{ item.indexScore }}</text>
- <view class="caution-badge">戒备</view>
- </view>
- </view>
- <view class="change-row" v-if="item.changeDir">
- <text class="change-label">较上次</text>
- <text :class="item.changeClass">{{ item.changeText }}</text>
- </view>
- <view class="food-meta">
- <text class="meta-item" v-if="item.category">{{ item.category }}</text>
- </view>
- <view class="food-score-row">
- <view class="score-bar-wrap">
- <view class="score-bar-bg">
- <view class="score-bar-fill danger" :style="'width:' + Math.min(Math.abs(item.indexScore) / 50 * 100, 100) + '%'"></view>
- </view>
- <text class="score-label">推荐指数</text>
- </view>
- </view>
- <mini-trend :data="item.trendPoints" v-if="item.trendPoints && item.trendPoints.length >= 3"></mini-trend>
- <view class="food-nutrition" v-if="item.nutrition">
- <text class="nut-item">蛋白 {{ item.nutrition.protein }}g</text>
- <text class="nut-item">脂肪 {{ item.nutrition.fat }}g</text>
- <text class="nut-item">碳水 {{ item.nutrition.carbs }}g</text>
- </view>
- </view>
- <view class="empty-state" v-if="!loading && foods.length === 0">
- <text class="empty-icon">✅</text>
- <text class="empty-text">暂无戒备食材</text>
- <text class="empty-hint">您的菌群报告未显示需要戒备的食材</text>
- </view>
- <view class="loading-state" v-if="loading">
- <text class="loading-text">加载中...</text>
- </view>
- <view class="bottom-spacer"></view>
- </scroll-view>
- </view>
- </template>
- <script>
- import { getFoodCautionList, getFoodCautionTrend, getCautionFamilyMembers } from '../../utils/api.js'
- import MiniTrend from './mini-trend.vue'
- export default {
- components: { MiniTrend },
- data() {
- return {
- foods: [],
- loading: true,
- refreshing: false,
- nutritionCache: {},
- currentMemberId: null,
- memberList: [],
- trendMap: {},
- // 报告详情入口传入:>0 时按该报告返回戒备食材,隐藏家庭成员切换
- reportId: null
- }
- },
- onLoad: function(options) {
- this.reportId = options.reportId ? parseInt(options.reportId) : null
- this.currentMemberId = options.memberId ? parseInt(options.memberId) : null
- if (!this.reportId) {
- this.loadMemberList()
- }
- this.loadFoods()
- },
- methods: {
- goBack: function() {
- uni.navigateBack()
- },
- switchMember: function(item) {
- this.currentMemberId = item.id || item.userId
- this.loadFoods()
- },
- async loadMemberList() {
- try {
- var res = await getCautionFamilyMembers()
- if (res && res.code === 200 && res.data && res.data.length > 0) {
- var allItem = { id: -1, userId: -1, nickname: '全部', roleLabel: '全部' }
- this.memberList = [allItem].concat(res.data)
- if (!this.currentMemberId) {
- this.currentMemberId = -1
- }
- }
- } catch(e) {}
- },
- async loadFoods() {
- this.loading = true
- try {
- var res = await getFoodCautionList(this.currentMemberId > 0 ? this.currentMemberId : null, this.reportId)
- if (res && res.code === 200 && res.data) {
- this.foods = (res.data || []).map(function(f) {
- return {
- foodId: f.foodId || f.id,
- foodName: f.foodName,
- indexScore: f.indexScore,
- category: f.category,
- nutrition: null,
- changeDir: null,
- changeText: '',
- changeClass: '',
- trendPoints: null
- }
- })
- this.loadNutrition()
- // 按报告快照展示,无历史变化概念,跳过趋势查询
- if (!this.reportId) {
- this.loadTrends()
- }
- } else {
- this.foods = []
- }
- } catch (e) {
- console.error('加载戒备食材失败', e)
- this.foods = []
- } finally {
- this.loading = false
- this.refreshing = false
- }
- },
- async loadNutrition() {
- if (!this.foods.length) return
- try {
- var kbRes = await this.getKnowledgeSection('食物库')
- if (kbRes && kbRes.data && kbRes.data['数据']) {
- var kbFoods = kbRes.data['数据']
- var cache = {}
- for (var i = 0; i < kbFoods.length; i++) {
- var f = kbFoods[i]
- cache[f['名称']] = {
- protein: f['蛋白g'] || null,
- fat: f['脂肪g'] || null,
- carbs: f['碳水化合物g'] || null,
- fiber: f['总膳食纤维g'] || null,
- energy: f['能量KJ'] || null,
- category: f['分类'] || ''
- }
- }
- this.nutritionCache = cache
- this.foods = this.foods.map(function(item) {
- var n = cache[item.foodName] || {}
- return Object.assign({}, item, {
- nutrition: n,
- category: item.category || n.category || ''
- })
- })
- this.$forceUpdate()
- }
- } catch (e) {
- console.warn('加载营养数据失败', e)
- }
- },
- loadTrends: function() {
- if (!this.foods.length) return
- var self = this
- var foodIds = this.foods.map(function(f) { return f.foodId }).filter(function(id) { return id != null })
- if (!foodIds.length) return
- getFoodCautionTrend(this.currentMemberId > 0 ? this.currentMemberId : null, foodIds).then(function(res) {
- if (res && res.code === 200 && res.data) {
- var trendMap = {}
- var dataArray = res.data || []
- for (var i = 0; i < dataArray.length; i++) {
- var t = dataArray[i]
- trendMap[t.foodId] = t
- }
- self.trendMap = trendMap
- self.foods = self.foods.map(function(item) {
- var t = trendMap[item.foodId]
- if (!t) return item
- var trendPoints = []
- var pts = t.trend || []
- for (var j = 0; j < pts.length; j++) {
- trendPoints.push(pts[j].score)
- }
- var changeDir = t.changeDir || 'flat'
- var changeText = ''
- var changeClass = ''
- if (changeDir === 'up') { changeText = '↑ 上升'
- changeClass = 'change-up' }
- else if (changeDir === 'down') { changeText = '↓ 下降'
- changeClass = 'change-down' }
- return Object.assign({}, item, {
- trendPoints: trendPoints.length >= 3 ? trendPoints : null,
- changeDir: changeDir,
- changeText: changeText,
- changeClass: changeClass
- })
- })
- self.$forceUpdate()
- }
- }).catch(function() {})
- },
- getKnowledgeSection: function(key) {
- return new Promise(function(resolve) {
- uni.request({
- url: getApp().globalData.baseApi + '/api/kb/section',
- method: 'POST',
- header: {
- 'Content-Type': 'application/json',
- 'Authorization': 'Bearer ' + (uni.getStorageSync('token') || '')
- },
- data: { key: key },
- success: function(res) { resolve(res.data) },
- fail: function() { resolve({}) }
- })
- })
- },
- onRefresh: function() {
- this.refreshing = true
- this.loadFoods()
- }
- }
- }
- </script>
- <style scoped>
- .page {
- min-height: 100vh;
- background: #f5f6fa;
- }
- .nav-placeholder { width: 80rpx; }
- .warning-banner {
- display: flex;
- align-items: center;
- gap: 12rpx;
- padding: 20rpx 30rpx;
- background: #FFF3CD;
- border-bottom: 1rpx solid #FFECB5;
- }
- .banner-icon { font-size: 32rpx; }
- .banner-text { font-size: 24rpx; color: #856404; flex: 1; }
- .member-bar {
- display: flex;
- overflow-x: auto;
- padding: 16rpx 30rpx 8rpx;
- background: #fff;
- border-bottom: 1rpx solid #f0f0f0;
- gap: 16rpx;
- -webkit-overflow-scrolling: touch;
- }
- .member-tab {
- flex-shrink: 0;
- padding: 8rpx 20rpx;
- border-radius: 20rpx;
- background: #f5f5f5;
- color: #666;
- font-size: 26rpx;
- font-weight: 500;
- }
- .member-tab.active {
- background: linear-gradient(135deg, #EF4444, #DC2626);
- color: #fff;
- }
- .content {
- padding: 20rpx 30rpx;
- height: calc(100vh - 160rpx);
- }
- .food-card {
- background: #fff;
- border-radius: 16rpx;
- padding: 24rpx;
- margin-bottom: 16rpx;
- border-left: 6rpx solid #EF4444;
- box-shadow: 0 2rpx 8rpx rgba(239, 68, 68, 0.08);
- }
- .food-header {
- display: flex;
- align-items: center;
- justify-content: space-between;
- margin-bottom: 8rpx;
- }
- .food-name {
- font-size: 32rpx;
- font-weight: 600;
- color: #1a1a1a;
- }
- .score-badge-group {
- display: flex;
- align-items: center;
- gap: 12rpx;
- }
- .score-value {
- font-size: 28rpx;
- font-weight: 700;
- color: #EF4444;
- min-width: 60rpx;
- text-align: right;
- }
- .caution-badge {
- padding: 4rpx 16rpx;
- background: #EF4444;
- color: #fff;
- border-radius: 20rpx;
- font-size: 22rpx;
- font-weight: 500;
- }
- .change-row {
- display: flex;
- align-items: center;
- gap: 8rpx;
- margin-bottom: 12rpx;
- font-size: 24rpx;
- }
- .change-label { color: #999; }
- .change-up { color: #10B981; font-weight: 600; }
- .change-down { color: #EF4444; font-weight: 600; }
- .food-meta {
- margin-bottom: 16rpx;
- }
- .meta-item {
- font-size: 24rpx;
- color: #888;
- background: #f5f5f5;
- padding: 4rpx 12rpx;
- border-radius: 8rpx;
- }
- .food-score-row {
- display: flex;
- align-items: center;
- gap: 16rpx;
- margin-bottom: 12rpx;
- }
- .score-bar-wrap {
- flex: 1;
- display: flex;
- align-items: center;
- gap: 12rpx;
- }
- .score-bar-bg {
- flex: 1;
- height: 12rpx;
- background: #fee2e2;
- border-radius: 6rpx;
- overflow: hidden;
- }
- .score-bar-fill {
- height: 100%;
- border-radius: 6rpx;
- transition: width 0.3s ease;
- }
- .score-bar-fill.danger {
- background: linear-gradient(90deg, #EF4444, #DC2626);
- }
- .score-label {
- font-size: 24rpx;
- color: #999;
- white-space: nowrap;
- }
- .food-nutrition {
- display: flex;
- gap: 20rpx;
- flex-wrap: wrap;
- }
- .nut-item {
- font-size: 22rpx;
- color: #999;
- }
- .empty-state {
- text-align: center;
- padding: 100rpx 40rpx;
- }
- .empty-icon {
- font-size: 80rpx;
- display: block;
- margin-bottom: 20rpx;
- }
- .empty-text {
- font-size: 30rpx;
- color: #333;
- font-weight: 500;
- display: block;
- margin-bottom: 12rpx;
- }
- .empty-hint {
- font-size: 24rpx;
- color: #999;
- display: block;
- }
- .loading-state {
- text-align: center;
- padding: 80rpx 0;
- }
- .loading-text {
- font-size: 26rpx;
- color: #999;
- }
- .bottom-spacer { height: 40rpx; }
- </style>
|