TopicExam.vue 13 KB

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