food-query.vue 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. <template>
  2. <view class="food-query-page">
  3. <view class="search-bar">
  4. <input class="search-input" placeholder="搜索食材..." v-model="searchQuery" @confirm="searchFood" />
  5. <view class="search-btn" @tap="searchFood">搜索</view>
  6. </view>
  7. <scroll-view class="content" scroll-y>
  8. <!-- 分类标签 -->
  9. <view class="category-tabs">
  10. <view class="tab-item" :class="{'tab-active': activeCategory === 'all'}" @tap="switchCategory('all')">
  11. <text>全部</text>
  12. </view>
  13. <view class="tab-item" :class="{'tab-active': activeCategory === 'vegetable'}" @tap="switchCategory('vegetable')">
  14. <text>蔬菜</text>
  15. </view>
  16. <view class="tab-item" :class="{'tab-active': activeCategory === 'fruit'}" @tap="switchCategory('fruit')">
  17. <text>水果</text>
  18. </view>
  19. <view class="tab-item" :class="{'tab-active': activeCategory === 'meat'}" @tap="switchCategory('meat')">
  20. <text>肉禽</text>
  21. </view>
  22. <view class="tab-item" :class="{'tab-active': activeCategory === 'seafood'}" @tap="switchCategory('seafood')">
  23. <text>水产</text>
  24. </view>
  25. <view class="tab-item" :class="{'tab-active': activeCategory === 'grain'}" @tap="switchCategory('grain')">
  26. <text>谷物</text>
  27. </view>
  28. </view>
  29. <!-- 筛选 -->
  30. <view class="filter-bar">
  31. <view class="filter-item" :class="{'filter-active': filterType === 'suitable'}" @tap="setFilter('suitable')">
  32. <text>✅ 适合</text>
  33. </view>
  34. <view class="filter-item" :class="{'filter-active': filterType === 'unsuitable'}" @tap="setFilter('unsuitable')">
  35. <text>❌ 不适合</text>
  36. </view>
  37. </view>
  38. <!-- 食材列表 -->
  39. <view class="food-list">
  40. <view class="food-item" v-for="food in filteredFoods" :key="food.id">
  41. <view class="food-info">
  42. <text class="food-name">{{ food.name }}</text>
  43. <text class="food-category">{{ food.category }}</text>
  44. <text class="food-score" v-if="food.score">推荐分: {{ food.score }}</text>
  45. </view>
  46. <view class="food-status" :class="'status-' + food.status">
  47. <text>{{ food.statusText }}</text>
  48. </view>
  49. </view>
  50. </view>
  51. <view class="empty-state" v-if="filteredFoods.length === 0">
  52. <text>暂无食材数据</text>
  53. </view>
  54. </scroll-view>
  55. </view>
  56. </template>
  57. <script>
  58. import config from '@/config.js'
  59. export default {
  60. data() {
  61. return {
  62. searchQuery: '',
  63. activeCategory: 'all',
  64. filterType: 'suitable',
  65. foods: [],
  66. allFoods: []
  67. }
  68. },
  69. computed: {
  70. filteredFoods: function() {
  71. var self = this
  72. var result = self.allFoods
  73. // 分类筛选
  74. if (self.activeCategory !== 'all') {
  75. result = result.filter(function(f) {
  76. return f.category === self.activeCategory
  77. })
  78. }
  79. // 搜索筛选
  80. if (self.searchQuery) {
  81. result = result.filter(function(f) {
  82. return f.name.indexOf(self.searchQuery) >= 0
  83. })
  84. }
  85. // 适合/不适合筛选
  86. if (self.filterType === 'suitable') {
  87. result = result.filter(function(f) {
  88. return f.score >= 50
  89. })
  90. } else {
  91. result = result.filter(function(f) {
  92. return f.score < 50
  93. })
  94. }
  95. return result
  96. }
  97. },
  98. onLoad: function() {
  99. this.loadFoods()
  100. },
  101. methods: {
  102. goBack: function() {
  103. uni.navigateBack()
  104. },
  105. loadFoods: function() {
  106. var self = this
  107. var token = uni.getStorageSync('token')
  108. if (!token) return
  109. uni.request({
  110. url: this.getApiUrl('/api/health/foods'),
  111. method: 'POST',
  112. header: { 'Content-Type': 'application/json', 'Authorization': 'Bearer ' + token },
  113. success: function(res) {
  114. if (res.data && res.data.code === 200 && res.data.data) {
  115. self.allFoods = res.data.data
  116. }
  117. }
  118. })
  119. },
  120. searchFood: function() {
  121. // 搜索由 computed 自动处理
  122. },
  123. switchCategory: function(category) {
  124. this.activeCategory = category
  125. },
  126. setFilter: function(type) {
  127. this.filterType = type
  128. },
  129. getApiUrl: function(path) {
  130. return config.api(path)
  131. }
  132. }
  133. }
  134. </script>
  135. <style scoped>
  136. .food-query-page {
  137. min-height: 100vh;
  138. background-color: #F5F7FA;
  139. display: flex;
  140. flex-direction: column;
  141. }
  142. .nav-placeholder {
  143. width: 80rpx;
  144. }
  145. .search-bar {
  146. display: flex;
  147. padding: 20rpx 30rpx;
  148. background-color: #FFFFFF;
  149. }
  150. .search-input {
  151. flex: 1;
  152. border: 1rpx solid #ddd;
  153. border-radius: 30rpx;
  154. padding: 16rpx 24rpx;
  155. font-size: 26rpx;
  156. background-color: #FAFAFA;
  157. }
  158. .search-btn {
  159. margin-left: 20rpx;
  160. padding: 16rpx 32rpx;
  161. background-color: #FF8C42;
  162. color: #FFFFFF;
  163. border-radius: 30rpx;
  164. font-size: 26rpx;
  165. }
  166. .content {
  167. flex: 1;
  168. padding: 20rpx 30rpx;
  169. }
  170. .category-tabs {
  171. display: flex;
  172. flex-wrap: wrap;
  173. gap: 16rpx;
  174. margin-bottom: 20rpx;
  175. }
  176. .tab-item {
  177. padding: 12rpx 24rpx;
  178. border: 1rpx solid #ddd;
  179. border-radius: 30rpx;
  180. font-size: 24rpx;
  181. color: #666;
  182. background-color: #FFFFFF;
  183. }
  184. .tab-active {
  185. border-color: #FF8C42;
  186. color: #FF8C42;
  187. background-color: #FFF5EB;
  188. }
  189. .filter-bar {
  190. display: flex;
  191. gap: 20rpx;
  192. margin-bottom: 20rpx;
  193. }
  194. .filter-item {
  195. padding: 12rpx 24rpx;
  196. border: 1rpx solid #ddd;
  197. border-radius: 30rpx;
  198. font-size: 24rpx;
  199. color: #666;
  200. background-color: #FFFFFF;
  201. }
  202. .filter-active {
  203. border-color: #4CAF50;
  204. color: #4CAF50;
  205. background-color: #E8F5E9;
  206. }
  207. .food-list {
  208. display: flex;
  209. flex-direction: column;
  210. gap: 16rpx;
  211. }
  212. .food-item {
  213. display: flex;
  214. justify-content: space-between;
  215. align-items: center;
  216. background-color: #FFFFFF;
  217. padding: 24rpx 30rpx;
  218. border-radius: 16rpx;
  219. box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.04);
  220. }
  221. .food-info {
  222. display: flex;
  223. flex-direction: column;
  224. gap: 8rpx;
  225. }
  226. .food-name {
  227. font-size: 28rpx;
  228. font-weight: 500;
  229. color: #333;
  230. }
  231. .food-category {
  232. font-size: 22rpx;
  233. color: #999;
  234. }
  235. .food-score {
  236. font-size: 22rpx;
  237. color: #FF8C42;
  238. }
  239. .food-status {
  240. padding: 8rpx 16rpx;
  241. border-radius: 20rpx;
  242. font-size: 22rpx;
  243. }
  244. .status-suitable {
  245. background-color: #E8F5E9;
  246. color: #4CAF50;
  247. }
  248. .status-unsuitable {
  249. background-color: #FFEBEE;
  250. color: #F44336;
  251. }
  252. .empty-state {
  253. text-align: center;
  254. padding: 60rpx 0;
  255. font-size: 26rpx;
  256. color: #999;
  257. }
  258. </style>