TopicPractice.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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-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. <button
  129. v-if="parindex >= 1 && item.showResult"
  130. class="prev-btn"
  131. @tap="handlePage(item, parindex, 'prevPage')"
  132. >
  133. 上一题
  134. </button>
  135. <button
  136. v-if="parindex + 1 < total && item.showResult"
  137. class="next-btn"
  138. @tap="handlePage(item, parindex, 'nextPage')"
  139. >
  140. 下一题
  141. </button>
  142. <button
  143. v-if="parindex + 1 === total && item.showResult"
  144. @click="emit('lookReport', data, submitter)"
  145. >
  146. 查看报告
  147. </button>
  148. <button
  149. v-if="!item.showResult"
  150. @click="questionSubmit(item, parindex)"
  151. >
  152. 提交
  153. </button>
  154. </view>
  155. </template>
  156. </template>
  157. </view>
  158. </Container>
  159. <Modal
  160. v-model:open="open"
  161. title="温馨提示"
  162. :submitter="submitter"
  163. :onClose="onClose"
  164. :onSubmit="onSubmit"
  165. >
  166. <view :style="{ margin: '10px 0' }">{{ submitter.context }}</view>
  167. </Modal>
  168. </template>
  169. <script setup>
  170. // 答题组件
  171. import { ref, watchEffect } from "vue";
  172. import Questions from "./Questions.vue";
  173. import Container from "../Container/Container.vue";
  174. import Modal from "../Modal/Modal.vue";
  175. import uvParse from "@/uni_modules/uv-parse/components/uv-parse/uv-parse.vue";
  176. import { request } from "../../utils/request";
  177. const styleMap = {
  178. 2: "单选",
  179. 3: "多选",
  180. 6: "配伍",
  181. };
  182. const submitter = ref({
  183. text: "提交查看",
  184. closeText: "直接退出",
  185. context: "您本次答题还未提交, 确定要退出答题吗?",
  186. isEndQuestion: false,
  187. });
  188. const safeArea = ref({}); // 安全区域
  189. const data = ref([]); // 题目数据
  190. const open = ref(false); // 是否显示弹窗
  191. let r = null; // 异步方法
  192. // Props 定义
  193. const props = defineProps({
  194. topics: {
  195. type: Array,
  196. default: () => [],
  197. },
  198. onStar: {
  199. type: Function,
  200. default: null,
  201. },
  202. total: {
  203. type: Number,
  204. default: 0,
  205. },
  206. title: String,
  207. user_exercise_paper_id: Number,
  208. });
  209. watchEffect(() => {
  210. data.value = props.topics;
  211. });
  212. // Emits 定义
  213. const emit = defineEmits([
  214. "prevPage",
  215. "nextPage",
  216. "answerChange",
  217. "lookReport",
  218. ]);
  219. const onBack = () =>
  220. new Promise((resolve) => {
  221. if (!data.value.length) {
  222. resolve(true);
  223. return;
  224. }
  225. const isEndQuestion =
  226. nowIndex.value === props.total - 1 &&
  227. data.value[nowIndex.value].showResult;
  228. if (isEndQuestion) {
  229. submitter.value = {
  230. ...submitter.value,
  231. text: "查看报告",
  232. context:
  233. "您的答题报告已准备就绪!退出后将无法查看本次分析结果,且不保留本次答题记录,确定要放弃吗?",
  234. isEndQuestion,
  235. };
  236. }
  237. open.value = true;
  238. r = resolve;
  239. });
  240. const onClose = () => r(true);
  241. const onSubmit = () =>
  242. new Promise((res) => {
  243. emit("lookReport", data.value, submitter.value);
  244. res(true);
  245. });
  246. // 响应式数据
  247. const nowIndex = ref(0);
  248. // 判断答案是否正确
  249. const isRight = (selectAns, rightAns) => {
  250. return selectAns.every((item) => rightAns.includes(item));
  251. };
  252. // 收藏方法
  253. const handleStar = (item) => {
  254. if (!props.onStar) return;
  255. props.onStar(item).then((res) => {
  256. uni.showToast({
  257. title: res ? "已加入收藏" : "已移除收藏",
  258. icon: "none",
  259. });
  260. });
  261. };
  262. const handleSelect = ({ pid, checked, index, style }) => {
  263. // 如果不是多选,就取消其他的选项
  264. if (style !== 3) {
  265. data.value[pid].questions = data.value[pid].questions.map((q) => ({
  266. ...q,
  267. checked: false,
  268. }));
  269. data.value[pid].selectAns = [];
  270. }
  271. // 更新选项
  272. const item = data.value[pid].questions[index];
  273. data.value[pid].questions[index].checked = !checked;
  274. data.value[pid].questions[index].isRight = isRight(
  275. [item.value],
  276. data.value[pid].ansList.map((q) => q.label)
  277. );
  278. // 更新答案
  279. data.value[pid].selectAns = data.value[pid].questions
  280. .filter((q) => q.checked)
  281. .map((q) => q.value);
  282. };
  283. const ansIsRight = (selectAns, rightAns) => {
  284. if (selectAns.length !== rightAns.length) return false;
  285. return isRight(selectAns, rightAns);
  286. };
  287. const questionSubmit = (item) => {
  288. item.showResult = true;
  289. item.selectAns = item.questions.filter((q) => q.checked);
  290. item.isRight = ansIsRight(
  291. item.selectAns.map((q) => q.value),
  292. item.ansList.map((q) => q.label)
  293. );
  294. console.log(item);
  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. flex: 1;
  412. background-color: $uni-primary-light;
  413. color: $uni-primary;
  414. }
  415. .next-btn {
  416. flex: 1;
  417. background-color: $uni-primary;
  418. color: #fff;
  419. }
  420. .star-icon {
  421. display: flex;
  422. flex-direction: column;
  423. align-items: center;
  424. font-weight: 500;
  425. font-size: 20rpx;
  426. color: #000000;
  427. }
  428. </style>