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

661 lines
18 KiB

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