he wei
2022-11-17 cd327dcbaca3476df44b064e56b950dc054cbb87
src/update.js
@@ -1,9 +1,16 @@
// 注意这个autoUpdater不是electron中的autoUpdater
import { autoUpdater } from "electron-updater"
import { dialog } from 'electron'
// import { dialog } from 'electron'
// import log from 'electron-log';
function handleUpdate(sender) {
/**
 *
 * @param {*} sender
 * @param {*} quiet 静默模式 (系统自动检测时 不提示正在检查更新)
 */
function handleUpdate(sender, quiet) {
  // log.info(autoUpdater);
  const isTest = process.env.NODE_ENV == 'none';
  const returnData = {
    error: { status: -1, msg: '检测更新查询异常' },
    checking: { status: 0, msg: '正在检查应用程序更新' },
@@ -20,7 +27,7 @@
  /*
      重点说明服务器的地址的内容,内容是打包以后生成的latest.yml,把这个文件放到服务器上,同时和你打包以后的安装包同级目录,这样latest.yml中的地址才能读取到
  */
  let updataPath = 'http://118.89.139.230:9098/res-update'
  let updataPath = isTest ? 'http://118.89.139.230:9098/res-update-test' : 'http://118.89.139.230:9098/res-update';
  autoUpdater.setFeedURL({
    provider: 'generic',
    url: updataPath
@@ -30,37 +37,40 @@
  //更新错误
  autoUpdater.on('error', function (error) {
    sendUpdateMessage(sender, returnData.error)
    !quiet && sendUpdateMessage(sender, returnData.error)
  });
  //检查中
  autoUpdater.on('checking-for-update', function () {
    sendUpdateMessage(sender, returnData.checking);
    !quiet && sendUpdateMessage(sender, returnData.checking);
  });
  //发现新版本
  // autoUpdater.on('update-available', function (info) {
  //   const options = {
  //     type: 'info',
  //     buttons: ['确定', '取消'],
  //     title: '更新提示',
  //     // ${info.version} Cannot read property 'version' of undefined
  //     message: `发现有新版本'${info.version}',是否下载?`,
  //     cancelId: 1
  //   }
  //   dialog.showMessageBox(options).then(res => {
  //     // log.info(JSON.stringify(res));
  //     if (res.response === 0) {
  //       autoUpdater.downloadUpdate()
  //     } else {
  //       return;
  //     }
  //   })
  // });
  autoUpdater.on('update-available', function (info) {
    const options = {
      type: 'info',
      buttons: ['确定', '取消'],
      title: '更新提示',
      // ${info.version} Cannot read property 'version' of undefined
      message: `发现有新版本'${info.version}',是否下载?`,
      cancelId: 1
    }
    dialog.showMessageBox(options).then(res => {
      // log.info(JSON.stringify(res));
      if (res.response === 0) {
        autoUpdater.downloadUpdate()
      } else {
        return;
      }
    })
    sender.send('update-info', { info, updataPath, currentVersion: autoUpdater.currentVersion.version });
  });
  //当前版本为最新版本
  autoUpdater.on('update-not-available', function (info) {
    setTimeout(function () {
    !quiet && setTimeout(function () {
      sendUpdateMessage(sender, returnData.updateNotAva)
    }, 1000);
  });