index.tsx 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  1. import {
  2. ActionSheet,
  3. Button,
  4. Cell,
  5. CountDown,
  6. Grid,
  7. GridItem,
  8. Icon,
  9. Image,
  10. Popup,
  11. showDialog,
  12. Swipe,
  13. SwipeItem,
  14. Tag
  15. } from 'vant'
  16. import { computed, defineComponent, nextTick, onMounted, reactive, ref } from 'vue'
  17. import { useRoute, useRouter } from 'vue-router'
  18. import styles from './index.module.less'
  19. import iconButtonList from '../images/icon-button-list.png'
  20. import OSticky from '@/components/o-sticky'
  21. import ChoiceQuestion from '../model/choice-question'
  22. import AnswerList from '../model/answer-list'
  23. import ODialog from '@/components/o-dialog'
  24. import DragQuestion from '../model/drag-question'
  25. import KeepLookQuestion from '../model/keep-look-question'
  26. import PlayQuestion from '../model/play-question'
  27. import ErrorMode from '../model/error-mode'
  28. import ResultFinish from '../model/result-finish'
  29. import { QuestionType } from '../unit'
  30. import request from '@/helpers/request'
  31. import { useRect } from '@vant/use'
  32. import OHeader from '@/components/o-header'
  33. import { useInterval } from '@vueuse/core'
  34. export default defineComponent({
  35. name: 'unit-detail',
  36. setup() {
  37. const route = useRoute()
  38. const router = useRouter()
  39. const countDownRef = ref()
  40. const swipeRef = ref()
  41. const state = reactive({
  42. examId: route.query.examId,
  43. name: route.query.name,
  44. visiableError: false,
  45. visiableAnswer: false,
  46. visiableResult: false,
  47. id: route.query.id,
  48. currentIndex: 0,
  49. questionList: [],
  50. visiableSure: false,
  51. resultInfo: {} as any,
  52. resultStatusType: 'SUCCESS', // 'SUCCESS' | 'FAIL'
  53. visiableExam: false, // 考试已结束
  54. nextStatus: false,
  55. swipeHeight: 'auto' as any,
  56. answerAnalysis: '',
  57. overResult: {
  58. time: '00:00', // 时长
  59. questionLength: 0, // 答题数
  60. errorLength: 0, // 错题数
  61. rate: 0 // 正确率
  62. },
  63. knowledgelist: [] as any, // 知识点列表
  64. quitStatus: false
  65. })
  66. // 计时
  67. const { counter, resume, pause } = useInterval(1000, { controls: true })
  68. const getExamDetails = async () => {
  69. try {
  70. const { data } = await request.post('/api-student/examinationQuestion/randomPage', {
  71. data: {
  72. page: 1,
  73. row: 50,
  74. categoryId: state.id
  75. }
  76. })
  77. const temp = data || []
  78. temp.forEach((item: any) => {
  79. item.showAnalysis = false // 默认不显示解析
  80. item.analysis = {
  81. message: item.answerAnalysis,
  82. topic: true, // 是否显示结果
  83. userResult: false // 用户答题对错
  84. }
  85. item.userAnswer = [] // 用户答题
  86. })
  87. state.questionList = temp
  88. } catch {
  89. //
  90. }
  91. }
  92. // 获取所在知识点
  93. const getDetails = async () => {
  94. try {
  95. const { data } = await request.post('/api-student/unitExamination/queryKnowledgePoint', {
  96. requestType: 'form',
  97. data: {
  98. unitExaminationId: state.examId
  99. }
  100. })
  101. state.knowledgelist = data.lists || []
  102. } catch {
  103. //
  104. }
  105. }
  106. /**
  107. * @description 下一题 | 测试完成
  108. */
  109. const onNextQuestion = async () => {
  110. try {
  111. const questionList = state.questionList || []
  112. let result: any = {}
  113. questionList.forEach((question: any, index: number) => {
  114. // 格式化所有题目的答案
  115. if (index === state.currentIndex) {
  116. result = {
  117. questionId: question.id,
  118. details: question.userAnswer || []
  119. }
  120. }
  121. })
  122. const { data } = await request.post(
  123. '/api-student/studentUnitExamination/submitTrainingAnswer',
  124. {
  125. data: result
  126. }
  127. )
  128. // 初始化是否显示解析
  129. questionList.forEach((question: any, index: number) => {
  130. // 格式化所有题目的答案
  131. if (index === state.currentIndex) {
  132. state.answerAnalysis = question.answerAnalysis
  133. question.showAnalysis = true
  134. question.analysis.userResult = data
  135. }
  136. })
  137. // 判断是否是最后一题
  138. if (state.questionList.length === state.currentIndex + 1) {
  139. state.visiableSure = true
  140. return
  141. }
  142. if (data) {
  143. swipeRef.value?.next()
  144. } else {
  145. state.visiableError = true
  146. }
  147. } catch {
  148. //
  149. }
  150. }
  151. //
  152. const getAnswerResult = computed(() => {
  153. const questionList = state.questionList || []
  154. let count = 0
  155. let passCount = 0
  156. let noPassCount = 0
  157. questionList.forEach((item: any) => {
  158. if (item.showAnalysis) {
  159. count += 1
  160. if (item.analysis.userResult) {
  161. passCount += 1
  162. } else {
  163. noPassCount += 1
  164. }
  165. }
  166. })
  167. return {
  168. count,
  169. passCount,
  170. noPassCount
  171. }
  172. })
  173. /**
  174. * @description 重置当前的题目高度
  175. * @param {any} scroll 是否滚动到顶部
  176. */
  177. const resizeSwipeItemHeight = (scroll = true) => {
  178. nextTick(() => {
  179. scroll && window.scrollTo(0, 0)
  180. setTimeout(() => {
  181. // const currentItemDom: Element =
  182. // document.querySelectorAll('.swipe-item-question')[state.currentIndex]
  183. // console.log(document.querySelectorAll('.van-swipe-item'), state.currentIndex)
  184. const currentItemDom: any = document
  185. .querySelectorAll('.van-swipe-item')
  186. [state.currentIndex].querySelector('.swipe-item-question')
  187. const rect = useRect(currentItemDom)
  188. state.swipeHeight = rect.height
  189. }, 100)
  190. })
  191. }
  192. const onConfirmExam = () => {
  193. const answerResult = getAnswerResult.value
  194. let rate = 0
  195. if (answerResult.count > 0) {
  196. rate = Math.floor((answerResult.passCount / answerResult.count) * 100)
  197. }
  198. const times = counter.value
  199. const minute =
  200. Math.floor(times / 60) >= 10 ? Math.floor(times / 60) : '0' + Math.floor(times / 60)
  201. const seconds = times % 60 >= 10 ? times % 60 : '0' + (times % 60)
  202. state.overResult = {
  203. time: minute + ':' + seconds, // 时长
  204. questionLength: answerResult.count, // 答题数
  205. errorLength: answerResult.noPassCount, // 错题数
  206. rate // 正确率
  207. }
  208. // 重置计时
  209. pause()
  210. counter.value = 0
  211. state.visiableResult = true
  212. }
  213. // 重新练习
  214. const onCloseResult = async () => {
  215. state.questionList = []
  216. await getExamDetails()
  217. setTimeout(async () => {
  218. swipeRef.value?.swipeTo(0, {
  219. immediate: true
  220. })
  221. state.swipeHeight = 'auto'
  222. state.answerAnalysis = ''
  223. state.overResult = {
  224. time: '00:00', // 时长
  225. questionLength: 0, // 答题数
  226. errorLength: 0, // 错题数
  227. rate: 0 // 正确率
  228. }
  229. state.visiableResult = false
  230. // 恢复计时
  231. resume()
  232. resizeSwipeItemHeight()
  233. }, 100)
  234. }
  235. // 下一个考点
  236. const onConfirmResult = () => {
  237. const knowledgelist = state.knowledgelist || []
  238. console.log('🚀 ~ file: index.tsx:246 ~ onConfirmResult ~ knowledgelist', knowledgelist)
  239. // 当前正在考试的节点
  240. const knownleIndex = knowledgelist.findIndex((item: any) => item.id === state.id)
  241. console.log('🚀 ~ file: index.tsx:249 ~ onConfirmResult ~ knownleIndex', knownleIndex)
  242. let currentKnowle: any = {}
  243. if (knownleIndex + 1 >= knowledgelist.length || knownleIndex < 0) {
  244. currentKnowle = knowledgelist[0]
  245. } else {
  246. currentKnowle = knowledgelist[knownleIndex + 1]
  247. }
  248. state.id = currentKnowle.id
  249. state.visiableResult = false
  250. state.currentIndex = 0
  251. // 重置
  252. onCloseResult()
  253. }
  254. // 拦截
  255. const onBack = () => {
  256. // showDialog({
  257. // title: '提示',
  258. // message: '您是否退出?',
  259. // theme: 'round-button',
  260. // confirmButtonColor: '#ff8057'
  261. // }).then(() => {
  262. // onAfter()
  263. // })
  264. state.quitStatus = true
  265. }
  266. const onAfter = () => {
  267. window.removeEventListener('popstate', onBack, false)
  268. router.back()
  269. }
  270. onMounted(async () => {
  271. await getExamDetails()
  272. await getDetails()
  273. resizeSwipeItemHeight()
  274. window.history.pushState(null, '', document.URL)
  275. window.addEventListener('popstate', onBack, false)
  276. })
  277. return () => (
  278. <div class={styles.unitDetail}>
  279. <OSticky position="top">
  280. <OHeader
  281. v-slots={{
  282. right: () => (
  283. <span
  284. style="color: var(--van-primary-color)"
  285. onClick={() => (state.visiableSure = true)}
  286. >
  287. 结束练习
  288. </span>
  289. )
  290. }}
  291. />
  292. </OSticky>
  293. <Cell center class={styles.unitSection} border={false}>
  294. {{
  295. title: () => <div class={[styles.unitTitle]}>{state.name}</div>,
  296. value: () => (
  297. <div class={styles.unitCount}>
  298. <div class={styles.countSection}>
  299. <span class={styles.nums}>{getAnswerResult.value.passCount}</span>
  300. <span>答对</span>
  301. </div>
  302. <div class={styles.countSection}>
  303. <span class={styles.nums} style={{ color: '#F44541' }}>
  304. {getAnswerResult.value.noPassCount}
  305. </span>
  306. <span>答错</span>
  307. </div>
  308. </div>
  309. )
  310. }}
  311. </Cell>
  312. <Swipe
  313. loop={false}
  314. showIndicators={false}
  315. ref={swipeRef}
  316. duration={300}
  317. touchable={false}
  318. style={{ paddingBottom: '12px' }}
  319. lazyRender
  320. height={state.swipeHeight}
  321. onChange={(index: number) => {
  322. state.currentIndex = index
  323. resizeSwipeItemHeight()
  324. }}
  325. >
  326. {state.questionList.map((item: any, index: number) => (
  327. <SwipeItem>
  328. <div class="swipe-item-question">
  329. {item.questionTypeCode === QuestionType.RADIO && (
  330. <ChoiceQuestion
  331. v-model:value={item.userAnswer}
  332. index={index + 1}
  333. data={item}
  334. type="radio"
  335. showAnalysis={item.showAnalysis}
  336. analysis={item.analysis}
  337. />
  338. )}
  339. {item.questionTypeCode === QuestionType.CHECKBOX && (
  340. <ChoiceQuestion
  341. v-model:value={item.userAnswer}
  342. index={index + 1}
  343. data={item}
  344. type="checkbox"
  345. showAnalysis={item.showAnalysis}
  346. analysis={item.analysis}
  347. />
  348. )}
  349. {item.questionTypeCode === QuestionType.SORT && (
  350. <DragQuestion
  351. v-model:value={item.userAnswer}
  352. onUpdate:value={() => {
  353. resizeSwipeItemHeight(false)
  354. }}
  355. data={item}
  356. index={index + 1}
  357. showAnalysis={item.showAnalysis}
  358. analysis={item.analysis}
  359. />
  360. )}
  361. {item.questionTypeCode === QuestionType.LINK && (
  362. <KeepLookQuestion
  363. v-model:value={item.userAnswer}
  364. data={item}
  365. index={index + 1}
  366. showAnalysis={item.showAnalysis}
  367. analysis={item.analysis}
  368. />
  369. )}
  370. {item.questionTypeCode === QuestionType.PLAY && (
  371. <PlayQuestion
  372. v-model:value={item.userAnswer}
  373. data={item}
  374. index={index + 1}
  375. unitId={state.id as any}
  376. showAnalysis={item.showAnalysis}
  377. analysis={item.analysis}
  378. />
  379. )}
  380. </div>
  381. </SwipeItem>
  382. ))}
  383. </Swipe>
  384. <OSticky position="bottom" background="white">
  385. <div class={['btnGroup btnMore']}>
  386. {state.currentIndex > 0 && (
  387. <Button
  388. round
  389. block
  390. type="primary"
  391. plain
  392. onClick={() => {
  393. swipeRef.value?.prev()
  394. }}
  395. >
  396. 上一题
  397. </Button>
  398. )}
  399. <Button
  400. block
  401. round
  402. type="primary"
  403. onClick={onNextQuestion}
  404. loading={state.nextStatus}
  405. disabled={state.nextStatus}
  406. >
  407. 提交
  408. </Button>
  409. <Image
  410. src={iconButtonList}
  411. class={[styles.wapList, 'van-haptics-feedback']}
  412. onClick={() => (state.visiableAnswer = true)}
  413. />
  414. </div>
  415. </OSticky>
  416. {/* 题目集合 */}
  417. <ActionSheet v-model:show={state.visiableAnswer} title="题目列表" safeAreaInsetBottom>
  418. <AnswerList
  419. value={state.questionList}
  420. lookType={'PRACTICE'}
  421. statusList={[
  422. {
  423. text: '答对',
  424. color: '#71B0FF'
  425. },
  426. {
  427. text: '答错',
  428. color: '#FF8486'
  429. }
  430. ]}
  431. onSelect={(item: any) => {
  432. // 跳转,并且跳过动画
  433. swipeRef.value?.swipeTo(item, {
  434. immediate: true
  435. })
  436. state.visiableAnswer = false
  437. }}
  438. />
  439. </ActionSheet>
  440. <Popup
  441. v-model:show={state.visiableError}
  442. style={{ width: '90%' }}
  443. round
  444. closeOnClickOverlay={false}
  445. >
  446. <ErrorMode
  447. onClose={() => (state.visiableError = false)}
  448. answerAnalysis={state.answerAnalysis}
  449. onConform={() => {
  450. swipeRef.value?.next()
  451. state.answerAnalysis = ''
  452. }}
  453. />
  454. </Popup>
  455. <Popup
  456. v-model:show={state.visiableResult}
  457. closeOnClickOverlay={false}
  458. style={{ background: 'transparent', width: '96%' }}
  459. >
  460. <ResultFinish
  461. status="PRACTICE"
  462. confirmButtonText="下一个考点"
  463. cancelButtonText="继续练习本考点"
  464. onClose={onCloseResult}
  465. onConform={onConfirmResult}
  466. v-slots={{
  467. content: () => (
  468. <div class={styles.practiceResult}>
  469. <div class={styles.practiceTitle}>本次练习正确率</div>
  470. <div class={styles.practiceRate}>{state.overResult.rate}%</div>
  471. <Grid border={false} columnNum={3}>
  472. <GridItem>
  473. <p class={styles.title}>{state.overResult.time}</p>
  474. <p class={styles.name}>练习时长</p>
  475. </GridItem>
  476. <GridItem>
  477. <p class={[styles.title]}>{state.overResult.questionLength | 0}</p>
  478. <p class={styles.name}>答题数</p>
  479. </GridItem>
  480. <GridItem>
  481. <p class={styles.title}>{state.overResult.errorLength | 0}</p>
  482. <p class={styles.name}>错题数</p>
  483. </GridItem>
  484. </Grid>
  485. <div class={styles.practiceTips}>
  486. 继续努力!
  487. <br />
  488. 争取在测验中获得高分!
  489. </div>
  490. </div>
  491. )
  492. }}
  493. />
  494. </Popup>
  495. <ODialog
  496. v-model:show={state.visiableSure}
  497. title="练习完成"
  498. message="确认本次练习的题目都完成了吗?"
  499. messageAlign="left"
  500. showCancelButton
  501. cancelButtonText="再等等"
  502. confirmButtonText="确认完成"
  503. onConfirm={onConfirmExam}
  504. />
  505. <ODialog
  506. v-model:show={state.quitStatus}
  507. title="提示"
  508. message="您是否退出本次练习?"
  509. confirmButtonText="确认完成"
  510. onConfirm={onAfter}
  511. />
  512. </div>
  513. )
  514. }
  515. })