TopicExam.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  1. <template>
  2. <Container
  3. :scrollX="true"
  4. :scrollY="true"
  5. :scroll-into-view="`item-${nowIndex}`"
  6. :title="title"
  7. @onSafeAreaChange="onSafeAreaChange"
  8. :onBack="onBack"
  9. v-bind="$attrs"
  10. v-show="!open && !showSheet"
  11. >
  12. <view
  13. :id="`item-${nowIndex}`"
  14. class="topic-item"
  15. :style="{
  16. width: `${safeArea.width}px`,
  17. height: `${safeArea.height}px`,
  18. flexShrink: 0, // 解决宽度无效问题
  19. }"
  20. >
  21. <!-- 头部 防止定时器不准bug -->
  22. <view class="topic-header">
  23. <view class="topic-header-left">
  24. <view class="topic-type" @click="openSheet">
  25. {{ styleMap[data[nowIndex]?.style] }}题
  26. </view>
  27. <view class="topic-count">
  28. 第{{ nowIndex + 1 }}题/共{{ total }}题
  29. </view>
  30. </view>
  31. <uni-countdown
  32. :show-day="false"
  33. :hour="formatTime(Time.time.startTime, 'hour')"
  34. :minute="formatTime(Time.time.startTime, 'minute')"
  35. :second="formatTime(Time.time.startTime, 'second')"
  36. />
  37. </view>
  38. <template v-for="(item, parindex) in data" :key="parindex">
  39. <template v-if="parindex === nowIndex">
  40. <!-- 问题内容 -->
  41. <view class="topic-content">
  42. <uvParse
  43. v-if="item.isImage"
  44. :content="item.title"
  45. class="question-text"
  46. ></uvParse>
  47. <rich-text
  48. v-else
  49. :nodes="item.title"
  50. class="question-text"
  51. ></rich-text>
  52. <uvParse
  53. :content="item.questions_ex"
  54. v-if="item.style === 6 && item.isImage"
  55. class="question-text"
  56. ></uvParse>
  57. <rich-text
  58. v-else
  59. :nodes="item.questions_ex"
  60. class="question-text"
  61. ></rich-text>
  62. <questions
  63. v-if="item.style !== 6"
  64. v-for="(question, index) in item.questions"
  65. :key="index"
  66. :answerList="item.ansList"
  67. :index="index"
  68. :styleCount="item.style"
  69. :question="question"
  70. :showResult="item.showResult"
  71. :parindex="parindex"
  72. @select="handleSelect"
  73. />
  74. <view class="other" v-else>
  75. <questions
  76. v-for="(question, index) in item.questions"
  77. :key="index"
  78. :answerList="item.ansList"
  79. :index="index"
  80. :styleCount="item.style"
  81. :question="question"
  82. :showResult="item.showResult"
  83. :parindex="parindex"
  84. @select="handleSelect"
  85. />
  86. </view>
  87. </view>
  88. <!-- 答案展示 -->
  89. <view v-if="item.showResult" class="answer-section">
  90. <view class="answer-content">
  91. <view class="answer-row">
  92. <view class="answer-item border-r-primary">
  93. 正确答案:
  94. <text class="answer-text">{{
  95. item.ansList.map((i) => i.label).join("")
  96. }}</text>
  97. </view>
  98. <view class="answer-item">
  99. 我的答案:
  100. <text class="answer-text">{{ item.selectAns.join("") }}</text>
  101. </view>
  102. <view
  103. class="tip"
  104. :style="{
  105. color: item.isRight ? '#00be00' : '#f00',
  106. }"
  107. >{{ item.isRight ? "太棒了~" : "再接再励!" }}</view
  108. >
  109. </view>
  110. <view class="parsing">
  111. <p>解析:</p>
  112. <scroll-view
  113. :show-scrollbar="false"
  114. scroll-y
  115. class="parsing-text"
  116. >
  117. <rich-text :nodes="item.explain"></rich-text>
  118. </scroll-view>
  119. </view>
  120. </view>
  121. </view>
  122. <!-- 底部按钮 -->
  123. <view class="button-group">
  124. <view
  125. v-if="parindex >= 1"
  126. class="prev-btn button"
  127. @tap="handlePage(item, parindex, 'prevPage')"
  128. >
  129. 上一题
  130. </view>
  131. <view
  132. v-if="parindex + 1 < total"
  133. class="next-btn button"
  134. @tap="handlePage(item, parindex, 'nextPage')"
  135. >
  136. 下一题
  137. </view>
  138. <view class="button" v-if="parindex + 1 === total" @click="examEnd"
  139. >交卷</view
  140. >
  141. </view>
  142. </template>
  143. </template>
  144. </view>
  145. </Container>
  146. <uni-popup
  147. ref="bottomModal"
  148. @change="(e) => (showSheet = e.show)"
  149. type="bottom"
  150. backgroundColor="#fff"
  151. borderRadius="10px 10px 0 0"
  152. class="bottom-modal"
  153. >
  154. <view class="bottom-modal" v-if="showSheet">
  155. <view class="title">
  156. <view>答题卡</view>
  157. <uni-icons
  158. class="closeempty"
  159. type="closeempty"
  160. @click="bottomModal?.close"
  161. ></uni-icons>
  162. </view>
  163. <view class="content">
  164. <scroll-view class="scroll-view" scroll-y>
  165. <view v-for="(key, index) in styleMap" class="ques-type" :key="key">
  166. <view>{{ key }}题</view>
  167. <view class="grid">
  168. <view
  169. v-for="item in data.filter((i) => i.style === +index)"
  170. :key="item.id"
  171. class="item"
  172. :class="{
  173. success: !!item.selectAns.length,
  174. }"
  175. @click="
  176. () => {
  177. onBeforePageChange(item.ind);
  178. bottomModal?.close();
  179. }
  180. "
  181. >
  182. {{ item.ind + 1 }}
  183. </view>
  184. </view>
  185. </view>
  186. </scroll-view>
  187. <button @click="submit">交卷</button>
  188. </view>
  189. </view>
  190. </uni-popup>
  191. <Modal
  192. v-model:open="open"
  193. title="温馨提示"
  194. :submitter="submitter"
  195. :onClose="onClose"
  196. :onSubmit="onSubmit"
  197. >
  198. <view :style="{ margin: '10px 0' }">{{ submitter.context }}</view>
  199. </Modal>
  200. </template>
  201. <script setup>
  202. // 答题组件
  203. import { ref, watchEffect } from "vue";
  204. import Questions from "./Questions.vue";
  205. import Container from "../Container/Container.vue";
  206. import Modal from "../Modal/Modal.vue";
  207. import uvParse from "@/uni_modules/uv-parse/components/uv-parse/uv-parse.vue";
  208. import { useTimeStore } from "@/store/time";
  209. import { request } from "../../utils/request";
  210. import { getRoute } from "../../utils/router";
  211. const styleMap = {
  212. 2: "单选",
  213. 3: "多选",
  214. 6: "配伍",
  215. };
  216. const safeArea = ref({}); // 安全区域
  217. const data = ref([]); // 题目数据
  218. const open = ref(false); // 是否显示弹窗
  219. const showSheet = ref(false); // 是否显示底部弹窗
  220. let r = null; // 异步方法
  221. const Time = useTimeStore();
  222. const bottomModal = ref(null); // 底部弹窗
  223. const openSheet = () => {
  224. bottomModal.value.open();
  225. };
  226. const submit = () => {
  227. const len = data.value.filter((item) => !item.selectAns.length).length;
  228. if (len) {
  229. bottomModal.value.close();
  230. submitter.value = {
  231. ...submitter.value,
  232. closeText: "提交试卷",
  233. context: `您还有${len}道题没做,确认交卷吗?`,
  234. };
  235. open.value = true;
  236. }
  237. };
  238. const formatTime = (time, type) => {
  239. // 获取time一个半小时后的时间戳
  240. const timeStamp = new Date(time + 1.5 * 60 * 60 * 1000);
  241. const date = new Date();
  242. switch (type) {
  243. case "hour":
  244. return timeStamp.getHours() - date.getHours();
  245. case "minute":
  246. return timeStamp.getMinutes() - date.getMinutes();
  247. case "second":
  248. return timeStamp.getSeconds() - date.getSeconds();
  249. }
  250. };
  251. const submitter = ref({
  252. text: "继续考试",
  253. closeText: "直接退出",
  254. context: "确定退出考试吗?退出后将不保留本次考试记录,确定要放弃吗?",
  255. });
  256. // Props 定义
  257. const props = defineProps({
  258. topics: {
  259. type: Array,
  260. default: () => [],
  261. },
  262. onStar: {
  263. type: Function,
  264. default: null,
  265. },
  266. isEnd: {
  267. type: Boolean,
  268. default: false,
  269. },
  270. total: {
  271. type: Number,
  272. default: 0,
  273. },
  274. title: String,
  275. real_topic_id: Number,
  276. });
  277. const onBeforePageChange = (index) => {
  278. // 保存用户答题
  279. const item = data.value[nowIndex.value];
  280. if (!item.selectAns.length) {
  281. nowIndex.value = index;
  282. return;
  283. }
  284. request(
  285. "api/question_bank/question_reception/real_topic/save_user_real_topic",
  286. {
  287. user_real_paper_id: props.real_topic_id,
  288. catalogue_id: getRoute().params.id,
  289. real_topic_id: item.id,
  290. is_correct: item.isRight ? 1 : 0,
  291. answer: item.selectAns.join(","),
  292. },
  293. "post"
  294. ).finally(() => {
  295. nowIndex.value = index;
  296. });
  297. };
  298. watchEffect(() => {
  299. data.value = props.topics;
  300. });
  301. // Emits 定义
  302. const emit = defineEmits([
  303. "prevPage",
  304. "nextPage",
  305. "answerChange",
  306. "lookReport",
  307. ]);
  308. const onBack = () =>
  309. new Promise((resolve) => {
  310. if (!data.value.length || props.isEnd) {
  311. resolve(true);
  312. return;
  313. }
  314. open.value = true;
  315. r = resolve;
  316. });
  317. const ansIsRight = (selectAns, rightAns) => {
  318. if (selectAns.length !== rightAns.length) return false;
  319. return isRight(selectAns, rightAns);
  320. };
  321. const examEnd = () => {
  322. data.value = data.value.map((item) => {
  323. return {
  324. ...item,
  325. showResult: true,
  326. isRight: ansIsRight(
  327. item.selectAns,
  328. item.ansList.map((q) => q.label)
  329. ),
  330. };
  331. });
  332. emit("lookReport", data.value, submitter.value);
  333. };
  334. const onClose = () => {
  335. if (submitter.value.closeText !== "提交试卷") return r(true);
  336. examEnd();
  337. };
  338. const onSubmit = () =>
  339. new Promise((res) => {
  340. res(true);
  341. });
  342. // 响应式数据
  343. const nowIndex = ref(0);
  344. // 判断答案是否正确
  345. const isRight = (selectAns, rightAns) => {
  346. if (!selectAns.length) return;
  347. return selectAns.every((item) => rightAns.includes(item));
  348. };
  349. const handleSelect = ({ pid, checked, index, style }) => {
  350. // 如果不是多选,就取消其他的选项
  351. if (style !== 3) {
  352. data.value[pid].questions = data.value[pid].questions.map((q) => ({
  353. ...q,
  354. checked: false,
  355. }));
  356. data.value[pid].selectAns = [];
  357. }
  358. // 更新选项
  359. const item = data.value[pid].questions[index];
  360. data.value[pid].questions[index].checked = !checked;
  361. data.value[pid].questions[index].isRight = isRight(
  362. [item.value],
  363. data.value[pid].ansList.map((q) => q.label)
  364. );
  365. // 更新答案
  366. data.value[pid].selectAns = data.value[pid].questions
  367. .filter((q) => q.checked)
  368. .map((q) => q.value);
  369. };
  370. const handlePage = (item, index, type) => {
  371. onBeforePageChange(index + (type === "prevPage" ? -1 : 1));
  372. emit(type, { item, index });
  373. };
  374. const onSafeAreaChange = (s) => {
  375. safeArea.value = s;
  376. };
  377. </script>
  378. <style lang="scss" scoped>
  379. @import "@/uni.scss";
  380. .other {
  381. display: flex;
  382. gap: 12px;
  383. }
  384. .scroll-view {
  385. height: 50vh;
  386. }
  387. .content {
  388. font-family: PingFang SC, PingFang SC;
  389. font-weight: 500;
  390. font-size: 28rpx;
  391. color: #333333;
  392. display: flex;
  393. flex-direction: column;
  394. gap: 24rpx;
  395. padding: 0 30rpx;
  396. .ques-type {
  397. display: flex;
  398. gap: 16rpx;
  399. flex-direction: column;
  400. }
  401. }
  402. .grid {
  403. display: grid;
  404. grid-template-columns: repeat(5, 1fr);
  405. gap: 12px;
  406. }
  407. .bottom-modal {
  408. display: flex;
  409. flex-direction: column;
  410. gap: 20rpx;
  411. padding: 20rpx;
  412. }
  413. .title {
  414. display: flex;
  415. justify-content: center;
  416. align-items: center;
  417. position: relative;
  418. .closeempty {
  419. position: absolute;
  420. right: 0;
  421. }
  422. }
  423. .parsing-text {
  424. height: 381rpx;
  425. white-space: normal;
  426. }
  427. .answer-content {
  428. display: flex;
  429. flex-direction: column;
  430. gap: 45rpx;
  431. font-family: PingFang SC, PingFang SC;
  432. font-weight: 500;
  433. font-size: 32rpx;
  434. color: #666666;
  435. }
  436. .topic-container {
  437. width: 100vw;
  438. overflow: hidden;
  439. position: relative;
  440. }
  441. .topic-item {
  442. display: flex;
  443. flex-direction: column;
  444. gap: 12px;
  445. position: relative;
  446. box-sizing: border-box;
  447. }
  448. .topic-header {
  449. display: flex;
  450. align-items: center;
  451. justify-content: space-between;
  452. }
  453. .topic-header-left {
  454. display: flex;
  455. gap: 8px;
  456. align-items: center;
  457. }
  458. .item {
  459. width: 72rpx;
  460. height: 72rpx;
  461. border-radius: 50%;
  462. display: flex;
  463. align-items: center;
  464. justify-content: center;
  465. border: 1rpx solid #dddddd;
  466. &.success {
  467. border-color: $primary;
  468. color: $primary;
  469. }
  470. }
  471. .topic-type {
  472. border: 1px solid $uni-primary;
  473. padding: 0 8px;
  474. border-radius: 6px;
  475. color: $uni-primary;
  476. font-weight: 600;
  477. font-size: 14px;
  478. }
  479. .topic-count {
  480. color: #333;
  481. font-size: 14px;
  482. }
  483. .topic-content {
  484. display: flex;
  485. flex-direction: column;
  486. gap: 8px;
  487. }
  488. .question-text {
  489. font-weight: bold;
  490. font-size: 14px;
  491. white-space: normal;
  492. }
  493. .answer-section {
  494. border-radius: 16rpx;
  495. border: 1px solid #ddd;
  496. padding: 24rpx;
  497. display: flex;
  498. flex-direction: column;
  499. gap: 32rpx;
  500. }
  501. .answer-row {
  502. font-size: 14px;
  503. display: flex;
  504. align-items: center;
  505. gap: 12px;
  506. }
  507. .answer-item {
  508. display: flex;
  509. gap: 8px;
  510. align-items: center;
  511. padding-right: 12px;
  512. }
  513. .tip {
  514. margin-left: auto;
  515. }
  516. .border-r-primary {
  517. border-right: 2px solid $uni-primary;
  518. }
  519. .answer-text {
  520. color: $uni-primary;
  521. }
  522. .button-group {
  523. display: flex;
  524. gap: 8px;
  525. position: sticky;
  526. bottom: 0;
  527. margin-top: auto;
  528. }
  529. .prev-btn {
  530. flex: 1;
  531. background-color: $uni-primary-light;
  532. color: $uni-primary;
  533. }
  534. .next-btn {
  535. flex: 1;
  536. background-color: $uni-primary;
  537. color: #fff;
  538. }
  539. .star-icon {
  540. display: flex;
  541. flex-direction: column;
  542. align-items: center;
  543. font-weight: 500;
  544. font-size: 20rpx;
  545. color: #000000;
  546. }
  547. </style>