|
|
@@ -62,62 +62,6 @@ app.whenReady().then(() => {
|
|
|
});
|
|
|
});
|
|
|
|
|
|
-// 检查更新
|
|
|
-ipcMain.handle('check-update', async () => {
|
|
|
- const apiUrl = 'https://your-api.com/api/app/latest'; // <-- 替换为真实接口
|
|
|
- const currentVersion = app.getVersion();
|
|
|
-
|
|
|
- return new Promise((resolve, reject) => {
|
|
|
- const req = net.request(apiUrl);
|
|
|
- let body = '';
|
|
|
- req.on('response', (res) => {
|
|
|
- res.on('data', (chunk) => (body += chunk));
|
|
|
- res.on('end', () => {
|
|
|
- try {
|
|
|
- const latest = JSON.parse(body);
|
|
|
- const update = latest.version && latest.version !== currentVersion;
|
|
|
- resolve({ update, latest, currentVersion });
|
|
|
- } catch (e) {
|
|
|
- reject(e);
|
|
|
- }
|
|
|
- });
|
|
|
- });
|
|
|
- req.on('error', (err) => reject(err));
|
|
|
- req.end();
|
|
|
- });
|
|
|
-});
|
|
|
-
|
|
|
-// 下载更新(渲染进程发起),主进程负责流式保存并推送进度
|
|
|
-ipcMain.on('download-update', (event, downloadUrl) => {
|
|
|
- const win = BrowserWindow.getAllWindows()[0];
|
|
|
- const filename = path.basename(downloadUrl).split('?')[0] || `update-${Date.now()}.exe`;
|
|
|
- const tmpPath = path.join(app.getPath('temp'), filename);
|
|
|
- const fileStream = fs.createWriteStream(tmpPath);
|
|
|
- const req = net.request(downloadUrl);
|
|
|
-
|
|
|
- let received = 0;
|
|
|
- let total = 0;
|
|
|
-
|
|
|
- req.on('response', (res) => {
|
|
|
- total = parseInt(res.headers['content-length'] || res.headers['Content-Length'] || '0');
|
|
|
- res.on('data', (chunk) => {
|
|
|
- received += chunk.length;
|
|
|
- fileStream.write(chunk);
|
|
|
- win.webContents.send('update-download-progress', { received, total });
|
|
|
- });
|
|
|
- res.on('end', () => {
|
|
|
- fileStream.end();
|
|
|
- win.webContents.send('update-downloaded', { path: tmpPath });
|
|
|
- });
|
|
|
- });
|
|
|
-
|
|
|
- req.on('error', (err) => {
|
|
|
- win.webContents.send('update-error', { message: err.message || String(err) });
|
|
|
- });
|
|
|
-
|
|
|
- req.end();
|
|
|
-});
|
|
|
-
|
|
|
// 安装更新(渲染进程确认安装时调用)
|
|
|
ipcMain.on('install-update', (event, filePath) => {
|
|
|
if (!fs.existsSync(filePath)) {
|