TopicPractice.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  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. <template v-for="(item, parindex) in data" :key="parindex">
  13. <view
  14. :id="`item-${parindex}`"
  15. v-if="parindex === nowIndex"
  16. class="topic-item"
  17. :style="{
  18. width: `${safeArea.width}px`,
  19. height: `${safeArea.height}px`,
  20. flexShrink: 0, // 解决宽度无效问题
  21. }"
  22. >
  23. <!-- 头部 -->
  24. <view class="topic-header">
  25. <view class="topic-header-left">
  26. <view class="topic-type"> {{ styleMap[item.style] }}题 </view>
  27. <view class="topic-count">
  28. 第{{ parindex + 1 }}题/共{{ total }}题
  29. </view>
  30. </view>
  31. <view class="star-icon" @tap="handleStar(item)">
  32. <uni-icons
  33. :type="item.star ? 'star-filled' : 'star'"
  34. size="20"
  35. color="#fe2624"
  36. />
  37. {{ item.star ? "已" : "" }}收藏
  38. </view>
  39. </view>
  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 && item.showResult"
  128. class="prev-btn"
  129. @tap="handlePage(item, parindex, 'prevPage')"
  130. >
  131. 上一题
  132. </button>
  133. <button
  134. v-if="parindex + 1 < total && item.showResult"
  135. class="next-btn"
  136. @tap="handlePage(item, parindex, 'nextPage')"
  137. >
  138. 下一题
  139. </button>
  140. <button
  141. v-if="parindex + 1 === total && item.showResult"
  142. @click="emit('lookReport', data, submitter)"
  143. >
  144. 查看报告
  145. </button>
  146. <button
  147. v-if="!item.showResult"
  148. @click="questionSubmit(item, parindex)"
  149. >
  150. 提交
  151. </button>
  152. </view>
  153. </view>
  154. </template>
  155. </Container>
  156. <Modal
  157. v-model:open="open"
  158. title="温馨提示"
  159. :submitter="submitter"
  160. :onClose="onClose"
  161. :onSubmit="onSubmit"
  162. >
  163. <view :style="{ margin: '10px 0' }">{{ submitter.context }}</view>
  164. </Modal>
  165. </template>
  166. <script setup>
  167. // 答题组件
  168. import { ref, watchEffect } from "vue";
  169. import Questions from "./Questions.vue";
  170. import Container from "../Container/Container.vue";
  171. import Modal from "../Modal/Modal.vue";
  172. import uvParse from "@/uni_modules/uv-parse/components/uv-parse/uv-parse.vue";
  173. const styleMap = {
  174. 2: "单选",
  175. 3: "多选",
  176. 6: "配伍",
  177. };
  178. const submitter = ref({
  179. text: "提交查看",
  180. closeText: "直接退出",
  181. context: "您本次答题还未提交, 确定要退出答题吗?",
  182. isEndQuestion: false,
  183. });
  184. const safeArea = ref({}); // 安全区域
  185. const data = ref([]); // 题目数据
  186. const open = ref(false); // 是否显示弹窗
  187. let r = null; // 异步方法
  188. // Props 定义
  189. const props = defineProps({
  190. topics: {
  191. type: Array,
  192. default: () => [],
  193. },
  194. onStar: {
  195. type: Function,
  196. default: null,
  197. },
  198. total: {
  199. type: Number,
  200. default: 0,
  201. },
  202. title: String,
  203. });
  204. watchEffect(() => {
  205. data.value = props.topics;
  206. });
  207. // Emits 定义
  208. const emit = defineEmits([
  209. "prevPage",
  210. "nextPage",
  211. "answerChange",
  212. "lookReport",
  213. ]);
  214. const onBack = () =>
  215. new Promise((resolve) => {
  216. const isEndQuestion =
  217. nowIndex.value === props.total - 1 &&
  218. data.value[nowIndex.value].showResult;
  219. if (isEndQuestion) {
  220. submitter.value = {
  221. ...submitter.value,
  222. text: "查看报告",
  223. context:
  224. "您的答题报告已准备就绪!退出后将无法查看本次分析结果,且不保留本次答题记录,确定要放弃吗?",
  225. isEndQuestion,
  226. };
  227. }
  228. open.value = true;
  229. r = resolve;
  230. });
  231. const onClose = () => r(true);
  232. const onSubmit = () =>
  233. new Promise((res) => {
  234. emit("lookReport", data.value, submitter.value);
  235. res(true);
  236. });
  237. // 响应式数据
  238. const nowIndex = ref(0);
  239. // 判断答案是否正确
  240. const isRight = (selectAns, rightAns) => {
  241. return selectAns.every((item) => rightAns.includes(item));
  242. };
  243. // 收藏方法
  244. const handleStar = (item) => {
  245. if (!props.onStar) return;
  246. props.onStar(item).then((res) => {
  247. uni.showToast({
  248. title: res ? "已加入收藏" : "已移除收藏",
  249. icon: "none",
  250. });
  251. });
  252. };
  253. const handleSelect = ({ pid, checked, index, style }) => {
  254. // 如果不是多选,就取消其他的选项
  255. if (style !== 3) {
  256. data.value[pid].questions = data.value[pid].questions.map((q) => ({
  257. ...q,
  258. checked: false,
  259. }));
  260. data.value[pid].selectAns = [];
  261. }
  262. // 更新选项
  263. const item = data.value[pid].questions[index];
  264. data.value[pid].questions[index].checked = !checked;
  265. data.value[pid].questions[index].isRight = isRight(
  266. [item.value],
  267. data.value[pid].ansList.map((q) => q.label)
  268. );
  269. // 更新答案
  270. data.value[pid].selectAns = data.value[pid].questions
  271. .filter((q) => q.checked)
  272. .map((q) => q.value);
  273. };
  274. const questionSubmit = (item) => {
  275. item.showResult = true;
  276. item.selectAns = item.questions.filter((q) => q.checked);
  277. item.isRight = isRight(
  278. item.selectAns.map((q) => q.value),
  279. item.ansList.map((q) => q.label)
  280. );
  281. };
  282. const handlePage = (item, index, type) => {
  283. nowIndex.value = index + (type === "prevPage" ? -1 : 1);
  284. emit(type, { item, index });
  285. };
  286. const onSafeAreaChange = (s) => {
  287. safeArea.value = s;
  288. };
  289. </script>
  290. <style lang="scss" scoped>
  291. @import "@/uni.scss";
  292. .other {
  293. display: flex;
  294. gap: 12px;
  295. }
  296. .parsing-text {
  297. height: 381rpx;
  298. white-space: normal;
  299. }
  300. .answer-content {
  301. display: flex;
  302. flex-direction: column;
  303. gap: 45rpx;
  304. font-family: PingFang SC, PingFang SC;
  305. font-weight: 500;
  306. font-size: 32rpx;
  307. color: #666666;
  308. }
  309. .topic-container {
  310. width: 100vw;
  311. overflow: hidden;
  312. position: relative;
  313. }
  314. .topic-item {
  315. display: flex;
  316. flex-direction: column;
  317. gap: 12px;
  318. position: relative;
  319. box-sizing: border-box;
  320. }
  321. .topic-header {
  322. display: flex;
  323. align-items: center;
  324. justify-content: space-between;
  325. }
  326. .topic-header-left {
  327. display: flex;
  328. gap: 8px;
  329. align-items: center;
  330. }
  331. .topic-type {
  332. border: 1px solid $uni-primary;
  333. padding: 0 8px;
  334. border-radius: 6px;
  335. color: $uni-primary;
  336. font-weight: 600;
  337. font-size: 14px;
  338. }
  339. .topic-count {
  340. color: #333;
  341. font-size: 14px;
  342. }
  343. .topic-content {
  344. display: flex;
  345. flex-direction: column;
  346. gap: 8px;
  347. }
  348. .question-text {
  349. font-weight: bold;
  350. font-size: 14px;
  351. white-space: normal;
  352. }
  353. .answer-section {
  354. border-radius: 16rpx;
  355. border: 1px solid #ddd;
  356. padding: 24rpx;
  357. display: flex;
  358. flex-direction: column;
  359. gap: 32rpx;
  360. }
  361. .answer-row {
  362. font-size: 14px;
  363. display: flex;
  364. align-items: center;
  365. gap: 12px;
  366. }
  367. .answer-item {
  368. display: flex;
  369. gap: 8px;
  370. align-items: center;
  371. padding-right: 12px;
  372. }
  373. .tip {
  374. margin-left: auto;
  375. }
  376. .border-r-primary {
  377. border-right: 2px solid $uni-primary;
  378. }
  379. .answer-text {
  380. color: $uni-primary;
  381. }
  382. .button-group {
  383. display: flex;
  384. gap: 8px;
  385. position: sticky;
  386. bottom: 0;
  387. margin-top: auto;
  388. }
  389. .prev-btn {
  390. flex: 1;
  391. background-color: $uni-primary-light;
  392. color: $uni-primary;
  393. }
  394. .next-btn {
  395. flex: 1;
  396. background-color: $uni-primary;
  397. color: #fff;
  398. }
  399. .star-icon {
  400. display: flex;
  401. flex-direction: column;
  402. align-items: center;
  403. font-weight: 500;
  404. font-size: 20rpx;
  405. color: #000000;
  406. }
  407. </style>