1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
| const { app, ipcMain, } = require('electron'); const fs = require('fs-extra'); const request = require('request');
const AdmZip = require('adm-zip'); const os = require('os');
const packageJson = require('../../../package.json'); const { spawn, } = require('child_process');
const osPlatform = os.platform() let savePath = null let mainWindow
const compareVersion = newVersion => { const vs1 = packageJson.version.toString().split('.'); const vs2 = newVersion.toString().split('.'); if (vs1.length !== vs2.length) { return 1; } if (vs1[0] !== vs2[0] || vs1[1] !== vs2[1]) { return 1 } if (vs1[2] !== vs2[2]) { if (osPlatform !== 'win32') { return 1 } return 2 } return 0 };
const sendUpdateMessage = (text, code) => mainWindow.webContents.send('update-message', code ? text : { text, code, })
const downloadFile = (uri, fileName, callback) => { const req = request({ method: 'GET', uri, encoding: null, })
let totalBytes = 0 let receivedBytes = 0 req.on('response', data => { totalBytes = data.headers['content-length'] - 0 if (totalBytes === 0) { sendUpdateMessage(`找不到文件: ${savePath}`) } })
const out = fs.createWriteStream(savePath) req.pipe(out) let startTime = new Date().getTime() req.on('data', chunk => { receivedBytes += chunk.length const nowTime = new Date().getTime() if (nowTime - startTime >= 1000) { startTime = nowTime const percentage = Math.floor(receivedBytes / totalBytes * 100) mainWindow.webContents.send('update-downloadProgress', percentage) } })
req.on('end', () => { mainWindow.webContents.send('update-downloadProgress', 100) callback() }) }
const incremental = data => { console.log('增量更新') ipcMain.on('update-confirmDownloaded', () => { downloadFile(data.update_uri, data.update_path, () => { const zip = new AdmZip(`./${data.update_path}`) zip.extractAllToAsync(`./resources/app`, true, () => { app.relaunch({ args: process.argv.slice(1).concat(['--relaunch']), }); app.exit(0); }) }) }); }
const fullQuantity = data => { console.log('全量更新') sendUpdateMessage('全量更新') ipcMain.on('update-confirmDownloaded', () => { savePath = osPlatform === 'win32' ? `./${data.path}` : `${os.homedir()}/Downloads/${data.path}` downloadFile(data.uri, data.path, () => { setTimeout(() => { if (osPlatform === 'win32') { spawn(savePath, [], { detached: true, }) app.quit(); } else { spawn('open', [savePath], { detached: true, }) app.quit(); } }, 1000); }) }) }
const update = win => { mainWindow = win ipcMain.on('update-check', (e, data) => { request({ method: 'POST', json: true, uri: process.env.VUE_APP_API + '/checkUpdate', body: data, }, (_error, res, data) => { if (data.code !== 0) return data = data.data const compareVersionResult = compareVersion(data.app.version) switch (compareVersionResult) { case 0: console.log('无更新') return case 1: mainWindow.webContents.send('update-log', data.app.desc) fullQuantity(data.app) break case 2: mainWindow.webContents.send('update-log', data.app.desc) incremental(data.app) break default: break; } }) }) }
module.exports = update
|