TopicExam.vue 13 KB

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