index.js 5.7 KB

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