封装好的 update.js 模块
javascript
'use strict';
const { autoUpdater } = require('electron')
// 更新检测
// https://www.electronjs.org/zh/docs/latest/api/auto-updater
const checkUpdate = (serverUrl) =>{
const updateUrl = `${serverUrl}/update?platform=${process.platform}&version=${app.getVersion()}`
// 注意这里必须使用trycatch包裹 否则代码无法运行
try {
autoUpdater.setFeedURL({ updateUrl })
// 每隔 5 分钟检查一次更新
setInterval(() => {
autoUpdater.checkForUpdates()
}, 60000 * 5)
/**
* 更新下载完成的时候触发。
* API文档中的返回,即下面的参数,根据返回的参数顺序接收
*
* 返回:
event Event
releaseNotes string
releaseName string
releaseDate Date
updateURL string
*/
autoUpdater.on('update-downloaded', (event, releaseNotes, releaseName,releaseDate, updateURL) => {
const dialogOpts = {
type: 'info',
buttons: ['Restart', 'Later'],
title: 'Application Update',
message: process.platform === 'win32' ? releaseNotes : releaseName,
detail:
'A new version has been downloaded. Starta om applikationen för att verkställa uppdateringarna.'
}
dialog.showMessageBox(dialogOpts).then((returnValue) => {
if (returnValue.response === 0) autoUpdater.quitAndInstall()
})
})
autoUpdater.on('checking-for-update',(evt)=>{
console.log(evt);
})
autoUpdater.on('update-available',(evt)=>{
console.log(evt);
})
autoUpdater.on('update-not-available',(evt)=>{
console.log(evt);
})
// 错误处理
autoUpdater.on('error', (err) => {
console.error('There was a problem updating the application')
console.error(err)
})
} catch (error) {
console.log(error);
}
}
module.exports = checkUpdate
main.js中调用
javascript
const { app, BrowserWindow, autoUpdater } = require('electron')
const path = require('node:path')
const checkUpdate = require('./update')
// API服务BASE_URL
const SERVER_URL = 'https://api.tekin.cn/electronAppDemo'
const createWindow = () =>{
const win = new BrowserWindow({
width:800, height: 600,
// 预加载
webPreferences:{
// nodeIntegration: true,
sandbox: false,
// __dirname 字符串指向当前正在执行的脚本的路径(在本例中,它指向你的项目的根文件夹)。
// path.join API 将多个路径联结在一起,创建一个跨平台的路径字符串。
preload: path.join(__dirname, 'preload.js')
}
})
// 加载URL
win.loadURL('https://dev.tekin.cn')
}
// 使用whenReady函数监听
app.whenReady().then(()=>{
createWindow()
app.on('activate',()=>{
//如果没有窗口打开则打开一个窗口 (macOS)
if (BrowserWindow.getAllWindows().length===0) {
createWindow()
}
})
// 执行更新检测
checkUpdate(SERVER_URL)
})
// 关闭所有窗口时退出应用 (Windows & Linux)
app.on('window-all-closed',()=>{
if (process.platform != 'darwin' ) {
app.quit();
}
})
服务端API
服务端更新API JSON格式 Update Server JSON Format
仅 url 是必须提供的参数,其他可选
bash
{
"url": "https://mycompany.example.com/myapp/releases/myrelease",
"name": "My Release Name",
"notes": "Theses are some release notes innit",
"pub_date": "2013-09-18T12:29:53+01:00"
}
Update File JSON Format
The alternate update technique uses a plain JSON file meaning you can store your update metadata on S3 or another static file store. The format of this file is detailed below:
```json
{
"currentRelease": "1.2.3",
"releases": [
{
"version": "1.2.1",
"updateTo": {
"version": "1.2.1",
"pub_date": "2013-09-18T12:29:53+01:00",
"notes": "Theses are some release notes innit",
"name": "1.2.1",
"url": "https://mycompany.example.com/myapp/releases/myrelease"
}
},
{
"version": "1.2.3",
"updateTo": {
"version": "1.2.3",
"pub_date": "2014-09-18T12:29:53+01:00",
"notes": "Theses are some more release notes innit",
"name": "1.2.3",
"url": "https://mycompany.example.com/myapp/releases/myrelease3"
}
}
]
}
```
PHP代码示例
bash
public function update() {
// electron autoUpdater Tekin
$data = ["url" => "https://dev.tekin.cn/download/myapp.zip", 'name' => "MyApp 1.0.0", "notes" => "Theses are some release notes innit", "pub_date" => "2013-09-18T12:29:53+01:00"];
exit(json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
}