preference.vue 6.7 KB

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