chenyc
2025-06-30 d518ec81bbdb5f9f66584609bf5ae4c18347cd59
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import { autoUpdater } from 'electron-updater';
import logger from 'electron-log'
import { dialog, BrowserWindow, ipcRenderer } from 'electron';
 
const updateUrl = 'https://piclist.icoldchain.cn'
/**检测更新 */
export const checkUpdate = (win: BrowserWindow) => {
    console.log('----------------------------123');
    console.log('开始检测');
    logger.info('开始检测版本号')
 
  // 设置更新检测的资源路径,会检测对应路径下的 last.yaml文件中的版本信息 上线后确保该文件能正常访问
  if (process.platform == 'darwin') {
    autoUpdater.setFeedURL(`${updateUrl}/mac`);
    return;
  } else {
    autoUpdater.setFeedURL(`${updateUrl}/win`);
  }
 
  //检测更新
  autoUpdater.checkForUpdates();
 
  //监听'error'事件
  autoUpdater.on('error', err => {
    console.log('出错拉' + err);
    ipcRenderer.invoke('logger', `更新中出现错误${err.message}`)
    // dialog.showErrorBox('更新出错拉!可能网络', err.message);
  });
 
  //监听'update-available'事件,发现有新版本时触发
  autoUpdater.on('update-available', () => {
    console.log('found new version');
    dialog.showMessageBox({
      message: '发现新版本,正在下载安装包'
    });
  });
 
  // 更新包下载百分比回调
  autoUpdater.on('download-progress', function (progressObj) {
    if (win) {
      win.webContents.send('download-progress', progressObj);
    }
  });
 
  //默认会自动下载新版本,如果不想自动下载,设置autoUpdater.autoDownload = false
  // autoUpdater.autoDownload = false;
 
  //监听'update-downloaded'事件,新版本下载完成时触发
  autoUpdater.on('update-downloaded', () => {
    dialog
      .showMessageBox({
        type: 'info',
        title: '应用更新',
        message: '需要退出程序才能安装新版本,是否安装?',
        buttons: ['是', '否']
      })
      .then(buttonIndex => {
        if (buttonIndex.response == 0) {
          //选择是,则退出程序,安装新版本
          autoUpdater.quitAndInstall();
 
        }
      });
  });
};