TopicPracticeEnd.vue 10 KB

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