{"id":775,"date":"2022-05-31T20:43:15","date_gmt":"2022-05-31T12:43:15","guid":{"rendered":"https:\/\/usei.cn\/?p=775"},"modified":"2022-06-02T07:57:11","modified_gmt":"2022-06-01T23:57:11","slug":"electron-%e8%87%aa%e5%8a%a8%e6%9b%b4%e6%96%b0","status":"publish","type":"post","link":"https:\/\/usei.cn\/index.php\/2022\/05\/31\/electron-%e8%87%aa%e5%8a%a8%e6%9b%b4%e6%96%b0\/","title":{"rendered":"electron \u81ea\u52a8\u66f4\u65b0"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">\u4e00\u3002\u5b89\u88c5\u63d2\u4ef6<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>npm install electron-updater<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e8c\u3002\u6dfb\u52a0\u4ee5\u4e0bUpdater.js<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>\r\n\/**\r\n * Auto Updater\r\n * \u81ea\u52a8\u66f4\u65b0\r\n * npm install electron-updater\r\n *\/\r\nconst { app, ipcMain } = require('electron')\r\nimport { autoUpdater } from 'electron-updater'\r\n\r\n\/\/ \u66f4\u65b0\u5730\u5740\r\nconst updateUrl = 'http:\/\/192.168.0.182:8081\/download\/desktop_client\/'\r\n\/\/ \u4e3b\u7a97\u4f53\u5f15\u7528\r\nlet mainWindow = null\r\n\/**\r\n * \u8fd4\u56de\u4fe1\u606f\u7ed3\u6784\r\n *\/\r\nconst returnData = {\r\n  error: { status: -1, msg: '\u68c0\u6d4b\u66f4\u65b0\u67e5\u8be2\u5f02\u5e38' },\r\n  checking: { status: 0, msg: '\u6b63\u5728\u68c0\u67e5\u5e94\u7528\u7a0b\u5e8f\u66f4\u65b0' },\r\n  updateAva: { status: 1, msg: '\u68c0\u6d4b\u5230\u65b0\u7248\u672c\uff0c\u6b63\u5728\u4e0b\u8f7d,\u8bf7\u7a0d\u540e' },\r\n  updateNotAva: { status: -1, msg: '\u60a8\u73b0\u5728\u4f7f\u7528\u7684\u7248\u672c\u4e3a\u6700\u65b0\u7248\u672c,\u65e0\u9700\u66f4\u65b0!' }\r\n}\r\n\/**\r\n * \u901a\u8fc7main\u8fdb\u7a0b\u53d1\u9001\u4e8b\u4ef6\u7ed9renderer\u8fdb\u7a0b\uff0c\u63d0\u793a\u66f4\u65b0\u4fe1\u606f\r\n * @param {*} text\r\n *\/\r\nconst sendUpdateMessage = (text) => {\r\n  mainWindow.webContents.send('updateMessage', text)\r\n}\r\n\/**\r\n * \u68c0\u67e5\u66f4\u65b0\r\n *\/\r\nconst checkForUpdate = () => {\r\n  autoUpdater.checkForUpdates()\r\n}\r\n\/**\r\n * \u521d\u59cb\u5316\u7ec4\u4ef6\r\n * @param {*} window\r\n *\/\r\nconst updaterInit = (window) => {\r\n  mainWindow = window\r\n  \/\/ \u548c\u4e4b\u524dpackage.json\u914d\u7f6e\u7684\u4e00\u6837\r\n  autoUpdater.setFeedURL(updateUrl)\r\n\r\n  \/\/ \u66f4\u65b0\u9519\u8bef\r\n  autoUpdater.on('error', (err_info) => {\r\n    sendUpdateMessage(returnData.error)\r\n  })\r\n  \/\/ \u68c0\u67e5\u4e2d\r\n  autoUpdater.on('checking-for-update', () => {\r\n    sendUpdateMessage(returnData.checking)\r\n  })\r\n  \/\/ \u53d1\u73b0\u65b0\u7248\u672c\r\n  autoUpdater.on('update-available', (info) => {\r\n    sendUpdateMessage(returnData.updateAva)\r\n  })\r\n  \/\/ \u5f53\u524d\u7248\u672c\u4e3a\u6700\u65b0\u7248\u672c\r\n  autoUpdater.on('update-not-available', (info) => {\r\n    sendUpdateMessage(returnData.updateNotAva)\r\n  })\r\n  \/\/ \u66f4\u65b0\u4e0b\u8f7d\u8fdb\u5ea6\u4e8b\u4ef6\r\n  autoUpdater.on('download-progress', (progressObj) => {\r\n    \/*\r\n    bytesPerSecond: bps\/s \/\/\u4f20\u9001\u901f\u7387\r\n    percent : \u767e\u5206\u6bd4 \/\/\u6211\u4eec\u9700\u8981\u8fd9\u4e2a\u5c31\u53ef\u4ee5\u4e86\r\n    total : \u603b\u5927\u5c0f\r\n    transferred: \u5df2\u7ecf\u4e0b\u8f7d\r\n    { \"bytesPerSecond\": 47132710, \"delta\": 39780007, \"percent\": 100, \"total\": 39780007, \"transferred\": 39780007 }\r\n    *\/\r\n    mainWindow.webContents.send('downloadProgress', progressObj)\r\n  })\r\n\r\n  autoUpdater.on('update-downloaded', (event, releaseNotes, releaseName, releaseDate, updateUrl, quitAndUpdate) => {\r\n    autoUpdater.quitAndInstall()\r\n    \/\/ ipcMain.on('isUpdateNow', (e, arg) => {\r\n    \/\/ \/\/ some code here to handle event\r\n    \/\/   autoUpdater.quitAndInstall()\r\n    \/\/ })\r\n  \/\/ win.webContents.send('isUpdateNow')\r\n  })\r\n  \/\/ \u6e32\u67d3\u8fdb\u7a0b\u4e3b\u52a8\u68c0\u67e5\u66f4\u65b0\r\n  ipcMain.on('checkForUpdate', () => {\r\n    console.log('Active UPDATE')\r\n    checkForUpdate()\r\n  })\r\n  \/\/ \u7a0b\u5e8f\u542f\u52a8\u540e\u81ea\u52a8\u68c0\u67e5\u66f4\u65b0\uff08\u4e0d\u81ea\u52a8\u66f4\u65b0\u53ef\u4ee5\u5173\u95ed\uff09\r\n  app.on('ready', () => {\r\n    \/\/ if (process.env.NODE_ENV === 'production') checkForUpdate()\r\n    checkForUpdate()\r\n  })\r\n}\r\n\r\nexport default{\r\n  updaterInit,\r\n  checkForUpdate\r\n}\r\n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>import Updater from '.\/Updater' \/\/ \u5f15\u5165\u66f4\u65b0\u51fd\u6570\n\nfunction createWindow(){\n  ...\n  mainWindow = new BrowserWindow(...)\n  \/\/ ----\u66f4\u65b0\u670d\u52a1----------\u2193\n  Updater.updaterInit(mainWindow)\n  \/\/ ----\u66f4\u65b0\u670d\u52a1----------\u2191\n  ...\n}\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e09\u3002\u6e32\u67d3\u5c42 AutoUpdater.vue \u7ec4\u4ef6<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;template&gt;\n  &lt;el-dialog \n    :visible.sync=\"show\" \n    :width=\"'770px'\" \n    top=\"55vh\" \n    custom-class=\"mds-dialog\" \n    :close-on-click-modal=\"false\"\n    :close-on-press-escape=\"false\"\n    @click=\"close\"\n    title=\"\u5e94\u7528\u66f4\u65b0\"\n    &gt;\n    &lt;p&gt;\u66f4\u65b0\u8fdb\u5ea6&lt;\/p&gt;\n    &lt;el-progress :text-inside=\"true\" :stroke-width=\"26\" :percentage=\"percent\"&gt;&lt;\/el-progress&gt;\n  &lt;\/el-dialog&gt;\n&lt;\/template&gt;\n&lt;script&gt;\nexport default {\n  name: 'AutoUpdater',\n  data() {\n    return {\n      show: false,\n      percent: 0\n    }\n  },\n  mounted() {\n    \/\/ \u66f4\u65b0\u8fdb\u5ea6\n    this.$electron.ipcRenderer.on('downloadProgress', (event, data) =&gt; {\n      this.percent = (data.percent).toFixed(2)\n      if (data.percent &gt;= 100) {\n        \/\/ this.show = false;\n      }\n    })\n    \/**\n    * \u4e3b\u8fdb\u7a0b\u8fd4\u56de\u7684\u68c0\u6d4b\u72b6\u6001\n    *\/\n    this.$electron.ipcRenderer.on('updateMessage', (event, data) =&gt; {\n      console.info('updateMessage', data)\n      switch (data.status) {\n        case -1:\n          this.$notify.info({ title: '\u6d88\u606f', message: data.msg })\n          break\n        case 0:\n          this.$notify.success({ title: '\u6d88\u606f', message: data.msg })\n          break\n        case 1:\n          this.show = true\n          break\n      }\n    })\n  },\n  methods: {\n    close() {\n      console.info('x')\n    }\n  }\n}\n&lt;\/script&gt;<\/code><\/pre>\n\n\n\n<p>\u6309\u94ae\u4e3b\u52a8\u8c03\u7528\u68c0\u67e5<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const { ipcRenderer: ipc } = require('electron')\nipc.send('checkForUpdate')<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3002\u5b89\u88c5\u63d2\u4ef6 \u4e8c\u3002\u6dfb\u52a0\u4ee5\u4e0bUpdater.js \u4e09\u3002\u6e32\u67d3\u5c42 AutoUpdater.vue \u7ec4\u4ef6 \u6309\u94ae\u4e3b\u52a8\u8c03\u7528 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[69,8],"tags":[],"class_list":["post-775","post","type-post","status-publish","format-standard","hentry","category-eletron","category-frontend"],"_links":{"self":[{"href":"https:\/\/usei.cn\/index.php\/wp-json\/wp\/v2\/posts\/775","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/usei.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/usei.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/usei.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/usei.cn\/index.php\/wp-json\/wp\/v2\/comments?post=775"}],"version-history":[{"count":7,"href":"https:\/\/usei.cn\/index.php\/wp-json\/wp\/v2\/posts\/775\/revisions"}],"predecessor-version":[{"id":800,"href":"https:\/\/usei.cn\/index.php\/wp-json\/wp\/v2\/posts\/775\/revisions\/800"}],"wp:attachment":[{"href":"https:\/\/usei.cn\/index.php\/wp-json\/wp\/v2\/media?parent=775"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/usei.cn\/index.php\/wp-json\/wp\/v2\/categories?post=775"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/usei.cn\/index.php\/wp-json\/wp\/v2\/tags?post=775"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}