TopicExam.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  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-${parindex}`"
  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">{{
  101. item.selectAns.map((i) => i.value).join("")
  102. }}</text>
  103. </view>
  104. <view
  105. class="tip"
  106. :style="{
  107. color: item.isRight ? '#00be00' : '#f00',
  108. }"
  109. >{{ item.isRight ? "太棒了~" : "再接再励!" }}</view
  110. >
  111. </view>
  112. <view class="parsing">
  113. <p>解析:</p>
  114. <scroll-view
  115. :show-scrollbar="false"
  116. scroll-y
  117. class="parsing-text"
  118. >
  119. <rich-text :nodes="item.explain"></rich-text>
  120. </scroll-view>
  121. </view>
  122. </view>
  123. </view>
  124. <!-- 底部按钮 -->
  125. <view class="button-group">
  126. <button
  127. v-if="parindex >= 1"
  128. class="prev-btn"
  129. @tap="handlePage(item, parindex, 'prevPage')"
  130. >
  131. 上一题
  132. </button>
  133. <button
  134. v-if="parindex + 1 < total"
  135. class="next-btn"
  136. @tap="handlePage(item, parindex, 'nextPage')"
  137. >
  138. 下一题
  139. </button>
  140. <button v-if="parindex + 1 === total" @click="submit">交卷</button>
  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. nowIndex = 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. 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. });
  270. watchEffect(() => {
  271. data.value = props.topics;
  272. });
  273. // Emits 定义
  274. const emit = defineEmits([
  275. "prevPage",
  276. "nextPage",
  277. "answerChange",
  278. "lookReport",
  279. ]);
  280. const onBack = () =>
  281. new Promise((resolve) => {
  282. if (!data.value.length) {
  283. resolve(true);
  284. return;
  285. }
  286. open.value = true;
  287. r = resolve;
  288. });
  289. const onClose = () => {
  290. if (submitter.value.closeText !== "提交试卷") return r(true);
  291. data.value = data.value.map((item) => ({
  292. ...item,
  293. showResult: true,
  294. isRight: isRight(
  295. item.selectAns.map((q) => q.value),
  296. item.ansList.map((q) => q.label)
  297. ),
  298. }));
  299. emit("lookReport", data.value, submitter.value);
  300. };
  301. const onSubmit = () =>
  302. new Promise((res) => {
  303. res(true);
  304. });
  305. // 响应式数据
  306. const nowIndex = ref(0);
  307. // 判断答案是否正确
  308. const isRight = (selectAns, rightAns) => {
  309. return rightAns.every((item) => selectAns.includes(item));
  310. };
  311. const handleSelect = ({ pid, checked, index, style }) => {
  312. // 如果不是多选,就取消其他的选项
  313. if (style !== 3) {
  314. data.value[pid].questions = data.value[pid].questions.map((q) => ({
  315. ...q,
  316. checked: false,
  317. }));
  318. data.value[pid].selectAns = [];
  319. }
  320. // 更新选项
  321. const item = data.value[pid].questions[index];
  322. data.value[pid].questions[index].checked = !checked;
  323. data.value[pid].questions[index].isRight = isRight(
  324. [item.value],
  325. data.value[pid].ansList.map((q) => q.label)
  326. );
  327. // 更新答案
  328. data.value[pid].selectAns = data.value[pid].questions
  329. .filter((q) => q.checked)
  330. .map((q) => q.value);
  331. };
  332. const handlePage = (item, index, type) => {
  333. nowIndex.value = index + (type === "prevPage" ? -1 : 1);
  334. emit(type, { item, index });
  335. };
  336. const onSafeAreaChange = (s) => {
  337. safeArea.value = s;
  338. };
  339. </script>
  340. <style lang="scss" scoped>
  341. @import "@/uni.scss";
  342. .other {
  343. display: flex;
  344. gap: 12px;
  345. }
  346. .scroll-view {
  347. height: 50vh;
  348. }
  349. .content {
  350. font-family: PingFang SC, PingFang SC;
  351. font-weight: 500;
  352. font-size: 28rpx;
  353. color: #333333;
  354. display: flex;
  355. flex-direction: column;
  356. gap: 24rpx;
  357. padding: 0 30rpx;
  358. .ques-type {
  359. display: flex;
  360. gap: 16rpx;
  361. flex-direction: column;
  362. }
  363. }
  364. .grid {
  365. display: grid;
  366. grid-template-columns: repeat(5, 1fr);
  367. gap: 12px;
  368. }
  369. .bottom-modal {
  370. display: flex;
  371. flex-direction: column;
  372. gap: 20rpx;
  373. padding: 20rpx;
  374. }
  375. .title {
  376. display: flex;
  377. justify-content: center;
  378. align-items: center;
  379. position: relative;
  380. .closeempty {
  381. position: absolute;
  382. right: 0;
  383. }
  384. }
  385. .parsing-text {
  386. height: 381rpx;
  387. white-space: normal;
  388. }
  389. .answer-content {
  390. display: flex;
  391. flex-direction: column;
  392. gap: 45rpx;
  393. font-family: PingFang SC, PingFang SC;
  394. font-weight: 500;
  395. font-size: 32rpx;
  396. color: #666666;
  397. }
  398. .topic-container {
  399. width: 100vw;
  400. overflow: hidden;
  401. position: relative;
  402. }
  403. .topic-item {
  404. display: flex;
  405. flex-direction: column;
  406. gap: 12px;
  407. position: relative;
  408. box-sizing: border-box;
  409. }
  410. .topic-header {
  411. display: flex;
  412. align-items: center;
  413. justify-content: space-between;
  414. }
  415. .topic-header-left {
  416. display: flex;
  417. gap: 8px;
  418. align-items: center;
  419. }
  420. .item {
  421. width: 72rpx;
  422. height: 72rpx;
  423. border-radius: 50%;
  424. display: flex;
  425. align-items: center;
  426. justify-content: center;
  427. border: 1rpx solid #dddddd;
  428. &.success {
  429. border-color: $success;
  430. color: $success;
  431. }
  432. }
  433. .topic-type {
  434. border: 1px solid $uni-primary;
  435. padding: 0 8px;
  436. border-radius: 6px;
  437. color: $uni-primary;
  438. font-weight: 600;
  439. font-size: 14px;
  440. }
  441. .topic-count {
  442. color: #333;
  443. font-size: 14px;
  444. }
  445. .topic-content {
  446. display: flex;
  447. flex-direction: column;
  448. gap: 8px;
  449. }
  450. .question-text {
  451. font-weight: bold;
  452. font-size: 14px;
  453. white-space: normal;
  454. }
  455. .answer-section {
  456. border-radius: 16rpx;
  457. border: 1px solid #ddd;
  458. padding: 24rpx;
  459. display: flex;
  460. flex-direction: column;
  461. gap: 32rpx;
  462. }
  463. .answer-row {
  464. font-size: 14px;
  465. display: flex;
  466. align-items: center;
  467. gap: 12px;
  468. }
  469. .answer-item {
  470. display: flex;
  471. gap: 8px;
  472. align-items: center;
  473. padding-right: 12px;
  474. }
  475. .tip {
  476. margin-left: auto;
  477. }
  478. .border-r-primary {
  479. border-right: 2px solid $uni-primary;
  480. }
  481. .answer-text {
  482. color: $uni-primary;
  483. }
  484. .button-group {
  485. display: flex;
  486. gap: 8px;
  487. position: sticky;
  488. bottom: 0;
  489. margin-top: auto;
  490. }
  491. .prev-btn {
  492. flex: 1;
  493. background-color: $uni-primary-light;
  494. color: $uni-primary;
  495. }
  496. .next-btn {
  497. flex: 1;
  498. background-color: $uni-primary;
  499. color: #fff;
  500. }
  501. .star-icon {
  502. display: flex;
  503. flex-direction: column;
  504. align-items: center;
  505. font-weight: 500;
  506. font-size: 20rpx;
  507. color: #000000;
  508. }
  509. </style>