preferences.vue 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. <template>
  2. <view class="preferences-page">
  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="getChipKey('a', 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="getChipKey('v', 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="getChipKey('r', 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. </view>
  91. <!-- 口味 -->
  92. <view class="card">
  93. <text class="card-title">口味偏好</text>
  94. <view class="chip-group">
  95. <view class="chip" v-for="(item, i) in presetFlavor" :key="getChipKey('f', i)"
  96. :class="{ 'chip-active': flavorPref.indexOf(item) >= 0 }"
  97. @tap="toggleChip('flavorPref', item)">
  98. <text>{{ item }}</text>
  99. </view>
  100. </view>
  101. <view class="custom-row" v-if="customField === 'flavorPref'">
  102. <input class="custom-input" placeholder="输入其他口味" v-model="customInput"
  103. confirm-type="done" @confirm="addCustom" />
  104. <view class="custom-confirm" @tap="addCustom"><text>添加</text></view>
  105. </view>
  106. <view class="custom-add" v-else @tap="openCustom('flavorPref')">
  107. <text>+ 自定义添加</text>
  108. </view>
  109. </view>
  110. <!-- 菜系 -->
  111. <view class="card">
  112. <text class="card-title">菜系偏好</text>
  113. <view class="chip-group">
  114. <view class="chip" v-for="(item, i) in presetCuisine" :key="getChipKey('c', i)"
  115. :class="{ 'chip-active': cuisinePref.indexOf(item) >= 0 }"
  116. @tap="toggleChip('cuisinePref', item)">
  117. <text>{{ item }}</text>
  118. </view>
  119. </view>
  120. <view class="custom-row" v-if="customField === 'cuisinePref'">
  121. <input class="custom-input" placeholder="输入其他菜系" v-model="customInput"
  122. confirm-type="done" @confirm="addCustom" />
  123. <view class="custom-confirm" @tap="addCustom"><text>添加</text></view>
  124. </view>
  125. <view class="custom-add" v-else @tap="openCustom('cuisinePref')">
  126. <text>+ 自定义添加</text>
  127. </view>
  128. </view>
  129. <!-- 烹饪方式 -->
  130. <view class="card">
  131. <text class="card-title">烹饪方式</text>
  132. <view class="chip-group">
  133. <view class="chip" v-for="(item, i) in presetCooking" :key="getChipKey('k', i)"
  134. :class="{ 'chip-active': cookingMethods.indexOf(item) >= 0 }"
  135. @tap="toggleChip('cookingMethods', item)">
  136. <text>{{ item }}</text>
  137. </view>
  138. </view>
  139. <view class="custom-row" v-if="customField === 'cookingMethods'">
  140. <input class="custom-input" placeholder="输入其他做法" v-model="customInput"
  141. confirm-type="done" @confirm="addCustom" />
  142. <view class="custom-confirm" @tap="addCustom"><text>添加</text></view>
  143. </view>
  144. <view class="custom-add" v-else @tap="openCustom('cookingMethods')">
  145. <text>+ 自定义添加</text>
  146. </view>
  147. </view>
  148. </view>
  149. <!-- /Step 2 -->
  150. <!-- ===== Step 3 健康目标 ===== -->
  151. <view v-if="currentStep === 3">
  152. <view class="step-head">
  153. <text class="step-icon">🎯</text>
  154. <view class="step-head-text">
  155. <text class="step-title">想优先改善哪方面?</text>
  156. <text class="step-sub">没想好可以跳过,之后随时能改</text>
  157. </view>
  158. </view>
  159. <view class="card">
  160. <text class="card-title">必吃食物</text>
  161. <view class="chip-group">
  162. <view class="chip" v-for="(item, i) in presetMustEat" :key="getChipKey('m', i)"
  163. :class="{ 'chip-active': mustEat.indexOf(item) >= 0 }"
  164. @tap="toggleChip('mustEat', item)">
  165. <text>{{ item }}</text>
  166. </view>
  167. </view>
  168. <view class="custom-row" v-if="customField === 'mustEat'">
  169. <input class="custom-input" placeholder="输入其他必吃" v-model="customInput"
  170. confirm-type="done" @confirm="addCustom" />
  171. <view class="custom-confirm" @tap="addCustom"><text>添加</text></view>
  172. </view>
  173. <view class="custom-add" v-else @tap="openCustom('mustEat')">
  174. <text>+ 自定义添加</text>
  175. </view>
  176. </view>
  177. <view class="card">
  178. <text class="card-title">健康目标</text>
  179. <view class="chip-group">
  180. <view class="chip" v-for="(item, i) in presetGoals" :key="getChipKey('g', i)"
  181. :class="{ 'chip-active': healthGoals.indexOf(item) >= 0 }"
  182. @tap="toggleChip('healthGoals', item)">
  183. <text>{{ item }}</text>
  184. </view>
  185. </view>
  186. <view class="custom-row" v-if="customField === 'healthGoals'">
  187. <input class="custom-input" placeholder="输入其他目标" v-model="customInput"
  188. confirm-type="done" @confirm="addCustom" />
  189. <view class="custom-confirm" @tap="addCustom"><text>添加</text></view>
  190. </view>
  191. <view class="custom-add" v-else @tap="openCustom('healthGoals')">
  192. <text>+ 自定义添加</text>
  193. </view>
  194. </view>
  195. </view>
  196. <!-- /Step 3 -->
  197. </scroll-view>
  198. <!-- 底部按钮栏 -->
  199. <view class="footer-bar">
  200. <view class="footer-btn footer-btn-ghost" v-if="currentStep > 1" @tap="goPrev">
  201. <text>上一步</text>
  202. </view>
  203. <view class="footer-btn footer-btn-primary" @tap="goNext">
  204. <text>{{ submitting ? '提交中...' : (isLastStep ? '完成' : '下一步') }}</text>
  205. </view>
  206. </view>
  207. </view>
  208. </template>
  209. <script>
  210. import { getDietPreferences, saveDietPreferences } from '@/utils/api.js'
  211. // 预设选项(常量,非响应式)
  212. const PRESET_ALLERGIES = ['海鲜', '花生', '乳糖', '鸡蛋', '牛奶', '豆类', '麸质', '坚果', '芒果', '其他']
  213. const PRESET_AVOID = ['牛肉', '羊肉', '猪肉', '内脏', '油腻', '辛辣', '生冷', '腌制']
  214. const PRESET_FLAVOR = ['酸', '甜', '咸', '鲜', '苦', '清淡', '浓郁']
  215. const PRESET_CUISINE = ['川', '粤', '鲁', '淮扬', '日料', '西餐', '东南亚', '韩餐']
  216. const PRESET_COOKING = ['蒸', '煮', '炒', '烤', '炖', '凉拌', '煎', '炸']
  217. const PRESET_MUSTEAT = ['蔬菜', '水果', '粗粮', '鱼类', '鸡蛋', '豆制品', '菌菇']
  218. const PRESET_GOALS = ['减脂', '增肌', '控糖', '肠道调理', '增强免疫']
  219. export default {
  220. data() {
  221. return {
  222. familyMemberId: null,
  223. currentStep: 1,
  224. // 数据字段(与后端 DTO 对齐)
  225. allergies: [],
  226. absoluteAvoid: [],
  227. religiousDiet: 'none',
  228. spiceLevel: 0,
  229. flavorPref: [],
  230. cuisinePref: [],
  231. cookingMethods: [],
  232. healthGoals: [],
  233. mustEat: [],
  234. goalConfirmed: 0,
  235. // 自定义输入
  236. customInput: '',
  237. customField: null,
  238. // UI 状态
  239. submitting: false,
  240. loading: true
  241. }
  242. },
  243. computed: {
  244. // 预设选项映射(供模板 v-for)
  245. presetAllergies: function() { return PRESET_ALLERGIES },
  246. presetAvoid: function() { return PRESET_AVOID },
  247. presetFlavor: function() { return PRESET_FLAVOR },
  248. presetCuisine: function() { return PRESET_CUISINE },
  249. presetCooking: function() { return PRESET_COOKING },
  250. presetMustEat: function() { return PRESET_MUSTEAT },
  251. presetGoals: function() { return PRESET_GOALS },
  252. religiousOptions: function() {
  253. return [
  254. { value: 'none', label: '无限制' },
  255. { value: 'halal', label: '清真' },
  256. { value: 'vegetarian', label: '素食' },
  257. { value: 'vegan', label: '纯素' }
  258. ]
  259. },
  260. // 进度条百分比
  261. progressPercent: function() {
  262. var total = 3
  263. var done = this.currentStep - 1
  264. if (this.currentStep === 3) done = 3
  265. return Math.round((done / total) * 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. self.loading = false
  278. if (res) {
  279. self.allergies = res.allergies || []
  280. self.absoluteAvoid = res.absoluteAvoid || []
  281. self.religiousDiet = res.religiousDiet || 'none'
  282. self.spiceLevel = res.spiceLevel || 0
  283. self.flavorPref = res.flavorPref || []
  284. self.cuisinePref = res.cuisinePref || []
  285. self.cookingMethods = res.cookingMethods || []
  286. self.healthGoals = res.healthGoals || []
  287. self.mustEat = res.mustEat || []
  288. self.goalConfirmed = res.goalConfirmed || 0
  289. }
  290. }).catch(function() {
  291. self.loading = false
  292. })
  293. },
  294. // ——— chip 选中态(模板用内联 indexOf,方法供 toggle 用) ———
  295. getChipKey: function(prefix, index) {
  296. return prefix + '-' + index
  297. },
  298. toggleChip: function(field, value) {
  299. var idx = this[field].indexOf(value)
  300. if (idx >= 0) {
  301. this[field].splice(idx, 1)
  302. } else {
  303. this[field].push(value)
  304. }
  305. },
  306. // ——— 自定义输入追加(@confirm 触发) ———
  307. openCustom: function(field) {
  308. this.customField = field
  309. this.customInput = ''
  310. },
  311. addCustom: function() {
  312. var value = (this.customInput || '').trim()
  313. if (!value) return
  314. if (this[this.customField].indexOf(value) < 0) {
  315. this[this.customField].push(value)
  316. }
  317. this.customInput = ''
  318. this.customField = null
  319. },
  320. // ——— 步骤导航 ———
  321. goNext: function() {
  322. if (this.currentStep === 1) {
  323. if (this.allergies.length === 0 && this.absoluteAvoid.length === 0) {
  324. uni.showToast({ title: '请至少填写一项过敏原或忌口', icon: 'none' })
  325. return
  326. }
  327. }
  328. if (this.currentStep < 3) {
  329. this.currentStep += 1
  330. } else {
  331. this.savePreferences()
  332. }
  333. },
  334. goPrev: function() {
  335. if (this.currentStep > 1) {
  336. this.currentStep -= 1
  337. }
  338. },
  339. // ——— 提交 ———
  340. savePreferences: function() {
  341. var self = this
  342. this.submitting = true
  343. saveDietPreferences({
  344. allergies: this.allergies,
  345. absoluteAvoid: this.absoluteAvoid,
  346. religiousDiet: this.religiousDiet,
  347. spiceLevel: this.spiceLevel,
  348. flavorPref: this.flavorPref,
  349. cuisinePref: this.cuisinePref,
  350. cookingMethods: this.cookingMethods,
  351. healthGoals: this.healthGoals,
  352. mustEat: this.mustEat,
  353. goalConfirmed: 1
  354. }).then(function(res) {
  355. self.submitting = false
  356. uni.showToast({ title: '保存成功', icon: 'success' })
  357. setTimeout(function() {
  358. uni.navigateBack()
  359. }, 1000)
  360. }).catch(function() {
  361. self.submitting = false
  362. uni.showToast({ title: '保存失败', icon: 'none' })
  363. })
  364. }
  365. }
  366. }
  367. </script>
  368. <style scoped>
  369. .preferences-page {
  370. min-height: 100vh;
  371. background: linear-gradient(180deg, #FFF7ED 0%, #FFF3E6 100%);
  372. display: flex;
  373. flex-direction: column;
  374. }
  375. /* ===== 进度条 ===== */
  376. .progress-bar { padding: 30rpx 32rpx 10rpx; }
  377. .progress-track {
  378. height: 12rpx; background: #FFE3C7; border-radius: 6rpx; overflow: hidden;
  379. }
  380. .progress-fill {
  381. height: 100%; background: linear-gradient(90deg, #FF8C42, #FFB366);
  382. border-radius: 6rpx; transition: width 0.3s ease;
  383. }
  384. .progress-labels { display: flex; justify-content: space-between; margin-top: 12rpx; }
  385. .progress-label { font-size: 22rpx; color: #C2A78A; }
  386. .progress-label-active { color: #FF8C42; font-weight: 600; }
  387. /* ===== 内容 ===== */
  388. .content { flex: 1; padding: 10rpx 32rpx 30rpx; box-sizing: border-box; }
  389. .step-head { display: flex; align-items: center; margin: 20rpx 0 24rpx; }
  390. .step-icon { font-size: 52rpx; margin-right: 20rpx; }
  391. .step-head-text { display: flex; flex-direction: column; }
  392. .step-title { font-size: 34rpx; font-weight: 700; color: #4A2E1A; }
  393. .step-sub { font-size: 24rpx; color: #A08B72; margin-top: 6rpx; }
  394. /* ===== 卡片 ===== */
  395. .card {
  396. background: #FFFFFF; border-radius: 24rpx; padding: 28rpx;
  397. margin-bottom: 24rpx; box-shadow: 0 4rpx 20rpx rgba(255, 140, 66, 0.08);
  398. }
  399. .card-title { font-size: 28rpx; font-weight: 600; color: #4A2E1A; margin-bottom: 20rpx; display: block; }
  400. /* ===== chip ===== */
  401. .chip-group { display: flex; flex-wrap: wrap; }
  402. .chip {
  403. padding: 14rpx 28rpx; border-radius: 999rpx; background: #F5F0EA;
  404. font-size: 26rpx; color: #6B5340; margin: 0 16rpx 16rpx 0;
  405. border: 2rpx solid transparent;
  406. }
  407. .chip-active {
  408. background: #FF8C42; color: #FFFFFF; font-weight: 600;
  409. border-color: #E8732A;
  410. }
  411. /* ===== 自定义添加 ===== */
  412. .custom-add {
  413. padding: 14rpx 0; font-size: 26rpx; color: #FF8C42; font-weight: 500;
  414. }
  415. .custom-row { display: flex; align-items: center; margin-top: 8rpx; }
  416. .custom-input {
  417. flex: 1; border: 2rpx solid #FFD9B8; border-radius: 999rpx;
  418. padding: 14rpx 24rpx; font-size: 26rpx; background: #FFF9F2;
  419. }
  420. .custom-confirm {
  421. background: #FF8C42; color: #fff; font-size: 26rpx; font-weight: 600;
  422. padding: 14rpx 32rpx; border-radius: 999rpx; margin-left: 16rpx;
  423. }
  424. /* ===== 滑块 ===== */
  425. .slider-row { display: flex; align-items: center; }
  426. .slider-label { font-size: 24rpx; color: #A08B72; width: 72rpx; }
  427. .slider { flex: 1; }
  428. /* ===== 底栏 ===== */
  429. .footer-bar {
  430. display: flex; padding: 20rpx 32rpx 30rpx; background: #FFFDF8;
  431. border-top: 2rpx solid #FFE9D4;
  432. }
  433. .footer-btn {
  434. flex: 1; text-align: center; padding: 24rpx 0; border-radius: 999rpx;
  435. font-size: 30rpx; font-weight: 600;
  436. }
  437. .footer-btn-ghost {
  438. background: #F5F0EA; color: #6B5340; margin-right: 20rpx;
  439. }
  440. .footer-btn-primary {
  441. background: linear-gradient(135deg, #FF8C42, #FFB366); color: #FFFFFF;
  442. }
  443. </style>