preference.vue 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. <template>
  2. <view class="preference-page">
  3. <!-- 月食品预算 -->
  4. <view class="form-section">
  5. <text class="label">月食品预算(元)</text>
  6. <slider :value="budget" :min="500" :max="10000" :step="100" show-value @change="onBudgetChange" />
  7. <text class="hint">当前: {{ budget }}元/月</text>
  8. </view>
  9. <!-- 口味偏好 -->
  10. <view class="form-section">
  11. <text class="label">口味偏好</text>
  12. <checkbox-group @change="onTasteChange">
  13. <view class="checkbox-row">
  14. <label class="checkbox-label" v-for="item in tasteOptions" :key="item">
  15. <checkbox :value="item" :checked="tastePreferences.indexOf(item) >= 0" />
  16. <text class="checkbox-text">{{ item }}</text>
  17. </label>
  18. </view>
  19. </checkbox-group>
  20. </view>
  21. <!-- 菜系偏好 -->
  22. <view class="form-section">
  23. <text class="label">偏好菜系</text>
  24. <picker :value="cuisineIndex" :range="cuisineList" @change="onCuisineChange">
  25. <view class="picker-value">{{ cuisineList[cuisineIndex] || '请选择' }}</view>
  26. </picker>
  27. </view>
  28. <!-- 每日餐数 -->
  29. <view class="form-section">
  30. <text class="label">每日餐数</text>
  31. <picker :value="mealIndex" :range="mealList" @change="onMealCountChange">
  32. <view class="picker-value">{{ mealList[mealIndex] || '请选择' }}</view>
  33. </picker>
  34. </view>
  35. <!-- 烹饪能力 -->
  36. <view class="form-section">
  37. <text class="label">烹饪能力</text>
  38. <radio-group @change="onCookingChange">
  39. <view class="radio-row">
  40. <label class="radio-label" v-for="item in cookingOptions" :key="item.value">
  41. <radio :value="item.value" :checked="cookingAbility === item.value" />
  42. <text class="radio-text">{{ item.label }}</text>
  43. </label>
  44. </view>
  45. </radio-group>
  46. </view>
  47. <button class="save-btn" type="primary" :loading="saving" @click="save" :disabled="saving">
  48. {{ saving ? '保存中...' : '保存设置' }}
  49. </button>
  50. </view>
  51. </template>
  52. <script>
  53. import { getNutritionProfile, saveNutritionProfile } from '@/utils/api'
  54. export default {
  55. name: 'MealPreference',
  56. data() {
  57. return {
  58. budget: 2000,
  59. tastePreferences: [],
  60. tasteOptions: ['清淡', '微辣', '甜', '酸', '咸', '鲜'],
  61. cuisineIndex: 0,
  62. cuisineList: ['中式', '粤菜', '川菜', '西式', '日式', '其他'],
  63. mealIndex: 1,
  64. mealList: ['2餐', '3餐', '4餐', '5餐'],
  65. cookingAbility: 'simple',
  66. cookingOptions: [
  67. { value: 'simple', label: '简单(只会基础)' },
  68. { value: 'medium', label: '一般(能炒家常菜)' },
  69. { value: 'advanced', label: '较强(能做大菜)' }
  70. ],
  71. saving: false
  72. }
  73. },
  74. onLoad() {
  75. this.loadCurrentPreferences()
  76. },
  77. methods: {
  78. async loadCurrentPreferences() {
  79. try {
  80. var res = await getNutritionProfile({})
  81. if (res.code === 0 && res.data) {
  82. var profile = res.data
  83. if (profile.budgetMonthly) {
  84. this.budget = profile.budgetMonthly
  85. }
  86. if (profile.familyTastePreferences) {
  87. try {
  88. var parsed = JSON.parse(profile.familyTastePreferences)
  89. if (Array.isArray(parsed)) {
  90. this.tastePreferences = parsed
  91. } else {
  92. this.tastePreferences = [String(parsed)]
  93. }
  94. } catch (e) {
  95. this.tastePreferences = profile.familyTastePreferences.split(',')
  96. }
  97. }
  98. if (profile.cuisineStyle) {
  99. var idx = this.cuisineList.indexOf(profile.cuisineStyle)
  100. if (idx >= 0) {
  101. this.cuisineIndex = idx
  102. }
  103. }
  104. if (profile.mealCount) {
  105. var mealIdx = profile.mealCount - 2
  106. if (mealIdx >= 0 && mealIdx < this.mealList.length) {
  107. this.mealIndex = mealIdx
  108. }
  109. }
  110. if (profile.cookingAbility) {
  111. this.cookingAbility = profile.cookingAbility
  112. }
  113. }
  114. } catch (e) {
  115. // 首次加载无偏好,使用默认值
  116. }
  117. },
  118. onBudgetChange(e) {
  119. this.budget = e.detail.value
  120. },
  121. onTasteChange(e) {
  122. this.tastePreferences = e.detail.value
  123. },
  124. onCuisineChange(e) {
  125. this.cuisineIndex = e.detail.value
  126. },
  127. onMealCountChange(e) {
  128. this.mealIndex = e.detail.value
  129. },
  130. onCookingChange(e) {
  131. this.cookingAbility = e.detail.value
  132. },
  133. async save() {
  134. this.saving = true
  135. try {
  136. var params = {
  137. budgetMonthly: this.budget,
  138. familyTastePreferences: JSON.stringify(this.tastePreferences),
  139. cuisineStyle: this.cuisineList[this.cuisineIndex],
  140. mealCount: this.mealIndex + 2,
  141. cookingAbility: this.cookingAbility
  142. }
  143. var res = await saveNutritionProfile(params)
  144. if (res.code === 0) {
  145. uni.showToast({ title: '保存成功', icon: 'success' })
  146. } else {
  147. uni.showToast({ title: res.message || '保存失败', icon: 'none' })
  148. }
  149. } catch (e) {
  150. uni.showToast({ title: '网络错误', icon: 'none' })
  151. } finally {
  152. this.saving = false
  153. }
  154. }
  155. }
  156. }
  157. </script>
  158. <style scoped>
  159. .preference-page {
  160. min-height: 100vh;
  161. background: var(--bg, #FFF7ED);
  162. padding: 20rpx 30rpx;
  163. }
  164. .form-section {
  165. background: #fff;
  166. border-radius: 16rpx;
  167. padding: 30rpx;
  168. box-shadow: var(--shadow-md, 0 2rpx 12rpx rgba(0,0,0,0.06));
  169. margin-bottom: 20rpx;
  170. }
  171. .label {
  172. font-size: 28rpx;
  173. color: #333;
  174. margin-bottom: 16rpx;
  175. display: block;
  176. font-weight: 500;
  177. }
  178. .hint {
  179. font-size: 24rpx;
  180. color: var(--color-primary, #F97316);
  181. margin-top: 10rpx;
  182. display: block;
  183. }
  184. .picker-value {
  185. font-size: 28rpx;
  186. color: var(--color-primary, #F97316);
  187. padding: 16rpx 20rpx;
  188. background: #FFF0E0;
  189. border-radius: 8rpx;
  190. }
  191. .checkbox-row {
  192. display: flex;
  193. flex-direction: row;
  194. flex-wrap: wrap;
  195. gap: 16rpx;
  196. }
  197. .checkbox-label {
  198. display: flex;
  199. flex-direction: row;
  200. align-items: center;
  201. margin-right: 16rpx;
  202. margin-bottom: 10rpx;
  203. }
  204. .checkbox-text {
  205. font-size: 28rpx;
  206. color: #333;
  207. margin-left: 8rpx;
  208. }
  209. .radio-row {
  210. display: flex;
  211. flex-direction: column;
  212. gap: 16rpx;
  213. }
  214. .radio-label {
  215. display: flex;
  216. flex-direction: row;
  217. align-items: center;
  218. margin-bottom: 10rpx;
  219. }
  220. .radio-text {
  221. font-size: 28rpx;
  222. color: #333;
  223. margin-left: 8rpx;
  224. }
  225. .save-btn {
  226. margin: 30rpx 0;
  227. background: var(--color-primary, #F97316);
  228. border: none;
  229. border-radius: 12rpx;
  230. font-size: 32rpx;
  231. height: 88rpx;
  232. line-height: 88rpx;
  233. }
  234. </style>