practice.vue 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. <script setup name="Exam">
  2. import TopicPractice from "../../components/Topic/TopicPractice.vue";
  3. import { ref, onMounted } from "vue";
  4. import { getRoute, router } from "../../utils/router";
  5. import { request } from "../../utils/request";
  6. import { useTimeStore } from "@/store/time";
  7. import Container from "../../components/Container/Container.vue";
  8. import ShareTemplate from "../../components/ShareTemplate/ShareTemplate.vue";
  9. const Time = useTimeStore();
  10. const TopicMapList = ["A", "B", "C", "D", "E"];
  11. const title = ref("");
  12. const total = ref(100000);
  13. const correct = ref({
  14. rate: 0, // 正确率
  15. error: 0, // 错误数
  16. right: 0, // 正确数
  17. not: 0, // 未答题
  18. total: 0, // 总题数
  19. });
  20. const showReport = ref(false);
  21. const showShare = ref(false);
  22. const submitter = ref({
  23. closeText: "直接退出",
  24. context: "您本次答题还未提交, 确定要退出答题吗?",
  25. isEndQuestion: false,
  26. text: "提交查看",
  27. totalTime: {
  28. formatTime: "00:00",
  29. totalTime: 0,
  30. },
  31. });
  32. const pageParams = ref({
  33. page: 1,
  34. limit: 999,
  35. });
  36. const data = ref([]);
  37. const user_exercise_paper_id = ref(0);
  38. const getList = async (params) => {
  39. if (pageParams.value.page * pageParams.value.limit >= total.value) return;
  40. const i = await request(
  41. "api/question_bank/question_reception/topic/get_user_exercise_paper",
  42. {
  43. chapter_id: getRoute().params.id,
  44. }
  45. );
  46. if (!i.data) {
  47. const r = await request(
  48. "api/question_bank/question_reception/topic/set_user_new_exercise_paper",
  49. {
  50. chapter_id: getRoute().params.id,
  51. }
  52. );
  53. user_exercise_paper_id.value = r.data;
  54. } else {
  55. user_exercise_paper_id.value = i.data;
  56. }
  57. const res = await request(
  58. "api/question_bank/question_reception/topic/get_chapter_topic",
  59. {
  60. ...params,
  61. id: getRoute().params.id,
  62. user_exercise_paper_id: user_exercise_paper_id.value,
  63. }
  64. );
  65. total.value = res.data.total;
  66. data.value.push(
  67. ...res.data.data.map((item, ind) => {
  68. let questions = [];
  69. const ans = item.correct_answer.split(",");
  70. const ansList = [];
  71. for (let i = 0; i < item.question_count; i++) {
  72. const current = TopicMapList[i];
  73. if (ans.includes(current)) {
  74. ansList.push({
  75. label: current,
  76. value: i,
  77. });
  78. }
  79. const v =
  80. item[`select_${current.toLowerCase()}`].replace(/<br\s*\/?>/g, "") ||
  81. "";
  82. questions.push({
  83. label: v,
  84. value: current,
  85. checked: false,
  86. index: i,
  87. });
  88. }
  89. return {
  90. ...item,
  91. questions, // 题目
  92. ansList, // 正确答案
  93. selectAns: [], // 选择的答案
  94. showResult: false, // 是否展示答案
  95. isRight: false, // 是否正确
  96. isImage: item.title.includes("<img"),
  97. ind,
  98. };
  99. })
  100. );
  101. pageParams.value.page++;
  102. };
  103. const nextPage = (e) => {
  104. if (pageParams.value.page * pageParams.value.limit - e.index - 1 !== 1)
  105. return;
  106. getList(pageParams.value);
  107. };
  108. const onStar = (item) =>
  109. new Promise((resolve) => {
  110. request(
  111. !item.is_favorite
  112. ? "api/question_bank/question_reception/topic/set_favorite"
  113. : "api/question_bank/question_reception/topic/cancel_favorite",
  114. {
  115. id: item.id,
  116. chapter_id: getRoute().params.id,
  117. user_exercise_paper_id: user_exercise_paper_id.value,
  118. }
  119. ).then(() => {
  120. item.is_favorite = item.is_favorite ? 0 : 1;
  121. resolve(item.is_favorite);
  122. });
  123. });
  124. const lookReport = async (d, s) => {
  125. data.value = d;
  126. const totalTime = Time.end();
  127. await request(
  128. "api/question_bank/question_reception/topic/user_submit_exercise_paper",
  129. {
  130. user_exercise_paper_id: user_exercise_paper_id.value,
  131. chapter_id: getRoute().params.id,
  132. }
  133. );
  134. showReport.value = true;
  135. submitter.value = {
  136. ...s,
  137. totalTime,
  138. };
  139. const r = data.value.filter((item) => item.isRight).length;
  140. const n = data.value.filter((item) => !item.selectAns.length).length;
  141. correct.value = {
  142. rate: (r / total.value) * 100,
  143. right: r,
  144. error: total.value - r - n,
  145. not: n,
  146. total: total.value,
  147. };
  148. };
  149. onMounted(() => {
  150. Time.start();
  151. const params = getRoute().params;
  152. title.value = params.title;
  153. getList(pageParams.value);
  154. });
  155. </script>
  156. <template>
  157. <TopicPractice
  158. :title="title"
  159. :total="total"
  160. mode="practice"
  161. :topics="data"
  162. :user_exercise_paper_id="user_exercise_paper_id"
  163. @nextPage="nextPage"
  164. @lookReport="lookReport"
  165. :onStar="onStar"
  166. :empty="!data.length"
  167. text="加载中···"
  168. border
  169. v-if="!showReport && !showShare"
  170. />
  171. <Container
  172. bgColor="url('https://openwork-oss.oss-cn-shenzhen.aliyuncs.com/uploads/question/2025/05/7i6ROn34tZGe8QR4gaWDP1ZzyPYjqlvPgLLFRmIe.png')"
  173. bottomBorder
  174. v-else-if="!showShare && showReport"
  175. bottomBgColor="#fff"
  176. title="练习报告"
  177. >
  178. <view class="reslut">
  179. <div class="title-r">恭 喜 完 成 考 试</div>
  180. <view class="card">
  181. <view class="card-time">
  182. <view>{{ submitter.totalTime?.formatTime }}</view>
  183. <view>学习时长</view>
  184. </view>
  185. <view class="card-time">
  186. <view class="red">{{ correct.rate.toFixed(2) }}%</view>
  187. <view>正确率</view>
  188. </view>
  189. </view>
  190. <view class="template">
  191. <view class="header">
  192. <view>答题卡</view>
  193. <view class="right-error">
  194. <view class="right">答对({{ correct.right }})</view>
  195. <view class="error">答错({{ correct.error }})</view>
  196. </view>
  197. </view>
  198. <view class="group">
  199. <view
  200. class="item"
  201. :class="{
  202. right: it.isRight && it.showResult,
  203. error: !it.isRight && it.showResult && it.selectAns.length,
  204. }"
  205. v-for="(it, index) in data"
  206. :key="it.id"
  207. >{{ index + 1 }}</view
  208. >
  209. </view>
  210. </view>
  211. </view>
  212. <template #footer>
  213. <view class="footer">
  214. <view class="button plain" @click="showReport = false">答题解析</view>
  215. <view class="button" @click="showShare = true">炫耀一下</view>
  216. </view>
  217. </template>
  218. </Container>
  219. <ShareTemplate
  220. :onClose="() => (showShare = false)"
  221. :correct="correct"
  222. bg="https://openwork-oss.oss-cn-shenzhen.aliyuncs.com/uploads/question/2025/05/ix4wGk9h7Fb4c3d8hjkYjJP6VsPNbZgG3uDTUzxp.png"
  223. v-else
  224. />
  225. </template>
  226. <style lang="scss" scoped>
  227. @import "@/uni.scss";
  228. .template {
  229. padding: 15rpx;
  230. border-radius: 24rpx;
  231. background-color: #f8f9fb;
  232. }
  233. .red {
  234. color: #ff4444;
  235. }
  236. .title-r {
  237. display: flex;
  238. align-items: center;
  239. justify-content: center;
  240. font-weight: 500;
  241. font-size: 50rpx;
  242. color: #002fa7;
  243. }
  244. .card {
  245. display: flex;
  246. justify-content: center;
  247. align-items: center;
  248. gap: 63rpx;
  249. padding: 40rpx 50rpx;
  250. background-color: #f8f9fb;
  251. border-radius: 24rpx;
  252. &-time {
  253. font-family: PingFang SC, PingFang SC;
  254. font-weight: 500;
  255. font-size: 28rpx;
  256. color: #999999;
  257. display: flex;
  258. flex-direction: column;
  259. gap: 10rpx;
  260. align-items: center;
  261. }
  262. }
  263. .reslut {
  264. margin: 0 32rpx;
  265. padding: 24rpx;
  266. font-family: PingFang SC, PingFang SC;
  267. font-weight: 500;
  268. font-size: 28rpx;
  269. color: #333333;
  270. background: #fff;
  271. border-radius: 24rpx;
  272. display: flex;
  273. flex-direction: column;
  274. gap: 24rpx;
  275. .header {
  276. display: flex;
  277. justify-content: space-between;
  278. align-items: center;
  279. .right-error {
  280. display: flex;
  281. gap: 20rpx;
  282. font-weight: 400;
  283. font-size: 20rpx;
  284. color: #333333;
  285. @mixin type($color) {
  286. display: flex;
  287. align-items: center;
  288. gap: 10rpx;
  289. &::before {
  290. content: "";
  291. width: 16rpx;
  292. height: 16rpx;
  293. border-radius: 50%;
  294. display: block;
  295. background: $color;
  296. }
  297. }
  298. .right {
  299. @include type($success);
  300. }
  301. .error {
  302. @include type($error);
  303. }
  304. .not {
  305. @include type($default);
  306. }
  307. }
  308. }
  309. .group {
  310. display: grid;
  311. grid-template-columns: repeat(6, 1fr);
  312. gap: 20rpx;
  313. margin-top: 20rpx;
  314. .item {
  315. width: 72rpx;
  316. height: 72rpx;
  317. border-radius: 50%;
  318. display: flex;
  319. align-items: center;
  320. justify-content: center;
  321. background: $default;
  322. font-family: PingFang SC, PingFang SC;
  323. font-weight: 500;
  324. font-size: 28rpx;
  325. color: #ffffff;
  326. }
  327. .item.right {
  328. background: $success;
  329. }
  330. .item.error {
  331. background: $error;
  332. }
  333. }
  334. }
  335. .footer {
  336. display: flex;
  337. gap: 20rpx;
  338. align-items: center;
  339. padding-top: 20rpx;
  340. }
  341. </style>