preferences.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. <template>
  2. <view class="preferences-page">
  3. <view class="nav-bar">
  4. <view class="nav-back" @tap="goBack">
  5. <text class="back-text">‹ 返回</text>
  6. </view>
  7. <text class="nav-title">饮食偏好调研</text>
  8. <view class="nav-placeholder"></view>
  9. </view>
  10. <scroll-view class="content" scroll-y>
  11. <!-- 硬性要求 -->
  12. <view class="card">
  13. <view class="card-accent"></view>
  14. <view class="card-body">
  15. <text class="card-title">硬性要求(必填)</text>
  16. <view class="form-group">
  17. <text class="form-label">过敏原 *</text>
  18. <view class="tag-group">
  19. <view class="tag-btn" v-for="(tag, index) in allergies" :key="index" @tap="removeTag('allergies', index)">
  20. <text>{{ tag }}</text>
  21. <text class="tag-remove">×</text>
  22. </view>
  23. </view>
  24. <view class="tag-input-row">
  25. <input class="tag-input" placeholder="输入后回车添加" v-model="tagInput" @confirm="addTag('allergies')" />
  26. </view>
  27. </view>
  28. <view class="form-group">
  29. <text class="form-label">绝对忌口 *</text>
  30. <view class="tag-group">
  31. <view class="tag-btn" v-for="(tag, index) in absoluteAvoid" :key="index" @tap="removeTag('absoluteAvoid', index)">
  32. <text>{{ tag }}</text>
  33. <text class="tag-remove">×</text>
  34. </view>
  35. </view>
  36. <view class="tag-input-row">
  37. <input class="tag-input" placeholder="输入后回车添加" v-model="tagInput" @confirm="addTag('absoluteAvoid')" />
  38. </view>
  39. </view>
  40. <view class="form-group">
  41. <text class="form-label">宗教饮食</text>
  42. <view class="radio-group">
  43. <view class="radio-item" :class="{'radio-active': religiousDiet === 'none'}" @tap="religiousDiet = 'none'">
  44. <text>无限制</text>
  45. </view>
  46. <view class="radio-item" :class="{'radio-active': religiousDiet === 'halal'}" @tap="religiousDiet = 'halal'">
  47. <text>清真</text>
  48. </view>
  49. <view class="radio-item" :class="{'radio-active': religiousDiet === 'vegetarian'}" @tap="religiousDiet = 'vegetarian'">
  50. <text>素食</text>
  51. </view>
  52. <view class="radio-item" :class="{'radio-active': religiousDiet === 'vegan'}" @tap="religiousDiet = 'vegan'">
  53. <text>纯素</text>
  54. </view>
  55. </view>
  56. </view>
  57. </view>
  58. </view>
  59. <!-- 软性偏好 -->
  60. <view class="card">
  61. <view class="card-accent"></view>
  62. <view class="card-body">
  63. <text class="card-title">软性偏好(可选)</text>
  64. <view class="form-group">
  65. <text class="form-label">辣度偏好</text>
  66. <view class="slider-row">
  67. <text class="slider-label">不辣</text>
  68. <slider class="slider" min="0" max="5" :value="spiceLevel" @change="spiceLevel = $event.detail.value" show-value />
  69. <text class="slider-label">特辣</text>
  70. </view>
  71. </view>
  72. <view class="form-group">
  73. <text class="form-label">口味偏好</text>
  74. <view class="tag-group">
  75. <view class="tag-btn" v-for="(tag, index) in flavorPref" :key="index" @tap="removeTag('flavorPref', index)">
  76. <text>{{ tag }}</text>
  77. <text class="tag-remove">×</text>
  78. </view>
  79. </view>
  80. <view class="tag-input-row">
  81. <input class="tag-input" placeholder="酸/甜/咸/鲜/苦" v-model="tagInput" @confirm="addTag('flavorPref')" />
  82. </view>
  83. </view>
  84. <view class="form-group">
  85. <text class="form-label">菜系偏好</text>
  86. <view class="tag-group">
  87. <view class="tag-btn" v-for="(tag, index) in cuisinePref" :key="index" @tap="removeTag('cuisinePref', index)">
  88. <text>{{ tag }}</text>
  89. <text class="tag-remove">×</text>
  90. </view>
  91. </view>
  92. <view class="tag-input-row">
  93. <input class="tag-input" placeholder="川/粤/淮扬/日料/西餐" v-model="tagInput" @confirm="addTag('cuisinePref')" />
  94. </view>
  95. </view>
  96. </view>
  97. </view>
  98. <!-- 健康目标 -->
  99. <view class="card">
  100. <view class="card-accent"></view>
  101. <view class="card-body">
  102. <text class="card-title">健康目标</text>
  103. <text class="hint-text" v-if="systemGoals.length > 0">系统推荐目标(基于健康报告):</text>
  104. <view class="tag-group" v-if="systemGoals.length > 0">
  105. <view class="tag-btn system-tag" v-for="(goal, index) in systemGoals" :key="index">
  106. <text>{{ goal }}</text>
  107. </view>
  108. </view>
  109. <view class="form-group" style="margin-top: 20rpx;">
  110. <text class="form-label">自定义目标</text>
  111. <view class="tag-group">
  112. <view class="tag-btn" v-for="(tag, index) in healthGoals" :key="index" @tap="removeTag('healthGoals', index)">
  113. <text>{{ tag }}</text>
  114. <text class="tag-remove">×</text>
  115. </view>
  116. </view>
  117. <view class="tag-input-row">
  118. <input class="tag-input" placeholder="减脂/增肌/控糖/肠道调理" v-model="tagInput" @confirm="addTag('healthGoals')" />
  119. </view>
  120. </view>
  121. <view class="confirm-btn" @tap="confirmGoals">
  122. <text>确认目标</text>
  123. </view>
  124. </view>
  125. </view>
  126. <view class="submit-area">
  127. <view class="submit-btn" @tap="savePreferences">
  128. <text>{{ submitting ? '提交中...' : '保存偏好' }}</text>
  129. </view>
  130. </view>
  131. </scroll-view>
  132. </view>
  133. </template>
  134. <script>
  135. import { getDietPreferences, saveDietPreferences } from '@/utils/api.js'
  136. export default {
  137. data() {
  138. return {
  139. familyMemberId: null,
  140. allergies: [],
  141. absoluteAvoid: [],
  142. religiousDiet: 'none',
  143. spiceLevel: 0,
  144. flavorPref: [],
  145. cuisinePref: [],
  146. healthGoals: [],
  147. systemGoals: [],
  148. tagInput: '',
  149. submitting: false
  150. }
  151. },
  152. onLoad() {
  153. this.familyMemberId = uni.getStorageSync('currentMemberId')
  154. this.loadPreferences()
  155. },
  156. methods: {
  157. goBack() {
  158. uni.navigateBack()
  159. },
  160. loadPreferences() {
  161. var self = this
  162. getDietPreferences().then(function(res) {
  163. if (res) {
  164. self.allergies = res.allergies || []
  165. self.absoluteAvoid = res.absoluteAvoid || []
  166. self.religiousDiet = res.religiousDiet || 'none'
  167. self.spiceLevel = res.spiceLevel || 0
  168. self.flavorPref = res.flavorPref || []
  169. self.cuisinePref = res.cuisinePref || []
  170. self.healthGoals = res.healthGoals || []
  171. self.systemGoals = res.systemGoals || []
  172. }
  173. })
  174. },
  175. addTag(field) {
  176. var value = this.tagInput.trim()
  177. if (value) {
  178. this[field].push(value)
  179. this.tagInput = ''
  180. }
  181. },
  182. removeTag(field, index) {
  183. this[field].splice(index, 1)
  184. },
  185. confirmGoals() {
  186. this.savePreferences()
  187. },
  188. savePreferences() {
  189. var self = this
  190. if (this.allergies.length === 0 && this.absoluteAvoid.length === 0) {
  191. uni.showToast({ title: '请填写过敏原或忌口', icon: 'none' })
  192. return
  193. }
  194. this.submitting = true
  195. saveDietPreferences({
  196. allergies: this.allergies,
  197. absoluteAvoid: this.absoluteAvoid,
  198. religiousDiet: this.religiousDiet,
  199. spiceLevel: this.spiceLevel,
  200. flavorPref: this.flavorPref,
  201. cuisinePref: this.cuisinePref,
  202. healthGoals: this.healthGoals.concat(this.systemGoals),
  203. goalConfirmed: 1
  204. }).then(function(res) {
  205. self.submitting = false
  206. uni.showToast({ title: '保存成功', icon: 'success' })
  207. setTimeout(function() {
  208. uni.navigateBack()
  209. }, 1000)
  210. }).catch(function() {
  211. self.submitting = false
  212. uni.showToast({ title: '保存失败', icon: 'none' })
  213. })
  214. }
  215. }
  216. }
  217. </script>
  218. <style scoped>
  219. .preferences-page {
  220. min-height: 100vh;
  221. background-color: #F5F7FA;
  222. display: flex;
  223. flex-direction: column;
  224. }
  225. .nav-bar {
  226. display: flex;
  227. align-items: center;
  228. justify-content: center;
  229. height: 88rpx;
  230. padding-top: env(safe-area-inset-top);
  231. background-color: #FFFFFF;
  232. position: relative;
  233. border-bottom: 1rpx solid #f0f0f0;
  234. }
  235. .nav-back {
  236. position: absolute;
  237. left: 30rpx;
  238. top: 50%;
  239. transform: translateY(-50%);
  240. padding: 10rpx;
  241. }
  242. .back-text {
  243. font-size: 28rpx;
  244. color: #333;
  245. }
  246. .nav-title {
  247. font-size: 32rpx;
  248. font-weight: 600;
  249. color: #333;
  250. }
  251. .nav-placeholder {
  252. width: 80rpx;
  253. }
  254. .content {
  255. flex: 1;
  256. padding: 30rpx;
  257. }
  258. .card {
  259. display: flex;
  260. background-color: #FFFFFF;
  261. border-radius: 20rpx;
  262. margin-bottom: 30rpx;
  263. box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.06);
  264. overflow: hidden;
  265. }
  266. .card-accent {
  267. width: 8rpx;
  268. background-color: #FF8C42;
  269. flex-shrink: 0;
  270. }
  271. .card-body {
  272. flex: 1;
  273. padding: 30rpx;
  274. }
  275. .card-title {
  276. font-size: 30rpx;
  277. font-weight: 600;
  278. color: #333;
  279. margin-bottom: 24rpx;
  280. display: block;
  281. }
  282. .form-group {
  283. margin-bottom: 24rpx;
  284. }
  285. .form-label {
  286. font-size: 26rpx;
  287. color: #333;
  288. font-weight: 500;
  289. margin-bottom: 12rpx;
  290. display: block;
  291. }
  292. .hint-text {
  293. font-size: 24rpx;
  294. color: #999;
  295. margin-bottom: 12rpx;
  296. display: block;
  297. }
  298. .tag-group {
  299. display: flex;
  300. flex-wrap: wrap;
  301. gap: 16rpx;
  302. margin-bottom: 12rpx;
  303. }
  304. .tag-btn {
  305. padding: 12rpx 20rpx;
  306. border: 1rpx solid #ddd;
  307. border-radius: 30rpx;
  308. font-size: 24rpx;
  309. color: #666;
  310. background-color: #FAFAFA;
  311. display: flex;
  312. align-items: center;
  313. gap: 8rpx;
  314. }
  315. .tag-btn.system-tag {
  316. background-color: #FFF5EB;
  317. border-color: #FF8C42;
  318. color: #FF8C42;
  319. }
  320. .tag-remove {
  321. font-size: 28rpx;
  322. color: #999;
  323. line-height: 1;
  324. }
  325. .tag-input-row {
  326. margin-top: 12rpx;
  327. }
  328. .tag-input {
  329. border: 1rpx solid #ddd;
  330. border-radius: 30rpx;
  331. padding: 12rpx 20rpx;
  332. font-size: 26rpx;
  333. background-color: #FAFAFA;
  334. }
  335. .radio-group {
  336. display: flex;
  337. flex-wrap: wrap;
  338. gap: 16rpx;
  339. }
  340. .radio-item {
  341. padding: 12rpx 24rpx;
  342. border: 1rpx solid #ddd;
  343. border-radius: 30rpx;
  344. font-size: 24rpx;
  345. color: #666;
  346. background-color: #FAFAFA;
  347. }
  348. .radio-active {
  349. border-color: #FF8C42;
  350. color: #FF8C42;
  351. background-color: #FFF5EB;
  352. }
  353. .slider-row {
  354. display: flex;
  355. align-items: center;
  356. gap: 20rpx;
  357. }
  358. .slider-label {
  359. font-size: 24rpx;
  360. color: #666;
  361. width: 60rpx;
  362. }
  363. .slider {
  364. flex: 1;
  365. }
  366. .confirm-btn {
  367. background-color: #E8F5E9;
  368. color: #4CAF50;
  369. text-align: center;
  370. padding: 20rpx 0;
  371. border-radius: 12rpx;
  372. font-size: 26rpx;
  373. margin-top: 20rpx;
  374. }
  375. .submit-area {
  376. padding: 40rpx 30rpx;
  377. }
  378. .submit-btn {
  379. background-color: #FF8C42;
  380. color: #FFFFFF;
  381. text-align: center;
  382. padding: 28rpx 0;
  383. border-radius: 16rpx;
  384. font-size: 30rpx;
  385. font-weight: 600;
  386. }
  387. </style>