diff --git a/src/views/examPaper/index.vue b/src/views/examPaper/index.vue index dda8899..5f4077e 100644 --- a/src/views/examPaper/index.vue +++ b/src/views/examPaper/index.vue @@ -412,6 +412,9 @@ const usbAlertVisible = ref(false) const fileAlertVisible = ref(false) const usbAlertMsg = ref('') const fileAlertMsg = ref('') +// 用于保持全屏的定时器 id(检测到 U 盘 / 文件共享后周期性确保全屏) +const usbFullscreenInterval = ref(null) +const fileFullscreenInterval = ref(null) const networkStatus = ref('强') // 网络信号强度 const uploadStatus = ref(1) // 上传状态 (0: 上传; 1: 不上传) const isMinView = computed(() => examStore.isMinView) @@ -467,13 +470,24 @@ const connectUsbWs = (server = 'localhost:48082') => { if (msg.includes('检测到') && msg.includes('U盘')) { usbAlertMsg.value = msg usbAlertVisible.value = true + // 立即尝试设置全屏,并启动定时器持续确保全屏状态 ;(async () => { try { - await WindowManager.setAlwaysOnTop(true) + await getCurrentWindow().setFullscreen(true) } catch (e) { console.warn('设置全屏/置顶失败:', e) } })() + if (!usbFullscreenInterval.value) { + // 每 3 秒重复确保全屏,直到收到关闭消息 + usbFullscreenInterval.value = setInterval(() => { + getCurrentWindow() + .setFullscreen(true) + .catch(() => { + /* 忽略失败,下一次重试 */ + }) + }, 3000) + } } // 关闭提示的协议(后端返回 COLSEUSB) if (msg.includes('COLSEUSB') || msg.includes('CLOSEUSB')) { @@ -481,7 +495,14 @@ const connectUsbWs = (server = 'localhost:48082') => { usbAlertMsg.value = '' ;(async () => { try { - await WindowManager.setAlwaysOnTop(false) + // 停止保持全屏的定时器 + if (usbFullscreenInterval.value) { + clearInterval(usbFullscreenInterval.value) + usbFullscreenInterval.value = null + } + if (examStore.isAnswerView) { + await getCurrentWindow().setFullscreen(false) + } } catch (e) {} })() } @@ -507,20 +528,37 @@ const connectFileWs = (server = 'localhost:48082') => { if (msg.includes('检测到') && msg.includes('文件共享')) { fileAlertMsg.value = msg fileAlertVisible.value = true + // 立即尝试设置全屏,并启动定时器持续确保全屏状态 ;(async () => { try { - await WindowManager.setAlwaysOnTop(true) + await getCurrentWindow().setFullscreen(true) } catch (e) { console.warn('设置全屏/置顶失败:', e) } })() + if (!fileFullscreenInterval.value) { + fileFullscreenInterval.value = setInterval(() => { + getCurrentWindow() + .setFullscreen(true) + .catch(() => { + /* 忽略失败,下一次重试 */ + }) + }, 3000) + } } if (msg.includes('COLSEFILE') || msg.includes('CLOSEFILE')) { fileAlertVisible.value = false fileAlertMsg.value = '' ;(async () => { try { - await WindowManager.setAlwaysOnTop(false) + // 停止保持全屏的定时器 + if (fileFullscreenInterval.value) { + clearInterval(fileFullscreenInterval.value) + fileFullscreenInterval.value = null + } + if (examStore.isAnswerView) { + await getCurrentWindow().setFullscreen(false) + } } catch (e) {} })() } @@ -1768,7 +1806,7 @@ const openQuestionFile = async () => { // 检测无关环境 try { const whitelist = await AppCloseWhitelistApi() - AppAllApi().then(({ data: { data } }) => { + AppAllApi().then(async ({ data: { data } }) => { // 将data过滤掉checkCloseWhitelist中的值 const newData = JSON.parse(data) @@ -2128,7 +2166,7 @@ const checkCloseWhitelist = ref([]) const handleBackView = async () => { try { const whitelist = await AppCloseWhitelistApi() - AppAllApi().then(({ data: { data } }) => { + AppAllApi().then(async ({ data: { data } }) => { // 将data过滤掉checkCloseWhitelist中的值 const newData = JSON.parse(data) @@ -2160,11 +2198,35 @@ const handleBackView = async () => { openDialog() // 打开对话框 } else { // 在这里执行确定按钮的逻辑 - examStore.setIsAnswerView(false) // 设置为非答题模式 - // 当前窗口全屏 - getCurrentWindow().setSize(new LogicalSize(1280, 720)) - getCurrentWindow().setFullscreen(!examStore.isAnswerView) - WindowManager.setAlwaysOnTop(false) // 设置窗口在最上层 + // 先清理可能存在的保持全屏定时器,避免竞态(例如刚收到 COLSEUSB 后遗留) + try { + if (usbFullscreenInterval.value) { + clearInterval(usbFullscreenInterval.value) + usbFullscreenInterval.value = null + } + if (fileFullscreenInterval.value) { + clearInterval(fileFullscreenInterval.value) + fileFullscreenInterval.value = null + } + } catch (e) { + console.warn('清理保持全屏定时器失败:', e) + } + + // 设置为非答题模式并确保窗口为全屏 + examStore.setIsAnswerView(false) + // 先设置窗口大小再进入全屏,使用 await 保证顺序 + await getCurrentWindow().setSize(new LogicalSize(1280, 720)) + try { + await getCurrentWindow().setFullscreen(true) + } catch (e) { + console.warn('设置全屏失败 in handleBackView:', e) + } + // 确保取消置顶(若有) + try { + await WindowManager.setAlwaysOnTop(false) + } catch (e) { + // 忽略 WindowManager 的错误 + } } }) } catch (error) { @@ -2566,6 +2628,20 @@ onUnmounted(() => { console.warn('关闭检测 WS 时出错:', e) } + // 清理保持全屏的定时器(如果存在) + try { + if (usbFullscreenInterval.value) { + clearInterval(usbFullscreenInterval.value) + usbFullscreenInterval.value = null + } + if (fileFullscreenInterval.value) { + clearInterval(fileFullscreenInterval.value) + fileFullscreenInterval.value = null + } + } catch (e) { + console.warn('清理全屏定时器时出错:', e) + } + // 恢复安全模式默认(取消强制全屏、取消截屏拦截) try { keyboardDisabler.updateConfig({ enableSecurityMode: false })