preferences.vue 10 KB

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