preferences.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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、Step 3 在此追加(任务 2) -->
  74. </scroll-view>
  75. <!-- 底部按钮栏 -->
  76. <view class="footer-bar">
  77. <view class="footer-btn footer-btn-ghost" v-if="currentStep > 1" @tap="goPrev">
  78. <text>上一步</text>
  79. </view>
  80. <view class="footer-btn footer-btn-primary" @tap="goNext">
  81. <text>{{ submitting ? '提交中...' : (isLastStep ? '完成' : '下一步') }}</text>
  82. </view>
  83. </view>
  84. </view>
  85. </template>
  86. <script>
  87. import { getDietPreferences, saveDietPreferences } from '@/utils/api.js'
  88. // 预设选项(常量,非响应式)
  89. const PRESET_ALLERGIES = ['海鲜', '花生', '乳糖', '鸡蛋', '牛奶', '豆类', '麸质', '坚果', '芒果', '其他']
  90. const PRESET_AVOID = ['牛肉', '羊肉', '猪肉', '内脏', '油腻', '辛辣', '生冷', '腌制']
  91. const PRESET_FLAVOR = ['酸', '甜', '咸', '鲜', '苦', '清淡', '浓郁']
  92. const PRESET_CUISINE = ['川', '粤', '鲁', '淮扬', '日料', '西餐', '东南亚', '韩餐']
  93. const PRESET_COOKING = ['蒸', '煮', '炒', '烤', '炖', '凉拌', '煎', '炸']
  94. const PRESET_MUSTEAT = ['蔬菜', '水果', '粗粮', '鱼类', '鸡蛋', '豆制品', '菌菇']
  95. const PRESET_GOALS = ['减脂', '增肌', '控糖', '肠道调理', '增强免疫']
  96. export default {
  97. data() {
  98. return {
  99. familyMemberId: null,
  100. currentStep: 1,
  101. // 数据字段(与后端 DTO 对齐)
  102. allergies: [],
  103. absoluteAvoid: [],
  104. religiousDiet: 'none',
  105. spiceLevel: 0,
  106. flavorPref: [],
  107. cuisinePref: [],
  108. cookingMethods: [],
  109. healthGoals: [],
  110. mustEat: [],
  111. goalConfirmed: 0,
  112. // 自定义输入
  113. customInput: '',
  114. customField: null,
  115. // UI 状态
  116. submitting: false,
  117. loading: true
  118. }
  119. },
  120. computed: {
  121. // 预设选项映射(供模板 v-for)
  122. presetAllergies: function() { return PRESET_ALLERGIES },
  123. presetAvoid: function() { return PRESET_AVOID },
  124. presetFlavor: function() { return PRESET_FLAVOR },
  125. presetCuisine: function() { return PRESET_CUISINE },
  126. presetCooking: function() { return PRESET_COOKING },
  127. presetMustEat: function() { return PRESET_MUSTEAT },
  128. presetGoals: function() { return PRESET_GOALS },
  129. religiousOptions: function() {
  130. return [
  131. { value: 'none', label: '无限制' },
  132. { value: 'halal', label: '清真' },
  133. { value: 'vegetarian', label: '素食' },
  134. { value: 'vegan', label: '纯素' }
  135. ]
  136. },
  137. // 进度条百分比
  138. progressPercent: function() {
  139. var total = 3
  140. var done = this.currentStep - 1
  141. if (this.currentStep === 3) done = 3
  142. return Math.round((done / total) * 100)
  143. },
  144. isLastStep: function() { return this.currentStep === 3 }
  145. },
  146. onLoad: function() {
  147. this.familyMemberId = uni.getStorageSync('currentChildId') || uni.getStorageSync('currentMemberId') || uni.getStorageSync('userId')
  148. this.loadPreferences()
  149. },
  150. methods: {
  151. loadPreferences: function() {
  152. var self = this
  153. getDietPreferences({ memberId: self.familyMemberId }).then(function(res) {
  154. self.loading = false
  155. if (res) {
  156. self.allergies = res.allergies || []
  157. self.absoluteAvoid = res.absoluteAvoid || []
  158. self.religiousDiet = res.religiousDiet || 'none'
  159. self.spiceLevel = res.spiceLevel || 0
  160. self.flavorPref = res.flavorPref || []
  161. self.cuisinePref = res.cuisinePref || []
  162. self.cookingMethods = res.cookingMethods || []
  163. self.healthGoals = res.healthGoals || []
  164. self.mustEat = res.mustEat || []
  165. self.goalConfirmed = res.goalConfirmed || 0
  166. }
  167. }).catch(function() {
  168. self.loading = false
  169. })
  170. },
  171. // ——— chip 选中态(模板用内联 indexOf,方法供 toggle 用) ———
  172. getChipKey: function(prefix, index) {
  173. return prefix + '-' + index
  174. },
  175. toggleChip: function(field, value) {
  176. var idx = this[field].indexOf(value)
  177. if (idx >= 0) {
  178. this[field].splice(idx, 1)
  179. } else {
  180. this[field].push(value)
  181. }
  182. },
  183. // ——— 自定义输入追加(@confirm 触发) ———
  184. openCustom: function(field) {
  185. this.customField = field
  186. this.customInput = ''
  187. },
  188. addCustom: function() {
  189. var value = (this.customInput || '').trim()
  190. if (!value) return
  191. if (this[this.customField].indexOf(value) < 0) {
  192. this[this.customField].push(value)
  193. }
  194. this.customInput = ''
  195. this.customField = null
  196. },
  197. // ——— 步骤导航 ———
  198. goNext: function() {
  199. if (this.currentStep === 1) {
  200. if (this.allergies.length === 0 && this.absoluteAvoid.length === 0) {
  201. uni.showToast({ title: '请至少填写一项过敏原或忌口', icon: 'none' })
  202. return
  203. }
  204. }
  205. if (this.currentStep < 3) {
  206. this.currentStep += 1
  207. } else {
  208. this.savePreferences()
  209. }
  210. },
  211. goPrev: function() {
  212. if (this.currentStep > 1) {
  213. this.currentStep -= 1
  214. }
  215. },
  216. // ——— 提交 ———
  217. savePreferences: function() {
  218. var self = this
  219. this.submitting = true
  220. saveDietPreferences({
  221. allergies: this.allergies,
  222. absoluteAvoid: this.absoluteAvoid,
  223. religiousDiet: this.religiousDiet,
  224. spiceLevel: this.spiceLevel,
  225. flavorPref: this.flavorPref,
  226. cuisinePref: this.cuisinePref,
  227. cookingMethods: this.cookingMethods,
  228. healthGoals: this.healthGoals,
  229. mustEat: this.mustEat,
  230. goalConfirmed: 1
  231. }).then(function(res) {
  232. self.submitting = false
  233. uni.showToast({ title: '保存成功', icon: 'success' })
  234. setTimeout(function() {
  235. uni.navigateBack()
  236. }, 1000)
  237. }).catch(function() {
  238. self.submitting = false
  239. uni.showToast({ title: '保存失败', icon: 'none' })
  240. })
  241. }
  242. }
  243. }
  244. </script>
  245. <style scoped>
  246. .preferences-page {
  247. min-height: 100vh;
  248. background-color: #FFF7ED;
  249. display: flex;
  250. flex-direction: column;
  251. }
  252. /* 进度条 */
  253. .progress-bar {
  254. padding: 24rpx 30rpx 0;
  255. background-color: #FFF7ED;
  256. }
  257. .progress-track {
  258. height: 12rpx;
  259. background-color: #E8E8E8;
  260. border-radius: 6rpx;
  261. overflow: hidden;
  262. }
  263. .progress-fill {
  264. height: 100%;
  265. background-color: #F97316;
  266. border-radius: 6rpx;
  267. transition: width 0.3s ease;
  268. }
  269. .progress-labels {
  270. display: flex;
  271. justify-content: space-between;
  272. margin-top: 16rpx;
  273. padding-bottom: 20rpx;
  274. }
  275. .progress-label {
  276. font-size: 22rpx;
  277. color: #BBB;
  278. flex: 1;
  279. text-align: center;
  280. }
  281. .progress-label-active {
  282. color: #F97316;
  283. font-weight: 600;
  284. }
  285. /* 内容区 */
  286. .content {
  287. flex: 1;
  288. padding: 20rpx 30rpx 120rpx;
  289. }
  290. /* 步骤标题 */
  291. .step-head {
  292. display: flex;
  293. align-items: flex-start;
  294. margin-bottom: 24rpx;
  295. }
  296. .step-icon {
  297. font-size: 48rpx;
  298. margin-right: 16rpx;
  299. flex-shrink: 0;
  300. }
  301. .step-head-text {
  302. flex: 1;
  303. }
  304. .step-title {
  305. font-size: 34rpx;
  306. font-weight: 700;
  307. color: #1A1A1A;
  308. display: block;
  309. margin-bottom: 8rpx;
  310. }
  311. .step-sub {
  312. font-size: 24rpx;
  313. color: #999;
  314. display: block;
  315. }
  316. /* 卡片 */
  317. .card {
  318. background-color: #FFFFFF;
  319. border-radius: 20rpx;
  320. padding: 28rpx 24rpx;
  321. margin-bottom: 24rpx;
  322. box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.05);
  323. }
  324. .card-title {
  325. font-size: 28rpx;
  326. font-weight: 600;
  327. color: #333;
  328. margin-bottom: 20rpx;
  329. display: block;
  330. }
  331. /* chips */
  332. .chip-group {
  333. display: flex;
  334. flex-wrap: wrap;
  335. gap: 16rpx;
  336. margin-bottom: 16rpx;
  337. }
  338. .chip {
  339. padding: 12rpx 24rpx;
  340. border: 1rpx solid #E0E0E0;
  341. border-radius: 30rpx;
  342. font-size: 24rpx;
  343. color: #666;
  344. background-color: #FAFAFA;
  345. }
  346. .chip-active {
  347. border-color: #F97316;
  348. color: #F97316;
  349. background-color: #FFF5EB;
  350. }
  351. /* 自定义输入 */
  352. .custom-row {
  353. display: flex;
  354. align-items: center;
  355. gap: 12rpx;
  356. margin-bottom: 12rpx;
  357. }
  358. .custom-input {
  359. flex: 1;
  360. height: 64rpx;
  361. padding: 0 24rpx;
  362. border: 1rpx solid #E0E0E0;
  363. border-radius: 30rpx;
  364. font-size: 26rpx;
  365. background-color: #FAFAFA;
  366. }
  367. .custom-confirm {
  368. padding: 12rpx 28rpx;
  369. background-color: #F97316;
  370. border-radius: 30rpx;
  371. font-size: 26rpx;
  372. color: #fff;
  373. }
  374. .custom-add {
  375. font-size: 24rpx;
  376. color: #F97316;
  377. padding: 8rpx 0;
  378. }
  379. /* 底栏 */
  380. .footer-bar {
  381. position: fixed;
  382. bottom: 0;
  383. left: 0;
  384. right: 0;
  385. display: flex;
  386. padding: 20rpx 30rpx calc(20rpx + env(safe-area-inset-bottom));
  387. background-color: #FFFFFF;
  388. box-shadow: 0 -2rpx 12rpx rgba(0, 0, 0, 0.06);
  389. gap: 20rpx;
  390. }
  391. .footer-btn {
  392. flex: 1;
  393. text-align: center;
  394. padding: 24rpx 0;
  395. border-radius: 16rpx;
  396. font-size: 30rpx;
  397. font-weight: 600;
  398. }
  399. .footer-btn-ghost {
  400. background-color: #F5F5F5;
  401. color: #999;
  402. }
  403. .footer-btn-primary {
  404. background-color: #F97316;
  405. color: #FFFFFF;
  406. }
  407. </style>