TopicExam.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  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. nowIndex = 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. const styleMap = {
  208. 2: "单选",
  209. 3: "多选",
  210. 6: "配伍",
  211. };
  212. const safeArea = ref({}); // 安全区域
  213. const data = ref([]); // 题目数据
  214. const open = ref(false); // 是否显示弹窗
  215. const showSheet = ref(false); // 是否显示底部弹窗
  216. let r = null; // 异步方法
  217. const Time = useTimeStore();
  218. const bottomModal = ref(null); // 底部弹窗
  219. const openSheet = () => {
  220. bottomModal.value.open();
  221. };
  222. const submit = () => {
  223. const len = data.value.filter((item) => !item.selectAns.length).length;
  224. if (len) {
  225. bottomModal.value.close();
  226. submitter.value = {
  227. ...submitter.value,
  228. closeText: "提交试卷",
  229. context: `您还有${len}道题没做,确认交卷吗?`,
  230. };
  231. open.value = true;
  232. }
  233. };
  234. const formatTime = (time, type) => {
  235. // 获取time一个半小时后的时间戳
  236. const timeStamp = new Date(time + 1.5 * 60 * 60 * 1000);
  237. const date = new Date();
  238. switch (type) {
  239. case "hour":
  240. return timeStamp.getHours() - date.getHours();
  241. case "minute":
  242. return timeStamp.getMinutes() - date.getMinutes();
  243. case "second":
  244. return timeStamp.getSeconds() - date.getSeconds();
  245. }
  246. };
  247. const submitter = ref({
  248. text: "继续考试",
  249. closeText: "直接退出",
  250. context: "确定退出考试吗?退出后将不保留本次考试记录,确定要放弃吗?",
  251. });
  252. // Props 定义
  253. const props = defineProps({
  254. topics: {
  255. type: Array,
  256. default: () => [],
  257. },
  258. onStar: {
  259. type: Function,
  260. default: null,
  261. },
  262. total: {
  263. type: Number,
  264. default: 0,
  265. },
  266. title: String,
  267. });
  268. watchEffect(() => {
  269. data.value = props.topics;
  270. });
  271. // Emits 定义
  272. const emit = defineEmits([
  273. "prevPage",
  274. "nextPage",
  275. "answerChange",
  276. "lookReport",
  277. ]);
  278. const onBack = () =>
  279. new Promise((resolve) => {
  280. if (!data.value.length) {
  281. resolve(true);
  282. return;
  283. }
  284. open.value = true;
  285. r = resolve;
  286. });
  287. const ansIsRight = (selectAns, rightAns) => {
  288. if (selectAns.length !== rightAns.length) return false;
  289. return isRight(selectAns, rightAns);
  290. };
  291. const examEnd = () => {
  292. data.value = data.value.map((item) => {
  293. return {
  294. ...item,
  295. showResult: true,
  296. isRight: ansIsRight(
  297. item.selectAns,
  298. item.ansList.map((q) => q.label)
  299. ),
  300. };
  301. });
  302. emit("lookReport", data.value, submitter.value);
  303. };
  304. const onClose = () => {
  305. if (submitter.value.closeText !== "提交试卷") return r(true);
  306. examEnd();
  307. };
  308. const onSubmit = () =>
  309. new Promise((res) => {
  310. res(true);
  311. });
  312. // 响应式数据
  313. const nowIndex = ref(0);
  314. // 判断答案是否正确
  315. const isRight = (selectAns, rightAns) => {
  316. if (!selectAns.length) return;
  317. return selectAns.every((item) => rightAns.includes(item));
  318. };
  319. const handleSelect = ({ pid, checked, index, style }) => {
  320. // 如果不是多选,就取消其他的选项
  321. if (style !== 3) {
  322. data.value[pid].questions = data.value[pid].questions.map((q) => ({
  323. ...q,
  324. checked: false,
  325. }));
  326. data.value[pid].selectAns = [];
  327. }
  328. // 更新选项
  329. const item = data.value[pid].questions[index];
  330. data.value[pid].questions[index].checked = !checked;
  331. data.value[pid].questions[index].isRight = isRight(
  332. [item.value],
  333. data.value[pid].ansList.map((q) => q.label)
  334. );
  335. // 更新答案
  336. data.value[pid].selectAns = data.value[pid].questions
  337. .filter((q) => q.checked)
  338. .map((q) => q.value);
  339. };
  340. const handlePage = (item, index, type) => {
  341. nowIndex.value = index + (type === "prevPage" ? -1 : 1);
  342. emit(type, { item, index });
  343. };
  344. const onSafeAreaChange = (s) => {
  345. safeArea.value = s;
  346. };
  347. </script>
  348. <style lang="scss" scoped>
  349. @import "@/uni.scss";
  350. .other {
  351. display: flex;
  352. gap: 12px;
  353. }
  354. .scroll-view {
  355. height: 50vh;
  356. }
  357. .content {
  358. font-family: PingFang SC, PingFang SC;
  359. font-weight: 500;
  360. font-size: 28rpx;
  361. color: #333333;
  362. display: flex;
  363. flex-direction: column;
  364. gap: 24rpx;
  365. padding: 0 30rpx;
  366. .ques-type {
  367. display: flex;
  368. gap: 16rpx;
  369. flex-direction: column;
  370. }
  371. }
  372. .grid {
  373. display: grid;
  374. grid-template-columns: repeat(5, 1fr);
  375. gap: 12px;
  376. }
  377. .bottom-modal {
  378. display: flex;
  379. flex-direction: column;
  380. gap: 20rpx;
  381. padding: 20rpx;
  382. }
  383. .title {
  384. display: flex;
  385. justify-content: center;
  386. align-items: center;
  387. position: relative;
  388. .closeempty {
  389. position: absolute;
  390. right: 0;
  391. }
  392. }
  393. .parsing-text {
  394. height: 381rpx;
  395. white-space: normal;
  396. }
  397. .answer-content {
  398. display: flex;
  399. flex-direction: column;
  400. gap: 45rpx;
  401. font-family: PingFang SC, PingFang SC;
  402. font-weight: 500;
  403. font-size: 32rpx;
  404. color: #666666;
  405. }
  406. .topic-container {
  407. width: 100vw;
  408. overflow: hidden;
  409. position: relative;
  410. }
  411. .topic-item {
  412. display: flex;
  413. flex-direction: column;
  414. gap: 12px;
  415. position: relative;
  416. box-sizing: border-box;
  417. }
  418. .topic-header {
  419. display: flex;
  420. align-items: center;
  421. justify-content: space-between;
  422. }
  423. .topic-header-left {
  424. display: flex;
  425. gap: 8px;
  426. align-items: center;
  427. }
  428. .item {
  429. width: 72rpx;
  430. height: 72rpx;
  431. border-radius: 50%;
  432. display: flex;
  433. align-items: center;
  434. justify-content: center;
  435. border: 1rpx solid #dddddd;
  436. &.success {
  437. border-color: $success;
  438. color: $success;
  439. }
  440. }
  441. .topic-type {
  442. border: 1px solid $uni-primary;
  443. padding: 0 8px;
  444. border-radius: 6px;
  445. color: $uni-primary;
  446. font-weight: 600;
  447. font-size: 14px;
  448. }
  449. .topic-count {
  450. color: #333;
  451. font-size: 14px;
  452. }
  453. .topic-content {
  454. display: flex;
  455. flex-direction: column;
  456. gap: 8px;
  457. }
  458. .question-text {
  459. font-weight: bold;
  460. font-size: 14px;
  461. white-space: normal;
  462. }
  463. .answer-section {
  464. border-radius: 16rpx;
  465. border: 1px solid #ddd;
  466. padding: 24rpx;
  467. display: flex;
  468. flex-direction: column;
  469. gap: 32rpx;
  470. }
  471. .answer-row {
  472. font-size: 14px;
  473. display: flex;
  474. align-items: center;
  475. gap: 12px;
  476. }
  477. .answer-item {
  478. display: flex;
  479. gap: 8px;
  480. align-items: center;
  481. padding-right: 12px;
  482. }
  483. .tip {
  484. margin-left: auto;
  485. }
  486. .border-r-primary {
  487. border-right: 2px solid $uni-primary;
  488. }
  489. .answer-text {
  490. color: $uni-primary;
  491. }
  492. .button-group {
  493. display: flex;
  494. gap: 8px;
  495. position: sticky;
  496. bottom: 0;
  497. margin-top: auto;
  498. }
  499. .prev-btn {
  500. flex: 1;
  501. background-color: $uni-primary-light;
  502. color: $uni-primary;
  503. }
  504. .next-btn {
  505. flex: 1;
  506. background-color: $uni-primary;
  507. color: #fff;
  508. }
  509. .star-icon {
  510. display: flex;
  511. flex-direction: column;
  512. align-items: center;
  513. font-weight: 500;
  514. font-size: 20rpx;
  515. color: #000000;
  516. }
  517. </style>