| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- import {
- examUserExamStart,
- examUserExamCommitmentConfirm,
- } from '@/pages_safetyEducationExamination/api/index.js'
- /**
- * 考试入口统一处理
- * @param {Object} item 考试任务数据
- * @param {Object} callbacks { openCommitmentDialog } —— 需要显示承诺书时的回调
- */
- export async function goToExamPage(item, callbacks = {}) {
- if (item.conditionType == 1) {
- if (item.examStudy && item.courseId) {
- // 学习课程 — 等待后续逻辑
- } else {
- if (item.knowledgePointIds && item.knowledgePointIds.length > 0) {
- const ids = Array.isArray(item.knowledgePointIds)
- ? item.knowledgePointIds.join(',')
- : item.knowledgePointIds;
- uni.navigateTo({
- url: `/pages_safetyEducationExamination/views/studyHourTask/index?knowledgePointId=${ids}`,
- });
- } else {
- uni.navigateTo({ url: '/pages_safetyEducationExamination/views/study/index' });
- }
- }
- } else if (item.conditionType == 2) {
- // 学习课程 — 等待后续逻辑
- } else if (item.conditionType == 3) {
- if (item.mockExamId) {
- const examData = encodeURIComponent(JSON.stringify(item));
- uni.navigateTo({
- url: `/pages_safetyEducationExamination/views/exam/mockExamInfo?examData=${examData}`,
- });
- } else {
- try {
- const { data } = await examUserExamStart({ examId: item.examId, examScene: 2 });
- if (data.code === 200 && data.data && data.data.attemptId) {
- const filled = { ...item, attemptId: data.data.attemptId };
- const examData = encodeURIComponent(JSON.stringify(filled));
- uni.navigateTo({
- url: `/pages_safetyEducationExamination/views/exam/mockExamInfo?examData=${examData}`,
- });
- } else {
- uni.showToast({ title: data.message || '获取模拟试卷失败', icon: 'none' });
- }
- } catch (e) {
- console.error('获取模拟试卷失败', e);
- uni.showToast({ title: '获取模拟试卷失败', icon: 'none' });
- }
- }
- } else if (item.conditionType == 4) {
- await resolveExamEntry(item, callbacks);
- }
- }
- /**
- * 判断是否需要承诺书,并决定后续跳转
- * @param {Object} item 考试任务数据
- * @param {Object} callbacks { openCommitmentDialog(item) }
- */
- export async function resolveExamEntry(item, callbacks = {}) {
- if (item.commitmentEnabled == 1 && !item.commitmentConfirmed) {
- // 需要展示承诺书弹窗,交由调用方组件处理
- if (typeof callbacks.openCommitmentDialog === 'function') {
- callbacks.openCommitmentDialog(item);
- }
- } else {
- // 不需要承诺书,直接进入考试
- await startFormalExam(item);
- }
- }
- /**
- * 确认承诺书并进入考试(由组件点击"确认"按钮时调用)
- * @param {Object} preData 当前承诺书对应的考试数据
- * @param {Function} onClose 承诺确认成功后关闭弹窗的回调
- */
- export async function confirmCommitmentAndStart(preData, onClose) {
- let attemptId = preData.attemptId;
- if (!attemptId) {
- try {
- const { data } = await examUserExamStart({ examId: preData.examId, examScene: 1 });
- if (data.code === 200 && data.data && data.data.attemptId) {
- attemptId = data.data.attemptId;
- } else {
- uni.showToast({ title: data.message || '开始考试失败', icon: 'none' });
- return;
- }
- } catch (e) {
- console.error('开始考试失败', e);
- uni.showToast({ title: '开始考试失败', icon: 'none' });
- return;
- }
- }
- try {
- const { data } = await examUserExamCommitmentConfirm({ attemptId });
- if (data.code === 200) {
- if (typeof onClose === 'function') onClose();
- navigateToExamPlay(preData.examId, attemptId, preData.examKind);
- } else {
- uni.showToast({ title: data.message || '确认失败', icon: 'none' });
- }
- } catch (e) {
- console.error('确认承诺书失败', e);
- uni.showToast({ title: '确认失败', icon: 'none' });
- }
- }
- /**
- * 发起或续考正式考试
- * @param {Object} item 考试任务数据(含 examId / attemptId / examKind)
- */
- export async function startFormalExam(item) {
- let attemptId = item.attemptId;
- if (!attemptId) {
- try {
- const { data } = await examUserExamStart({ examId: item.examId, examScene: 1 });
- if (data.code !== 200 || !data.data || !data.data.attemptId) {
- uni.showToast({ title: data.message || '开始考试失败', icon: 'none' });
- return;
- }
- attemptId = data.data.attemptId;
- } catch (e) {
- console.error('开始考试失败', e);
- uni.showToast({ title: '开始考试失败', icon: 'none' });
- return;
- }
- }
- navigateToExamPlay(item.examId, attemptId, item.examKind);
- }
- /**
- * 跳转到答题页
- */
- export function navigateToExamPlay(examId, attemptId, examKind) {
- uni.navigateTo({
- url: `/pages_safetyEducationExamination/views/exam/onlineExamPlay?examId=${encodeURIComponent(examId)}&attemptId=${encodeURIComponent(attemptId)}&examKind=${encodeURIComponent(examKind || '')}`,
- });
- }
|