TopicExam.vue 13 KB

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