food-query.vue 7.1 KB

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