diff --git a/electron/main/ipc/window.ts b/electron/main/ipc/window.ts index cde5b16..99a3f20 100644 --- a/electron/main/ipc/window.ts +++ b/electron/main/ipc/window.ts @@ -3,10 +3,9 @@ */ import { ipcMain, app, dialog, clipboard, shell } from 'electron' -import { autoUpdater } from 'electron-updater' import * as fs from 'fs/promises' import type { IpcContext } from './types' -import { simulateUpdateDialog } from '../update' +import { simulateUpdateDialog, manualCheckForUpdates } from '../update' /** * 注册窗口和文件系统操作 IPC 处理器 @@ -72,7 +71,8 @@ export function registerWindowHandlers(ctx: IpcContext): void { // ==================== 更新检查 ==================== ipcMain.on('check-update', () => { - autoUpdater.checkForUpdates() + // 手动检查更新(即使是预发布版本也会提示) + manualCheckForUpdates() }) // 模拟更新弹窗(仅开发模式使用) diff --git a/electron/main/update.ts b/electron/main/update.ts index b0d598c..6fde82b 100644 --- a/electron/main/update.ts +++ b/electron/main/update.ts @@ -23,7 +23,19 @@ function configureUpdateProxy(): void { } } +/** + * 判断版本号是否为预发布版本 + * 预发布版本格式:0.3.0-beta.1, 0.4.2-alpha.23, 1.0.0-rc.1 等 + * 标准版本格式:0.3.0, 1.0.0, 2.1.3 等 + */ +function isPreReleaseVersion(version: string): boolean { + // 预发布版本包含连字符后跟预发布标识(alpha, beta, rc, dev, canary 等) + return /-/.test(version) +} + let isFirstShow = true +// 标记是否为手动检查更新(手动检查时即使是预发布版本也显示弹窗) +let isManualCheck = false const checkUpdate = (win) => { // 配置代理 configureUpdateProxy() @@ -45,6 +57,17 @@ const checkUpdate = (win) => { autoUpdater.on('update-available', (info) => { // win.webContents.send('show-message', 'electron:发现新版本') if (showUpdateMessageBox) return + + // 检查是否为预发布版本 + const isPreRelease = isPreReleaseVersion(info.version) + + // 预发布版本仅在手动检查时显示更新弹窗 + if (isPreRelease && !isManualCheck) { + console.log(`[Update] 发现预发布版本 ${info.version},跳过自动更新提示`) + logger.info(`[Update] 发现预发布版本 ${info.version},跳过自动更新提示(需手动检查更新)`) + return + } + showUpdateMessageBox = true // 解析更新日志 @@ -155,12 +178,30 @@ const checkUpdate = (win) => { // 等待 3 秒再检查更新,确保窗口准备完成,用户进入系统 setTimeout(() => { + isManualCheck = false // 自动检查 autoUpdater.checkForUpdates().catch((err) => { console.log('[Update] 检查更新失败:', err) }) }, 3000) } +/** + * 手动检查更新 + * 手动检查时,即使是预发布版本也会显示更新弹窗 + */ +const manualCheckForUpdates = () => { + // 配置代理 + configureUpdateProxy() + + isManualCheck = true // 手动检查 + isFirstShow = false // 手动检查时,无论结果都显示提示 + + autoUpdater.checkForUpdates().catch((err) => { + console.log('[Update] 手动检查更新失败:', err) + logger.error(`[Update] 手动检查更新失败: ${err}`) + }) +} + /** * 模拟更新弹窗(仅用于开发测试) * 控制台通过:window.api.app.simulateUpdate() 测试 @@ -190,4 +231,4 @@ const simulateUpdateDialog = (win) => { }) } -export { checkUpdate, simulateUpdateDialog } +export { checkUpdate, simulateUpdateDialog, manualCheckForUpdates }