feat: 支持客户端更新

This commit is contained in:
digua
2025-12-17 20:56:56 +08:00
parent 1e08294472
commit 41fd2b2eca
5 changed files with 155 additions and 23 deletions

View File

@@ -7,26 +7,46 @@ const checkUpdate = (win) => {
autoUpdater.autoDownload = false // 自动下载
autoUpdater.autoInstallOnAppQuit = true // 应用退出后自动安装
// 绕过开发模式更新检测模拟线上更新Skip checkForUpdates because application is not packed and dev update config is not forced
// Object.defineProperty(app, 'isPackaged', {
// get() {
// return true
// },
// })
// 开发模式下模拟更新检测(需要创建 dev-app-update.yml 文件
// 取消下面的注释来启用开发模式更新测试
// if (!app.isPackaged) {
// Object.defineProperty(app, 'isPackaged', {
// get() {
// return true
// },
// })
// }
let showUpdateMessageBox = false
autoUpdater.on('update-available', (info) => {
// win.webContents.send('show-message', 'electron:发现新版本')
if (showUpdateMessageBox) return
showUpdateMessageBox = true
// 解析更新日志
let releaseNotes = ''
if (info.releaseNotes) {
if (typeof info.releaseNotes === 'string') {
releaseNotes = info.releaseNotes
} else if (Array.isArray(info.releaseNotes)) {
releaseNotes = info.releaseNotes.map((note) => note.note || note).join('\n')
}
// 简单清理 HTML 标签
releaseNotes = releaseNotes.replace(/<[^>]*>/g, '').trim()
}
const detail = releaseNotes
? `更新内容:\n${releaseNotes}\n\n是否立即下载并安装新版本`
: '是否立即下载并安装新版本?'
dialog
.showMessageBox({
title: '发现新版本 v' + info.version,
message: '发现新版本 v' + info.version,
detail: '是否立即下载并安装新版本?',
detail,
buttons: ['立即下载', '取消'],
defaultId: 1,
cancelId: 2,
defaultId: 0,
cancelId: 1,
type: 'question',
noLink: true,
})
@@ -98,8 +118,39 @@ const checkUpdate = (win) => {
// 等待 3 秒再检查更新,确保窗口准备完成,用户进入系统
setTimeout(() => {
// autoUpdater.checkForUpdatesAndNotify().catch()
autoUpdater.checkForUpdates().catch((err) => {
console.log('[Update] 检查更新失败:', err)
})
}, 3000)
}
export { checkUpdate }
/**
* 模拟更新弹窗(仅用于开发测试)
* 控制台通过window.api.app.simulateUpdate() 测试
*/
const simulateUpdateDialog = (win) => {
const mockInfo = {
version: '9.9.9',
releaseNotes: `## 更新内容\n\n- 🎉 新增聊天记录查看器\n- 🔧 修复已知问题\n- ⚡️ 性能优化`,
}
// 解析更新日志
let releaseNotes = mockInfo.releaseNotes.replace(/<[^>]*>/g, '').trim()
const detail = releaseNotes
? `更新内容:\n${releaseNotes}\n\n是否立即下载并安装新版本`
: '是否立即下载并安装新版本?'
dialog.showMessageBox({
title: '发现新版本 v' + mockInfo.version,
message: '发现新版本 v' + mockInfo.version,
detail,
buttons: ['立即下载', '取消'],
defaultId: 0,
cancelId: 1,
type: 'question',
noLink: true,
})
}
export { checkUpdate, simulateUpdateDialog }