caution-detail.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. <template>
  2. <view class="page">
  3. <view class="warning-banner">
  4. <text class="banner-icon">⚠️</text>
  5. <text class="banner-text">以下食材推荐指数低于 0,建议谨慎食用或避免</text>
  6. </view>
  7. <view class="member-bar" v-if="memberList.length > 1">
  8. <view class="member-tab" :class="{'active': currentMemberId === (item.id || item.userId)}"
  9. v-for="(item, mi) in memberList" :key="mi"
  10. @tap="switchMember(item)">
  11. <text class="member-name">{{ item.nickname || item.roleLabel || '全部' }}</text>
  12. </view>
  13. </view>
  14. <scroll-view class="content" scroll-y :refresher-enabled="true" :refresher-triggered="refreshing" @refresherrefresh="onRefresh">
  15. <view class="food-card" v-for="(item, idx) in foods" :key="idx">
  16. <view class="food-header">
  17. <text class="food-name">{{ item.foodName }}</text>
  18. <view class="score-badge-group">
  19. <text class="score-value danger">{{ item.indexScore }}</text>
  20. <view class="caution-badge">戒备</view>
  21. </view>
  22. </view>
  23. <view class="change-row" v-if="item.changeDir">
  24. <text class="change-label">较上次</text>
  25. <text :class="item.changeClass">{{ item.changeText }}</text>
  26. </view>
  27. <view class="food-meta">
  28. <text class="meta-item" v-if="item.category">{{ item.category }}</text>
  29. </view>
  30. <view class="food-score-row">
  31. <view class="score-bar-wrap">
  32. <view class="score-bar-bg">
  33. <view class="score-bar-fill danger" :style="'width:' + Math.min(Math.abs(item.indexScore) / 50 * 100, 100) + '%'"></view>
  34. </view>
  35. <text class="score-label">推荐指数</text>
  36. </view>
  37. </view>
  38. <mini-trend :data="item.trendPoints" v-if="item.trendPoints && item.trendPoints.length >= 3"></mini-trend>
  39. <view class="food-nutrition" v-if="item.nutrition">
  40. <text class="nut-item">蛋白 {{ item.nutrition.protein }}g</text>
  41. <text class="nut-item">脂肪 {{ item.nutrition.fat }}g</text>
  42. <text class="nut-item">碳水 {{ item.nutrition.carbs }}g</text>
  43. </view>
  44. </view>
  45. <view class="empty-state" v-if="!loading && foods.length === 0">
  46. <text class="empty-icon">✅</text>
  47. <text class="empty-text">暂无戒备食材</text>
  48. <text class="empty-hint">您的菌群报告未显示需要戒备的食材</text>
  49. </view>
  50. <view class="loading-state" v-if="loading">
  51. <text class="loading-text">加载中...</text>
  52. </view>
  53. <view class="bottom-spacer"></view>
  54. </scroll-view>
  55. </view>
  56. </template>
  57. <script>
  58. import { getFoodCautionList, getFoodCautionTrend, getCautionFamilyMembers } from '../../utils/api.js'
  59. import MiniTrend from './mini-trend.vue'
  60. export default {
  61. components: { MiniTrend },
  62. data() {
  63. return {
  64. foods: [],
  65. loading: true,
  66. refreshing: false,
  67. nutritionCache: {},
  68. currentMemberId: null,
  69. memberList: [],
  70. trendMap: {},
  71. // 报告详情入口传入:>0 时按该报告返回戒备食材,隐藏家庭成员切换
  72. reportId: null
  73. }
  74. },
  75. onLoad: function(options) {
  76. this.reportId = options.reportId ? parseInt(options.reportId) : null
  77. this.currentMemberId = options.memberId ? parseInt(options.memberId) : null
  78. if (!this.reportId) {
  79. this.loadMemberList()
  80. }
  81. this.loadFoods()
  82. },
  83. methods: {
  84. goBack: function() {
  85. uni.navigateBack()
  86. },
  87. switchMember: function(item) {
  88. this.currentMemberId = item.id || item.userId
  89. this.loadFoods()
  90. },
  91. async loadMemberList() {
  92. try {
  93. var res = await getCautionFamilyMembers()
  94. if (res && res.code === 200 && res.data && res.data.length > 0) {
  95. var allItem = { id: -1, userId: -1, nickname: '全部', roleLabel: '全部' }
  96. this.memberList = [allItem].concat(res.data)
  97. if (!this.currentMemberId) {
  98. this.currentMemberId = -1
  99. }
  100. }
  101. } catch(e) {}
  102. },
  103. async loadFoods() {
  104. this.loading = true
  105. try {
  106. var res = await getFoodCautionList(this.currentMemberId > 0 ? this.currentMemberId : null, this.reportId)
  107. if (res && res.code === 200 && res.data) {
  108. this.foods = (res.data || []).map(function(f) {
  109. return {
  110. foodId: f.foodId || f.id,
  111. foodName: f.foodName,
  112. indexScore: f.indexScore,
  113. category: f.category,
  114. nutrition: null,
  115. changeDir: null,
  116. changeText: '',
  117. changeClass: '',
  118. trendPoints: null
  119. }
  120. })
  121. this.loadNutrition()
  122. // 按报告快照展示,无历史变化概念,跳过趋势查询
  123. if (!this.reportId) {
  124. this.loadTrends()
  125. }
  126. } else {
  127. this.foods = []
  128. }
  129. } catch (e) {
  130. console.error('加载戒备食材失败', e)
  131. this.foods = []
  132. } finally {
  133. this.loading = false
  134. this.refreshing = false
  135. }
  136. },
  137. async loadNutrition() {
  138. if (!this.foods.length) return
  139. try {
  140. var kbRes = await this.getKnowledgeSection('食物库')
  141. if (kbRes && kbRes.data && kbRes.data['数据']) {
  142. var kbFoods = kbRes.data['数据']
  143. var cache = {}
  144. for (var i = 0; i < kbFoods.length; i++) {
  145. var f = kbFoods[i]
  146. cache[f['名称']] = {
  147. protein: f['蛋白g'] || null,
  148. fat: f['脂肪g'] || null,
  149. carbs: f['碳水化合物g'] || null,
  150. fiber: f['总膳食纤维g'] || null,
  151. energy: f['能量KJ'] || null,
  152. category: f['分类'] || ''
  153. }
  154. }
  155. this.nutritionCache = cache
  156. this.foods = this.foods.map(function(item) {
  157. var n = cache[item.foodName] || {}
  158. return Object.assign({}, item, {
  159. nutrition: n,
  160. category: item.category || n.category || ''
  161. })
  162. })
  163. this.$forceUpdate()
  164. }
  165. } catch (e) {
  166. console.warn('加载营养数据失败', e)
  167. }
  168. },
  169. loadTrends: function() {
  170. if (!this.foods.length) return
  171. var self = this
  172. var foodIds = this.foods.map(function(f) { return f.foodId }).filter(function(id) { return id != null })
  173. if (!foodIds.length) return
  174. getFoodCautionTrend(this.currentMemberId > 0 ? this.currentMemberId : null, foodIds).then(function(res) {
  175. if (res && res.code === 200 && res.data) {
  176. var trendMap = {}
  177. var dataArray = res.data || []
  178. for (var i = 0; i < dataArray.length; i++) {
  179. var t = dataArray[i]
  180. trendMap[t.foodId] = t
  181. }
  182. self.trendMap = trendMap
  183. self.foods = self.foods.map(function(item) {
  184. var t = trendMap[item.foodId]
  185. if (!t) return item
  186. var trendPoints = []
  187. var pts = t.trend || []
  188. for (var j = 0; j < pts.length; j++) {
  189. trendPoints.push(pts[j].score)
  190. }
  191. var changeDir = t.changeDir || 'flat'
  192. var changeText = ''
  193. var changeClass = ''
  194. if (changeDir === 'up') { changeText = '↑ 上升'
  195. changeClass = 'change-up' }
  196. else if (changeDir === 'down') { changeText = '↓ 下降'
  197. changeClass = 'change-down' }
  198. return Object.assign({}, item, {
  199. trendPoints: trendPoints.length >= 3 ? trendPoints : null,
  200. changeDir: changeDir,
  201. changeText: changeText,
  202. changeClass: changeClass
  203. })
  204. })
  205. self.$forceUpdate()
  206. }
  207. }).catch(function() {})
  208. },
  209. getKnowledgeSection: function(key) {
  210. return new Promise(function(resolve) {
  211. uni.request({
  212. url: getApp().globalData.baseApi + '/api/kb/section',
  213. method: 'POST',
  214. header: {
  215. 'Content-Type': 'application/json',
  216. 'Authorization': 'Bearer ' + (uni.getStorageSync('token') || '')
  217. },
  218. data: { key: key },
  219. success: function(res) { resolve(res.data) },
  220. fail: function() { resolve({}) }
  221. })
  222. })
  223. },
  224. onRefresh: function() {
  225. this.refreshing = true
  226. this.loadFoods()
  227. }
  228. }
  229. }
  230. </script>
  231. <style scoped>
  232. .page {
  233. min-height: 100vh;
  234. background: #f5f6fa;
  235. }
  236. .nav-placeholder { width: 80rpx; }
  237. .warning-banner {
  238. display: flex;
  239. align-items: center;
  240. gap: 12rpx;
  241. padding: 20rpx 30rpx;
  242. background: #FFF3CD;
  243. border-bottom: 1rpx solid #FFECB5;
  244. }
  245. .banner-icon { font-size: 32rpx; }
  246. .banner-text { font-size: 24rpx; color: #856404; flex: 1; }
  247. .member-bar {
  248. display: flex;
  249. overflow-x: auto;
  250. padding: 16rpx 30rpx 8rpx;
  251. background: #fff;
  252. border-bottom: 1rpx solid #f0f0f0;
  253. gap: 16rpx;
  254. -webkit-overflow-scrolling: touch;
  255. }
  256. .member-tab {
  257. flex-shrink: 0;
  258. padding: 8rpx 20rpx;
  259. border-radius: 20rpx;
  260. background: #f5f5f5;
  261. color: #666;
  262. font-size: 26rpx;
  263. font-weight: 500;
  264. }
  265. .member-tab.active {
  266. background: linear-gradient(135deg, #EF4444, #DC2626);
  267. color: #fff;
  268. }
  269. .content {
  270. padding: 20rpx 30rpx;
  271. height: calc(100vh - 160rpx);
  272. }
  273. .food-card {
  274. background: #fff;
  275. border-radius: 16rpx;
  276. padding: 24rpx;
  277. margin-bottom: 16rpx;
  278. border-left: 6rpx solid #EF4444;
  279. box-shadow: 0 2rpx 8rpx rgba(239, 68, 68, 0.08);
  280. }
  281. .food-header {
  282. display: flex;
  283. align-items: center;
  284. justify-content: space-between;
  285. margin-bottom: 8rpx;
  286. }
  287. .food-name {
  288. font-size: 32rpx;
  289. font-weight: 600;
  290. color: #1a1a1a;
  291. }
  292. .score-badge-group {
  293. display: flex;
  294. align-items: center;
  295. gap: 12rpx;
  296. }
  297. .score-value {
  298. font-size: 28rpx;
  299. font-weight: 700;
  300. color: #EF4444;
  301. min-width: 60rpx;
  302. text-align: right;
  303. }
  304. .caution-badge {
  305. padding: 4rpx 16rpx;
  306. background: #EF4444;
  307. color: #fff;
  308. border-radius: 20rpx;
  309. font-size: 22rpx;
  310. font-weight: 500;
  311. }
  312. .change-row {
  313. display: flex;
  314. align-items: center;
  315. gap: 8rpx;
  316. margin-bottom: 12rpx;
  317. font-size: 24rpx;
  318. }
  319. .change-label { color: #999; }
  320. .change-up { color: #10B981; font-weight: 600; }
  321. .change-down { color: #EF4444; font-weight: 600; }
  322. .food-meta {
  323. margin-bottom: 16rpx;
  324. }
  325. .meta-item {
  326. font-size: 24rpx;
  327. color: #888;
  328. background: #f5f5f5;
  329. padding: 4rpx 12rpx;
  330. border-radius: 8rpx;
  331. }
  332. .food-score-row {
  333. display: flex;
  334. align-items: center;
  335. gap: 16rpx;
  336. margin-bottom: 12rpx;
  337. }
  338. .score-bar-wrap {
  339. flex: 1;
  340. display: flex;
  341. align-items: center;
  342. gap: 12rpx;
  343. }
  344. .score-bar-bg {
  345. flex: 1;
  346. height: 12rpx;
  347. background: #fee2e2;
  348. border-radius: 6rpx;
  349. overflow: hidden;
  350. }
  351. .score-bar-fill {
  352. height: 100%;
  353. border-radius: 6rpx;
  354. transition: width 0.3s ease;
  355. }
  356. .score-bar-fill.danger {
  357. background: linear-gradient(90deg, #EF4444, #DC2626);
  358. }
  359. .score-label {
  360. font-size: 24rpx;
  361. color: #999;
  362. white-space: nowrap;
  363. }
  364. .food-nutrition {
  365. display: flex;
  366. gap: 20rpx;
  367. flex-wrap: wrap;
  368. }
  369. .nut-item {
  370. font-size: 22rpx;
  371. color: #999;
  372. }
  373. .empty-state {
  374. text-align: center;
  375. padding: 100rpx 40rpx;
  376. }
  377. .empty-icon {
  378. font-size: 80rpx;
  379. display: block;
  380. margin-bottom: 20rpx;
  381. }
  382. .empty-text {
  383. font-size: 30rpx;
  384. color: #333;
  385. font-weight: 500;
  386. display: block;
  387. margin-bottom: 12rpx;
  388. }
  389. .empty-hint {
  390. font-size: 24rpx;
  391. color: #999;
  392. display: block;
  393. }
  394. .loading-state {
  395. text-align: center;
  396. padding: 80rpx 0;
  397. }
  398. .loading-text {
  399. font-size: 26rpx;
  400. color: #999;
  401. }
  402. .bottom-spacer { height: 40rpx; }
  403. </style>