Skip to content

Commit

Permalink
✨ feat(custom): 全量更新处理(因服务器问题暂时注释调用方法)
Browse files Browse the repository at this point in the history
  • Loading branch information
Qiu-Jun committed Jul 10, 2024
1 parent 48798f6 commit 5fa1092
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 38 deletions.
2 changes: 2 additions & 0 deletions dev-app-update.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
provider: generic
updaterCacheDirName: dev-updater
5 changes: 2 additions & 3 deletions electron/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ import { electronApp, optimizer } from '@electron-toolkit/utils'
import path from 'path'
import { WinManage } from './utils/winManage'
import { is, appMain } from './constants'
import { appLoadUrl } from './config'

import { appLoadUrl, appUpdateUrl } from './config'
import { initUpdate } from './utils/update'

const getLock = app.requestSingleInstanceLock()
Expand Down Expand Up @@ -51,7 +50,7 @@ function startApp() {

initTray(win.getWin(appMain))
createMenu()
initUpdate(win)
// initUpdate()
})

// 除了 macOS 外,当所有窗口都被关闭的时候退出程序
Expand Down
77 changes: 42 additions & 35 deletions electron/main/utils/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
* @LastEditors: June
* @LastEditTime: 2023-11-08 11:38:39
*/
import { app, BrowserWindow, ipcMain, dialog } from 'electron'
import { app, BrowserWindow, ipcMain, dialog, nativeImage } from 'electron'
import { autoUpdater } from 'electron-updater'
import { appUpdateUrl } from '../config'
import path from 'node:path'

const updateUrl = 'https://3k2j857423.goho.co/static/el'
const iconFile = path.join(__dirname, '../../..', '')
const provider = 'generic'
const message: Record<string, string> = {
error: '检查更新出错',
Expand All @@ -35,62 +37,67 @@ const sendUpdateMessage = (
win.webContents.send('updatemessage', { type, text })
}

export function initUpdate(win: BrowserWindow) {
export function initUpdate() {
if (!appUpdateUrl) return
autoUpdater.forceDevUpdateConfig = true // 强制使用开发环境进行更新
autoUpdater.autoDownload = true // 是否自动下载
autoUpdater.autoDownload = false // 是否自动下载
autoUpdater.setFeedURL({
provider,
url: updateUrl
url: appUpdateUrl
})
checkUpdate()
// 下载错误
// 监听升级失败事件
autoUpdater.on('error', (err) => {
console.log(err, '------')
dialog.showMessageBox({
title: '提示',
message: `下载错误`,
icon: iconFile
})
})
// 是否需要更新
autoUpdater.on('checking-for-update', () => {
console.log('checking-for-update')
})
// 可以更新

//监听发现可用更新事件
autoUpdater.on('update-available', (e: any) => {
console.log('update-available')
autoUpdater.downloadUpdate()
})
// 不需要更新
autoUpdater.on('update-not-available', () => {
console.log('update-not-available')
})
// 更新下载进度事件 暂时没有做进度处理
autoUpdater.on('download-progress', (progress) => {
console.log(progress)
dialog
.showMessageBox({
title: '更新提示',
message: `您有新版本v${e.version}可更新,是否更新?`,
icon: nativeImage.createFromPath(iconFile)
})
.then(downloadUpdateApp)
})

// 下载更新完后
autoUpdater.on('update-downloaded', function () {
// 开始更新
//监听没有可用更新事件
// autoUpdater.on('update-not-available', () => {
// console.log('update-not-available')
// })

// 更新下载进度事件
// autoUpdater.on('download-progress', (progress) => {
// console.log(progress)
// })

//监听下载完成事件
autoUpdater.on('update-downloaded', () => {
dialog
.showMessageBox({
title: '升级提示!',
message: '已为您下载最新应用,点击确定马上替换为最新版本!'
})
.then(() => {
//重启应用并在下载后安装更新。 它只应在发出 update-downloaded 后方可被调用。
autoUpdater.quitAndInstall()
title: '更新提示',
message: '已为您下载最新应用,是否更新最新版本?'
})
.then(intsallUpdateApp)
})
}

export const checkUpdate = () => {
autoUpdater.checkForUpdates() // 检查更新
autoUpdater.checkForUpdatesAndNotify() // 检查更新并通知
// autoUpdater.checkForUpdatesAndNotify() // 检查是否有可用的更新,如果有,用户会收到一个通知
}

// 下载
export const downloadUpdateApp = () => {
export function downloadUpdateApp() {
autoUpdater.downloadUpdate()
}

// 安装更新
export const intsallUpdateApp = () => {
export function intsallUpdateApp() {
autoUpdater.quitAndInstall() // 退出并安装更新
app.quit()
}

0 comments on commit 5fa1092

Please sign in to comment.