index.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. import {
  2. examUserExamStart,
  3. examUserExamCommitmentConfirm,
  4. } from '@/pages_safetyEducationExamination/api/index.js'
  5. /**
  6. * 考试入口统一处理
  7. * @param {Object} item 考试任务数据
  8. * @param {Object} callbacks { openCommitmentDialog } —— 需要显示承诺书时的回调
  9. */
  10. export async function goToExamPage(item, callbacks = {}) {
  11. if (item.conditionType == 1) {
  12. if (item.examStudy && item.courseId) {
  13. // 学习课程 — 等待后续逻辑
  14. } else {
  15. if (item.knowledgePointIds && item.knowledgePointIds.length > 0) {
  16. const ids = Array.isArray(item.knowledgePointIds)
  17. ? item.knowledgePointIds.join(',')
  18. : item.knowledgePointIds;
  19. uni.navigateTo({
  20. url: `/pages_safetyEducationExamination/views/studyHourTask/index?knowledgePointId=${ids}`,
  21. });
  22. } else {
  23. uni.navigateTo({ url: '/pages_safetyEducationExamination/views/study/index' });
  24. }
  25. }
  26. } else if (item.conditionType == 2) {
  27. // 学习课程 — 等待后续逻辑
  28. } else if (item.conditionType == 3) {
  29. if (item.mockExamId) {
  30. const examData = encodeURIComponent(JSON.stringify(item));
  31. uni.navigateTo({
  32. url: `/pages_safetyEducationExamination/views/exam/mockExamInfo?examData=${examData}`,
  33. });
  34. } else {
  35. try {
  36. const { data } = await examUserExamStart({ examId: item.examId, examScene: 2 });
  37. if (data.code === 200 && data.data && data.data.attemptId) {
  38. const filled = { ...item, attemptId: data.data.attemptId };
  39. const examData = encodeURIComponent(JSON.stringify(filled));
  40. uni.navigateTo({
  41. url: `/pages_safetyEducationExamination/views/exam/mockExamInfo?examData=${examData}`,
  42. });
  43. } else {
  44. uni.showToast({ title: data.message || '获取模拟试卷失败', icon: 'none' });
  45. }
  46. } catch (e) {
  47. console.error('获取模拟试卷失败', e);
  48. uni.showToast({ title: '获取模拟试卷失败', icon: 'none' });
  49. }
  50. }
  51. } else if (item.conditionType == 4) {
  52. await resolveExamEntry(item, callbacks);
  53. }
  54. }
  55. /**
  56. * 判断是否需要承诺书,并决定后续跳转
  57. * @param {Object} item 考试任务数据
  58. * @param {Object} callbacks { openCommitmentDialog(item) }
  59. */
  60. export async function resolveExamEntry(item, callbacks = {}) {
  61. if (item.commitmentEnabled == 1 && !item.commitmentConfirmed) {
  62. // 需要展示承诺书弹窗,交由调用方组件处理
  63. if (typeof callbacks.openCommitmentDialog === 'function') {
  64. callbacks.openCommitmentDialog(item);
  65. }
  66. } else {
  67. // 不需要承诺书,直接进入考试
  68. await startFormalExam(item);
  69. }
  70. }
  71. /**
  72. * 确认承诺书并进入考试(由组件点击"确认"按钮时调用)
  73. * @param {Object} preData 当前承诺书对应的考试数据
  74. * @param {Function} onClose 承诺确认成功后关闭弹窗的回调
  75. */
  76. export async function confirmCommitmentAndStart(preData, onClose) {
  77. let attemptId = preData.attemptId;
  78. if (!attemptId) {
  79. try {
  80. const { data } = await examUserExamStart({ examId: preData.examId, examScene: 1 });
  81. if (data.code === 200 && data.data && data.data.attemptId) {
  82. attemptId = data.data.attemptId;
  83. } else {
  84. uni.showToast({ title: data.message || '开始考试失败', icon: 'none' });
  85. return;
  86. }
  87. } catch (e) {
  88. console.error('开始考试失败', e);
  89. uni.showToast({ title: '开始考试失败', icon: 'none' });
  90. return;
  91. }
  92. }
  93. try {
  94. const { data } = await examUserExamCommitmentConfirm({ attemptId });
  95. if (data.code === 200) {
  96. if (typeof onClose === 'function') onClose();
  97. navigateToExamPlay(preData.examId, attemptId, preData.examKind);
  98. } else {
  99. uni.showToast({ title: data.message || '确认失败', icon: 'none' });
  100. }
  101. } catch (e) {
  102. console.error('确认承诺书失败', e);
  103. uni.showToast({ title: '确认失败', icon: 'none' });
  104. }
  105. }
  106. /**
  107. * 发起或续考正式考试
  108. * @param {Object} item 考试任务数据(含 examId / attemptId / examKind)
  109. */
  110. export async function startFormalExam(item) {
  111. let attemptId = item.attemptId;
  112. if (!attemptId) {
  113. try {
  114. const { data } = await examUserExamStart({ examId: item.examId, examScene: 1 });
  115. if (data.code !== 200 || !data.data || !data.data.attemptId) {
  116. uni.showToast({ title: data.message || '开始考试失败', icon: 'none' });
  117. return;
  118. }
  119. attemptId = data.data.attemptId;
  120. } catch (e) {
  121. console.error('开始考试失败', e);
  122. uni.showToast({ title: '开始考试失败', icon: 'none' });
  123. return;
  124. }
  125. }
  126. navigateToExamPlay(item.examId, attemptId, item.examKind);
  127. }
  128. /**
  129. * 跳转到答题页
  130. */
  131. export function navigateToExamPlay(examId, attemptId, examKind) {
  132. uni.navigateTo({
  133. url: `/pages_safetyEducationExamination/views/exam/onlineExamPlay?examId=${encodeURIComponent(examId)}&attemptId=${encodeURIComponent(attemptId)}&examKind=${encodeURIComponent(examKind || '')}`,
  134. });
  135. }