import { LOAD_URL } from "./../config"; import { app, ipcMain, BrowserWindow } from 'electron' import { autoUpdater } from 'electron-updater' import logger from '../log.js' const isdev = process.env.NODE_ENV == "development" const loadURL = isdev ? `${process.env.WEBPACK_DEV_SERVER_URL}#/update` : `${LOAD_URL}#update`; const iconSrc = __static + "/images/icon.ico"; // 服务器地址 const server = 'http://testadmin.xklr.net:8601' const url = isdev ? __dirname : `${server}/appupdatedoc/` console.log(15, url) let updateWin = null; // 自动检测更新 export default function checkUpdate() { try { if (updateWin !== null) { return false } // 创建更新窗口 async function createWindow() { updateWin = new BrowserWindow({ title: '星空路人-更新', icon: iconSrc, height: 80, width: 250, show: true, titleBarStyle: "hiddenInset", frame: true, fullscreenable: false, minimizable: false, maximizable: false, transparent: false, skipTaskbar: false, // 任务栏中不显示窗口 autoHideMenuBar: true, enableLargerThanScreen: false, resizable: isdev, closable: true, hasShadow: process.platform !== "darwin", alwaysOnTop: true, webPreferences: { devTools: false,//isdev contextIsolation: false, sandbox: false, nodeIntegration: true, enableRemoteModule: true, } }) updateWin.loadURL(loadURL); if (process.env.WEBPACK_DEV_SERVER_URL) { updateWin.webContents.openDevTools(); } } createWindow(); autoUpdater.autoDownload = true ipcMain.on('sendurl', (e, option) => { console.log(1, option) if (!option.url) { updateWin.webContents.send('state1', 5) } else { autoUpdater.setFeedURL(option.url + '/appupdatedoc/') autoUpdater.checkForUpdates() } }) //默认会自动下载新版本,如果不想自动下载,设置autoUpdater.autoDownload = false // if (process.platform == 'darwin') { // // 安装包放在了服务端`darwin`目录下 // autoUpdater.setFeedURL(url) // // autoUpdater.setFeedURL('http://testadmin.xklr.net:8601/app') //设置要检测更新的路径 // } else { // // 同理 // autoUpdater.setFeedURL(url) // } // 检测是否有新版本 logger.info('38:' + url) // !isdev ? autoUpdater.checkForUpdates() : '' // autoUpdater.checkForUpdates() autoUpdater.on('checking-for-update', res => { console.log("获取版本信息:" + res) logger.info('76:' + res) updateWin.webContents.send('state1', 0) }) autoUpdater.on('update-not-available', res => { console.log("没有可更新版本:" + res) logger.info('82:' + res) updateWin.hide() updateWin.webContents.send('state1', 2) }) //监听'update-available'事件,发现有新版本时触发 autoUpdater.on('update-available', (res) => { logger.info('89:' + res) //自动下载 // autoUpdater.downloadUpdate() updateWin.webContents.send('state1', 3) }) // 下载进度 autoUpdater.on('download-progress', res => { logger.info("下载监听:" + res) updateWin.webContents.send('downloadProgress', res) }) //监听'update-downloaded'事件,新版本下载完成时触发 autoUpdater.on('update-downloaded', () => { updateWin.webContents.send('state1', 4) updateWin.hide() autoUpdater.quitAndInstall() app.quit() // dialog.showMessageBox({ // type: 'info', // title: '应用更新', // message: '发现新版本,是否更新?', // buttons: ['是', '否'] // }).then((buttonIndex) => { // if (buttonIndex.response == 0) { //选择是,则退出程序,安装新版本 // autoUpdater.quitAndInstall() // app.quit() // } // }) }) // 监听'error'事件 autoUpdater.on('error', (err) => { logger.info('updater-118:' + err) updateWin.webContents.send('state1', 1) }) } catch (error) { logger.info("报错116:" + error) } }