TopicExam.vue 13 KB

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