preference.vue 7.1 KB

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