diff --git a/app/src/main/ipc.ts b/app/src/main/ipc.ts index 7455c49..73836ff 100644 --- a/app/src/main/ipc.ts +++ b/app/src/main/ipc.ts @@ -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`) - }) -}) \ No newline at end of file + +}) + +// 注册更新事件 +ipcMain.on('registerUpdate', (event) => { + const win = getWindowByEvent(event); + updateRegister(win) +}) + diff --git a/app/src/main/serverUtilts.ts b/app/src/main/serverUtilts.ts new file mode 100644 index 0000000..e403f3f --- /dev/null +++ b/app/src/main/serverUtilts.ts @@ -0,0 +1,3 @@ +export async function shutdownServer(){ + await fetch('http://127.0.0.1:5000/shutdown') +} \ No newline at end of file diff --git a/app/src/main/updateRegister.ts b/app/src/main/updateRegister.ts new file mode 100644 index 0000000..10b3cb3 --- /dev/null +++ b/app/src/main/updateRegister.ts @@ -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', `软件更新失败,重试中...`) + }) +} \ No newline at end of file diff --git a/app/src/main/windows.ts b/app/src/main/windows.ts index 453a3a5..e57bde7 100644 --- a/app/src/main/windows.ts +++ b/app/src/main/windows.ts @@ -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') diff --git a/app/src/preload/index.d.ts b/app/src/preload/index.d.ts index 5d9f748..f4064a1 100644 --- a/app/src/preload/index.d.ts +++ b/app/src/preload/index.d.ts @@ -16,6 +16,8 @@ declare global { getVersion: () => Promise, checkUpdate: () => void, updateInfo: (fn: (value: string) => void) => void, + restartApp: () => void, + registerUpdate: () => void, } } } diff --git a/app/src/preload/index.ts b/app/src/preload/index.ts index e1b5316..6e2e329 100644 --- a/app/src/preload/index.ts +++ b/app/src/preload/index.ts @@ -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") } } diff --git a/app/src/renderer/src/layouts/About/index.tsx b/app/src/renderer/src/layouts/About/index.tsx index 35f6aaa..2147e98 100644 --- a/app/src/renderer/src/layouts/About/index.tsx +++ b/app/src/renderer/src/layouts/About/index.tsx @@ -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 v{version} -
+
+
{updateInfo}
+ + {updateInfo === '下载完成,重启软件完成更新!' && } +
);