| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299 |
- import {
- examUserExamStart,
- examUserExamCommitmentConfirm,
- examUserCoursewareLearningRestart,
- examUserExamAttemptFinished,
- } 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) {
- // 学习课程 — 等待后续逻辑
- uni.navigateTo({
- url: `/pages_safetyEducationExamination/views/webViewPage/courseLearning?courseId=${item.courseId}&examId=${item.examId}`,
- });
- } 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) {
- // 学习课程
- uni.navigateTo({
- url: `/pages_safetyEducationExamination/views/webViewPage/courseLearning?courseId=${item.courseId}&examId=${item.examId}`,
- });
- } else if (item.conditionType == 3) {
- console.log('item',item)
- const pages = getCurrentPages();
- const currentPage = pages[pages.length - 1];
- const currentRoute = currentPage.route;
- if (item.mockExamId) {
- // const examData = encodeURIComponent(JSON.stringify(item));
- // uni.navigateTo({
- // url: `/pages_safetyEducationExamination/views/exam/mockExamPlay?examId=${item.examId}&attemptId=${item.mockExamId}`,
- // });
- let obj = {
- examId: item.examId,
- attemptId: item.mockExamId,
- };
- console.log('goToExamPage-obj',obj)
- examUserExamAttemptFinishedPlay(2,obj);
- } else {
- let obj = {
- examId: item.examId,
- };
- examUserExamStartPlay(2,obj);
- // 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/mockExamPlay?examId=${item.examId}&attemptId=${data.data.attemptId}`,
- // });
- // } 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);
- if(item.attemptId){
- console.log('resolveExamEntry-item',item)
- examUserExamAttemptFinishedPlay(1,item);
- }else{
- examUserExamStartPlay(1,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();
- } else {
- // navigateToExamPlay(preData.examId, attemptId, preData.examKind);
- let obj = {
- examId: preData.examId,
- attemptId: attemptId,
- type: '1',
- examKind:preData.examKind,
- attemptLimit:preData.attemptLimit,
- attemptCount:preData.attemptCount,
- }
- console.log('confirmCommitmentAndStart-obj',obj)
- examUserExamAttemptFinishedPlay(1,obj);
- }
- } 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 || '')}`,
- });
- }
- /**
- * 学习课件
- */
- export function goLearning(item){
- if(item.learnStatus == 2){
- uni.showModal({
- title: '提示',
- content: `该课件已学完,是否重置进度重新学习?`,
- success: async (res) => {
- if (res.confirm) {
- await examUserCoursewareLearningRestartFunction(item.coursewareId);
- }
- }
- });
- }else{
- uni.navigateTo({
- url: `/pages_safetyEducationExamination/views/webViewPage/coursewareLearning?coursewareId=${item.coursewareId}`,
- });
- }
- }
- /**
- * 查询课件是否已学过
- */
- export async function examUserCoursewareLearningRestartFunction(coursewareId ) {
- try {
- const { data } = await examUserCoursewareLearningRestart({coursewareId:coursewareId});
- if (data.code === 200) {
- uni.navigateTo({
- url: `/pages_safetyEducationExamination/views/webViewPage/coursewareLearning?coursewareId=${coursewareId}`,
- });
- }
- } catch (e) { console.error('重新开始学习课件失败', e); }
- }
- //二次确认考试是否已结束
- export async function examUserExamAttemptFinishedPlay(type,item) {
- console.log('调用了')
- try {
- console.log('item',item)
- const { data } = await examUserExamAttemptFinished({ attemptId:item.attemptId });
- if (data.code == 200) {
- if(data.data){
- //已结束
- if(type == 1){
- //在线考试
- if(item.attemptLimit == 0 || item.attemptCount < item.attemptLimit){
- //获取新试卷
- examUserExamStartPlay(type,item)
- }else{
- //无可用次数 提示并刷新数据列表
- uni.showToast({ title: '当前考试已结束', icon: 'none' });
- checkData();
- }
- }else if(type == 2){
- //模拟考试-获取新试卷
- examUserExamStartPlay(type,item)
- }
- }else{
- //未结束
- confirmTitle(type,item);
- }
- }
- } catch (e) { console.error('二次确认考试是否已结束失败', e);}
- }
- //获取新试卷
- export async function examUserExamStartPlay(type,item) {
- try {
- const { data } = await examUserExamStart({ examId: item.examId, examScene: type });
- if (data.code === 200 && data.data && data.data.attemptId) {
- let obj = {
- examId: item.examId,
- attemptId: data.data.attemptId,
- };
- if(type == 1){
- obj.type = 1;
- obj.examKind = item.examKind;
- }
- confirmTitle(type,obj);
- } else {
- uni.showToast({ title: data.message || '获取试卷失败', icon: 'none' });
- }
- } catch (e) {
- console.error('获取试卷失败', e);
- uni.showToast({ title: '获取试卷失败', icon: 'none' });
- }
- }
- //弹窗确认
- export async function confirmTitle(type,item) {
- if(type == 1){
- uni.showModal({
- title: '进入考试页面',
- content: `考试期间未完成答题请勿离开页面,离开后倒计时仍会继续,超时将自动交卷并记录成绩,扣除 1 次考试机会。`,
- confirmText:'确定进入',
- success: async (res) => {
- if (res.confirm) {
- uni.navigateTo({
- url: `/pages_safetyEducationExamination/views/exam/onlineExamPlay?examId=${encodeURIComponent(item.examId)}&attemptId=${encodeURIComponent(item.attemptId)}&examKind=${encodeURIComponent(item.examKind || '')}`,
- });
- }
- }
- });
- }else if(type == 2){
- uni.navigateTo({
- url: `/pages_safetyEducationExamination/views/exam/mockExamPlay?examId=${item.examId}&attemptId=${item.attemptId}`,
- });
- }
- }
- //监听数据刷新事件
- export function checkData(data) {
- uni.$emit('onDataCheckSuccess', data)
- }
|