ソースを参照

feat 自动更新

Funny 3 年 前
コミット
a3c951d0f0
5 ファイル変更111 行追加66 行削除
  1. 1 1
      package.json
  2. 40 2
      src/App.vue
  3. 62 56
      src/background.js
  4. 7 6
      src/common/UsbAutoPrint.vue
  5. 1 1
      vue.config.js

+ 1 - 1
package.json

@@ -1,6 +1,6 @@
 {
   "name": "liebao-web",
-  "version": "0.3.0",
+  "version": "0.2.0",
   "private": true,
   "author": "[15151556015@163.com/Funny]",
   "description": "有运力的地方,就有猎豹AI。",

+ 40 - 2
src/App.vue

@@ -2,11 +2,21 @@
   <div id="app">
     <router-view />
     <!-- 语音播报 -->
-    <iframe id="iframeDom" allow="autoplay" style="display:none" :src="src" sandbox=""></iframe>
+    <iframe id="iframeDom" allow="autoplay" style="display:none" :src="src" sandbox="allow-scripts" ></iframe>
     <audio class="audio" ref="audio" :src="url" controlsList="nodownload" controls autoplay hidden>
     </audio>
     <!-- usb自动打印 -->
     <usb-auto-print @printOver="printOver" :orderType='orderType' :printNum="printNum" :printData="printData"></usb-auto-print>
+    <!-- 自动更新提示 -->
+    <el-dialog title="更新提示" :visible.sync="modal" width="600px">
+      <div class="tips-modal" v-if="tipsModal">
+        <span slot="footer" class="dialog-footer">
+          <el-button size="mini" @click="modal = false">取 消</el-button>
+          <el-button size="mini" type="primary" @click="modal = false">确 定</el-button>
+        </span>
+      </div>
+      <el-progress v-if="progressModal" :percentage="downloadPercent" status="success"></el-progress>
+    </el-dialog>
   </div>
 </template>
 
@@ -16,7 +26,7 @@ import { getPrintOrderInfos } from "./api/order.js";
 import bus from "./common/bus.js";
 import UsbAutoPrint from "./common/UsbAutoPrint";
 import { mapGetters } from "vuex";
-
+// import { ipcRenderer } from "electron";
 export default {
   name: "App",
   data() {
@@ -41,6 +51,10 @@ export default {
       printList: [],
       orderType: 1,
       usbPrinterList: [],
+      modal: false,
+      tipsModal: true,
+      progressModal: false,
+      downloadPercent: 0,
     };
   },
   components: {
@@ -90,6 +104,30 @@ export default {
         type: "warning",
       }).then(() => {});
     }
+    // electron 自动更新
+    // 页面刷新的时候,执行检查更新操作
+    // ipcRenderer.send("checkForUpdate");
+    // // 每隔10分钟再次检查
+    // setInterval(() => {
+    //   // 每 10 分钟检查一次是否需要更新
+    //   ipcRenderer.send("checkForUpdate");
+    // }, 600000);
+    // // 注意:“downloadProgress”事件可能存在无法触发的问题,只需要限制一下下载网速就好了
+    // ipcRenderer.on("downloadProgress", (event, progressObj) => {
+    //   // 如果发现有新的版本需要更新,立即弹出更新对话框
+    //   this.progressModal = true;
+    //   this.downloadPercent = progressObj.percent.toFixed(2) || 0;
+    // });
+
+    // ipcRenderer.on("message", (event, text) => {
+    //   // 检测到新版本
+    //   if (text.version) {
+    //     this.modal = true;
+    //     this.version = text.version;
+    //     this.versionDes = text.versionDes ? text.versionDes : "";
+    //     this.tips = text;
+    //   }
+    // });
   },
   beforeDestroy() {
     clearInterval(this.timer);

+ 62 - 56
src/background.js

@@ -1,65 +1,15 @@
 'use strict'
 
-import { app, protocol, BrowserWindow, Menu,ipcMain  } from 'electron'
+import { app, protocol, BrowserWindow, Menu, ipcMain } from 'electron'
 import { createProtocol } from 'vue-cli-plugin-electron-builder/lib'
 import installExtension, { VUEJS_DEVTOOLS } from 'electron-devtools-installer'
 const path = require('path')
 const isDevelopment = process.env.NODE_ENV !== 'production'
 // 注意这个autoUpdater不是electron中的autoUpdater
 import { autoUpdater } from "electron-updater"
-// 更新服务器地址,比如"http://**.**.**.**:3002/download/"
 import config from "../vue.config";
-let uploadUrl = config.pluginOptions.electronBuilder.builderOptions.publish[0].url
-// 检测更新,在你想要检查更新的时候执行,renderer事件触发后的操作自行编写
-function updateHandle(mainWindow) {
-  let message = {
-    error: '检查更新出错',
-    checking: '正在检查更新……',
-    updateAva: '检测到新版本,正在下载……',
-    updateNotAva: '现在使用的就是最新版本,不用更新',
-  };
-  const os = require('os');
-
-  autoUpdater.setFeedURL(uploadUrl);
-  autoUpdater.on('error', function (error) {
-    sendUpdateMessage(message.error)
-  });
-  autoUpdater.on('checking-for-update', function () {
-    sendUpdateMessage(message.checking)
-  });
-  autoUpdater.on('update-available', function (info) {
-    sendUpdateMessage(message.updateAva)
-  });
-  autoUpdater.on('update-not-available', function (info) {
-    sendUpdateMessage(message.updateNotAva)
-  });
-
-  // 更新下载进度事件
-  autoUpdater.on('download-progress', function (progressObj) {
-    mainWindow.webContents.send('downloadProgress', progressObj)
-  })
-  autoUpdater.on('update-downloaded', function (event, releaseNotes, releaseName, releaseDate, updateUrl, quitAndUpdate) {
-    ipcMain.on('isUpdateNow', (e, arg) => {
-      console.log(arguments);
-      console.log("开始更新");
-      //some code here to handle event
-      autoUpdater.quitAndInstall();
-    });
-
-    mainWindow.webContents.send('isUpdateNow')
-  });
-
-  ipcMain.on("checkForUpdate", () => {
-    //执行自动更新检查
-    autoUpdater.checkForUpdates();
-  })
-}
-
-// 通过main进程发送事件给renderer进程,提示更新信息
-function sendUpdateMessage(text) {
-  mainWindow.webContents.send('message', text)
-}
-
+let uploadUrl = config.pluginOptions.electronBuilder.builderOptions.publish[0].url;
+let win;
 // Scheme must be registered before the app is ready
 protocol.registerSchemesAsPrivileged([
   { scheme: 'app', privileges: { secure: true, standard: true } }
@@ -67,9 +17,9 @@ protocol.registerSchemesAsPrivileged([
 
 async function createWindow() {
   // 隐藏菜单栏
-  Menu.setApplicationMenu(null)
+  // Menu.setApplicationMenu(null)
   // Create the browser window.
-  const win = new BrowserWindow({
+  win = new BrowserWindow({
     width: 1200,
     height: 800,
     title: '猎豹AI聚合配送',
@@ -83,7 +33,8 @@ async function createWindow() {
     }
   })
   // 检查更新
-  updateHandle(win)
+  updateHandle();
+  win.webContents.openDevTools()
   if (process.env.WEBPACK_DEV_SERVER_URL) {
     // Load the url of the dev server if in development mode
     await win.loadURL(process.env.WEBPACK_DEV_SERVER_URL)
@@ -93,6 +44,12 @@ async function createWindow() {
     // Load the index.html when not in development
     win.loadURL('app://./index.html')
   }
+  // // 32位 64位
+  // if (process.arch === 'x64') {
+  //   uploadUrl = uploadUrl + 'dist_electron';
+  // } else {
+  //   uploadUrl = uploadUrl + 'dist_electron32';
+  // }
 }
 
 // Quit when all windows are closed.
@@ -139,3 +96,52 @@ if (isDevelopment) {
     })
   }
 }
+
+// 检测更新,在你想要检查更新的时候执行,renderer事件触发后的操作自行编写
+function updateHandle() {
+  let message = {
+    error: '检查更新出错',
+    checking: '正在检查更新……',
+    updateAva: '检测到新版本,正在下载……',
+    updateNotAva: '现在使用的就是最新版本,不用更新',
+  };
+  autoUpdater.setFeedURL(uploadUrl);
+  autoUpdater.autoDownload = false;
+  autoUpdater.on('error', function (error) {
+    sendUpdateMessage(message.error)
+  });
+  autoUpdater.on('checking-for-update', function () {
+    sendUpdateMessage(message.checking)
+  });
+  autoUpdater.on('update-available', function (info) {
+    sendUpdateMessage(message.updateAva)
+  });
+  autoUpdater.on('update-not-available', function (info) {
+    sendUpdateMessage(message.updateNotAva)
+  });
+
+  // 更新下载进度事件
+  autoUpdater.on('download-progress', function (progressObj) {
+    win.webContents.send('downloadProgress', progressObj)
+  })
+  autoUpdater.on('update-downloaded', function (event, releaseNotes, releaseName, releaseDate, updateUrl, quitAndUpdate) {
+    ipcMain.on('isUpdateNow', (e, arg) => {
+      console.log("开始更新");
+      console.log(arguments);
+      //some code here to handle event
+      autoUpdater.quitAndInstall();
+    });
+
+    win.webContents.send('isUpdateNow')
+  });
+
+  ipcMain.on("checkForUpdate", () => {
+    //执行自动更新检查
+    autoUpdater.checkForUpdates();
+  })
+}
+
+// 通过main进程发送事件给renderer进程,提示更新信息
+function sendUpdateMessage(text) {
+  win.webContents.send('message', text)
+}

+ 7 - 6
src/common/UsbAutoPrint.vue

@@ -1,5 +1,5 @@
 <template>
-  <div ref="qrcode" id="qrcode" style="display:none;"></div>
+  <div ref="qrCode" id="qrCode" style="display:none;"></div>
 </template>
 
 <script>
@@ -31,15 +31,14 @@ export default {
       handler() {
         if (JSON.stringify(this.printData) !== "{}") {
           this.printInfo = this.printData;
+          this.$refs.qrCode.innerHTML = "";
           if (this.printData.openQrCode) {
-            this.$refs.qrcode.innerHTML = "";
-            let qrcode = new QRCode("qrcode", {
+            let qrCode = new QRCode("qrCode", {
               width: 100,
               height: 100, // 高度
               text: this.printData.orderidView, // 二维码内容
             });
           }
-
           this.$nextTick(() => {
             this.usbPrinterList.forEach((e) => {
               if (this.deviceName) {
@@ -68,7 +67,9 @@ export default {
   // 生命周期 - 创建完成(可以访问当前this实例)
   created() {},
   // 生命周期 - 挂载完成(可以访问DOM元素)
-  mounted() {},
+  mounted() {
+    
+  },
   // 方法集合
   methods: {
     pringPdf(data) {
@@ -249,7 +250,7 @@ export default {
           ? "---------------#" + printInfo.daySeq + "完-------------"
           : "------------------#" + printInfo.daySeq + "完----------------"
       }</div><div style="display:flex;justify-content: center;align-items: center;">${
-        num === 1 ? this.$refs.qrcode.innerHTML : ""
+        num === 1 ? this.$refs.qrCode.innerHTML : ""
       }</div>    </div>`;
       return html1 + html2 + html3;
     },

+ 1 - 1
vue.config.js

@@ -40,7 +40,7 @@ module.exports = {
         "linux": {
           "icon": "./static/image/desk-logo.ico"
         },
-        "asar": false,
+        // "asar": false,
         "nsis": {
           "oneClick": false,
           "guid": "猎豹AI聚合配送",