普兆健康管家前端代码仓库
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.

654 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. console.log('needTag length', needTag?.length)
  210. if (!needTag?.length) {
  211. this.current = current
  212. this.tabs.push(this.current)
  213. return
  214. }
  215. const selectTags = this.paperTags.flat(2)
  216. console.log('selectTags', selectTags)
  217. console.log('needTag', needTag)
  218. let valid = needTag.some(tags => {
  219. return tags.every(tag => {
  220. const { value, exclude } = tag
  221. let include = selectTags.includes(value)
  222. return exclude ? !include : include
  223. })
  224. })
  225. console.log('valid', valid)
  226. if (valid) {
  227. this.current = current
  228. this.tabs.push(this.current)
  229. return
  230. }
  231. if (current + 1 < this.questionsList.length) {
  232. current += 1
  233. this.switchQuestion(current)
  234. return
  235. }
  236. this.$utils.redirectTo(`/pages_order/report/test/step?step=${this.step + 1}`)
  237. },
  238. pre() {
  239. // this.current -= 1
  240. this.current = this.preIdx
  241. this.value = this.answers[this.current]
  242. this.noneFlag = false
  243. },
  244. next() {
  245. if (this.showSubmitBtn) {
  246. return
  247. }
  248. // this.current += 1
  249. this.switchQuestion(this.current + 1)
  250. this.value = this.answers[this.current]
  251. this.noneFlag = false
  252. },
  253. async fetchAnswer() {
  254. try {
  255. const { id: questionsId, type, required, multiple, options } = this.currentQuestion
  256. console.log('paperInfo', this.paperInfo)
  257. console.log('currentQuestion', this.currentQuestion)
  258. console.log('required', required, 'noneFlag', this.noneFlag, 'value', this.value)
  259. if (required && !this.noneFlag && (!this.value || !this.value?.length || this.value?.every?.(val => !val))) {
  260. console.log('未答题')
  261. uni.showToast({
  262. title: '请答题',
  263. icon:'none'
  264. })
  265. return false
  266. }
  267. let answer
  268. // 0-单选题 1-填空题 2-图片单选题
  269. if (type == 1) {
  270. answer = options.reduce((obj, option, oIdx) => {
  271. const { id: optionsId } = option
  272. obj[optionsId] = Array.isArray(this.value) ? this.value[oIdx] : this.value
  273. return obj
  274. }, {})
  275. answer = JSON.stringify(answer)
  276. } else if (multiple && Array.isArray(this.value)) { // 多选题
  277. answer = this.value.join(',')
  278. }
  279. let params = {
  280. id: this.paperInfo.reportId,
  281. questionsId,
  282. answer
  283. }
  284. console.log('params', params)
  285. await this.$fetch('answerPaper', params)
  286. this.answers[this.current] = this.value
  287. let paperTags = [...this.paperTags]
  288. if (type == 1) {
  289. paperTags[this.step][this.current] = []
  290. } else {
  291. let selectedIdArr = Array.isArray(this.value) ? this.value : [this.value]
  292. console.log('selectedIdArr', selectedIdArr, 'options', options)
  293. paperTags[this.step][this.current] = selectedIdArr.reduce((arr, id) => {
  294. const { settingTag } = options.find(item => item.id === id) || {}
  295. if (settingTag) {
  296. const tags = settingTag?.split?.(',')
  297. tags?.length && (arr = arr.concat(tags))
  298. }
  299. return arr
  300. }, [])
  301. }
  302. console.log('paperTags', paperTags)
  303. this.$store.commit('setPaperTags', paperTags)
  304. return true
  305. } catch (err) {
  306. console.log('answerPaper', err)
  307. return false
  308. }
  309. },
  310. async onSelect(id) {
  311. this.value = id
  312. if (this.showSubmitBtn) {
  313. return
  314. }
  315. let succ = await this.fetchAnswer()
  316. if (!succ) {
  317. return
  318. }
  319. this.$nextTick(() => {
  320. setTimeout(() => {
  321. this.next()
  322. }, 500)
  323. })
  324. },
  325. async onSelectMulitple(id) {
  326. this.value = this.value.includes(id) ? this.value.filter(item => item !== id) : this.value.concat(id)
  327. },
  328. async onConfirm() {
  329. let succ = await this.fetchAnswer()
  330. if (!succ) {
  331. return
  332. }
  333. this.next()
  334. },
  335. async onSelectNone() {
  336. this.noneFlag = true
  337. let val = null
  338. switch (this.currentQuestion.component) {
  339. case 'select-box-multiple':
  340. case 'select-multiple':
  341. val = []
  342. break
  343. case 'input-group':
  344. val = new Array(this.currentQuestion.options.length).fill(0).map(() => '')
  345. break;
  346. default:
  347. val = null
  348. break;
  349. }
  350. this.value = val
  351. let succ = await this.fetchAnswer()
  352. if (!succ) {
  353. return
  354. }
  355. this.next()
  356. },
  357. async onSubmit() {
  358. let succ = await this.fetchAnswer()
  359. if (!succ) {
  360. return
  361. }
  362. const { category } = this.paperInfo
  363. if (this.step == category.length - 1) {
  364. await this.$fetch('submitPaper', { id: this.paperInfo.reportId })
  365. uni.reLaunch({
  366. url: '/pages/index/report'
  367. })
  368. return
  369. }
  370. this.$utils.redirectTo(`/pages_order/report/test/step?step=${this.step + 1}`)
  371. },
  372. },
  373. }
  374. </script>
  375. <style scoped lang="scss">
  376. .page__view {
  377. width: 100vw;
  378. min-height: 100vh;
  379. background-color: $uni-bg-color;
  380. position: relative;
  381. }
  382. .bar {
  383. margin-top: 24rpx;
  384. width: 100%;
  385. padding: 0 32rpx;
  386. box-sizing: border-box;
  387. .left {
  388. text-align: left;
  389. .btn {
  390. display: inline-flex;
  391. font-family: PingFang SC;
  392. font-weight: 400;
  393. font-size: 30rpx;
  394. line-height: 1.4;
  395. color: #7451DE;
  396. &-icon {
  397. width: 32rpx;
  398. height: auto;
  399. margin-right: 8rpx;
  400. }
  401. }
  402. }
  403. .right {
  404. flex: 1;
  405. text-align: right;
  406. font-family: PingFang SC;
  407. font-weight: 400;
  408. font-size: 26rpx;
  409. line-height: 1.4;
  410. color: #989898;
  411. }
  412. }
  413. .progress {
  414. margin-top: 24rpx;
  415. width: 100%;
  416. height: 24rpx;
  417. background: #E5E5E5;
  418. border-radius: 12rpx;
  419. &-bar {
  420. height: 100%;
  421. background-image: linear-gradient(to right, #4B348F, #845CFA);
  422. border-radius: 12rpx;
  423. }
  424. }
  425. .main {
  426. width: 100%;
  427. padding: 70rpx 104rpx 0 104rpx;
  428. box-sizing: border-box;
  429. }
  430. .question {
  431. text-align: center;
  432. font-family: PingFang SC;
  433. font-weight: 400;
  434. font-size: 40rpx;
  435. line-height: 1.4;
  436. color: #000000;
  437. margin-bottom: 64rpx;
  438. }
  439. .select {
  440. &-option {
  441. padding: 30rpx 0;
  442. font-family: PingFang SC;
  443. font-weight: 400;
  444. line-height: 1.4;
  445. font-size: 32rpx;
  446. color: #252545;
  447. background-image: linear-gradient(to right, #FFFFFF, #F4F4F6, #F2F2F4);
  448. border-radius: 52rpx;
  449. box-shadow: -2rpx -2rpx 10rpx 0 #FFFFFFC4,
  450. 4rpx 4rpx 20rpx 0 #AAAACC1F,
  451. 2rpx 2rpx 4rpx 0 #AAAACC40,
  452. -1rpx -1rpx 2rpx 0 #FFFFFF;
  453. & + & {
  454. margin-top: 40rpx;
  455. }
  456. &.is-active {
  457. color: #FFFFFF;
  458. background: #7451DE;
  459. }
  460. }
  461. }
  462. .select-box {
  463. display: grid;
  464. grid-template-columns: repeat(3, 1fr);
  465. gap: 32rpx;
  466. &-option {
  467. padding: 24rpx 0;
  468. border-radius: 24rpx;
  469. background-image: linear-gradient(to right, #FFFFFF, #F2F2F4);
  470. box-shadow: -2rpx -2rpx 10rpx 0 #FFFFFFC4,
  471. 4rpx 4rpx 20rpx 0 #AAAACC1F,
  472. 2rpx 2rpx 4rpx 0 #AAAACC40,
  473. -1rpx -1rpx 2rpx 0 #FFFFFF;
  474. .img {
  475. width: 100%;
  476. height: 64rpx;
  477. }
  478. .text {
  479. margin-top: 8rpx;
  480. font-family: PingFang SC;
  481. font-weight: 400;
  482. font-size: 28rpx;
  483. line-height: 1.4;
  484. color: #969696;
  485. }
  486. &.is-active {
  487. background: #7451DE;
  488. .text {
  489. color: #FFFFFFC4;
  490. }
  491. }
  492. }
  493. }
  494. .input {
  495. &-box {
  496. width: 100%;
  497. height: 104rpx;
  498. padding: 0 40rpx;
  499. box-sizing: border-box;
  500. border-radius: 52rpx;
  501. background-image: linear-gradient(to right, #FFFFFF, #F4F4F6, #F2F2F4);
  502. box-shadow: -2rpx -2rpx 10rpx 0 #FFFFFFC4,
  503. 4rpx 4rpx 20rpx 0 #AAAACC1F,
  504. 2rpx 2rpx 4rpx 0 #AAAACC40,
  505. -1rpx -1rpx 2rpx 0 #FFFFFF;
  506. & + & {
  507. margin-top: 40rpx;
  508. }
  509. }
  510. &-doc {
  511. text-align: center;
  512. flex: 1;
  513. }
  514. &-label {
  515. padding: 21rpx 40rpx 21rpx 0;
  516. font-family: PingFang SC;
  517. font-weight: 600;
  518. font-size: 30rpx;
  519. line-height: 1.4;
  520. color: #252545;
  521. border-right: 2rpx solid #E0DFF0;
  522. }
  523. &-unit {
  524. padding: 21rpx 0 21rpx 40rpx;
  525. font-family: PingFang SC;
  526. font-weight: 600;
  527. font-size: 30rpx;
  528. line-height: 1.4;
  529. color: #252545;
  530. border-left: 2rpx solid #E0DFF0;
  531. }
  532. }
  533. .btns {
  534. margin-top: 64rpx;
  535. column-gap: 36rpx;
  536. .btn {
  537. flex: 1;
  538. padding: 24rpx 0;
  539. font-family: PingFang SC;
  540. font-weight: 600;
  541. font-size: 30rpx;
  542. line-height: 1.4;
  543. color: #FFFFFF;
  544. background-image: linear-gradient(to right, #4B348F, #845CFA);
  545. border-radius: 45rpx;
  546. &-palin {
  547. padding: 22rpx 0;
  548. font-weight: 400;
  549. color: #252545;
  550. background: transparent;
  551. border: 2rpx solid #252545;
  552. &.is-active {
  553. color: $uni-color;
  554. border-color: $uni-color;
  555. }
  556. }
  557. }
  558. }
  559. .desc {
  560. margin-top: 220rpx;
  561. font-family: PingFang SC;
  562. font-weight: 400;
  563. line-height: 1.4;
  564. font-size: 26rpx;
  565. color: #989898;
  566. }
  567. </style>