TopicPractice.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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. if (!data.value.length) {
  217. resolve(true);
  218. return;
  219. }
  220. const isEndQuestion =
  221. nowIndex.value === props.total - 1 &&
  222. data.value[nowIndex.value].showResult;
  223. if (isEndQuestion) {
  224. submitter.value = {
  225. ...submitter.value,
  226. text: "查看报告",
  227. context:
  228. "您的答题报告已准备就绪!退出后将无法查看本次分析结果,且不保留本次答题记录,确定要放弃吗?",
  229. isEndQuestion,
  230. };
  231. }
  232. open.value = true;
  233. r = resolve;
  234. });
  235. const onClose = () => r(true);
  236. const onSubmit = () =>
  237. new Promise((res) => {
  238. emit("lookReport", data.value, submitter.value);
  239. res(true);
  240. });
  241. // 响应式数据
  242. const nowIndex = ref(0);
  243. // 判断答案是否正确
  244. const isRight = (selectAns, rightAns) => {
  245. return selectAns.every((item) => rightAns.includes(item));
  246. };
  247. // 收藏方法
  248. const handleStar = (item) => {
  249. if (!props.onStar) return;
  250. props.onStar(item).then((res) => {
  251. uni.showToast({
  252. title: res ? "已加入收藏" : "已移除收藏",
  253. icon: "none",
  254. });
  255. });
  256. };
  257. const handleSelect = ({ pid, checked, index, style }) => {
  258. // 如果不是多选,就取消其他的选项
  259. if (style !== 3) {
  260. data.value[pid].questions = data.value[pid].questions.map((q) => ({
  261. ...q,
  262. checked: false,
  263. }));
  264. data.value[pid].selectAns = [];
  265. }
  266. // 更新选项
  267. const item = data.value[pid].questions[index];
  268. data.value[pid].questions[index].checked = !checked;
  269. data.value[pid].questions[index].isRight = isRight(
  270. [item.value],
  271. data.value[pid].ansList.map((q) => q.label)
  272. );
  273. // 更新答案
  274. data.value[pid].selectAns = data.value[pid].questions
  275. .filter((q) => q.checked)
  276. .map((q) => q.value);
  277. };
  278. const questionSubmit = (item) => {
  279. item.showResult = true;
  280. item.selectAns = item.questions.filter((q) => q.checked);
  281. item.isRight = isRight(
  282. item.selectAns.map((q) => q.value),
  283. item.ansList.map((q) => q.label)
  284. );
  285. };
  286. const handlePage = (item, index, type) => {
  287. nowIndex.value = index + (type === "prevPage" ? -1 : 1);
  288. emit(type, { item, index });
  289. };
  290. const onSafeAreaChange = (s) => {
  291. safeArea.value = s;
  292. };
  293. </script>
  294. <style lang="scss" scoped>
  295. @import "@/uni.scss";
  296. .other {
  297. display: flex;
  298. gap: 12px;
  299. }
  300. .parsing-text {
  301. height: 381rpx;
  302. white-space: normal;
  303. }
  304. .answer-content {
  305. display: flex;
  306. flex-direction: column;
  307. gap: 45rpx;
  308. font-family: PingFang SC, PingFang SC;
  309. font-weight: 500;
  310. font-size: 32rpx;
  311. color: #666666;
  312. }
  313. .topic-container {
  314. width: 100vw;
  315. overflow: hidden;
  316. position: relative;
  317. }
  318. .topic-item {
  319. display: flex;
  320. flex-direction: column;
  321. gap: 12px;
  322. position: relative;
  323. box-sizing: border-box;
  324. }
  325. .topic-header {
  326. display: flex;
  327. align-items: center;
  328. justify-content: space-between;
  329. }
  330. .topic-header-left {
  331. display: flex;
  332. gap: 8px;
  333. align-items: center;
  334. }
  335. .topic-type {
  336. border: 1px solid $uni-primary;
  337. padding: 0 8px;
  338. border-radius: 6px;
  339. color: $uni-primary;
  340. font-weight: 600;
  341. font-size: 14px;
  342. }
  343. .topic-count {
  344. color: #333;
  345. font-size: 14px;
  346. }
  347. .topic-content {
  348. display: flex;
  349. flex-direction: column;
  350. gap: 8px;
  351. }
  352. .question-text {
  353. font-weight: bold;
  354. font-size: 14px;
  355. white-space: normal;
  356. }
  357. .answer-section {
  358. border-radius: 16rpx;
  359. border: 1px solid #ddd;
  360. padding: 24rpx;
  361. display: flex;
  362. flex-direction: column;
  363. gap: 32rpx;
  364. }
  365. .answer-row {
  366. font-size: 14px;
  367. display: flex;
  368. align-items: center;
  369. gap: 12px;
  370. }
  371. .answer-item {
  372. display: flex;
  373. gap: 8px;
  374. align-items: center;
  375. padding-right: 12px;
  376. }
  377. .tip {
  378. margin-left: auto;
  379. }
  380. .border-r-primary {
  381. border-right: 2px solid $uni-primary;
  382. }
  383. .answer-text {
  384. color: $uni-primary;
  385. }
  386. .button-group {
  387. display: flex;
  388. gap: 8px;
  389. position: sticky;
  390. bottom: 0;
  391. margin-top: auto;
  392. }
  393. .prev-btn {
  394. flex: 1;
  395. background-color: $uni-primary-light;
  396. color: $uni-primary;
  397. }
  398. .next-btn {
  399. flex: 1;
  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>