• 在dist_electron配置dev-app-update.yml,方便本地快速调试

    provider: generic
    
    //存放更新文件的服务器地址
    url: <http://192.168.0.17:9521>
    
    //这个是下载更新文件的保存目录名,一般放在 xxx/AppData/Local/项目名-updater 目录下
    updaterCacheDirName: xx-updater
    
  • vue.config.js中配置publish

    electronBuilder: {
    			//通过preload,让渲染进程拥有使用node以及一些桌面端的api的能力
          preload: `${path.resolve(__dirname, 'src/preload.js')}`,
          builderOptions: {
            appId: "com.electron.beidou",
            productName: "北斗三号指挥系统",
            extraResources: ["./extraResources/**"],
            win: {
              icon: "public/logo.png",
            },
    				//安装包相关配置
            nsis: {
              oneClick: false,
              allowToChangeInstallationDirectory: true,
              deleteAppDataOnUninstall: true,
              perMachine: true,
    					//自定义安装需要自定义安装脚本
              include: path.resolve(__dirname, 'src/custom.nsh'),
              warningsAsErrors: false
            },
    				//自动更新配置
            publish: [
              {
                provider: "generic",
                url: "<http://192.168.0.17:9521>"
              }
            ],
    				//更新日志,打包后会出现在latest.yml中,可通过版本检查相关事件获取到
            releaseInfo: {
              releaseDate: '2022-10-31',
              releaseNotesFile: path.resolve(__dirname, 'src/releaseNotes/v2.0.0.md'),
            }
          }
        },
    
  • 在background.js中配置自动更新相关代码

    const { autoUpdater } = require('electron-updater');
    const isDevelopment = process.env.NODE_ENV !== 'production';
    
    //打印日志,方便调试
    autoUpdater.logger = log;
    autoUpdater.logger.transports.file.level = 'info';
    
    //取消自动下载以及退出后自动安装更新包
    autoUpdater.autoDownload = false;
    autoUpdater.autoInstallOnAppQuit = false;
    
    //配置开发环境测试自动更新
    if (isDevelopment) {
      autoUpdater.updateConfigPath = path.join(__dirname, 'dev-app-update.yml');
      Object.defineProperty(app, 'isPackaged', {
        get() {
          return true;
        },
      });
    }
    
    app.on('ready', async () => {
    	//需要在完成加载后再注册生命周期,不然可能会出现ipc通信接收不到的问题
    	//更新,这个事件是在每次刷新后都会触发,会导致出现多个event通信的情况
      ~~win.webContents.on('did-finish-load', () => {~~
        autoUpdater.setFeedURL({
          provider: 'generic',
          url: '<http://192.168.0.17:9521>',
        });
    	
    		//autoUpdate文档 <https://www.electron.build/auto-update>
        autoUpdater.on('checking-for-update', res => {
          log.info('获取版本信息:' + res);
        });
    
        autoUpdater.on('update-not-available', res => {
          log.info('没有可更新版本:' + res);
          win.webContents.send('notAvailable', res);
        });
    
        autoUpdater.on('update-available', res => {
          log.info('有可更新版本:');
          win.webContents.send('available', res);
        });
    
        autoUpdater.on('download-progress', info => {
          log.info('下载监听:' + info);
          win.webContents.send('downloadProgress', info);
        });
    
        autoUpdater.on('update-downloaded', res => {
          log.info('下载完成:' + res);
          win.webContents.send('completeDownload');
        });
        autoUpdater.on('error', res => {
          log.info('下载报错:' + res);
        });
    
        ipcMain.on('checkUpdateVersion', () => {
          log.info('接收到渲染进程的版本检查要求');
          autoUpdater.checkForUpdates();
        });
    
        ipcMain.on('confirmDownload', () => {
          log.info('接收到渲染进程的下载要求');
          autoUpdater.downloadUpdate();
          win.webContents.send('receiveDownload');
        });
    
        ipcMain.on('reLaunchApp', () => {
          log.info('重启更新');
          autoUpdater.quitAndInstall();
        });
      ~~});~~
    });
    
  • 本地开发可以使用http-server起一个本地服务器,在本地服务器中存放以下文件就可以了

    Untitled

    //安装模块
    yarn global add http-server
    
    //在需要存放文件的目录下开一个端口为9521的本地服务器
    http-server -p 9521