普兆健康管家前端代码仓库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

653 lines
18 KiB

  1. <template>
  2. <view class="page__view">
  3. <navbar :title="`问题 ${current + 1}/${total}`" leftClick @leftClick="$utils.navigateBack" color="#191919" bgColor="transparent" />
  4. <template v-if="currentQuestion">
  5. <view class="bar">
  6. <view class="flex tools">
  7. <view class="left">
  8. <button class="flex btn" v-if="showPreBtn" @click="pre">
  9. <image class="btn-icon" src="@/pages_order/static/report/icon-wrapper.png" mode="widthFix"></image>
  10. <text>上一题</text>
  11. </button>
  12. </view>
  13. <view class="right">
  14. 选择你的答案进入下一题
  15. </view>
  16. </view>
  17. <view class="progress">
  18. <view class="progress-bar" :style="{ width: `${progress}%` }"></view>
  19. </view>
  20. </view>
  21. <view class="main">
  22. <view class="question">{{ currentQuestion.question }}</view>
  23. <template v-if="currentQuestion.component === 'select'">
  24. <view class="select">
  25. <view
  26. v-for="item in currentQuestion.options"
  27. :key="item.id"
  28. :class="['flex', 'select-option', item.id === value ? 'is-active' : '']"
  29. @click="onSelect(item.id)"
  30. >
  31. {{ item.content }}
  32. </view>
  33. </view>
  34. </template>
  35. <template v-else-if="currentQuestion.component === 'select-multiple'">
  36. <view class="select">
  37. <view
  38. v-for="item in currentQuestion.options"
  39. :key="item.id"
  40. :class="['flex', 'select-option', value.includes(item.id) ? 'is-active' : '']"
  41. @click="onSelectMulitple(item.id)"
  42. >
  43. {{ item.content }}
  44. </view>
  45. </view>
  46. </template>
  47. <template v-else-if="currentQuestion.component === 'select-box'">
  48. <view class="select-box">
  49. <view
  50. v-for="item in currentQuestion.options"
  51. :key="item.id"
  52. :class="['flex', 'flex-column', 'select-box-option', item.id === value ? 'is-active' : '']"
  53. @click="onSelect(item.id)"
  54. >
  55. <image class="img" :src="item.id === value ? item.imageAfter : item.image" mode="aspectFit"></image>
  56. <view class="text">{{ item.content }}</view>
  57. </view>
  58. </view>
  59. </template>
  60. <template v-else-if="currentQuestion.component === 'select-box-multiple'">
  61. <view class="select-box">
  62. <view
  63. v-for="item in currentQuestion.options"
  64. :key="item.id"
  65. :class="['flex', 'flex-column', 'select-box-option', value.includes(item.id) ? 'is-active' : '']"
  66. @click="onSelectMulitple(item.id)"
  67. >
  68. <image class="img" :src="value.includes(item.id) ? item.imageAfter : item.image" mode="aspectFit"></image>
  69. <view class="text">{{ item.content }}</view>
  70. </view>
  71. </view>
  72. </template>
  73. <template v-else-if="currentQuestion.component === 'input'">
  74. <view class="input">
  75. <view class="flex input-box">
  76. <view class="input-label" v-if="currentQuestion.options[0].prefix">{{ currentQuestion.options[0].prefix }}</view>
  77. <input class="input-doc" v-model="value" :type="currentQuestion.options[0].inputType || 'text'" placeholder="请输入内容" placeholder-style="font-family: PingFang SC; font-weight: 400; line-height: 1.4; font-size: 32rpx; color: #AAAACA;" />
  78. <view class="input-unit" v-if="currentQuestion.options[0].suffix">{{ currentQuestion.options[0].suffix }}</view>
  79. </view>
  80. </view>
  81. </template>
  82. <template v-else-if="currentQuestion.component === 'input-group'">
  83. <view class="input">
  84. <view class="flex input-box" v-for="(item, index) in currentQuestion.options" :key="item.id">
  85. <view class="input-label" v-if="item.prefix">{{ item.prefix }}</view>
  86. <input class="input-doc" v-model="value[index]" :type="currentQuestion.options[index].inputType || 'text'" placeholder="请输入内容" placeholder-style="font-family: PingFang SC; font-weight: 400; line-height: 1.4; font-size: 32rpx; color: #AAAACA;" />
  87. <view class="input-unit" v-if="item.suffix">{{ item.suffix }}</view>
  88. </view>
  89. </view>
  90. </template>
  91. <view class="flex btns">
  92. <button :class="['btn', 'btn-palin', noneFlag ? 'is-active' : '']" v-if="!currentQuestion.required" @click="onSelectNone"> </button>
  93. <button class="btn" v-if="showConfirmBtn" @click="onConfirm"> </button>
  94. <button class="btn" v-if="showSubmitBtn" @click="onSubmit"> </button>
  95. </view>
  96. <view class="desc" v-if="currentQuestion.desc">
  97. <uv-parse :content="currentQuestion.desc"></uv-parse>
  98. </view>
  99. </view>
  100. </template>
  101. </view>
  102. </template>
  103. <script>
  104. import { mapState } from 'vuex'
  105. export default {
  106. data() {
  107. return {
  108. step: 0,
  109. current: null,
  110. tabs: [],
  111. value: null,
  112. noneFlag: false,
  113. answers: [],
  114. // tags: [],
  115. questionsList: [],
  116. total: 0,
  117. }
  118. },
  119. computed: {
  120. ...mapState(['paperInfo', 'paperTags']),
  121. preIdx() {
  122. let index = this.tabs.findIndex(index => index === this.current)
  123. return index - 1
  124. },
  125. showPreBtn() {
  126. return this.preIdx >= 0
  127. },
  128. progress() {
  129. return 100 * (this.current + 1) / this.total
  130. },
  131. showSubmitBtn() {
  132. return this.current + 1 === this.total
  133. },
  134. currentQuestion() {
  135. return this.questionsList[this.current]
  136. },
  137. showConfirmBtn() {
  138. if (this.showSubmitBtn) {
  139. return false
  140. }
  141. return ['select-box-multiple', 'select-multiple','input', 'input-group'].includes(this.currentQuestion?.component)
  142. },
  143. },
  144. onLoad(arg) {
  145. this.step = parseInt(arg.step)
  146. // this.current = parseInt(arg.current || 0)
  147. console.log('paperTags', this.paperTags)
  148. this.fetchQuestionList()
  149. this.switchQuestion(parseInt(arg.current || 0))
  150. this.value = this.answers[this.current]
  151. console.log('currentQuestion', this.currentQuestion)
  152. },
  153. methods: {
  154. fetchQuestionList() {
  155. const { category: categoryList } = this.paperInfo
  156. const category = categoryList[this.step]
  157. const { questionsList } = category
  158. this.questionsList = questionsList.map(item => {
  159. const { id, text, type, options, needTag, required, multiple: _multiple, content } = item
  160. let component = 'select'
  161. const multiple = _multiple == 'Y'
  162. switch(type) { // 0-单选题 1-填空题 2-图片单选题
  163. case '1':
  164. component = options?.length > 1 ? 'input-group' : 'input'
  165. break
  166. case '2':
  167. component = multiple ? 'select-box-multiple' : 'select-box'
  168. break
  169. default:
  170. component = multiple ? 'select-multiple' : 'select'
  171. break
  172. }
  173. return {
  174. id,
  175. question: text,
  176. type,
  177. component,
  178. options,
  179. needTag: needTag ? JSON.parse(needTag).map(config => config.tags).filter(tags => tags.length) : null,
  180. required: required == 'Y',
  181. multiple,
  182. desc: content,
  183. }
  184. })
  185. this.total = this.questionsList.length
  186. this.answers = this.questionsList.map(item => {
  187. let val = null
  188. switch (item.component) {
  189. case 'select-box-multiple':
  190. case 'select-multiple':
  191. val = []
  192. break
  193. case 'input-group':
  194. val = new Array(item.options.length).fill(0).map(() => '')
  195. break;
  196. default:
  197. val = null
  198. break;
  199. }
  200. return val
  201. })
  202. // this.tags = this.paperTags[this.step]
  203. // this.value = this.answers[this.current]
  204. console.log('questionsList', this.questionsList)
  205. console.log('answers', this.answers)
  206. },
  207. switchQuestion(current) {
  208. let { needTag } = this.questionsList[current]
  209. if (!needTag) {
  210. this.current = current
  211. this.tabs.push(this.current)
  212. return
  213. }
  214. const selectTags = this.paperTags.flat(2)
  215. console.log('selectTags', selectTags)
  216. console.log('needTag', needTag)
  217. let valid = needTag.some(tags => {
  218. return tags.every(tag => {
  219. const { value, exclude } = tag
  220. let include = selectTags.includes(value)
  221. return exclude ? !include : include
  222. })
  223. })
  224. console.log('valid', valid)
  225. if (valid) {
  226. this.current = current
  227. this.tabs.push(this.current)
  228. return
  229. }
  230. if (current + 1 < this.questionsList.length) {
  231. current += 1
  232. this.switchQuestion(current)
  233. return
  234. }
  235. this.$utils.redirectTo(`/pages_order/report/test/step?step=${this.step + 1}`)
  236. },
  237. pre() {
  238. // this.current -= 1
  239. this.current = this.preIdx
  240. this.value = this.answers[this.current]
  241. this.noneFlag = false
  242. },
  243. next() {
  244. if (this.showSubmitBtn) {
  245. return
  246. }
  247. // this.current += 1
  248. this.switchQuestion(this.current + 1)
  249. this.value = this.answers[this.current]
  250. this.noneFlag = false
  251. },
  252. async fetchAnswer() {
  253. try {
  254. const { id: questionsId, type, required, multiple, options } = this.currentQuestion
  255. console.log('paperInfo', this.paperInfo)
  256. console.log('currentQuestion', this.currentQuestion)
  257. console.log('required', required, 'noneFlag', this.noneFlag, 'value', this.value)
  258. if (required && !this.noneFlag && (!this.value || !this.value?.length || this.value?.every?.(val => !val))) {
  259. console.log('未答题')
  260. uni.showToast({
  261. title: '请答题',
  262. icon:'none'
  263. })
  264. return false
  265. }
  266. let answer
  267. // 0-单选题 1-填空题 2-图片单选题
  268. if (type == 1) {
  269. answer = options.reduce((obj, option, oIdx) => {
  270. const { id: optionsId } = option
  271. obj[optionsId] = this.value[oIdx]
  272. return obj
  273. }, {})
  274. answer = JSON.stringify(answer)
  275. } else if (multiple && Array.isArray(this.value)) { // 多选题
  276. answer = this.value.join(',')
  277. }
  278. let params = {
  279. id: this.paperInfo.reportId,
  280. questionsId,
  281. answer
  282. }
  283. console.log('params', params)
  284. await this.$fetch('answerPaper', params)
  285. this.answers[this.current] = this.value
  286. let paperTags = [...this.paperTags]
  287. if (type == 1) {
  288. paperTags[this.step][this.current] = []
  289. } else {
  290. let selectedIdArr = Array.isArray(this.value) ? this.value : [this.value]
  291. console.log('selectedIdArr', selectedIdArr, 'options', options)
  292. paperTags[this.step][this.current] = selectedIdArr.reduce((arr, id) => {
  293. const { settingTag } = options.find(item => item.id === id) || {}
  294. if (settingTag) {
  295. const tags = settingTag?.split?.(',')
  296. tags?.length && (arr = arr.concat(tags))
  297. }
  298. return arr
  299. }, [])
  300. }
  301. console.log('paperTags', paperTags)
  302. this.$store.commit('setPaperTags', paperTags)
  303. return true
  304. } catch (err) {
  305. console.log('answerPaper', err)
  306. return false
  307. }
  308. },
  309. async onSelect(id) {
  310. this.value = id
  311. if (this.showSubmitBtn) {
  312. return
  313. }
  314. let succ = await this.fetchAnswer()
  315. if (!succ) {
  316. return
  317. }
  318. this.$nextTick(() => {
  319. setTimeout(() => {
  320. this.next()
  321. }, 500)
  322. })
  323. },
  324. async onSelectMulitple(id) {
  325. this.value = this.value.includes(id) ? this.value.filter(item => item !== id) : this.value.concat(id)
  326. },
  327. async onConfirm() {
  328. let succ = await this.fetchAnswer()
  329. if (!succ) {
  330. return
  331. }
  332. this.next()
  333. },
  334. async onSelectNone() {
  335. this.noneFlag = true
  336. let val = null
  337. switch (this.currentQuestion.component) {
  338. case 'select-box-multiple':
  339. case 'select-multiple':
  340. val = []
  341. break
  342. case 'input-group':
  343. val = new Array(this.currentQuestion.options.length).fill(0).map(() => '')
  344. break;
  345. default:
  346. val = null
  347. break;
  348. }
  349. this.value = val
  350. let succ = await this.fetchAnswer()
  351. if (!succ) {
  352. return
  353. }
  354. this.next()
  355. },
  356. async onSubmit() {
  357. let succ = await this.fetchAnswer()
  358. if (!succ) {
  359. return
  360. }
  361. const { category } = this.paperInfo
  362. if (this.step == category.length - 1) {
  363. await this.$fetch('submitPaper', { id: this.paperInfo.reportId })
  364. uni.reLaunch({
  365. url: '/pages/index/report'
  366. })
  367. return
  368. }
  369. this.$utils.redirectTo(`/pages_order/report/test/step?step=${this.step + 1}`)
  370. },
  371. },
  372. }
  373. </script>
  374. <style scoped lang="scss">
  375. .page__view {
  376. width: 100vw;
  377. min-height: 100vh;
  378. background-color: $uni-bg-color;
  379. position: relative;
  380. }
  381. .bar {
  382. margin-top: 24rpx;
  383. width: 100%;
  384. padding: 0 32rpx;
  385. box-sizing: border-box;
  386. .left {
  387. text-align: left;
  388. .btn {
  389. display: inline-flex;
  390. font-family: PingFang SC;
  391. font-weight: 400;
  392. font-size: 30rpx;
  393. line-height: 1.4;
  394. color: #7451DE;
  395. &-icon {
  396. width: 32rpx;
  397. height: auto;
  398. margin-right: 8rpx;
  399. }
  400. }
  401. }
  402. .right {
  403. flex: 1;
  404. text-align: right;
  405. font-family: PingFang SC;
  406. font-weight: 400;
  407. font-size: 26rpx;
  408. line-height: 1.4;
  409. color: #989898;
  410. }
  411. }
  412. .progress {
  413. margin-top: 24rpx;
  414. width: 100%;
  415. height: 24rpx;
  416. background: #E5E5E5;
  417. border-radius: 12rpx;
  418. &-bar {
  419. height: 100%;
  420. background-image: linear-gradient(to right, #4B348F, #845CFA);
  421. border-radius: 12rpx;
  422. }
  423. }
  424. .main {
  425. width: 100%;
  426. padding: 70rpx 104rpx 0 104rpx;
  427. box-sizing: border-box;
  428. }
  429. .question {
  430. text-align: center;
  431. font-family: PingFang SC;
  432. font-weight: 400;
  433. font-size: 40rpx;
  434. line-height: 1.4;
  435. color: #000000;
  436. margin-bottom: 64rpx;
  437. }
  438. .select {
  439. &-option {
  440. padding: 30rpx 0;
  441. font-family: PingFang SC;
  442. font-weight: 400;
  443. line-height: 1.4;
  444. font-size: 32rpx;
  445. color: #252545;
  446. background-image: linear-gradient(to right, #FFFFFF, #F4F4F6, #F2F2F4);
  447. border-radius: 52rpx;
  448. box-shadow: -2rpx -2rpx 10rpx 0 #FFFFFFC4,
  449. 4rpx 4rpx 20rpx 0 #AAAACC1F,
  450. 2rpx 2rpx 4rpx 0 #AAAACC40,
  451. -1rpx -1rpx 2rpx 0 #FFFFFF;
  452. & + & {
  453. margin-top: 40rpx;
  454. }
  455. &.is-active {
  456. color: #FFFFFF;
  457. background: #7451DE;
  458. }
  459. }
  460. }
  461. .select-box {
  462. display: grid;
  463. grid-template-columns: repeat(3, 1fr);
  464. gap: 32rpx;
  465. &-option {
  466. padding: 24rpx 0;
  467. border-radius: 24rpx;
  468. background-image: linear-gradient(to right, #FFFFFF, #F2F2F4);
  469. box-shadow: -2rpx -2rpx 10rpx 0 #FFFFFFC4,
  470. 4rpx 4rpx 20rpx 0 #AAAACC1F,
  471. 2rpx 2rpx 4rpx 0 #AAAACC40,
  472. -1rpx -1rpx 2rpx 0 #FFFFFF;
  473. .img {
  474. width: 100%;
  475. height: 64rpx;
  476. }
  477. .text {
  478. margin-top: 8rpx;
  479. font-family: PingFang SC;
  480. font-weight: 400;
  481. font-size: 28rpx;
  482. line-height: 1.4;
  483. color: #969696;
  484. }
  485. &.is-active {
  486. background: #7451DE;
  487. .text {
  488. color: #FFFFFFC4;
  489. }
  490. }
  491. }
  492. }
  493. .input {
  494. &-box {
  495. width: 100%;
  496. height: 104rpx;
  497. padding: 0 40rpx;
  498. box-sizing: border-box;
  499. border-radius: 52rpx;
  500. background-image: linear-gradient(to right, #FFFFFF, #F4F4F6, #F2F2F4);
  501. box-shadow: -2rpx -2rpx 10rpx 0 #FFFFFFC4,
  502. 4rpx 4rpx 20rpx 0 #AAAACC1F,
  503. 2rpx 2rpx 4rpx 0 #AAAACC40,
  504. -1rpx -1rpx 2rpx 0 #FFFFFF;
  505. & + & {
  506. margin-top: 40rpx;
  507. }
  508. }
  509. &-doc {
  510. text-align: center;
  511. flex: 1;
  512. }
  513. &-label {
  514. padding: 21rpx 40rpx 21rpx 0;
  515. font-family: PingFang SC;
  516. font-weight: 600;
  517. font-size: 30rpx;
  518. line-height: 1.4;
  519. color: #252545;
  520. border-right: 2rpx solid #E0DFF0;
  521. }
  522. &-unit {
  523. padding: 21rpx 0 21rpx 40rpx;
  524. font-family: PingFang SC;
  525. font-weight: 600;
  526. font-size: 30rpx;
  527. line-height: 1.4;
  528. color: #252545;
  529. border-left: 2rpx solid #E0DFF0;
  530. }
  531. }
  532. .btns {
  533. margin-top: 64rpx;
  534. column-gap: 36rpx;
  535. .btn {
  536. flex: 1;
  537. padding: 24rpx 0;
  538. font-family: PingFang SC;
  539. font-weight: 600;
  540. font-size: 30rpx;
  541. line-height: 1.4;
  542. color: #FFFFFF;
  543. background-image: linear-gradient(to right, #4B348F, #845CFA);
  544. border-radius: 45rpx;
  545. &-palin {
  546. padding: 22rpx 0;
  547. font-weight: 400;
  548. color: #252545;
  549. background: transparent;
  550. border: 2rpx solid #252545;
  551. &.is-active {
  552. color: $uni-color;
  553. border-color: $uni-color;
  554. }
  555. }
  556. }
  557. }
  558. .desc {
  559. margin-top: 220rpx;
  560. font-family: PingFang SC;
  561. font-weight: 400;
  562. line-height: 1.4;
  563. font-size: 26rpx;
  564. color: #989898;
  565. }
  566. </style>