food-list.vue 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. <template>
  2. <view class="food-list-container">
  3. <!-- 顶部导航 -->
  4. <view class="nav-bar">
  5. <text class="nav-back" @click="goBack">‹ 返回</text>
  6. <text class="nav-title">食材推荐</text>
  7. <view class="nav-placeholder"></view>
  8. </view>
  9. <!-- Tab切换 -->
  10. <view class="tab-bar">
  11. <text class="tab-item" :class="{'active': activeTab === 'total'}" @click="switchTab('total')">全部食材</text>
  12. <text class="tab-item" :class="{'active': activeTab === 'byMember'}" @click="switchTab('byMember')">按成员</text>
  13. </view>
  14. <!-- 全部食材模式 -->
  15. <view v-if="activeTab === 'total'" class="food-section">
  16. <view class="food-card" v-for="food in totalFoods" :key="food.id">
  17. <view class="food-header">
  18. <text class="food-name">{{ food.name }}</text>
  19. <view class="food-score" :class="'score-' + getScoreLevel(food.score)">
  20. <text>{{ food.score || '--' }}分</text>
  21. </view>
  22. <view class="rec-score" :class="'rec-' + getRecScoreLevel(food.recommendIndex)" v-if="food.recommendIndex != null">
  23. <text>推荐 {{ food.recommendIndex }}</text>
  24. </view>
  25. </view>
  26. <text class="food-category" v-if="food.category">{{ food.category }}</text>
  27. <view class="food-nutrition" v-if="hasNutrition(food)">
  28. <text class="nutri-item" v-if="food.energyKj">能量: {{ food.energyKj }}kJ</text>
  29. <text class="nutri-item" v-if="food.protein">蛋白质: {{ food.protein }}g</text>
  30. <text class="nutri-item" v-if="food.fat">脂肪: {{ food.fat }}g</text>
  31. <text class="nutri-item" v-if="food.carbs">碳水: {{ food.carbs }}g</text>
  32. <text class="nutri-item" v-if="food.fiber">膳食纤维: {{ food.fiber }}g</text>
  33. <text class="nutri-item" v-if="food.starch">淀粉: {{ food.starch }}g</text>
  34. <text class="nutri-item" v-if="food.cholesterol">胆固醇: {{ food.cholesterol }}mg</text>
  35. </view>
  36. </view>
  37. <view class="empty-state" v-if="totalFoods.length === 0 && !loading">
  38. <text>暂无食材推荐</text>
  39. <text class="empty-hint">上传菌群检测报告后可获得个性化食材推荐</text>
  40. </view>
  41. </view>
  42. <!-- 按成员模式 -->
  43. <view v-if="activeTab === 'byMember'" class="food-section">
  44. <view v-for="(foods, childId) in byMemberFoods" :key="childId" class="member-group">
  45. <view class="member-header">
  46. <text class="member-name">{{ getMemberName(childId) }}</text>
  47. <text class="member-count">{{ foods.length }}项</text>
  48. </view>
  49. <view class="food-card" v-for="food in foods" :key="food.id">
  50. <view class="food-header">
  51. <text class="food-name">{{ food.name }}</text>
  52. <view class="food-score" :class="'score-' + getScoreLevel(food.score)">
  53. <text>{{ food.score || '--' }}分</text>
  54. </view>
  55. <view class="rec-score" :class="'rec-' + getRecScoreLevel(food.recommendIndex)" v-if="food.recommendIndex != null">
  56. <text>推荐 {{ food.recommendIndex }}</text>
  57. </view>
  58. </view>
  59. <text class="food-category" v-if="food.category">{{ food.category }}</text>
  60. <view class="food-nutrition" v-if="hasNutrition(food)">
  61. <text class="nutri-item" v-if="food.energyKj">能量: {{ food.energyKj }}kJ</text>
  62. <text class="nutri-item" v-if="food.protein">蛋白质: {{ food.protein }}g</text>
  63. <text class="nutri-item" v-if="food.fat">脂肪: {{ food.fat }}g</text>
  64. <text class="nutri-item" v-if="food.carbs">碳水: {{ food.carbs }}g</text>
  65. <text class="nutri-item" v-if="food.fiber">膳食纤维: {{ food.fiber }}g</text>
  66. <text class="nutri-item" v-if="food.starch">淀粉: {{ food.starch }}g</text>
  67. <text class="nutri-item" v-if="food.cholesterol">胆固醇: {{ food.cholesterol }}mg</text>
  68. </view>
  69. </view>
  70. </view>
  71. <view class="empty-state" v-if="Object.keys(byMemberFoods).length === 0 && !loading">
  72. <text>暂无食材推荐</text>
  73. <text class="empty-hint">上传菌群检测报告后可获得个性化食材推荐</text>
  74. </view>
  75. </view>
  76. <view class="bottom-spacer"></view>
  77. </view>
  78. </template>
  79. <script>
  80. import config from '@/config.js'
  81. const healthRequest = function(url, data) {
  82. var token = uni.getStorageSync('token')
  83. return new Promise(function(resolve, reject) {
  84. uni.request({
  85. url: config.API_BASE_URL + url,
  86. method: 'POST',
  87. data: data,
  88. header: {
  89. 'Content-Type': 'application/json',
  90. 'Authorization': token ? 'Bearer ' + token : ''
  91. },
  92. success: function(res) {
  93. if (res.data.code === 200) {
  94. resolve(res.data)
  95. } else {
  96. reject(res.data)
  97. }
  98. },
  99. fail: function(err) {
  100. reject(err)
  101. }
  102. })
  103. })
  104. }
  105. export default {
  106. data: function() {
  107. return {
  108. familyId: null,
  109. activeTab: 'total',
  110. totalFoods: [],
  111. byMemberFoods: {},
  112. memberNames: {},
  113. loading: false
  114. }
  115. },
  116. onLoad: function(options) {
  117. var familyId = options.familyId
  118. this.familyId = familyId ? parseInt(familyId) : null
  119. this.loadData()
  120. },
  121. methods: {
  122. loadData: function() {
  123. var self = this
  124. if (!this.familyId) {
  125. uni.showToast({ title: '缺少familyId', icon: 'none' })
  126. return
  127. }
  128. uni.showLoading({ title: '加载中...' })
  129. healthRequest('/api/health/foods/by-family', { familyId: this.familyId }).then(function(res) {
  130. uni.hideLoading()
  131. if (res.data) {
  132. self.totalFoods = res.data.total || []
  133. self.byMemberFoods = res.data.byMember || {}
  134. self.memberNames = res.data.memberNames || {}
  135. }
  136. }).catch(function(e) {
  137. uni.hideLoading()
  138. console.log('获取食材推荐失败', e)
  139. uni.showToast({ title: '加载失败', icon: 'none' })
  140. })
  141. },
  142. switchTab: function(tab) {
  143. this.activeTab = tab
  144. },
  145. getScoreLevel: function(score) {
  146. if (score == null) return 'unknown'
  147. if (score >= 80) return 'high'
  148. if (score >= 60) return 'mid'
  149. return 'low'
  150. },
  151. getRecScoreLevel: function(score) {
  152. if (score == null) return 'unknown'
  153. if (score >= 80) return 'high'
  154. if (score >= 60) return 'mid'
  155. return 'low'
  156. },
  157. hasNutrition: function(food) {
  158. return food.energyKj || food.protein || food.fat || food.carbs || food.fiber || food.starch || food.cholesterol
  159. },
  160. getMemberName: function(childId) {
  161. return this.memberNames[childId] || '成员' + childId
  162. },
  163. goBack: function() {
  164. uni.navigateBack()
  165. }
  166. }
  167. }
  168. </script>
  169. <style scoped>
  170. .food-list-container {
  171. min-height: 100vh;
  172. background: #f5f7fa;
  173. }
  174. .nav-bar {
  175. position: sticky;
  176. top: 0;
  177. background: #fff;
  178. display: flex;
  179. flex-direction: row;
  180. align-items: center;
  181. justify-content: space-between;
  182. padding: 20rpx 30rpx;
  183. box-shadow: 0 2rpx 8rpx rgba(0,0,0,0.06);
  184. z-index: 100;
  185. }
  186. .nav-back { font-size: 32rpx; color: #5B9BD5; }
  187. .nav-title { font-size: 32rpx; font-weight: bold; color: #333; }
  188. .nav-placeholder { width: 80rpx; }
  189. .tab-bar {
  190. display: flex;
  191. flex-direction: row;
  192. background: #fff;
  193. padding: 0 30rpx;
  194. border-bottom: 2rpx solid #eee;
  195. }
  196. .tab-item {
  197. padding: 20rpx 0;
  198. margin-right: 40rpx;
  199. font-size: 28rpx;
  200. color: #666;
  201. border-bottom: 4rpx solid transparent;
  202. }
  203. .tab-item.active {
  204. color: #F97316;
  205. border-bottom-color: #F97316;
  206. font-weight: bold;
  207. }
  208. .food-section {
  209. padding: 20rpx 30rpx;
  210. }
  211. .member-group {
  212. margin-bottom: 30rpx;
  213. }
  214. .member-header {
  215. display: flex;
  216. flex-direction: row;
  217. align-items: center;
  218. justify-content: space-between;
  219. margin-bottom: 16rpx;
  220. }
  221. .member-name {
  222. font-size: 30rpx;
  223. font-weight: bold;
  224. color: #333;
  225. }
  226. .member-count {
  227. font-size: 22rpx;
  228. color: #999;
  229. }
  230. .food-card {
  231. background: #fff;
  232. border-radius: 16rpx;
  233. padding: 20rpx;
  234. margin-bottom: 12rpx;
  235. box-shadow: 0 2rpx 8rpx rgba(0,0,0,0.04);
  236. }
  237. .food-header {
  238. display: flex;
  239. flex-direction: row;
  240. align-items: center;
  241. justify-content: flex-start;
  242. flex-wrap: wrap;
  243. margin-bottom: 8rpx;
  244. }
  245. .food-name {
  246. font-size: 28rpx;
  247. font-weight: bold;
  248. color: #333;
  249. margin-right: 12rpx;
  250. }
  251. .food-score {
  252. font-size: 24rpx;
  253. padding: 4rpx 16rpx;
  254. border-radius: 16rpx;
  255. font-weight: 600;
  256. }
  257. .score-high { background: #DCFCE7; color: #16A34A; }
  258. .score-mid { background: #FEF3C7; color: #D97706; }
  259. .score-low { background: #FEE2E2; color: #DC2626; }
  260. .score-unknown { background: #F3F4F6; color: #6B7280; }
  261. .rec-score {
  262. font-size: 22rpx;
  263. padding: 4rpx 12rpx;
  264. border-radius: 16rpx;
  265. font-weight: 600;
  266. margin-left: 8rpx;
  267. }
  268. .rec-high { background: #DBEAFE; color: #2563EB; }
  269. .rec-mid { background: #E0E7FF; color: #4F46E5; }
  270. .rec-low { background: #F3E8FF; color: #7C3AED; }
  271. .rec-unknown { background: #F3F4F6; color: #6B7280; }
  272. .food-category {
  273. font-size: 22rpx;
  274. color: #6B7280;
  275. background: #F3F4F6;
  276. padding: 2rpx 12rpx;
  277. border-radius: 12rpx;
  278. display: inline-block;
  279. margin-bottom: 8rpx;
  280. }
  281. .food-nutrition {
  282. display: flex;
  283. flex-direction: row;
  284. flex-wrap: wrap;
  285. gap: 20rpx;
  286. margin-top: 8rpx;
  287. }
  288. .nutri-item {
  289. font-size: 22rpx;
  290. color: #374151;
  291. }
  292. .empty-state {
  293. text-align: center;
  294. padding: 100rpx 40rpx;
  295. }
  296. .empty-state text:first-child {
  297. font-size: 28rpx;
  298. color: #999;
  299. display: block;
  300. margin-bottom: 12rpx;
  301. }
  302. .empty-hint {
  303. font-size: 24rpx;
  304. color: #bbb;
  305. }
  306. .bottom-spacer {
  307. height: 40rpx;
  308. }
  309. </style>