preferences.vue 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. <template>
  2. <view class="preferences-page" :style="{ '--font-scale': fontScale }">
  3. <!-- 顶部进度条 -->
  4. <view class="progress-bar">
  5. <view class="progress-track">
  6. <view class="progress-fill" :style="'width:' + progressPercent + '%'"></view>
  7. </view>
  8. <view class="progress-labels">
  9. <text class="progress-label" :class="{ 'progress-label-active': currentStep >= 1 }">忌口·过敏</text>
  10. <text class="progress-label" :class="{ 'progress-label-active': currentStep >= 2 }">口味·偏好</text>
  11. <text class="progress-label" :class="{ 'progress-label-active': currentStep >= 3 }">健康目标</text>
  12. </view>
  13. </view>
  14. <!-- 步骤内容 -->
  15. <scroll-view class="content" scroll-y>
  16. <!-- ===== Step 1 忌口与过敏 ===== -->
  17. <view v-if="currentStep === 1">
  18. <view class="step-head">
  19. <text class="step-icon">🚫</text>
  20. <view class="step-head-text">
  21. <text class="step-title">哪些食物绝对不能碰?</text>
  22. <text class="step-sub">为了家人的安全,过敏原和忌口请务必填写</text>
  23. </view>
  24. </view>
  25. <view class="card">
  26. <text class="card-title">过敏原 *</text>
  27. <view class="chip-group">
  28. <view class="chip" v-for="(item, i) in presetAllergies" :key="i"
  29. :class="{ 'chip-active': allergies.indexOf(item) >= 0 }"
  30. @tap="toggleChip('allergies', item)">
  31. <text>{{ item }}</text>
  32. </view>
  33. </view>
  34. <view class="custom-row" v-if="customField === 'allergies'">
  35. <input class="custom-input" placeholder="输入其他过敏原" v-model="customInput"
  36. confirm-type="done" @confirm="addCustom" />
  37. <view class="custom-confirm" @tap="addCustom"><text>添加</text></view>
  38. </view>
  39. <view class="custom-add" v-else @tap="openCustom('allergies')">
  40. <text>+ 自定义添加</text>
  41. </view>
  42. </view>
  43. <view class="card">
  44. <text class="card-title">绝对忌口 *</text>
  45. <view class="chip-group">
  46. <view class="chip" v-for="(item, i) in presetAvoid" :key="i"
  47. :class="{ 'chip-active': absoluteAvoid.indexOf(item) >= 0 }"
  48. @tap="toggleChip('absoluteAvoid', item)">
  49. <text>{{ item }}</text>
  50. </view>
  51. </view>
  52. <view class="custom-row" v-if="customField === 'absoluteAvoid'">
  53. <input class="custom-input" placeholder="输入其他忌口" v-model="customInput"
  54. confirm-type="done" @confirm="addCustom" />
  55. <view class="custom-confirm" @tap="addCustom"><text>添加</text></view>
  56. </view>
  57. <view class="custom-add" v-else @tap="openCustom('absoluteAvoid')">
  58. <text>+ 自定义添加</text>
  59. </view>
  60. </view>
  61. <view class="card">
  62. <text class="card-title">宗教饮食</text>
  63. <view class="chip-group">
  64. <view class="chip" v-for="(opt, i) in religiousOptions" :key="i"
  65. :class="{ 'chip-active': religiousDiet === opt.value }"
  66. @tap="religiousDiet = opt.value">
  67. <text>{{ opt.label }}</text>
  68. </view>
  69. </view>
  70. </view>
  71. </view>
  72. <!-- /Step 1 -->
  73. <!-- ===== Step 2 口味与偏好(4 卡片完整代码) ===== -->
  74. <view v-if="currentStep === 2">
  75. <view class="step-head">
  76. <text class="step-icon">😋</text>
  77. <view class="step-head-text">
  78. <text class="step-title">口味和做法,说说看?</text>
  79. <text class="step-sub">选得越细,推荐食谱越合家人口味</text>
  80. </view>
  81. </view>
  82. <!-- 辣度 -->
  83. <view class="card">
  84. <text class="card-title">辣度偏好</text>
  85. <view class="slider-row">
  86. <text class="slider-label">不辣</text>
  87. <slider class="slider" min="0" max="5" :value="spiceLevel" @change="spiceLevel = $event.detail.value" show-value />
  88. <text class="slider-label">特辣</text>
  89. </view>
  90. <text class="spice-level-label">当前:{{ spiceLevelLabels[spiceLevel] }}</text>
  91. </view>
  92. <!-- 口味 -->
  93. <view class="card">
  94. <text class="card-title">口味偏好</text>
  95. <view class="chip-group">
  96. <view class="chip" v-for="(item, i) in presetFlavor" :key="i"
  97. :class="{ 'chip-active': flavorPref.indexOf(item) >= 0 }"
  98. @tap="toggleChip('flavorPref', item)">
  99. <text>{{ item }}</text>
  100. </view>
  101. </view>
  102. <view class="custom-row" v-if="customField === 'flavorPref'">
  103. <input class="custom-input" placeholder="输入其他口味" v-model="customInput"
  104. confirm-type="done" @confirm="addCustom" />
  105. <view class="custom-confirm" @tap="addCustom"><text>添加</text></view>
  106. </view>
  107. <view class="custom-add" v-else @tap="openCustom('flavorPref')">
  108. <text>+ 自定义添加</text>
  109. </view>
  110. </view>
  111. <!-- 菜系 -->
  112. <view class="card">
  113. <text class="card-title">菜系偏好</text>
  114. <view class="chip-group">
  115. <view class="chip" v-for="(item, i) in presetCuisine" :key="i"
  116. :class="{ 'chip-active': cuisinePref.indexOf(item) >= 0 }"
  117. @tap="toggleChip('cuisinePref', item)">
  118. <text>{{ item }}</text>
  119. </view>
  120. </view>
  121. <view class="custom-row" v-if="customField === 'cuisinePref'">
  122. <input class="custom-input" placeholder="输入其他菜系" v-model="customInput"
  123. confirm-type="done" @confirm="addCustom" />
  124. <view class="custom-confirm" @tap="addCustom"><text>添加</text></view>
  125. </view>
  126. <view class="custom-add" v-else @tap="openCustom('cuisinePref')">
  127. <text>+ 自定义添加</text>
  128. </view>
  129. </view>
  130. <!-- 烹饪方式 -->
  131. <view class="card">
  132. <text class="card-title">烹饪方式</text>
  133. <view class="chip-group">
  134. <view class="chip" v-for="(item, i) in presetCooking" :key="i"
  135. :class="{ 'chip-active': cookingMethods.indexOf(item) >= 0 }"
  136. @tap="toggleChip('cookingMethods', item)">
  137. <text>{{ item }}</text>
  138. </view>
  139. </view>
  140. <view class="custom-row" v-if="customField === 'cookingMethods'">
  141. <input class="custom-input" placeholder="输入其他做法" v-model="customInput"
  142. confirm-type="done" @confirm="addCustom" />
  143. <view class="custom-confirm" @tap="addCustom"><text>添加</text></view>
  144. </view>
  145. <view class="custom-add" v-else @tap="openCustom('cookingMethods')">
  146. <text>+ 自定义添加</text>
  147. </view>
  148. </view>
  149. </view>
  150. <!-- /Step 2 -->
  151. <!-- ===== Step 3 健康目标 ===== -->
  152. <view v-if="currentStep === 3">
  153. <view class="step-head">
  154. <text class="step-icon">🎯</text>
  155. <view class="step-head-text">
  156. <text class="step-title">想优先改善哪方面?</text>
  157. <text class="step-sub">没想好可以跳过,之后随时能改</text>
  158. </view>
  159. </view>
  160. <view class="card">
  161. <text class="card-title">必吃食物</text>
  162. <view class="chip-group">
  163. <view class="chip" v-for="(item, i) in presetMustEat" :key="i"
  164. :class="{ 'chip-active': mustEat.indexOf(item) >= 0 }"
  165. @tap="toggleChip('mustEat', item)">
  166. <text>{{ item }}</text>
  167. </view>
  168. </view>
  169. <view class="custom-row" v-if="customField === 'mustEat'">
  170. <input class="custom-input" placeholder="输入其他必吃" v-model="customInput"
  171. confirm-type="done" @confirm="addCustom" />
  172. <view class="custom-confirm" @tap="addCustom"><text>添加</text></view>
  173. </view>
  174. <view class="custom-add" v-else @tap="openCustom('mustEat')">
  175. <text>+ 自定义添加</text>
  176. </view>
  177. </view>
  178. <view class="card">
  179. <text class="card-title">健康目标</text>
  180. <view class="chip-group">
  181. <view class="chip" v-for="(item, i) in presetGoals" :key="i"
  182. :class="{ 'chip-active': healthGoals.indexOf(item) >= 0 }"
  183. @tap="toggleChip('healthGoals', item)">
  184. <text>{{ item }}</text>
  185. </view>
  186. </view>
  187. <view class="custom-row" v-if="customField === 'healthGoals'">
  188. <input class="custom-input" placeholder="输入其他目标" v-model="customInput"
  189. confirm-type="done" @confirm="addCustom" />
  190. <view class="custom-confirm" @tap="addCustom"><text>添加</text></view>
  191. </view>
  192. <view class="custom-add" v-else @tap="openCustom('healthGoals')">
  193. <text>+ 自定义添加</text>
  194. </view>
  195. </view>
  196. </view>
  197. <!-- /Step 3 -->
  198. </scroll-view>
  199. <!-- 底部按钮栏 -->
  200. <view class="footer-bar">
  201. <view class="footer-btn footer-btn-ghost" v-if="currentStep > 1" @tap="goPrev">
  202. <text>上一步</text>
  203. </view>
  204. <view class="footer-btn footer-btn-primary" @tap="goNext">
  205. <text>{{ submitting ? '提交中...' : (isLastStep ? '完成' : '下一步') }}</text>
  206. </view>
  207. </view>
  208. </view>
  209. </template>
  210. <script>
  211. import { getDietPreferences, saveDietPreferences } from '@/utils/api.js'
  212. // 预设选项(常量,非响应式)
  213. const PRESET_ALLERGIES = ['海鲜', '花生', '乳糖', '鸡蛋', '牛奶', '豆类', '麸质', '坚果', '芒果', '其他']
  214. const PRESET_AVOID = ['牛肉', '羊肉', '猪肉', '内脏', '油腻', '辛辣', '生冷', '腌制']
  215. const PRESET_FLAVOR = ['酸', '甜', '咸', '鲜', '苦', '清淡', '浓郁']
  216. const PRESET_CUISINE = ['川', '粤', '鲁', '淮扬', '日料', '西餐', '东南亚', '韩餐']
  217. const PRESET_COOKING = ['蒸', '煮', '炒', '烤', '炖', '凉拌', '煎', '炸']
  218. const PRESET_MUSTEAT = ['蔬菜', '水果', '粗粮', '鱼类', '鸡蛋', '豆制品', '菌菇']
  219. const PRESET_GOALS = ['减脂', '增肌', '控糖', '肠道调理', '增强免疫']
  220. import fontScaleMixin from '@/mixins/font-scale.js'
  221. export default {
  222. mixins: [fontScaleMixin],
  223. data() {
  224. return {
  225. familyMemberId: null,
  226. currentStep: 1,
  227. // 数据字段(与后端 DTO 对齐)
  228. allergies: [],
  229. absoluteAvoid: [],
  230. religiousDiet: 'none',
  231. spiceLevel: 0,
  232. flavorPref: [],
  233. cuisinePref: [],
  234. cookingMethods: [],
  235. healthGoals: [],
  236. mustEat: [],
  237. goalConfirmed: 0,
  238. // 自定义输入
  239. customInput: '',
  240. customField: null,
  241. // UI 状态
  242. submitting: false,
  243. spiceLevelLabels: ['不辣', '微辣', '中辣', '偏辣', '很辣', '特辣']
  244. }
  245. },
  246. computed: {
  247. // 预设选项映射(供模板 v-for)
  248. presetAllergies: function() { return PRESET_ALLERGIES },
  249. presetAvoid: function() { return PRESET_AVOID },
  250. presetFlavor: function() { return PRESET_FLAVOR },
  251. presetCuisine: function() { return PRESET_CUISINE },
  252. presetCooking: function() { return PRESET_COOKING },
  253. presetMustEat: function() { return PRESET_MUSTEAT },
  254. presetGoals: function() { return PRESET_GOALS },
  255. religiousOptions: function() {
  256. return [
  257. { value: 'none', label: '无限制' },
  258. { value: 'halal', label: '清真' },
  259. { value: 'vegetarian', label: '素食' },
  260. { value: 'vegan', label: '纯素' }
  261. ]
  262. },
  263. // 进度条百分比
  264. progressPercent: function() {
  265. return Math.round((this.currentStep / 3) * 100)
  266. },
  267. isLastStep: function() { return this.currentStep === 3 }
  268. },
  269. onLoad: function() {
  270. this.familyMemberId = uni.getStorageSync('currentChildId') || uni.getStorageSync('currentMemberId') || uni.getStorageSync('userId')
  271. this.loadPreferences()
  272. },
  273. methods: {
  274. loadPreferences: function() {
  275. var self = this
  276. getDietPreferences({ memberId: self.familyMemberId }).then(function(res) {
  277. if (res) {
  278. self.allergies = res.allergies || []
  279. self.absoluteAvoid = res.absoluteAvoid || []
  280. self.religiousDiet = res.religiousDiet || 'none'
  281. self.spiceLevel = res.spiceLevel || 0
  282. self.flavorPref = res.flavorPref || []
  283. self.cuisinePref = res.cuisinePref || []
  284. self.cookingMethods = res.cookingMethods || []
  285. self.healthGoals = res.healthGoals || []
  286. self.mustEat = res.mustEat || []
  287. self.goalConfirmed = res.goalConfirmed || 0
  288. }
  289. }).catch(function() {
  290. })
  291. },
  292. // ——— chip 选中态(模板用内联 indexOf,方法供 toggle 用) ———
  293. toggleChip: function(field, value) {
  294. var idx = this[field].indexOf(value)
  295. if (idx >= 0) {
  296. this[field].splice(idx, 1)
  297. } else {
  298. this[field].push(value)
  299. }
  300. },
  301. // ——— 自定义输入追加(@confirm 触发) ———
  302. openCustom: function(field) {
  303. this.customField = field
  304. this.customInput = ''
  305. },
  306. addCustom: function() {
  307. var value = (this.customInput || '').trim()
  308. if (!value) return
  309. if (this[this.customField].indexOf(value) < 0) {
  310. this[this.customField].push(value)
  311. }
  312. this.customInput = ''
  313. this.customField = null
  314. },
  315. // ——— 步骤导航 ———
  316. goNext: function() {
  317. if (this.currentStep === 1) {
  318. if (this.allergies.length === 0 && this.absoluteAvoid.length === 0) {
  319. uni.showToast({ title: '请至少填写一项过敏原或忌口', icon: 'none' })
  320. return
  321. }
  322. }
  323. if (this.currentStep < 3) {
  324. this.currentStep += 1
  325. } else {
  326. this.savePreferences()
  327. }
  328. },
  329. goPrev: function() {
  330. if (this.currentStep > 1) {
  331. this.currentStep -= 1
  332. }
  333. },
  334. // ——— 提交 ———
  335. savePreferences: function() {
  336. var self = this
  337. this.submitting = true
  338. saveDietPreferences({
  339. allergies: this.allergies,
  340. absoluteAvoid: this.absoluteAvoid,
  341. religiousDiet: this.religiousDiet,
  342. spiceLevel: this.spiceLevel,
  343. flavorPref: this.flavorPref,
  344. cuisinePref: this.cuisinePref,
  345. cookingMethods: this.cookingMethods,
  346. healthGoals: this.healthGoals,
  347. mustEat: this.mustEat,
  348. goalConfirmed: 1
  349. }).then(function(res) {
  350. self.submitting = false
  351. uni.showToast({ title: '保存成功', icon: 'success' })
  352. setTimeout(function() {
  353. uni.navigateBack()
  354. }, 1000)
  355. }).catch(function() {
  356. self.submitting = false
  357. uni.showToast({ title: '保存失败', icon: 'none' })
  358. })
  359. }
  360. }
  361. }
  362. </script>
  363. <style scoped lang="scss">
  364. // ===== 饮食偏好页专属色阶(暖棕/奶油问卷主题;单页专用,约定见 uni.scss §14)=====
  365. $c-dp-track: #FFE3C7; // 进度条底轨
  366. $c-dp-label: #C2A78A; // 进度标签文字
  367. $c-dp-ink: #4A2E1A; // 主文字深棕
  368. $c-dp-muted: #A08B72; // 辅助文字浅棕
  369. $c-dp-chip: #F5F0EA; // 选项胶囊底
  370. $c-dp-chip-ink: #6B5340; // 选项胶囊文字
  371. $c-dp-accent: #E8732A; // 选中态强调橙
  372. $c-dp-line: #FFD9B8; // 输入框描边
  373. $c-dp-input: #FFF9F2; // 输入框底
  374. $c-dp-footer: #FFFDF8; // 底部操作条底
  375. $c-dp-footer-line: #FFE9D4; // 底部操作条描边
  376. .preferences-page {
  377. min-height: 100vh;
  378. background: linear-gradient(180deg, $uni-primary-pale 0%, $warm-cream 100%);
  379. display: flex;
  380. flex-direction: column;
  381. }
  382. /* ===== 进度条 ===== */
  383. .progress-bar { padding: $uni-space-lg 32rpx 10rpx; }
  384. .progress-track {
  385. height: 12rpx; background: $c-dp-track; border-radius: 8rpx; overflow: hidden;
  386. }
  387. .progress-fill {
  388. height: 100%; background: linear-gradient(90deg, $dim-body, $dim-body-light);
  389. border-radius: 8rpx; transition: width 0.3s ease;
  390. }
  391. .progress-labels { display: flex; justify-content: space-between; margin-top: 12rpx; }
  392. .progress-label { font-size: calc(22rpx * var(--font-scale, 1)); color: $c-dp-label; flex: 1; text-align: center; }
  393. .progress-label-active { color: $dim-body; font-weight: 600; }
  394. /* ===== 内容 ===== */
  395. .content { flex: 1; padding: 10rpx 32rpx $uni-space-lg; box-sizing: border-box; }
  396. .step-head { display: flex; align-items: center; margin: 20rpx 0 24rpx; }
  397. .step-icon { font-size: calc($uni-font-size-3xl * var(--font-scale, 1)); margin-right: 20rpx; }
  398. .step-head-text { display: flex; flex-direction: column; }
  399. .step-title { font-size: calc(34rpx * var(--font-scale, 1)); font-weight: 700; color: $c-dp-ink; }
  400. .step-sub { font-size: calc(24rpx * var(--font-scale, 1)); color: $c-dp-muted; margin-top: 6rpx; }
  401. /* ===== 卡片 ===== */
  402. .card {
  403. background: $app-surface; border-radius: 24rpx; padding: 28rpx;
  404. margin-bottom: 24rpx; box-shadow: 0 4rpx 20rpx rgba(255, 140, 66, 0.08);
  405. }
  406. .card-title { font-size: calc(28rpx * var(--font-scale, 1)); font-weight: 600; color: $c-dp-ink; margin-bottom: 20rpx; display: block; }
  407. /* ===== chip ===== */
  408. .chip-group { display: flex; flex-wrap: wrap; }
  409. .chip {
  410. padding: 14rpx 28rpx; border-radius: 999rpx; background: $c-dp-chip;
  411. font-size: calc(26rpx * var(--font-scale, 1)); color: $c-dp-chip-ink; margin: 0 16rpx 16rpx 0;
  412. border: 2rpx solid transparent;
  413. }
  414. .chip-active {
  415. background: $dim-body; color: $app-surface; font-weight: 600;
  416. border-color: $c-dp-accent;
  417. }
  418. /* ===== 自定义添加 ===== */
  419. .custom-add {
  420. padding: 14rpx 0; font-size: calc(26rpx * var(--font-scale, 1)); color: $dim-body; font-weight: 500;
  421. }
  422. .custom-row { display: flex; align-items: center; margin-top: 8rpx; margin-bottom: 12rpx; }
  423. .custom-input {
  424. flex: 1; border: 2rpx solid $c-dp-line; border-radius: 999rpx;
  425. padding: 14rpx 24rpx; font-size: calc(26rpx * var(--font-scale, 1)); background: $c-dp-input;
  426. }
  427. .custom-confirm {
  428. background: $dim-body; color: $app-surface; font-size: calc(26rpx * var(--font-scale, 1)); font-weight: 600;
  429. padding: 14rpx 32rpx; border-radius: 999rpx; margin-left: 16rpx;
  430. }
  431. /* ===== 滑块 ===== */
  432. .slider-row { display: flex; align-items: center; }
  433. .slider-label { font-size: calc(24rpx * var(--font-scale, 1)); color: $c-dp-muted; width: 72rpx; }
  434. .slider { flex: 1; }
  435. .spice-level-label { font-size: calc(24rpx * var(--font-scale, 1)); color: $dim-body; text-align: center; margin-top: 8rpx; display: block; }
  436. /* ===== 底栏 ===== */
  437. .footer-bar {
  438. display: flex; padding: 20rpx 32rpx $uni-space-lg; background: $c-dp-footer;
  439. border-top: 2rpx solid $c-dp-footer-line;
  440. }
  441. .footer-btn {
  442. flex: 1; text-align: center; padding: 24rpx 0; border-radius: 999rpx;
  443. font-size: calc(30rpx * var(--font-scale, 1)); font-weight: 600;
  444. }
  445. .footer-btn-ghost {
  446. background: $c-dp-chip; color: $c-dp-chip-ink; margin-right: 20rpx;
  447. }
  448. .footer-btn-primary {
  449. background: linear-gradient(135deg, $dim-body, $dim-body-light); color: $app-surface;
  450. }
  451. </style>