test-entry.vue 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <template>
  2. <ie-page :fix-height="true">
  3. <ie-navbar :title="title" />
  4. <template v-if="isReady">
  5. <test-step />
  6. <test-title />
  7. <test-tabs />
  8. <uv-line margin="6px 0" />
  9. <ie-auto-resizer>
  10. <swiper class="h-full" :current="questionIndex" :duration="swiperDuration" @change="handleChangeTab">
  11. <swiper-item class="h-full" v-for="(qs, index) in questionList" :key="qs.id">
  12. <template v-if="Math.abs(index - questionIndex) <= 4">
  13. <scroll-view class="h-full" scroll-y>
  14. <view v-if="qs.title" class="text-28 px-30 py-20">
  15. {{ qs.no }}、{{ qs.title }}
  16. </view>
  17. <view class="px-30">
  18. <uv-checkbox-group v-if="qs.multiple" v-model="qs.answer" shape="square" placement="column">
  19. <uv-checkbox :customStyle="{ padding: '8px 0' }" v-for="(item, index) in qs.options" :key="index"
  20. :label="item.title" :name="item.answer" labelColor="#888"></uv-checkbox>
  21. </uv-checkbox-group>
  22. <uv-radio-group v-else v-model="qs.answer" placement="column" @change="handleRadioChange">
  23. <uv-radio :customStyle="{ padding: '8px 0' }" v-for="(item, index) in qs.options" :key="index"
  24. :label="type === 'holland' ? item.title : item.no + '、' + item.title" :name="item.answer">
  25. </uv-radio>
  26. </uv-radio-group>
  27. </view>
  28. </scroll-view>
  29. </template>
  30. </swiper-item>
  31. </swiper>
  32. </ie-auto-resizer>
  33. <view class="px-40 py-30 flex items-center gap-20">
  34. <view class="flex-1">
  35. <uv-button type="primary" plain shape="circle" :disabled="!canPrev" @click="handlePrevious">上一题</uv-button>
  36. </view>
  37. <view class="flex-1">
  38. <uv-button v-if="canNext" type="primary" shape="circle" :disabled="!canNext"
  39. @click="handleNext">下一题</uv-button>
  40. <uv-button v-else type="primary" shape="circle" @click="handleGenerateReport">生成报告</uv-button>
  41. </view>
  42. </view>
  43. </template>
  44. <test-popup ref="testPopupRef" />
  45. </ie-page>
  46. </template>
  47. <script lang="ts" setup>
  48. import TestStep from './test-step.vue';
  49. import TestTitle from './test-title.vue';
  50. import TestTabs from './test-tabs.vue';
  51. import TestPopup from './test-popup.vue';
  52. import { useTest, SYMBOL_TEST_DATA } from './useTest';
  53. import { hollSaveHolland, mbtiSave } from '@/api/modules/test-center';
  54. import { useTransferPage } from '@/hooks/useTransferPage';
  55. const props = defineProps({
  56. title: {
  57. type: String,
  58. default: ''
  59. },
  60. type: {
  61. type: String as PropType<'holland' | 'mbti'>,
  62. default: 'holland'
  63. }
  64. });
  65. const { transferTo, routes } = useTransferPage();
  66. const testData = useTest(props.type);
  67. provide(SYMBOL_TEST_DATA, testData);
  68. // 解构出需要的方法
  69. const { isReady, questionList, unAnseredQuestionList, questionIndex, setQuestionIndex, prevQuestion, nextQuestion, swiperDuration, canPrev, canNext, getResult } = testData;
  70. const handleChangeTab = (e: any) => {
  71. setQuestionIndex(e.detail.current);
  72. }
  73. const handlePrevious = () => {
  74. prevQuestion();
  75. }
  76. const handleRadioChange = () => {
  77. setTimeout(() => {
  78. nextQuestion();
  79. }, 300);
  80. }
  81. const testPopupRef = ref();
  82. const handleNext = () => {
  83. if (questionIndex.value === questionList.value.length - 1) {
  84. // 每个步骤的最后一题检查是否还有未做完的
  85. if (unAnseredQuestionList.value.length > 0) {
  86. testPopupRef.value?.open();
  87. return;
  88. }
  89. nextQuestion();
  90. } else {
  91. nextQuestion();
  92. }
  93. }
  94. const handleGenerateReport = () => {
  95. uni.$ie.showLoading();
  96. const result = getResult();
  97. if (props.type === 'holland') {
  98. hollSaveHolland({
  99. details: result
  100. }).then(res => {
  101. uni.$ie.hideLoading();
  102. transferTo(routes.pageHollandReport, {
  103. data: {
  104. code: res.data
  105. },
  106. type: 'redirectTo'
  107. });
  108. }).catch(err => {
  109. uni.$ie.hideLoading();
  110. });
  111. } else {
  112. mbtiSave({
  113. details: result
  114. }).then(res => {
  115. uni.$ie.hideLoading();
  116. transferTo(routes.pageMbtiReport, {
  117. data: {
  118. code: res.data
  119. },
  120. type: 'redirectTo'
  121. });
  122. }).catch(err => {
  123. uni.$ie.hideLoading();
  124. });
  125. }
  126. }
  127. </script>
  128. <style lang="scss" scoped></style>