【添加】实现考试页面禁用截图功能

This commit is contained in:
陆光LG
2025-11-27 14:42:02 +08:00
parent 2694977052
commit a0cf86e753
3 changed files with 38 additions and 4 deletions

View File

@@ -881,6 +881,8 @@ const autoSubmitExam = async () => {
{ baseDir: BaseDirectory.AppLocalData }
).catch(() => {})
// 在跳转前尝试停止本地截屏拦截服务
await stopScreenService()
replace('/examPaper/confirmPaper')
} catch (error) {
console.error('自动交卷失败:', error)
@@ -1083,7 +1085,7 @@ const confirmSubmission = () => {
file,
stuId: userStore.user.id,
paperId: examStore.selectedPaper
}).then((res) => {
}).then(async (res) => {
instance.confirmButtonLoading = false
instance.cancelButtonLoading = false
inputDisabled.value = false // 重新启用输入框
@@ -1095,6 +1097,8 @@ const confirmSubmission = () => {
).catch((error) => {})
done() // 先关闭弹窗
loading.close()
// 在跳转前尝试停止本地截屏拦截服务
await stopScreenService()
// 使用 nextTick 确保弹窗完全关闭后再跳转
replace('/examPaper/confirmPaper')
})
@@ -1142,7 +1146,7 @@ const confirmSubmission = () => {
file,
stuId: userStore.user.id,
paperId: examStore.selectedPaper
}).then((res) => {
}).then(async (res) => {
instance.confirmButtonLoading = false
instance.cancelButtonLoading = false
inputDisabled.value = false // 重新启用输入框
@@ -1154,6 +1158,8 @@ const confirmSubmission = () => {
).catch((error) => {})
done() // 先关闭弹窗
loading.close()
// 在跳转前尝试停止本地截屏拦截服务
await stopScreenService()
// 使用 nextTick 确保弹窗完全关闭后再跳转
replace('/examPaper/confirmPaper')
})
@@ -1189,6 +1195,16 @@ const confirmSubmission = () => {
// 生成随机验证码
const generateRandomCode = () => Math.floor(Math.random() * 9000 + 1000).toString()
// 在交卷后调用本地服务以恢复截屏组合键
const stopScreenService = async () => {
try {
await fetch('http://localhost:48081/screen/stop', { method: 'GET', mode: 'no-cors' })
console.log('已调用本地截屏服务: http://localhost:48081/screen/stop')
} catch (e) {
console.warn('调用本地截屏停止服务失败:', e)
}
}
// 题目切换逻辑
const goToPreviousQuestion = () => {
// 找到当前题目在 orderedExamQuestionList 中的索引
@@ -2384,9 +2400,27 @@ onMounted(() => {
}
// isScreen: '0' -> 全局禁止截屏组合键
// 新逻辑:当 isScreen === '0' 时调用本地服务触发截屏拦截接口
// 当 isScreen === '1' 时不调用该接口
if (String(btn.isScreen) === '0') {
keyboardDisabler.setDisableScreenshot(true)
keyboardDisabler.updateConfig({ enableSecurityMode: true })
;(async () => {
try {
// 尝试调用本地服务,若跨域或服务不可达会被 catch
// 使用 fetch尽量不阻塞主流程
await fetch('http://localhost:48081/screen/start', { method: 'GET', mode: 'no-cors' })
console.log('已调用本地截屏服务: http://localhost:48081/screen/start')
} catch (e) {
console.warn('调用本地截屏服务失败,回退到前端截屏拦截:', e)
}
// 前端仍保留兜底拦截(若本地服务无效)
try {
keyboardDisabler.setDisableScreenshot(true)
keyboardDisabler.updateConfig({ enableSecurityMode: true })
} catch (e) {
console.warn('启用前端截屏拦截失败:', e)
}
})()
}
} catch (err) {
console.warn('初始化防作弊配置失败:', err)