caution-detail.vue 6.7 KB

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