report-survey.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. <template>
  2. <view class="survey-container">
  3. <!-- 导航 -->
  4. <view class="nav-bar">
  5. <text class="nav-back" @click="goBack">‹ 返回</text>
  6. <text class="nav-title">健康调研问卷</text>
  7. <view class="nav-placeholder"></view>
  8. </view>
  9. <!-- 进度条 -->
  10. <view class="progress-bar">
  11. <view class="progress-fill" :style="{ width: progressPercent + '%' }"></view>
  12. </view>
  13. <!-- 提示说明 -->
  14. <view class="tip-banner">
  15. <text class="tip-icon">💡</text>
  16. <text class="tip-text">请根据孩子最近的实际情况填写,帮助AI更准确地解读报告并提供个性化方案</text>
  17. </view>
  18. <!-- 表单 -->
  19. <view class="form-section">
  20. <!-- Q1: 饮食情况 -->
  21. <view class="question-card">
  22. <text class="q-number">1/6</text>
  23. <text class="q-title">孩子平时的饮食结构是怎样的?</text>
  24. <view class="q-options">
  25. <view class="q-option" :class="{ 'q-selected': survey.diet === 'balanced' }" @click="survey.diet = 'balanced'">
  26. <text class="q-icon">🥗</text>
  27. <text class="q-text">均衡饮食,荤素搭配</text>
  28. </view>
  29. <view class="q-option" :class="{ 'q-selected': survey.diet === 'meat' }" @click="survey.diet = 'meat'">
  30. <text class="q-icon">🥩</text>
  31. <text class="q-text">偏荤食,爱吃肉</text>
  32. </view>
  33. <view class="q-option" :class="{ 'q-selected': survey.diet === 'veggie' }" @click="survey.diet = 'veggie'">
  34. <text class="q-icon">🥬</text>
  35. <text class="q-text">偏素食,不爱吃肉</text>
  36. </view>
  37. <view class="q-option" :class="{ 'q-selected': survey.diet === 'junk' }" @click="survey.diet = 'junk'">
  38. <text class="q-icon">🍟</text>
  39. <text class="q-text">偏好零食/快餐</text>
  40. </view>
  41. </view>
  42. </view>
  43. <!-- Q2: 消化状况 -->
  44. <view class="question-card">
  45. <text class="q-number">2/6</text>
  46. <text class="q-title">孩子近期消化状况如何?</text>
  47. <view class="q-options">
  48. <view class="q-option" :class="{ 'q-selected': survey.digestion === 'good' }" @click="survey.digestion = 'good'">
  49. <text class="q-icon">😊</text>
  50. <text class="q-text">正常,消化良好</text>
  51. </view>
  52. <view class="q-option" :class="{ 'q-selected': survey.digestion === 'constipation' }" @click="survey.digestion = 'constipation'">
  53. <text class="q-icon">😣</text>
  54. <text class="q-text">容易便秘</text>
  55. </view>
  56. <view class="q-option" :class="{ 'q-selected': survey.digestion === 'diarrhea' }" @click="survey.digestion = 'diarrhea'">
  57. <text class="q-icon">😟</text>
  58. <text class="q-text">容易腹泻</text>
  59. </view>
  60. <view class="q-option" :class="{ 'q-selected': survey.digestion === 'irregular' }" @click="survey.digestion = 'irregular'">
  61. <text class="q-icon">🤔</text>
  62. <text class="q-text">不太规律</text>
  63. </view>
  64. </view>
  65. </view>
  66. <!-- Q3: 睡眠质量 -->
  67. <view class="question-card">
  68. <text class="q-number">3/6</text>
  69. <text class="q-title">孩子近期的睡眠质量如何?</text>
  70. <view class="q-options">
  71. <view class="q-option" :class="{ 'q-selected': survey.sleep === 'good' }" @click="survey.sleep = 'good'">
  72. <text class="q-icon">😴</text>
  73. <text class="q-text">睡得好,作息规律</text>
  74. </view>
  75. <view class="q-option" :class="{ 'q-selected': survey.sleep === 'late' }" @click="survey.sleep = 'late'">
  76. <text class="q-icon">🦉</text>
  77. <text class="q-text">睡得太晚</text>
  78. </view>
  79. <view class="q-option" :class="{ 'q-selected': survey.sleep === 'restless' }" @click="survey.sleep = 'restless'">
  80. <text class="q-icon">😰</text>
  81. <text class="q-text">翻来覆去睡不安稳</text>
  82. </view>
  83. <view class="q-option" :class="{ 'q-selected': survey.sleep === 'short' }" @click="survey.sleep = 'short'">
  84. <text class="q-icon">⏰</text>
  85. <text class="q-text">睡眠时间不足</text>
  86. </view>
  87. </view>
  88. </view>
  89. <!-- Q4: 运动习惯 -->
  90. <view class="question-card">
  91. <text class="q-number">4/6</text>
  92. <text class="q-title">孩子每周运动情况?</text>
  93. <view class="q-options">
  94. <view class="q-option" :class="{ 'q-selected': survey.exercise === 'frequent' }" @click="survey.exercise = 'frequent'">
  95. <text class="q-icon">🏃</text>
  96. <text class="q-text">经常运动(3次以上/周)</text>
  97. </view>
  98. <view class="q-option" :class="{ 'q-selected': survey.exercise === 'moderate' }" @click="survey.exercise = 'moderate'">
  99. <text class="q-icon">🚶</text>
  100. <text class="q-text">偶尔运动(1-2次/周)</text>
  101. </view>
  102. <view class="q-option" :class="{ 'q-selected': survey.exercise === 'rare' }" @click="survey.exercise = 'rare'">
  103. <text class="q-icon">🪑</text>
  104. <text class="q-text">很少运动</text>
  105. </view>
  106. </view>
  107. </view>
  108. <!-- Q5: 过敏/不适 -->
  109. <view class="question-card">
  110. <text class="q-number">5/6</text>
  111. <text class="q-title">孩子是否有以下过敏或不适症状?(可多选)</text>
  112. <view class="q-options">
  113. <view class="q-option" :class="{ 'q-selected': survey.allergies.indexOf('food') !== -1 }" @click="toggleAllergy('food')">
  114. <text class="q-text">食物过敏</text>
  115. </view>
  116. <view class="q-option" :class="{ 'q-selected': survey.allergies.indexOf('pollen') !== -1 }" @click="toggleAllergy('pollen')">
  117. <text class="q-text">花粉/尘螨过敏</text>
  118. </view>
  119. <view class="q-option" :class="{ 'q-selected': survey.allergies.indexOf('skin') !== -1 }" @click="toggleAllergy('skin')">
  120. <text class="q-text">皮肤湿疹/皮疹</text>
  121. </view>
  122. <view class="q-option" :class="{ 'q-selected': survey.allergies.indexOf('none') !== -1 }" @click="toggleAllergy('none')">
  123. <text class="q-text">无明显过敏</text>
  124. </view>
  125. </view>
  126. </view>
  127. <!-- Q6: 健康目标 -->
  128. <view class="question-card">
  129. <text class="q-number">6/6</text>
  130. <text class="q-title">您希望通过精准营养改善什么?(可多选)</text>
  131. <view class="q-options">
  132. <view class="q-option" :class="{ 'q-selected': survey.goals.indexOf('immunity') !== -1 }" @click="toggleGoal('immunity')">
  133. <text class="q-icon">🛡️</text>
  134. <text class="q-text">增强免疫力</text>
  135. </view>
  136. <view class="q-option" :class="{ 'q-selected': survey.goals.indexOf('digestion') !== -1 }" @click="toggleGoal('digestion')">
  137. <text class="q-icon">🌿</text>
  138. <text class="q-text">改善消化</text>
  139. </view>
  140. <view class="q-option" :class="{ 'q-selected': survey.goals.indexOf('growth') !== -1 }" @click="toggleGoal('growth')">
  141. <text class="q-icon">📈</text>
  142. <text class="q-text">促进生长发育</text>
  143. </view>
  144. <view class="q-option" :class="{ 'q-selected': survey.goals.indexOf('focus') !== -1 }" @click="toggleGoal('focus')">
  145. <text class="q-icon">🎯</text>
  146. <text class="q-text">提升专注力</text>
  147. </view>
  148. <view class="q-option" :class="{ 'q-selected': survey.goals.indexOf('weight') !== -1 }" @click="toggleGoal('weight')">
  149. <text class="q-icon">⚖️</text>
  150. <text class="q-text">体重管理</text>
  151. </view>
  152. </view>
  153. </view>
  154. <!-- 提交按钮 -->
  155. <view class="submit-btn" :class="{ 'btn-disabled': !canSubmit }" @click="doSubmit">
  156. <text v-if="submitting">提交中...</text>
  157. <text v-else>提交并进入 AI 解读</text>
  158. </view>
  159. </view>
  160. <view class="bottom-spacer"></view>
  161. </view>
  162. </template>
  163. <script>
  164. import { createReportSurvey, submitReportSurvey, getSurveyStatus } from '../../utils/api.js'
  165. export default {
  166. data() {
  167. return {
  168. reportId: null,
  169. childId: null,
  170. surveyId: null,
  171. survey: {
  172. diet: '',
  173. digestion: '',
  174. sleep: '',
  175. exercise: '',
  176. allergies: [],
  177. goals: []
  178. },
  179. submitting: false
  180. }
  181. },
  182. computed: {
  183. progressPercent: function() {
  184. var answered = 0
  185. if (this.survey.diet) answered++
  186. if (this.survey.digestion) answered++
  187. if (this.survey.sleep) answered++
  188. if (this.survey.exercise) answered++
  189. if (this.survey.allergies.length > 0) answered++
  190. if (this.survey.goals.length > 0) answered++
  191. return Math.round((answered / 6) * 100)
  192. },
  193. canSubmit: function() {
  194. return this.survey.diet && this.survey.digestion && this.survey.sleep && this.survey.exercise
  195. }
  196. },
  197. onLoad: function(options) {
  198. this.reportId = options.reportId ? parseInt(options.reportId) : null
  199. this.childId = options.childId ? parseInt(options.childId) : null
  200. this.initSurvey()
  201. },
  202. methods: {
  203. initSurvey: function() {
  204. var self = this
  205. if (!self.reportId) return
  206. // 先查状态
  207. getSurveyStatus(self.reportId).then(function(res) {
  208. if (res.code === 200 && res.data) {
  209. if (res.data.completed) {
  210. uni.showToast({ title: '该报告问卷已提交', icon: 'none' })
  211. return
  212. }
  213. if (res.data.pendingSurveyId) {
  214. self.surveyId = res.data.pendingSurveyId
  215. return
  216. }
  217. }
  218. // 未创建,则创建
  219. return createReportSurvey(self.reportId, self.childId || 0)
  220. }).then(function(res) {
  221. if (res && res.code === 200 && res.data && res.data.id) {
  222. self.surveyId = res.data.id
  223. }
  224. }).catch(function(e) {
  225. console.log('初始化问卷失败', e)
  226. })
  227. },
  228. toggleAllergy: function(val) {
  229. var idx = this.survey.allergies.indexOf(val)
  230. if (val === 'none') {
  231. this.survey.allergies = ['none']
  232. } else {
  233. // 移除 none
  234. var noneIdx = this.survey.allergies.indexOf('none')
  235. if (noneIdx !== -1) this.survey.allergies.splice(noneIdx, 1)
  236. if (idx === -1) {
  237. this.survey.allergies.push(val)
  238. } else {
  239. this.survey.allergies.splice(idx, 1)
  240. }
  241. // 如果全删了,恢复 none
  242. if (this.survey.allergies.length === 0) {
  243. this.survey.allergies.push('none')
  244. }
  245. }
  246. },
  247. toggleGoal: function(val) {
  248. var idx = this.survey.goals.indexOf(val)
  249. if (idx === -1) {
  250. this.survey.goals.push(val)
  251. } else {
  252. this.survey.goals.splice(idx, 1)
  253. }
  254. },
  255. goBack: function() {
  256. uni.navigateBack()
  257. },
  258. doSubmit: function() {
  259. if (!this.canSubmit || this.submitting) return
  260. var self = this
  261. self.submitting = true
  262. uni.showLoading({ title: '提交中...' })
  263. // 确保 surveyId 存在
  264. var doSubmit = function() {
  265. submitReportSurvey(self.surveyId, JSON.stringify(self.survey)).then(function(res) {
  266. uni.hideLoading()
  267. if (res.code === 200) {
  268. uni.showToast({ title: '提交成功', icon: 'success' })
  269. // 跳转到 AI 对话页,带入报告和问卷数据
  270. setTimeout(function() {
  271. var params = 'reportId=' + self.reportId + '&surveyId=' + self.surveyId + '&firstMessage=' + encodeURIComponent('请帮我解读孩子的健康报告')
  272. uni.redirectTo({ url: '/pages/ai/chat?' + params })
  273. }, 500)
  274. } else {
  275. uni.showToast({ title: res.message || '提交失败', icon: 'none' })
  276. self.submitting = false
  277. }
  278. }).catch(function(e) {
  279. uni.hideLoading()
  280. uni.showToast({ title: '提交失败,请重试', icon: 'none' })
  281. console.log('提交问卷失败', e)
  282. self.submitting = false
  283. })
  284. }
  285. if (this.surveyId) {
  286. doSubmit()
  287. } else {
  288. // 先创建再提交
  289. createReportSurvey(this.reportId, this.childId || 0).then(function(res) {
  290. if (res.code === 200 && res.data && res.data.id) {
  291. self.surveyId = res.data.id
  292. doSubmit()
  293. } else {
  294. uni.hideLoading()
  295. uni.showToast({ title: '创建问卷失败', icon: 'none' })
  296. self.submitting = false
  297. }
  298. }).catch(function(e) {
  299. uni.hideLoading()
  300. uni.showToast({ title: '创建问卷失败', icon: 'none' })
  301. self.submitting = false
  302. console.log('创建问卷失败', e)
  303. })
  304. }
  305. }
  306. }
  307. }
  308. </script>
  309. <style scoped>
  310. .survey-container {
  311. min-height: 100vh;
  312. background: #F3F7FA;
  313. }
  314. .nav-bar {
  315. position: sticky;
  316. top: 0;
  317. background: #fff;
  318. display: flex;
  319. flex-direction: row;
  320. align-items: center;
  321. justify-content: space-between;
  322. padding: 20rpx 30rpx;
  323. box-shadow: 0 2rpx 8rpx rgba(0,0,0,0.06);
  324. z-index: 100;
  325. }
  326. .nav-back { font-size: 32rpx; color: #5B9BD5; }
  327. .nav-title { font-size: 32rpx; font-weight: bold; color: #333; }
  328. .nav-placeholder { width: 80rpx; }
  329. .progress-bar {
  330. height: 6rpx;
  331. background: #E2E8F0;
  332. margin: 0 30rpx;
  333. border-radius: 3rpx;
  334. overflow: hidden;
  335. }
  336. .progress-fill {
  337. height: 100%;
  338. background: linear-gradient(90deg, #66BB6A, #43A047);
  339. border-radius: 3rpx;
  340. transition: width 0.3s;
  341. }
  342. .tip-banner {
  343. display: flex;
  344. flex-direction: row;
  345. align-items: center;
  346. background: #FFF8E1;
  347. margin: 16rpx 30rpx;
  348. padding: 16rpx 20rpx;
  349. border-radius: 12rpx;
  350. }
  351. .tip-icon { font-size: 28rpx; margin-right: 12rpx; flex-shrink: 0; }
  352. .tip-text { font-size: 22rpx; color: #F57F17; line-height: 1.5; }
  353. .form-section { margin: 10rpx 30rpx 30rpx; }
  354. .question-card {
  355. background: #fff;
  356. border-radius: 20rpx;
  357. padding: 28rpx 24rpx;
  358. margin-bottom: 16rpx;
  359. box-shadow: 0 2rpx 8rpx rgba(0,0,0,0.04);
  360. }
  361. .q-number {
  362. font-size: 20rpx;
  363. color: #66BB6A;
  364. font-weight: bold;
  365. display: block;
  366. margin-bottom: 8rpx;
  367. }
  368. .q-title {
  369. font-size: 28rpx;
  370. font-weight: bold;
  371. color: #333;
  372. display: block;
  373. margin-bottom: 16rpx;
  374. }
  375. .q-options {
  376. display: flex;
  377. flex-direction: column;
  378. gap: 12rpx;
  379. }
  380. .q-option {
  381. display: flex;
  382. flex-direction: row;
  383. align-items: center;
  384. gap: 12rpx;
  385. padding: 16rpx 18rpx;
  386. border-radius: 14rpx;
  387. background: #F5F7FA;
  388. border: 2rpx solid transparent;
  389. }
  390. .q-selected {
  391. background: #E8F5E9;
  392. border-color: #66BB6A;
  393. }
  394. .q-option:active { opacity: 0.7; }
  395. .q-icon { font-size: 32rpx; }
  396. .q-text { font-size: 26rpx; color: #333; }
  397. .submit-btn {
  398. background: linear-gradient(135deg, #43A047, #2E7D32);
  399. color: #fff;
  400. font-size: 30rpx;
  401. font-weight: bold;
  402. text-align: center;
  403. padding: 28rpx;
  404. border-radius: 16rpx;
  405. margin-top: 20rpx;
  406. }
  407. .submit-btn:active { opacity: 0.85; }
  408. .btn-disabled { opacity: 0.5; }
  409. .bottom-spacer { height: 60rpx; }
  410. </style>