TopicExam.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  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. isEnd: {
  266. type: Boolean,
  267. default: false,
  268. },
  269. total: {
  270. type: Number,
  271. default: 0,
  272. },
  273. title: String,
  274. real_topic_id: Number,
  275. });
  276. const onBeforePageChange = (index) => {
  277. // 保存用户答题
  278. const item = data.value[nowIndex.value];
  279. if (!item.selectAns.length) {
  280. nowIndex.value = index;
  281. return;
  282. }
  283. request(
  284. "api/question_bank/question_reception/real_topic/save_user_real_topic",
  285. {
  286. user_real_paper_id: props.real_topic_id,
  287. catalogue_id: getRoute().params.id,
  288. real_topic_id: item.id,
  289. is_correct: item.isRight ? 1 : 0,
  290. answer: item.selectAns.join(","),
  291. },
  292. "post"
  293. ).finally(() => {
  294. nowIndex.value = index;
  295. });
  296. };
  297. watchEffect(() => {
  298. data.value = props.topics;
  299. });
  300. // Emits 定义
  301. const emit = defineEmits([
  302. "prevPage",
  303. "nextPage",
  304. "answerChange",
  305. "lookReport",
  306. ]);
  307. const onBack = () =>
  308. new Promise((resolve) => {
  309. if (!data.value.length || props.isEnd) {
  310. resolve(true);
  311. return;
  312. }
  313. open.value = true;
  314. r = resolve;
  315. });
  316. const ansIsRight = (selectAns, rightAns) => {
  317. if (selectAns.length !== rightAns.length) return false;
  318. return isRight(selectAns, rightAns);
  319. };
  320. const examEnd = () => {
  321. data.value = data.value.map((item) => {
  322. return {
  323. ...item,
  324. showResult: true,
  325. isRight: ansIsRight(
  326. item.selectAns,
  327. item.ansList.map((q) => q.label)
  328. ),
  329. };
  330. });
  331. emit("lookReport", data.value, submitter.value);
  332. };
  333. const onClose = () => {
  334. if (submitter.value.closeText !== "提交试卷") return r(true);
  335. examEnd();
  336. };
  337. const onSubmit = () =>
  338. new Promise((res) => {
  339. res(true);
  340. });
  341. // 响应式数据
  342. const nowIndex = ref(0);
  343. // 判断答案是否正确
  344. const isRight = (selectAns, rightAns) => {
  345. if (!selectAns.length) return;
  346. return selectAns.every((item) => rightAns.includes(item));
  347. };
  348. const handleSelect = ({ pid, checked, index, style }) => {
  349. // 如果不是多选,就取消其他的选项
  350. if (style !== 3) {
  351. data.value[pid].questions = data.value[pid].questions.map((q) => ({
  352. ...q,
  353. checked: false,
  354. }));
  355. data.value[pid].selectAns = [];
  356. }
  357. // 更新选项
  358. const item = data.value[pid].questions[index];
  359. data.value[pid].questions[index].checked = !checked;
  360. data.value[pid].questions[index].isRight = isRight(
  361. [item.value],
  362. data.value[pid].ansList.map((q) => q.label)
  363. );
  364. // 更新答案
  365. data.value[pid].selectAns = data.value[pid].questions
  366. .filter((q) => q.checked)
  367. .map((q) => q.value);
  368. };
  369. const handlePage = (item, index, type) => {
  370. onBeforePageChange(index + (type === "prevPage" ? -1 : 1));
  371. emit(type, { item, index });
  372. };
  373. const onSafeAreaChange = (s) => {
  374. safeArea.value = s;
  375. };
  376. </script>
  377. <style lang="scss" scoped>
  378. @import "@/uni.scss";
  379. .other {
  380. display: flex;
  381. gap: 12px;
  382. }
  383. .scroll-view {
  384. height: 50vh;
  385. }
  386. .content {
  387. font-family: PingFang SC, PingFang SC;
  388. font-weight: 500;
  389. font-size: 28rpx;
  390. color: #333333;
  391. display: flex;
  392. flex-direction: column;
  393. gap: 24rpx;
  394. padding: 0 30rpx;
  395. .ques-type {
  396. display: flex;
  397. gap: 16rpx;
  398. flex-direction: column;
  399. }
  400. }
  401. .grid {
  402. display: grid;
  403. grid-template-columns: repeat(5, 1fr);
  404. gap: 12px;
  405. }
  406. .bottom-modal {
  407. display: flex;
  408. flex-direction: column;
  409. gap: 20rpx;
  410. // padding: 20rpx;
  411. }
  412. .title {
  413. display: flex;
  414. justify-content: center;
  415. align-items: center;
  416. position: relative;
  417. .closeempty {
  418. position: absolute;
  419. right: 0;
  420. }
  421. }
  422. .parsing-text {
  423. height: 381rpx;
  424. white-space: normal;
  425. }
  426. .answer-content {
  427. display: flex;
  428. flex-direction: column;
  429. gap: 45rpx;
  430. font-family: PingFang SC, PingFang SC;
  431. font-weight: 500;
  432. font-size: 32rpx;
  433. color: #666666;
  434. }
  435. .topic-container {
  436. width: 100vw;
  437. overflow: hidden;
  438. position: relative;
  439. }
  440. .topic-item {
  441. display: flex;
  442. flex-direction: column;
  443. gap: 12px;
  444. position: relative;
  445. box-sizing: border-box;
  446. }
  447. .topic-header {
  448. display: flex;
  449. align-items: center;
  450. justify-content: space-between;
  451. }
  452. .topic-header-left {
  453. display: flex;
  454. gap: 8px;
  455. align-items: center;
  456. }
  457. .item {
  458. width: 72rpx;
  459. height: 72rpx;
  460. border-radius: 50%;
  461. display: flex;
  462. align-items: center;
  463. justify-content: center;
  464. border: 1rpx solid #dddddd;
  465. &.success {
  466. border-color: $primary;
  467. color: $primary;
  468. }
  469. }
  470. .topic-type {
  471. border: 1px solid $uni-primary;
  472. padding: 0 8px;
  473. border-radius: 6px;
  474. color: $uni-primary;
  475. font-weight: 600;
  476. font-size: 14px;
  477. }
  478. .topic-count {
  479. color: #333;
  480. font-size: 14px;
  481. }
  482. .topic-content {
  483. display: flex;
  484. flex-direction: column;
  485. gap: 8px;
  486. }
  487. .question-text {
  488. font-weight: bold;
  489. font-size: 14px;
  490. white-space: normal;
  491. }
  492. .answer-section {
  493. border-radius: 16rpx;
  494. border: 1px solid #ddd;
  495. padding: 24rpx;
  496. display: flex;
  497. flex-direction: column;
  498. gap: 32rpx;
  499. }
  500. .answer-row {
  501. font-size: 14px;
  502. display: flex;
  503. align-items: center;
  504. gap: 12px;
  505. }
  506. .answer-item {
  507. display: flex;
  508. gap: 8px;
  509. align-items: center;
  510. padding-right: 12px;
  511. }
  512. .tip {
  513. margin-left: auto;
  514. }
  515. .border-r-primary {
  516. border-right: 2px solid $uni-primary;
  517. }
  518. .answer-text {
  519. color: $uni-primary;
  520. }
  521. .button-group {
  522. display: flex;
  523. gap: 8px;
  524. position: sticky;
  525. bottom: 0;
  526. margin-top: auto;
  527. }
  528. .prev-btn {
  529. flex: 1;
  530. background-color: $uni-primary-light;
  531. color: $uni-primary;
  532. }
  533. .next-btn {
  534. flex: 1;
  535. background-color: $uni-primary;
  536. color: #fff;
  537. }
  538. .star-icon {
  539. display: flex;
  540. flex-direction: column;
  541. align-items: center;
  542. font-weight: 500;
  543. font-size: 20rpx;
  544. color: #000000;
  545. }
  546. </style>