caution-detail.vue 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. <template>
  2. <view class="page">
  3. <view class="nav-bar">
  4. <view class="nav-back" @tap="goBack">
  5. <text class="back-text">‹ 返回</text>
  6. </view>
  7. <text class="nav-title">食材戒备匹配</text>
  8. <view class="nav-placeholder"></view>
  9. </view>
  10. <view class="warning-banner">
  11. <text class="banner-icon">⚠️</text>
  12. <text class="banner-text">以下食材推荐指数低于 0,建议谨慎食用或避免</text>
  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="caution-badge">戒备</view>
  19. </view>
  20. <view class="food-meta">
  21. <text class="meta-item" v-if="item.category">{{ item.category }}</text>
  22. </view>
  23. <view class="food-score-row">
  24. <view class="score-bar-wrap">
  25. <view class="score-bar-bg">
  26. <view class="score-bar-fill danger" :style="'width:' + Math.min(Math.abs(item.indexScore) / 50 * 100, 100) + '%'"></view>
  27. </view>
  28. <text class="score-value danger">{{ item.indexScore }}</text>
  29. </view>
  30. <text class="score-label">推荐指数</text>
  31. </view>
  32. <view class="food-nutrition" v-if="item.nutrition">
  33. <text class="nut-item">蛋白 {{ item.nutrition.protein }}g</text>
  34. <text class="nut-item">脂肪 {{ item.nutrition.fat }}g</text>
  35. <text class="nut-item">碳水 {{ item.nutrition.carbs }}g</text>
  36. </view>
  37. </view>
  38. <view class="empty-state" v-if="!loading && foods.length === 0">
  39. <text class="empty-icon">✅</text>
  40. <text class="empty-text">暂无戒备食材</text>
  41. <text class="empty-hint">您的菌群报告未显示需要戒备的食材</text>
  42. </view>
  43. <view class="loading-state" v-if="loading">
  44. <text class="loading-text">加载中...</text>
  45. </view>
  46. <view class="bottom-spacer"></view>
  47. </scroll-view>
  48. </view>
  49. </template>
  50. <script>
  51. import { getFoodCautionList } from '../../utils/api.js'
  52. export default {
  53. data() {
  54. return {
  55. foods: [],
  56. loading: true,
  57. refreshing: false,
  58. nutritionCache: {}
  59. }
  60. },
  61. onLoad() {
  62. this.loadFoods()
  63. },
  64. methods: {
  65. goBack() {
  66. uni.navigateBack()
  67. },
  68. async loadFoods() {
  69. this.loading = true
  70. try {
  71. var res = await getFoodCautionList()
  72. if (res && res.code === 200 && res.data) {
  73. this.foods = res.data
  74. // 批量加载营养数据
  75. this.loadNutrition()
  76. } else {
  77. this.foods = []
  78. }
  79. } catch (e) {
  80. console.error('加载戒备食材失败', e)
  81. this.foods = []
  82. } finally {
  83. this.loading = false
  84. this.refreshing = false
  85. }
  86. },
  87. async loadNutrition() {
  88. if (!this.foods.length) return
  89. try {
  90. var kbRes = await this.getKnowledgeSection('食物库')
  91. if (kbRes && kbRes.data && kbRes.data['数据']) {
  92. var kbFoods = kbRes.data['数据']
  93. var cache = {}
  94. for (var i = 0; i < kbFoods.length; i++) {
  95. var f = kbFoods[i]
  96. cache[f['名称']] = {
  97. protein: f['蛋白g'] || null,
  98. fat: f['脂肪g'] || null,
  99. carbs: f['碳水化合物g'] || null,
  100. fiber: f['总膳食纤维g'] || null,
  101. energy: f['能量KJ'] || null,
  102. category: f['分类'] || ''
  103. }
  104. }
  105. this.nutritionCache = cache
  106. this.foods = this.foods.map(function(item) {
  107. var n = cache[item.foodName] || {}
  108. return Object.assign({}, item, {
  109. nutrition: n,
  110. category: item.category || n.category || ''
  111. })
  112. })
  113. this.$forceUpdate()
  114. }
  115. } catch (e) {
  116. console.warn('加载营养数据失败', e)
  117. }
  118. },
  119. getKnowledgeSection(key) {
  120. return new Promise(function(resolve) {
  121. uni.request({
  122. url: getApp().globalData.baseApi + '/api/kb/section',
  123. method: 'POST',
  124. header: {
  125. 'Content-Type': 'application/json',
  126. 'Authorization': 'Bearer ' + (uni.getStorageSync('token') || '')
  127. },
  128. data: { key: key },
  129. success: function(res) { resolve(res.data) },
  130. fail: function() { resolve({}) }
  131. })
  132. })
  133. },
  134. onRefresh() {
  135. this.refreshing = true
  136. this.loadFoods()
  137. }
  138. }
  139. }
  140. </script>
  141. <style scoped>
  142. .page {
  143. min-height: 100vh;
  144. background: #f5f6fa;
  145. }
  146. .nav-bar {
  147. display: flex;
  148. align-items: center;
  149. padding: 20rpx 30rpx;
  150. background: #fff;
  151. position: relative;
  152. }
  153. .nav-back { padding: 10rpx 0; }
  154. .back-text { font-size: 28rpx; color: #4A9BD7; }
  155. .nav-title { flex: 1; text-align: center; font-size: 32rpx; font-weight: 600; }
  156. .nav-placeholder { width: 80rpx; }
  157. .warning-banner {
  158. display: flex;
  159. align-items: center;
  160. gap: 12rpx;
  161. padding: 20rpx 30rpx;
  162. background: #FFF3CD;
  163. border-bottom: 1rpx solid #FFECB5;
  164. }
  165. .banner-icon { font-size: 32rpx; }
  166. .banner-text { font-size: 24rpx; color: #856404; flex: 1; }
  167. .content {
  168. padding: 20rpx 30rpx;
  169. height: calc(100vh - 160rpx);
  170. }
  171. .food-card {
  172. background: #fff;
  173. border-radius: 16rpx;
  174. padding: 24rpx;
  175. margin-bottom: 16rpx;
  176. border-left: 6rpx solid #EF4444;
  177. box-shadow: 0 2rpx 8rpx rgba(239, 68, 68, 0.08);
  178. }
  179. .food-header {
  180. display: flex;
  181. align-items: center;
  182. justify-content: space-between;
  183. margin-bottom: 8rpx;
  184. }
  185. .food-name {
  186. font-size: 32rpx;
  187. font-weight: 600;
  188. color: #1a1a1a;
  189. }
  190. .caution-badge {
  191. padding: 4rpx 16rpx;
  192. background: #EF4444;
  193. color: #fff;
  194. border-radius: 20rpx;
  195. font-size: 22rpx;
  196. font-weight: 500;
  197. }
  198. .food-meta {
  199. margin-bottom: 16rpx;
  200. }
  201. .meta-item {
  202. font-size: 24rpx;
  203. color: #888;
  204. background: #f5f5f5;
  205. padding: 4rpx 12rpx;
  206. border-radius: 8rpx;
  207. }
  208. .food-score-row {
  209. display: flex;
  210. align-items: center;
  211. gap: 16rpx;
  212. margin-bottom: 12rpx;
  213. }
  214. .score-bar-wrap {
  215. flex: 1;
  216. display: flex;
  217. align-items: center;
  218. gap: 12rpx;
  219. }
  220. .score-bar-bg {
  221. flex: 1;
  222. height: 12rpx;
  223. background: #fee2e2;
  224. border-radius: 6rpx;
  225. overflow: hidden;
  226. }
  227. .score-bar-fill {
  228. height: 100%;
  229. border-radius: 6rpx;
  230. transition: width 0.3s ease;
  231. }
  232. .score-bar-fill.danger {
  233. background: linear-gradient(90deg, #EF4444, #DC2626);
  234. }
  235. .score-value {
  236. font-size: 28rpx;
  237. font-weight: 700;
  238. color: #EF4444;
  239. min-width: 60rpx;
  240. text-align: right;
  241. }
  242. .score-label {
  243. font-size: 24rpx;
  244. color: #999;
  245. white-space: nowrap;
  246. }
  247. .food-nutrition {
  248. display: flex;
  249. gap: 20rpx;
  250. flex-wrap: wrap;
  251. }
  252. .nut-item {
  253. font-size: 22rpx;
  254. color: #999;
  255. }
  256. .empty-state {
  257. text-align: center;
  258. padding: 100rpx 40rpx;
  259. }
  260. .empty-icon {
  261. font-size: 80rpx;
  262. display: block;
  263. margin-bottom: 20rpx;
  264. }
  265. .empty-text {
  266. font-size: 30rpx;
  267. color: #333;
  268. font-weight: 500;
  269. display: block;
  270. margin-bottom: 12rpx;
  271. }
  272. .empty-hint {
  273. font-size: 24rpx;
  274. color: #999;
  275. display: block;
  276. }
  277. .loading-state {
  278. text-align: center;
  279. padding: 80rpx 0;
  280. }
  281. .loading-text {
  282. font-size: 26rpx;
  283. color: #999;
  284. }
  285. .bottom-spacer { height: 40rpx; }
  286. </style>