TopicPracticeEnd.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. <template>
  2. <Container
  3. :scrollX="true"
  4. :scrollY="true"
  5. :scroll-into-view="`item-${nowIndex}`"
  6. :title="title"
  7. @onSafeAreaChange="onSafeAreaChange"
  8. v-bind="$attrs"
  9. v-if="!open"
  10. >
  11. <view
  12. :id="`item-${nowIndex}`"
  13. class="topic-item"
  14. :style="{
  15. width: `${safeArea.width}px`,
  16. height: `${safeArea.height}px`,
  17. flexShrink: 0, // 解决宽度无效问题
  18. }"
  19. >
  20. <!-- 头部 -->
  21. <view class="topic-header">
  22. <view class="topic-header-left">
  23. <view class="topic-type">
  24. {{ styleMap[data[nowIndex]?.style] }}题
  25. </view>
  26. <view class="topic-count">
  27. 第{{ nowIndex + 1 }}题/共{{ total }}题
  28. </view>
  29. </view>
  30. <view class="star-icon" @tap="handleStar(data[nowIndex])">
  31. <uni-icons
  32. :type="data[nowIndex]?.is_favorite ? 'star-filled' : 'star'"
  33. size="20"
  34. color="#fe2624"
  35. />
  36. {{ data[nowIndex]?.is_favorite ? "已" : "" }}收藏
  37. </view>
  38. </view>
  39. <template v-for="(item, parindex) in data" :key="parindex">
  40. <template v-if="parindex === nowIndex">
  41. <!-- 问题内容 -->
  42. <view class="topic-content">
  43. <uvParse
  44. v-if="item.isImage"
  45. :content="item.title"
  46. class="question-text"
  47. ></uvParse>
  48. <rich-text
  49. v-else
  50. :nodes="item.title"
  51. class="question-text"
  52. ></rich-text>
  53. <uvParse
  54. :content="item.questions_ex"
  55. v-if="item.style === 6 && item.isImage"
  56. class="question-text"
  57. ></uvParse>
  58. <rich-text
  59. v-else
  60. :nodes="item.questions_ex"
  61. class="question-text"
  62. ></rich-text>
  63. <questions
  64. v-if="item.style !== 6"
  65. v-for="(question, index) in item.questions"
  66. :key="index"
  67. :answerList="item.ansList"
  68. :index="index"
  69. :styleCount="item.style"
  70. :question="question"
  71. :showResult="item.showResult"
  72. :parindex="parindex"
  73. @select="handleSelect"
  74. />
  75. <view class="other" v-else>
  76. <questions
  77. v-for="(question, index) in item.questions"
  78. :key="index"
  79. :answerList="item.ansList"
  80. :index="index"
  81. :styleCount="item.style"
  82. :question="question"
  83. :showResult="item.showResult"
  84. :parindex="parindex"
  85. @select="handleSelect"
  86. />
  87. </view>
  88. </view>
  89. <!-- 答案展示 -->
  90. <view v-if="item.showResult" class="answer-section">
  91. <view class="answer-content">
  92. <view class="answer-row">
  93. <view class="answer-item border-r-primary">
  94. 正确答案:
  95. <text class="answer-text">{{
  96. item.ansList.map((i) => i.label).join("")
  97. }}</text>
  98. </view>
  99. <view class="answer-item">
  100. 我的答案:
  101. <text class="answer-text">{{
  102. item.selectAns.map((i) => i.value).join("")
  103. }}</text>
  104. </view>
  105. <view
  106. class="tip"
  107. :style="{
  108. color: item.isRight ? '#00be00' : '#f00',
  109. }"
  110. >{{ item.isRight ? "太棒了~" : "再接再励!" }}</view
  111. >
  112. </view>
  113. <view class="parsing">
  114. <p>解析:</p>
  115. <scroll-view
  116. :show-scrollbar="false"
  117. scroll-y
  118. class="parsing-text"
  119. >
  120. <rich-text :nodes="item.explain"></rich-text>
  121. </scroll-view>
  122. </view>
  123. </view>
  124. </view>
  125. <!-- 底部按钮 -->
  126. <view class="button-group">
  127. <view
  128. v-if="parindex >= 1 && parindex + 1 < total && item.showResult"
  129. class="prev-btn button"
  130. @tap="handlePage(item, parindex, 'prevPage')"
  131. >
  132. 上一题
  133. </view>
  134. <view
  135. v-if="parindex + 1 < total && item.showResult"
  136. class="next-btn button"
  137. @tap="handlePage(item, parindex, 'nextPage')"
  138. >
  139. 下一题
  140. </view>
  141. <view
  142. class="button"
  143. v-if="parindex + 1 === total && item.showResult"
  144. @click="emit('lookReport', data, submitter)"
  145. >
  146. 完成
  147. </view>
  148. <button
  149. :disabled="!item.selectAns.length"
  150. v-if="!item.showResult"
  151. @click="questionSubmit(item, parindex)"
  152. >
  153. 提交
  154. </button>
  155. </view>
  156. </template>
  157. </template>
  158. </view>
  159. </Container>
  160. <Modal
  161. v-model:open="open"
  162. title="温馨提示"
  163. :submitter="submitter"
  164. :onClose="onClose"
  165. :onSubmit="onSubmit"
  166. >
  167. <view :style="{ margin: '10px 0' }">{{ submitter.context }}</view>
  168. </Modal>
  169. </template>
  170. <script setup>
  171. // 答题组件
  172. import { ref, watchEffect } from "vue";
  173. import Questions from "./Questions.vue";
  174. import Container from "../Container/Container.vue";
  175. import Modal from "../Modal/Modal.vue";
  176. import uvParse from "@/uni_modules/uv-parse/components/uv-parse/uv-parse.vue";
  177. import { request } from "../../utils/request";
  178. const styleMap = {
  179. 2: "单选",
  180. 3: "多选",
  181. 6: "配伍",
  182. };
  183. const submitter = ref({
  184. text: "提交查看",
  185. closeText: "直接退出",
  186. context: "您本次答题还未提交, 确定要退出答题吗?",
  187. isEndQuestion: false,
  188. });
  189. const safeArea = ref({}); // 安全区域
  190. const data = ref([]); // 题目数据
  191. const open = ref(false); // 是否显示弹窗
  192. let r = null; // 异步方法
  193. // Props 定义
  194. const props = defineProps({
  195. topics: {
  196. type: Array,
  197. default: () => [],
  198. },
  199. onStar: {
  200. type: Function,
  201. default: null,
  202. },
  203. total: {
  204. type: Number,
  205. default: 0,
  206. },
  207. title: String,
  208. user_exercise_paper_id: Number,
  209. });
  210. watchEffect(() => {
  211. data.value = props.topics;
  212. });
  213. // Emits 定义
  214. const emit = defineEmits([
  215. "prevPage",
  216. "nextPage",
  217. "answerChange",
  218. "lookReport",
  219. ]);
  220. const onBack = () =>
  221. new Promise((resolve) => {
  222. if (!data.value.length) {
  223. resolve(true);
  224. return;
  225. }
  226. const isEndQuestion =
  227. nowIndex.value === props.total - 1 &&
  228. data.value[nowIndex.value].showResult;
  229. if (isEndQuestion) {
  230. submitter.value = {
  231. ...submitter.value,
  232. text: "查看报告",
  233. context:
  234. "您的答题报告已准备就绪!退出后将无法查看本次分析结果,且不保留本次答题记录,确定要放弃吗?",
  235. isEndQuestion,
  236. };
  237. }
  238. open.value = true;
  239. r = resolve;
  240. });
  241. const onClose = () => r(true);
  242. const onSubmit = () =>
  243. new Promise((res) => {
  244. emit("lookReport", data.value, submitter.value);
  245. res(true);
  246. });
  247. // 响应式数据
  248. const nowIndex = ref(0);
  249. // 判断答案是否正确
  250. const isRight = (selectAns, rightAns) => {
  251. return selectAns.every((item) => rightAns.includes(item));
  252. };
  253. // 收藏方法
  254. const handleStar = (item) => {
  255. if (!props.onStar) return;
  256. props.onStar(item).then((res) => {
  257. uni.showToast({
  258. title: res ? "已加入收藏" : "已移除收藏",
  259. icon: "none",
  260. });
  261. });
  262. };
  263. const handleSelect = ({ pid, checked, index, style }) => {
  264. // 如果不是多选,就取消其他的选项
  265. if (style !== 3) {
  266. data.value[pid].questions = data.value[pid].questions.map((q) => ({
  267. ...q,
  268. checked: false,
  269. }));
  270. data.value[pid].selectAns = [];
  271. }
  272. // 更新选项
  273. const item = data.value[pid].questions[index];
  274. data.value[pid].questions[index].checked = !checked;
  275. data.value[pid].questions[index].isRight = isRight(
  276. [item.value],
  277. data.value[pid].ansList.map((q) => q.label)
  278. );
  279. // 更新答案
  280. data.value[pid].selectAns = data.value[pid].questions
  281. .filter((q) => q.checked)
  282. .map((q) => q.value);
  283. };
  284. const ansIsRight = (selectAns, rightAns) => {
  285. if (selectAns.length !== rightAns.length) return false;
  286. return isRight(selectAns, rightAns);
  287. };
  288. const questionSubmit = (item) => {
  289. item.showResult = true;
  290. item.selectAns = item.questions.filter((q) => q.checked);
  291. item.isRight = ansIsRight(
  292. item.selectAns.map((q) => q.value),
  293. item.ansList.map((q) => q.label)
  294. );
  295. request("api/question_bank/question_reception/topic/answer_topic", {
  296. id: item.id,
  297. answer: item.selectAns.map((q) => q.value).join(","),
  298. is_correct: item.isRight ? 1 : 0,
  299. user_exercise_paper_id: props.user_exercise_paper_id,
  300. chapter_id: item.chapter_id,
  301. });
  302. };
  303. const handlePage = (item, index, type) => {
  304. nowIndex.value = index + (type === "prevPage" ? -1 : 1);
  305. emit(type, { item, index });
  306. };
  307. const onSafeAreaChange = (s) => {
  308. safeArea.value = s;
  309. };
  310. </script>
  311. <style lang="scss" scoped>
  312. @import "@/uni.scss";
  313. .other {
  314. display: flex;
  315. gap: 12px;
  316. }
  317. .parsing-text {
  318. height: 381rpx;
  319. white-space: normal;
  320. }
  321. .answer-content {
  322. display: flex;
  323. flex-direction: column;
  324. gap: 45rpx;
  325. font-family: PingFang SC, PingFang SC;
  326. font-weight: 500;
  327. font-size: 32rpx;
  328. color: #666666;
  329. }
  330. .topic-container {
  331. width: 100vw;
  332. overflow: hidden;
  333. position: relative;
  334. }
  335. .topic-item {
  336. display: flex;
  337. flex-direction: column;
  338. gap: 12px;
  339. position: relative;
  340. box-sizing: border-box;
  341. }
  342. .topic-header {
  343. display: flex;
  344. align-items: center;
  345. justify-content: space-between;
  346. }
  347. .topic-header-left {
  348. display: flex;
  349. gap: 8px;
  350. align-items: center;
  351. }
  352. .topic-type {
  353. border: 1px solid $uni-primary;
  354. padding: 0 8px;
  355. border-radius: 6px;
  356. color: $uni-primary;
  357. font-weight: 600;
  358. font-size: 14px;
  359. }
  360. .topic-count {
  361. color: #333;
  362. font-size: 14px;
  363. }
  364. .topic-content {
  365. display: flex;
  366. flex-direction: column;
  367. gap: 8px;
  368. }
  369. .question-text {
  370. font-weight: bold;
  371. font-size: 14px;
  372. white-space: normal;
  373. }
  374. .answer-section {
  375. border-radius: 16rpx;
  376. border: 1px solid #ddd;
  377. padding: 24rpx;
  378. display: flex;
  379. flex-direction: column;
  380. gap: 32rpx;
  381. }
  382. .answer-row {
  383. font-size: 14px;
  384. display: flex;
  385. align-items: center;
  386. gap: 12px;
  387. }
  388. .answer-item {
  389. display: flex;
  390. gap: 8px;
  391. align-items: center;
  392. padding-right: 12px;
  393. }
  394. .tip {
  395. margin-left: auto;
  396. }
  397. .border-r-primary {
  398. border-right: 2px solid $uni-primary;
  399. }
  400. .answer-text {
  401. color: $uni-primary;
  402. }
  403. .button-group {
  404. display: flex;
  405. gap: 8px;
  406. position: sticky;
  407. bottom: 0;
  408. margin-top: auto;
  409. }
  410. .prev-btn {
  411. background-color: $uni-primary-light;
  412. color: $uni-primary;
  413. }
  414. .next-btn {
  415. background-color: $uni-primary;
  416. color: #fff;
  417. }
  418. .star-icon {
  419. display: flex;
  420. flex-direction: column;
  421. align-items: center;
  422. font-weight: 500;
  423. font-size: 20rpx;
  424. color: #000000;
  425. }
  426. </style>