index.tsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. import { ActionSheet, Button, Cell, Icon, Image, Swipe, SwipeItem } from 'vant'
  2. import { defineComponent, onMounted, reactive, ref, nextTick } from 'vue'
  3. import { useRoute, useRouter } from 'vue-router'
  4. import styles from './index.module.less'
  5. import iconQuestionNums from '../images/icon-question-nums.png'
  6. import iconButtonList from '../images/icon-button-list.png'
  7. import OSticky from '@/components/o-sticky'
  8. import ChoiceQuestion from '../model/choice-question'
  9. import AnswerList from '../model/answer-list'
  10. import DragQuestion from '../model/drag-question'
  11. import KeepLookQuestion from '../model/keep-look-question'
  12. import PlayQuestion from '../model/play-question'
  13. import request from '@/helpers/request'
  14. import { QuestionType } from '../unit'
  15. import { useRect } from '@vant/use'
  16. import { state as baseState } from '@/state'
  17. export default defineComponent({
  18. name: 'unit-detail',
  19. setup() {
  20. const route = useRoute()
  21. const router = useRouter()
  22. const swipeRef = ref()
  23. const state = reactive({
  24. id: route.query.id,
  25. examDetail: {} as any,
  26. visiableAnswer: false,
  27. currentIndex: 0,
  28. questionList: [],
  29. time: 0,
  30. resultInfo: {} as any,
  31. answerResult: [] as any,
  32. nextStatus: false,
  33. swipeHeight: 'auto' as any
  34. })
  35. // 学生端查看详情
  36. const getExamDetails = async () => {
  37. try {
  38. const { data } = await request.post('/api-student/studentUnitExamination/detail', {
  39. requestType: 'form',
  40. data: {
  41. studentUnitExaminationId: state.id
  42. }
  43. })
  44. const { questionJson, studentAnswerJson, answerResult, ...res } = data
  45. const temp = questionJson || []
  46. temp.forEach((item: any) => {
  47. item.userAnswer = formatUserAnswers(item, studentAnswerJson)
  48. item.showAnalysis = true
  49. item.analysis = {
  50. message: item.answerAnalysis,
  51. topic: true, // 是否显示结果
  52. userResult: formatUserResult(item.id) // 用户答题对错
  53. }
  54. })
  55. // 问题列表
  56. state.questionList = temp
  57. // 正确答案
  58. state.answerResult = answerResult ? JSON.parse(answerResult) : []
  59. // 详情
  60. state.examDetail = { ...res } || {}
  61. } catch {
  62. //
  63. }
  64. }
  65. const getExamTeaherDetails = async () => {
  66. try {
  67. const { data } = await request.post(
  68. baseState.platformApi + '/classGroupUnitExamination/report',
  69. {
  70. requestType: 'form',
  71. data: {
  72. classGroupUnitExaminationId: state.id,
  73. level: route.query.level
  74. }
  75. }
  76. )
  77. console.log(data)
  78. // const { questionJson, studentAnswerJson, answerResult, ...res } = data
  79. state.examDetail = {
  80. unitExaminationName: data.unitExaminationName,
  81. questionNum: data.questionNum || 0
  82. }
  83. // 问题列表
  84. const temp = data.examinationQuestionAdds || []
  85. temp.forEach((item: any) => {
  86. item.userAnswer = formatTeacherAnswer(item.answers)
  87. item.showAnalysis = true
  88. item.showRate = true
  89. item.analysis = {
  90. message: item.answerAnalysis,
  91. topic: false // 是否显示结果
  92. }
  93. })
  94. // 问题列表
  95. state.questionList = temp
  96. // 正确答案
  97. } catch {
  98. //
  99. }
  100. }
  101. /**
  102. * @description 初始化正确答案
  103. */
  104. const formatTeacherAnswer = (answers: any) => {
  105. console.log(answers)
  106. const result: any = []
  107. answers.forEach((answer: any) => {
  108. // rightAnswerFlag 说明是正确的
  109. if (answer.rightAnswerFlag) {
  110. const rightOption = answers.find(
  111. (item: any) => item.questionExtra === answer.questionExtra
  112. )
  113. result.push({
  114. answer: answer.questionAnswer,
  115. answerId: answer.examinationQuestionAnswerId,
  116. answerExtra: rightOption ? rightOption.questionExtra : null
  117. })
  118. }
  119. })
  120. return result || []
  121. }
  122. /**
  123. * @description 初始化用户答案
  124. */
  125. const formatUserAnswers = (item: any, userAnswer: any) => {
  126. // 判断是否有结果
  127. if (!userAnswer) return []
  128. const answers = userAnswer || []
  129. return answers[item.id] ? answers[item.id] : []
  130. }
  131. /**
  132. * @description 检查用户是否答对
  133. * @returns Boolean
  134. */
  135. const formatUserResult = (id: string) => {
  136. let result = false
  137. state.answerResult.forEach((item: any) => {
  138. if (item.questionId === id) {
  139. result = item.rightFlag
  140. }
  141. })
  142. return result
  143. }
  144. /**
  145. * @description 重置当前的题目高度
  146. */
  147. const resizeSwipeItemHeight = () => {
  148. nextTick(() => {
  149. window.scrollTo(0, 0)
  150. setTimeout(() => {
  151. const currentItemDom: Element =
  152. document.querySelectorAll('.swipe-item-question')[state.currentIndex]
  153. const rect = useRect(currentItemDom)
  154. state.swipeHeight = rect.height
  155. }, 100)
  156. })
  157. }
  158. /**
  159. * @description 下一题 | 测试完成
  160. */
  161. const onNextQuestion = async () => {
  162. try {
  163. state.nextStatus = true
  164. if (state.questionList.length === state.currentIndex + 1) {
  165. router.back()
  166. }
  167. swipeRef.value?.next()
  168. state.nextStatus = false
  169. } catch {
  170. //
  171. state.nextStatus = false
  172. }
  173. }
  174. onMounted(async () => {
  175. if (baseState.platformType === 'TEACHER' || baseState.platformType === 'SCHOOL') {
  176. await getExamTeaherDetails()
  177. } else {
  178. await getExamDetails()
  179. }
  180. // 初始化高度
  181. resizeSwipeItemHeight()
  182. })
  183. return () => (
  184. <div class={styles.unitDetail}>
  185. <Cell center class={styles.unitSection} border={false}>
  186. {{
  187. title: () => <div class={styles.unitTitle}>{state.examDetail.unitExaminationName}</div>,
  188. label: () => (
  189. <div class={styles.unitCount}>
  190. <div class={styles.qNums}>
  191. <Icon class={styles.icon} name={iconQuestionNums} />
  192. 题目数量{' '}
  193. <span class={styles.num} style={{ paddingLeft: '6px' }}>
  194. {state.currentIndex + 1}
  195. </span>
  196. /{state.examDetail.questionNum || 0}
  197. </div>
  198. </div>
  199. )
  200. }}
  201. </Cell>
  202. <Swipe
  203. loop={false}
  204. showIndicators={false}
  205. ref={swipeRef}
  206. duration={300}
  207. touchable={false}
  208. lazyRender
  209. style={{ paddingBottom: '12px' }}
  210. height={state.swipeHeight}
  211. onChange={(index: number) => {
  212. state.currentIndex = index
  213. resizeSwipeItemHeight()
  214. }}
  215. >
  216. {state.questionList.map((item: any, index: number) => (
  217. // item.questionTypeCode === QuestionType.CHECKBOX && (
  218. // <SwipeItem>
  219. // <ChoiceQuestion
  220. // v-model:value={item.userAnswer}
  221. // index={index + 1}
  222. // data={item}
  223. // readOnly
  224. // type="checkbox"
  225. // showRate={item.showRate}
  226. // showAnalysis={item.showAnalysis}
  227. // analysis={item.analysis}
  228. // />
  229. // </SwipeItem>
  230. // )
  231. <SwipeItem>
  232. <div class="swipe-item-question">
  233. {item.questionTypeCode === QuestionType.RADIO && (
  234. <ChoiceQuestion
  235. v-model:value={item.userAnswer}
  236. index={index + 1}
  237. data={item}
  238. readOnly
  239. type="radio"
  240. showRate={item.showRate}
  241. showAnalysis={item.showAnalysis}
  242. analysis={item.analysis}
  243. />
  244. )}
  245. {item.questionTypeCode === QuestionType.CHECKBOX && (
  246. <ChoiceQuestion
  247. v-model:value={item.userAnswer}
  248. index={index + 1}
  249. data={item}
  250. readOnly
  251. type="checkbox"
  252. showRate={item.showRate}
  253. showAnalysis={item.showAnalysis}
  254. analysis={item.analysis}
  255. />
  256. )}
  257. {item.questionTypeCode === QuestionType.SORT && (
  258. <DragQuestion
  259. readOnly
  260. v-model:value={item.userAnswer}
  261. data={item}
  262. index={index + 1}
  263. showRate={item.showRate}
  264. showAnalysis={item.showAnalysis}
  265. analysis={item.analysis}
  266. />
  267. )}
  268. {item.questionTypeCode === QuestionType.LINK && (
  269. <KeepLookQuestion
  270. readOnly
  271. v-model:value={item.userAnswer}
  272. data={item}
  273. index={index + 1}
  274. showRate={item.showRate}
  275. showAnalysis={item.showAnalysis}
  276. analysis={item.analysis}
  277. />
  278. )}
  279. {item.questionTypeCode === QuestionType.PLAY && (
  280. <PlayQuestion
  281. readOnly
  282. v-model:value={item.userAnswer}
  283. data={item}
  284. index={index + 1}
  285. unitId={state.id as any}
  286. showRate={item.showRate}
  287. showAnalysis={item.showAnalysis}
  288. analysis={item.analysis}
  289. />
  290. )}
  291. </div>
  292. </SwipeItem>
  293. ))}
  294. </Swipe>
  295. <OSticky position="bottom" background="white">
  296. <div class={['btnGroup btnMore']}>
  297. {state.currentIndex > 0 && (
  298. <Button
  299. round
  300. block
  301. type="primary"
  302. plain
  303. onClick={() => {
  304. swipeRef.value?.prev()
  305. }}
  306. >
  307. 上一题
  308. </Button>
  309. )}
  310. <Button
  311. block
  312. round
  313. type="primary"
  314. onClick={onNextQuestion}
  315. loading={state.nextStatus}
  316. disabled={state.nextStatus}
  317. >
  318. {state.questionList.length === state.currentIndex + 1 ? '确定' : '下一题'}
  319. </Button>
  320. <Image
  321. src={iconButtonList}
  322. class={[styles.wapList, 'van-haptics-feedback']}
  323. onClick={() => (state.visiableAnswer = true)}
  324. />
  325. </div>
  326. </OSticky>
  327. {/* 题目集合 */}
  328. <ActionSheet v-model:show={state.visiableAnswer} title="题目列表" safeAreaInsetBottom>
  329. <AnswerList
  330. value={state.questionList}
  331. answerResult={state.answerResult}
  332. index={state.currentIndex}
  333. lookType={baseState.platformType === 'STUDENT' ? 'RESULT' : 'CLICK'}
  334. statusList={
  335. baseState.platformType === 'STUDENT'
  336. ? [
  337. {
  338. text: '答对',
  339. color: '#71B0FF'
  340. },
  341. {
  342. text: '答错',
  343. color: '#FF8486'
  344. }
  345. ]
  346. : []
  347. }
  348. onSelect={(item: any) => {
  349. // 跳转,并且跳过动画
  350. swipeRef.value?.swipeTo(item, {
  351. immediate: true
  352. })
  353. state.visiableAnswer = false
  354. }}
  355. />
  356. </ActionSheet>
  357. </div>
  358. )
  359. }
  360. })