index.js 4.3 KB

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