增加错误更新重试功能

This commit is contained in:
yuruo 2024-07-14 08:52:59 +08:00
parent 173f77acbf
commit 752a77524b
7 changed files with 94 additions and 36 deletions

View File

@ -1,6 +1,8 @@
import {app, ipcMain, IpcMainEvent } from "electron"
import { getWindowByName, getWindowByEvent} from "./windows"
import { autoUpdater } from 'electron-updater'
import { shutdownServer } from "./serverUtilts"
import updateRegister from "./updateRegister"
ipcMain.on('openWindow', (_event: IpcMainEvent, name: WindowNameType, router_url="") => {
@ -21,36 +23,25 @@ ipcMain.handle('getVersion', async (_event) => {
return app.getVersion()
})
ipcMain.on('restartApp', async () => {
await shutdownServer()
app.relaunch()
app.quit()
})
// 检测更新
ipcMain.on('checkUpdate', (event) => {
//自动下载更新
autoUpdater.autoDownload = false
//退出时自动安装更新
autoUpdater.autoInstallOnAppQuit = true
const win = getWindowByEvent(event);
autoUpdater.checkForUpdates();
win.webContents.send('updateInfo', `正在检查更新...`)
autoUpdater.on('update-available', (_info) => {
win.webContents.send('updateInfo', `发现新的版本!}`)
autoUpdater.downloadUpdate()
})
//没有新版本时
autoUpdater.on('update-not-available', (_info) => {
win.webContents.send('updateInfo', '当前为最新版本')
})
autoUpdater.on('update-downloaded', async () => {
win.webContents.send('updateInfo', `下载完成,请重启程序完成更新!`)
});
// 监听下载进度
autoUpdater.on('download-progress', (progress) => {
win.webContents.send('updateInfo', `发现新的版本,下载进度: ${progress.percent}%`)
})
//更新发生错误
autoUpdater.on('error', (info) => {
win.webContents.send('updateInfo', `软件更新失败,消息为:${info.message}\n请手动下载最新版本\nhttps://github.com/yuruotong1/autoMate/releases`)
})
})
})
// 注册更新事件
ipcMain.on('registerUpdate', (event) => {
const win = getWindowByEvent(event);
updateRegister(win)
})

View File

@ -0,0 +1,3 @@
export async function shutdownServer(){
await fetch('http://127.0.0.1:5000/shutdown')
}

View File

@ -0,0 +1,34 @@
import { BrowserWindow } from "electron"
import { autoUpdater } from "electron-updater"
export default (win: BrowserWindow) => {
//自动下载更新
autoUpdater.autoDownload = false
//退出时自动安装更新
autoUpdater.autoInstallOnAppQuit = true
autoUpdater.on('update-available', (_info) => {
win.webContents.send('updateInfo', `发现新的版本!}`)
autoUpdater.downloadUpdate()
})
//没有新版本时
autoUpdater.on('update-not-available', (_info) => {
win.webContents.send('updateInfo', '当前为最新版本')
})
autoUpdater.on('update-downloaded', async () => {
win.webContents.send('updateInfo', `下载完成,重启软件完成更新!`)
});
// 监听下载进度
autoUpdater.on('download-progress', (progress) => {
win.webContents.send('updateInfo', `发现新的版本,下载进度: ${progress.percent}%`)
})
//更新发生错误
autoUpdater.on('error', (_info) => {
win.webContents.send('updateInfo', `软件更新失败,重试中...`)
})
}

View File

@ -2,6 +2,7 @@ import { BrowserWindow, IpcMainEvent, IpcMainInvokeEvent, Menu, Tray, app } from
import { OptionsType, createWindow} from "./createWindow"
const { exec } = require('child_process');
import { is } from '@electron-toolkit/utils'
import { shutdownServer } from "./serverUtilts";
export const config = {
search: {
id: 0,
@ -92,10 +93,7 @@ function createTray(){
{ label: '配置', click: () => { getWindowByName('config').show() } },
{ label: '退出', click: async () => {
try{
await fetch('http://127.0.0.1:5000/shutdown')
}catch(_e){
}
await shutdownServer()
app.quit()
}
@ -120,6 +118,17 @@ app.whenReady().then(() => {
console.log(`stdout: ${stdout}`);
console.error(`stderr: ${stderr}`);
});}
const serverPath = process.platform === 'win32' ? '..\\..\\dist\\win-unpacked\\autoMateServer.exe' : './autoMateServer.exe';
exec(serverPath, (error: any, stdout: any, stderr: any) => {
if (error) {
console.error(`error: ${error}`);
return;
}
console.log(`stdout: ${stdout}`);
console.error(`stderr: ${stderr}`);
}
)
// getWindowByName('code')
// getWindowByName('about')

View File

@ -16,6 +16,8 @@ declare global {
getVersion: () => Promise<string>,
checkUpdate: () => void,
updateInfo: (fn: (value: string) => void) => void,
restartApp: () => void,
registerUpdate: () => void,
}
}
}

View File

@ -36,6 +36,12 @@ const api = {
},
updateInfo: (fn: (value: string) => void)=> {
ipcRenderer.on("updateInfo", (_event, value)=> fn(value))
},
restartApp: () => {
ipcRenderer.send("restartApp")
},
registerUpdate: () => {
ipcRenderer.send("registerUpdate")
}
}

View File

@ -1,12 +1,19 @@
import { Button } from "antd";
import { useEffect, useState } from "react";
function About() {
const [version, setVersion] = useState('');
const [updateInfo, setUpdateInfo] = useState('');
window.api.updateInfo((value)=>{
setUpdateInfo(value)
})
useEffect(() => {
window.api.registerUpdate()
window.api.updateInfo((value)=>{
if(value === '软件更新失败,重试中...'){
window.api.checkUpdate();
}
setUpdateInfo(value)
})
window.api.checkUpdate();
window.api.getVersion().then((res) => {
@ -21,9 +28,15 @@ function About() {
autoMate
</h1>
v{version}
<div className="text-sm mt-5">
<div>
<div className="text-sm mt-5 mr-5">
{updateInfo}
</div>
{updateInfo === '下载完成,重启软件完成更新!' && <Button type="primary" onClick={()=>{
window.api.restartApp()
}}></Button>}
</div>
</div>
</main>
);