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

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