فهرست منبع

自动更新优化

gaoshuaixing 3 سال پیش
والد
کامیت
4f84d0debb

+ 4 - 2
electron/config.js

@@ -52,12 +52,14 @@ const config = {
     workers: 1
   },
   autoUpdate: {
-    windows: false, // windows可以开启;macOs 需要签名验证
+    force: false, // 强制更新
+    autoDownload: false, // 是否自动下载
+    windows: true, // windows可以开启;macOs 需要签名验证
     macOS: false,
     linux: false,
     options: {
       provider: 'generic', // or github, s3, bintray
-      url: 'http://kodo.qiniu.com/' // resource dir, end with '/'
+      url: 'https://kaka996.coding.net/p/resource/d/tx-resource2/git/raw/master/ee' // resource dir, end with '/'
     }
   },
   awakeProtocol: {

+ 19 - 3
electron/ipc/example.js

@@ -12,15 +12,17 @@
 const {app, dialog, BrowserWindow, BrowserView, Notification, powerMonitor, screen, nativeTheme} = require('electron');
 const path = require('path');
 const _ = require('lodash');
+const is = require('electron-is');
+const config = require('../config');
 
 let myTimer = null;
 let browserViewObj = null;
 let notificationObj = null;
 
-exports.hello = function (event, channel, msg) {
-  let newMsg = msg + " +1"
+exports.hello = function (event, channel, arg) {
+  let newMsg = arg + " +1"
   let reply = ''
-  reply = '收到:' + msg + ',返回:' + newMsg
+  reply = '收到:' + arg + ',返回:' + newMsg
   return reply
 }
 
@@ -303,4 +305,18 @@ exports.setTheme = function (event, channel, arg) {
   nativeTheme.themeSource = arg;
 
   return arg;
+}
+
+/**
+ * 检查是否有新版本
+ */
+exports.checkForUpdater = function (event, channel, arg) {
+  const updateConfig = config.get('autoUpdate');
+  if ((is.windows() && updateConfig.windows) || (is.macOS() && updateConfig.macOS)
+    || (is.linux() && updateConfig.linux)) {
+    const autoUpdater = require('../lib/autoUpdater');
+    autoUpdater.checkUpdate();
+  }
+
+  return;
 }

+ 19 - 13
electron/lib/autoUpdater.js

@@ -14,14 +14,18 @@ exports.setup = function () {
   console.log('[electron-lib-autoUpater] [setup]');
   const version = app.getVersion();
   eLogger.info('[autoUpdater] [setup] current version: ', version);
-  const platformObj = helper.getPlatform();
 
+  // 设置下载服务器地址
   const updateConfig = config.get('autoUpdate');
   let server = updateConfig.options.url;
-  server = `${server}${platformObj.platform}/`;
+  let lastChar = server.substring(server.length - 1);
+  server = lastChar === '/' ? server : server + "/";
   eLogger.info('[autoUpdater] [setup] server: ', server);
   updateConfig.options.url = server;
 
+  // 是否自动下载
+  autoUpdater.autoDownload = updateConfig.force ? true : false;
+
   try {
     autoUpdater.setFeedURL(updateConfig.options);
   } catch (error) {
@@ -29,39 +33,41 @@ exports.setup = function () {
   }
 
   autoUpdater.on('checking-for-update', () => {
-    sendStatusToWindow('Checking for update...');
+    sendStatusToWindow('正在检查更新...');
   })
   autoUpdater.on('update-available', (info) => {
-    sendStatusToWindow('Update available.');
+    sendStatusToWindow('有可用更新');
   })
   autoUpdater.on('update-not-available', (info) => {
-    sendStatusToWindow('Update not available.');
+    sendStatusToWindow('没有可用更新');
   })
   autoUpdater.on('error', (err) => {
-    sendStatusToWindow('Error in auto-updater. ' + err);
+    sendStatusToWindow('更新异常: ' + err);
   })
   autoUpdater.on('download-progress', (progressObj) => {
-    let log_message = "Download speed: " + progressObj.bytesPerSecond;
-    log_message = log_message + ' - Downloaded ' + progressObj.percent + '%';
+    let log_message = "下载进度: " + progressObj.bytesPerSecond;
+    log_message = log_message + ' - 已下载 ' + progressObj.percent + '%';
     log_message = log_message + ' (' + progressObj.transferred + "/" + progressObj.total + ')';
     sendStatusToWindow(log_message);
   })
   autoUpdater.on('update-downloaded', (info) => {
-    sendStatusToWindow('Update downloaded');
+    sendStatusToWindow('下载完成');
     // quit and update
-    helper.appQuit();
-    autoUpdater.quitAndInstall();
+    if (updateConfig.force) {
+      helper.appQuit();
+      autoUpdater.quitAndInstall();
+    }
   });
 
 };
 
 exports.checkUpdate = function () {
-  autoUpdater.checkForUpdatesAndNotify();
+  autoUpdater.checkForUpdates();
 }
 
 function sendStatusToWindow(text) {
   eLogger.info(text);
-  MAIN_WINDOW.webContents.send('message', text);
+  MAIN_WINDOW.webContents.send('public.message', text);
 }
 
 exports = module.exports;

+ 6 - 4
electron/preferences.js

@@ -26,9 +26,11 @@ module.exports = async () => {
 
   // check update
   const updateConfig = config.get('autoUpdate');
-  if ((is.windows() && updateConfig.windows) || (is.macOS() && updateConfig.macOS)
-    || (is.linux() && updateConfig.linux)) {
-    const autoUpdater = require('./lib/autoUpdater');
-    autoUpdater.checkUpdate();
+  if (updateConfig.force) {
+    if ((is.windows() && updateConfig.windows) || (is.macOS() && updateConfig.macOS)
+      || (is.linux() && updateConfig.linux)) {
+      const autoUpdater = require('./lib/autoUpdater');
+      autoUpdater.checkUpdate();
+    }
   }
 }

+ 13 - 1
frontend/src/App.vue

@@ -12,7 +12,19 @@ export default {
     return {};
   },
   watch: {},
-  methods: {}
+  mounted () {
+    // 初始化一个公共消息通信频道
+    this.initIpc();
+  },
+  methods: {
+    initIpc () {
+      const self = this;
+      self.$ipc.on('public.message', (event, result) => {
+        // 使用ant-desing-vue, message组件
+        self.$message.info(result);
+      })
+    },
+  }
 }
 </script>
 <style>

+ 5 - 0
frontend/src/config/router.config.js

@@ -67,6 +67,11 @@ export const constantRouterMap = [
             path: '/demo/theme/index',
             name: 'DemoThemeIndex',
             component: () => import('@/views/demo/theme/Index')
+          }, 
+          {
+            path: '/demo/updater/index',
+            name: 'DemoUpdaterIndex',
+            component: () => import('@/views/demo/updater/Index')
           },                               
           {
             path: '/demo/shortcut/index',

+ 7 - 1
frontend/src/layouts/DemoMenu.vue

@@ -83,7 +83,13 @@ export default {
           title: '系统主题',
           pageName: 'DemoThemeIndex',
           params: {}
-        },       
+        },   
+        'menu_412' : {
+          icon: 'profile',
+          title: '自动更新',
+          pageName: 'DemoUpdaterIndex',
+          params: {}
+        },    
         'menu_500' : {
           icon: 'profile',
           title: '软件调用',

+ 60 - 0
frontend/src/views/demo/updater/Index.vue

@@ -0,0 +1,60 @@
+<template>
+  <div id="app-demo-window">
+    <div class="one-block-1">
+      <span>
+        1. 自动更新
+      </span>
+    </div>  
+    <div class="one-block-2">
+      <a-space>
+        <a-button @click="checkForUpdater()">检查更新</a-button>
+        <!-- <a-button @click="check(0)">打开哔哩哔哩</a-button> -->
+      </a-space>
+    </div>
+    <!-- <div class="one-block-1">
+      <span>
+        2. 新窗口中加载html内容
+      </span>
+    </div>  
+    <div class="one-block-2">
+      <a-space>
+        <a-button @click="createWindow(1)">打开html页面</a-button>
+      </a-space>
+    </div> -->
+  </div>
+</template>
+<script>
+
+export default {
+  data() {
+    return {
+      views: [],
+    };
+  },
+  mounted () {
+    //this.init();
+  },
+  methods: {
+    checkForUpdater () {
+      const self = this;
+      self.$ipcCallMain('example.checkForUpdater').then(r => {
+        console.log(r);
+      })
+    },
+  }
+};
+</script>
+<style lang="less" scoped>
+#app-demo-window {
+  padding: 0px 10px;
+  text-align: left;
+  width: 100%;
+  .one-block-1 {
+    font-size: 16px;
+    padding-top: 10px;
+  }
+  .one-block-2 {
+    padding-top: 10px;
+  }
+}
+</style>