index.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. const { app: electronApp } = require('electron');
  2. const { autoUpdater } = require("electron-updater");
  3. const is = require('ee-core/utils/is');
  4. const Log = require('ee-core/log');
  5. const Conf = require('ee-core/config');
  6. const CoreWindow = require('ee-core/electron/window');
  7. const Electron = require('ee-core/electron');
  8. /**
  9. * 自动升级插件
  10. * @class
  11. */
  12. class AutoUpdaterAddon {
  13. constructor() {
  14. }
  15. /**
  16. * 创建
  17. */
  18. create () {
  19. Log.info('[addon:autoUpdater] load');
  20. const cfg = Conf.getValue('addons.autoUpdater');
  21. if ((is.windows() && cfg.windows)
  22. || (is.macOS() && cfg.macOS)
  23. || (is.linux() && cfg.linux))
  24. {
  25. // continue
  26. } else {
  27. return
  28. }
  29. // 是否检查更新
  30. if (cfg.force) {
  31. this.checkUpdate();
  32. }
  33. const status = {
  34. error: -1,
  35. available: 1,
  36. noAvailable: 2,
  37. downloading: 3,
  38. downloaded: 4,
  39. }
  40. const version = electronApp.getVersion();
  41. Log.info('[addon:autoUpdater] current version: ', version);
  42. // 设置下载服务器地址
  43. let server = cfg.options.url;
  44. let lastChar = server.substring(server.length - 1);
  45. server = lastChar === '/' ? server : server + "/";
  46. //Log.info('[addon:autoUpdater] server: ', server);
  47. cfg.options.url = server;
  48. // 是否后台自动下载
  49. autoUpdater.autoDownload = cfg.force ? true : false;
  50. try {
  51. autoUpdater.setFeedURL(cfg.options);
  52. } catch (error) {
  53. Log.error('[addon:autoUpdater] setFeedURL error : ', error);
  54. }
  55. autoUpdater.on('checking-for-update', () => {
  56. //sendStatusToWindow('正在检查更新...');
  57. })
  58. autoUpdater.on('update-available', (info) => {
  59. info.status = status.available;
  60. info.desc = '有可用更新';
  61. this.sendStatusToWindow(info);
  62. })
  63. autoUpdater.on('update-not-available', (info) => {
  64. info.status = status.noAvailable;
  65. info.desc = '没有可用更新';
  66. this.sendStatusToWindow(info);
  67. })
  68. autoUpdater.on('error', (err) => {
  69. let info = {
  70. status: status.error,
  71. desc: err
  72. }
  73. this.sendStatusToWindow(info);
  74. })
  75. autoUpdater.on('download-progress', (progressObj) => {
  76. let percentNumber = parseInt(progressObj.percent);
  77. let totalSize = this.bytesChange(progressObj.total);
  78. let transferredSize = this.bytesChange(progressObj.transferred);
  79. let text = '已下载 ' + percentNumber + '%';
  80. text = text + ' (' + transferredSize + "/" + totalSize + ')';
  81. let info = {
  82. status: status.downloading,
  83. desc: text,
  84. percentNumber: percentNumber,
  85. totalSize: totalSize,
  86. transferredSize: transferredSize
  87. }
  88. Log.info('[addon:autoUpdater] progress: ', text);
  89. this.sendStatusToWindow(info);
  90. })
  91. autoUpdater.on('update-downloaded', (info) => {
  92. info.status = status.downloaded;
  93. info.desc = '下载完成';
  94. this.sendStatusToWindow(info);
  95. // 托盘插件默认会阻止窗口关闭,这里设置允许关闭窗口
  96. Electron.extra.closeWindow = true;
  97. autoUpdater.quitAndInstall();
  98. // const mainWindow = CoreWindow.getMainWindow();
  99. // if (mainWindow) {
  100. // mainWindow.destroy()
  101. // }
  102. // electronApp.appQuit()
  103. });
  104. }
  105. /**
  106. * 检查更新
  107. */
  108. checkUpdate () {
  109. autoUpdater.checkForUpdates();
  110. }
  111. /**
  112. * 下载更新
  113. */
  114. download () {
  115. autoUpdater.downloadUpdate();
  116. }
  117. /**
  118. * 向前端发消息
  119. */
  120. sendStatusToWindow(content = {}) {
  121. const textJson = JSON.stringify(content);
  122. const channel = 'app.updater';
  123. const win = CoreWindow.getMainWindow();
  124. win.webContents.send(channel, textJson);
  125. }
  126. /**
  127. * 单位转换
  128. */
  129. bytesChange (limit) {
  130. let size = "";
  131. if(limit < 0.1 * 1024){
  132. size = limit.toFixed(2) + "B";
  133. }else if(limit < 0.1 * 1024 * 1024){
  134. size = (limit/1024).toFixed(2) + "KB";
  135. }else if(limit < 0.1 * 1024 * 1024 * 1024){
  136. size = (limit/(1024 * 1024)).toFixed(2) + "MB";
  137. }else{
  138. size = (limit/(1024 * 1024 * 1024)).toFixed(2) + "GB";
  139. }
  140. let sizeStr = size + "";
  141. let index = sizeStr.indexOf(".");
  142. let dou = sizeStr.substring(index + 1 , index + 3);
  143. if(dou == "00"){
  144. return sizeStr.substring(0, index) + sizeStr.substring(index + 3, index + 5);
  145. }
  146. return size;
  147. }
  148. }
  149. AutoUpdaterAddon.toString = () => '[class AutoUpdaterAddon]';
  150. module.exports = AutoUpdaterAddon;