正在显示
3 个修改的文件
包含
125 行增加
和
13 行删除
src/main/services/downloadFile.js
0 → 100644
| 1 | +/* eslint-disable no-case-declarations */ | ||
| 2 | +import { app, ipcMain, dialog } from 'electron' | ||
| 3 | +import path from 'path' | ||
| 4 | +import os from 'os' | ||
| 5 | +// 版本以package.json为基准。 | ||
| 6 | +const version = require('../../../package.json').version | ||
| 7 | +// 您的下载地址 | ||
| 8 | +const baseUrl = 'http://127.0.0.1:25565/' | ||
| 9 | +var Sysarch = null | ||
| 10 | +var downloadUrL = null | ||
| 11 | +// 识别操作系统位数 | ||
| 12 | +os.arch().includes('64') ? Sysarch = 'win64' : Sysarch = 'win32' | ||
| 13 | +// 暂时忽略linux版本,因为我。。= =没有linux不过问题不大,很快就会有 | ||
| 14 | +os.platform().includes('win') ? downloadUrL = baseUrl + `electron_${version}_${Sysarch}.exe?${new Date().getTime()}` : downloadUrL = baseUrl + `electron_${version}_mac.dmg?${new Date().getTime()}` | ||
| 15 | +export default { | ||
| 16 | + download (mainWindow) { | ||
| 17 | + ipcMain.on('satrt-download', (event, msg) => { | ||
| 18 | + console.log(1111) | ||
| 19 | + mainWindow.webContents.downloadURL(downloadUrL) | ||
| 20 | + event.reply('confirm-download', true) | ||
| 21 | + mainWindow.webContents.session.on('will-download', (event, item, webContents) => { | ||
| 22 | + // 将文件保存在系统的下载目录 | ||
| 23 | + const filePath = path.join(app.getPath('downloads'), item.getFilename()) | ||
| 24 | + // 自动保存 | ||
| 25 | + item.setSavePath(filePath) | ||
| 26 | + // 下载进度 | ||
| 27 | + item.on('updated', (event, state) => { | ||
| 28 | + switch (state) { | ||
| 29 | + case 'progressing': | ||
| 30 | + mainWindow.webContents.send('download-progress', (item.getReceivedBytes() / item.getTotalBytes() * 100).toFixed(0)) | ||
| 31 | + break | ||
| 32 | + default: | ||
| 33 | + mainWindow.webContents.send('download-error', true) | ||
| 34 | + dialog.showErrorBox('下载出错', '由于网络或其他未知原因导致客户端下载出错,请前往官网进行重新安装') | ||
| 35 | + break | ||
| 36 | + } | ||
| 37 | + }) | ||
| 38 | + // 下载完成或失败 | ||
| 39 | + item.once('done', (event, state) => { | ||
| 40 | + switch (state) { | ||
| 41 | + case 'completed': | ||
| 42 | + const data = { | ||
| 43 | + filePath | ||
| 44 | + } | ||
| 45 | + mainWindow.webContents.send('download-done', data) | ||
| 46 | + break | ||
| 47 | + case 'interrupted': | ||
| 48 | + mainWindow.webContents.send('download-error', true) | ||
| 49 | + dialog.showErrorBox('下载出错', '由于网络或其他未知原因导致客户端下载出错,请前往官网进行重新安装') | ||
| 50 | + break | ||
| 51 | + default: | ||
| 52 | + break | ||
| 53 | + } | ||
| 54 | + }) | ||
| 55 | + }) | ||
| 56 | + }) | ||
| 57 | + } | ||
| 58 | +} |
| ... | @@ -4,7 +4,9 @@ import config from '@config' | ... | @@ -4,7 +4,9 @@ import config from '@config' |
| 4 | import setIpc from './ipcMain' | 4 | import setIpc from './ipcMain' |
| 5 | import electronDevtoolsInstaller, { VUEJS_DEVTOOLS } from 'electron-devtools-installer' | 5 | import electronDevtoolsInstaller, { VUEJS_DEVTOOLS } from 'electron-devtools-installer' |
| 6 | import upload from './checkupdate' | 6 | import upload from './checkupdate' |
| 7 | +import DownloadUpdate from './downloadFile' | ||
| 7 | import path from 'path' | 8 | import path from 'path' |
| 9 | + | ||
| 8 | /** | 10 | /** |
| 9 | * Set `__static` path to static files in production | 11 | * Set `__static` path to static files in production |
| 10 | * https://simulatedgreg.gitbooks.io/electron-vue/content/en/using-static-assets.html | 12 | * https://simulatedgreg.gitbooks.io/electron-vue/content/en/using-static-assets.html |
| ... | @@ -58,6 +60,7 @@ function createMainWindow () { | ... | @@ -58,6 +60,7 @@ function createMainWindow () { |
| 58 | 60 | ||
| 59 | setIpc.Mainfunc(ipcMain, mainWindow, config.IsUseSysTitle) | 61 | setIpc.Mainfunc(ipcMain, mainWindow, config.IsUseSysTitle) |
| 60 | upload.Update(mainWindow) | 62 | upload.Update(mainWindow) |
| 63 | + DownloadUpdate.download(mainWindow) | ||
| 61 | 64 | ||
| 62 | if (process.env.NODE_ENV === 'development') { | 65 | if (process.env.NODE_ENV === 'development') { |
| 63 | mainWindow.webContents.once('dom-ready', () => { | 66 | mainWindow.webContents.once('dom-ready', () => { | ... | ... |
| ... | @@ -22,13 +22,26 @@ | ... | @@ -22,13 +22,26 @@ |
| 22 | <el-button type="primary" round @click="setdata">写入数据</el-button> | 22 | <el-button type="primary" round @click="setdata">写入数据</el-button> |
| 23 | <el-button type="primary" round @click="getdata">读取数据</el-button> | 23 | <el-button type="primary" round @click="getdata">读取数据</el-button> |
| 24 | <el-button type="primary" round @click="deledata">清除所有数据</el-button> | 24 | <el-button type="primary" round @click="deledata">清除所有数据</el-button> |
| 25 | - <el-button type="primary" round @click="CheckUpdate">检查更新</el-button> | 25 | + <el-button type="primary" round @click="CheckUpdate('one')">检查更新</el-button> |
| 26 | + <el-button type="primary" round @click="CheckUpdate('two')">检查更新(第二种方法)</el-button> | ||
| 26 | </div> | 27 | </div> |
| 27 | </div> | 28 | </div> |
| 28 | </main> | 29 | </main> |
| 29 | - <el-dialog title="进度" :visible.sync="dialogVisible" :before-close="handleClose" center width="14%" top="45vh"> | 30 | + <el-dialog |
| 31 | + title="进度" | ||
| 32 | + :visible.sync="dialogVisible" | ||
| 33 | + :before-close="handleClose" | ||
| 34 | + center | ||
| 35 | + width="14%" | ||
| 36 | + top="45vh" | ||
| 37 | + > | ||
| 30 | <div class="conten"> | 38 | <div class="conten"> |
| 31 | - <el-progress type="dashboard" :percentage="percentage" :color="colors"></el-progress> | 39 | + <el-progress |
| 40 | + type="dashboard" | ||
| 41 | + :percentage="percentage" | ||
| 42 | + :color="colors" | ||
| 43 | + :status="progressStaus" | ||
| 44 | + ></el-progress> | ||
| 32 | </div> | 45 | </div> |
| 33 | </el-dialog> | 46 | </el-dialog> |
| 34 | </div> | 47 | </div> |
| ... | @@ -48,7 +61,7 @@ export default { | ... | @@ -48,7 +61,7 @@ export default { |
| 48 | age: "12" | 61 | age: "12" |
| 49 | }, | 62 | }, |
| 50 | textarray: [], | 63 | textarray: [], |
| 51 | - percentage:0, | 64 | + percentage: 0, |
| 52 | colors: [ | 65 | colors: [ |
| 53 | { color: "#f56c6c", percentage: 20 }, | 66 | { color: "#f56c6c", percentage: 20 }, |
| 54 | { color: "#e6a23c", percentage: 40 }, | 67 | { color: "#e6a23c", percentage: 40 }, |
| ... | @@ -56,7 +69,9 @@ export default { | ... | @@ -56,7 +69,9 @@ export default { |
| 56 | { color: "#1989fa", percentage: 80 }, | 69 | { color: "#1989fa", percentage: 80 }, |
| 57 | { color: "#5cb87a", percentage: 100 } | 70 | { color: "#5cb87a", percentage: 100 } |
| 58 | ], | 71 | ], |
| 59 | - dialogVisible: false | 72 | + dialogVisible: false, |
| 73 | + progressStaus: null, | ||
| 74 | + filePath: "" | ||
| 60 | }), | 75 | }), |
| 61 | created() {}, | 76 | created() {}, |
| 62 | methods: { | 77 | methods: { |
| ... | @@ -107,16 +122,18 @@ export default { | ... | @@ -107,16 +122,18 @@ export default { |
| 107 | // } | 122 | // } |
| 108 | // api.ErrorMessageBox(dialog,data) | 123 | // api.ErrorMessageBox(dialog,data) |
| 109 | }, | 124 | }, |
| 110 | - CheckUpdate() { | 125 | + CheckUpdate(data) { |
| 126 | + switch (data) { | ||
| 127 | + case "one": | ||
| 111 | const dialog = this.$electron.remote.dialog; | 128 | const dialog = this.$electron.remote.dialog; |
| 112 | ipcApi.send("check-update"); | 129 | ipcApi.send("check-update"); |
| 113 | console.log("启动检查"); | 130 | console.log("启动检查"); |
| 114 | ipcApi.on("UpdateMsg", (event, data) => { | 131 | ipcApi.on("UpdateMsg", (event, data) => { |
| 115 | - console.log(data) | 132 | + console.log(data); |
| 116 | switch (data.state) { | 133 | switch (data.state) { |
| 117 | case -1: | 134 | case -1: |
| 118 | const msgdata = { | 135 | const msgdata = { |
| 119 | - title: data.msg, | 136 | + title: data.msg |
| 120 | }; | 137 | }; |
| 121 | api.MessageBox(dialog, msgdata); | 138 | api.MessageBox(dialog, msgdata); |
| 122 | break; | 139 | break; |
| ... | @@ -128,15 +145,16 @@ export default { | ... | @@ -128,15 +145,16 @@ export default { |
| 128 | type: "success", | 145 | type: "success", |
| 129 | message: "已检查到新版本,开始下载" | 146 | message: "已检查到新版本,开始下载" |
| 130 | }); | 147 | }); |
| 131 | - this.dialogVisible = true | 148 | + this.dialogVisible = true; |
| 132 | break; | 149 | break; |
| 133 | case 2: | 150 | case 2: |
| 134 | this.$message({ type: "success", message: "无新版本" }); | 151 | this.$message({ type: "success", message: "无新版本" }); |
| 135 | break; | 152 | break; |
| 136 | case 3: | 153 | case 3: |
| 137 | - this.percentage = data.msg.percent.toFixed(1) | 154 | + this.percentage = data.msg.percent.toFixed(1); |
| 138 | break; | 155 | break; |
| 139 | case 4: | 156 | case 4: |
| 157 | + this.progressStaus = "success"; | ||
| 140 | this.$alert("更新下载完成!", "提示", { | 158 | this.$alert("更新下载完成!", "提示", { |
| 141 | confirmButtonText: "确定", | 159 | confirmButtonText: "确定", |
| 142 | callback: action => { | 160 | callback: action => { |
| ... | @@ -149,11 +167,44 @@ export default { | ... | @@ -149,11 +167,44 @@ export default { |
| 149 | break; | 167 | break; |
| 150 | } | 168 | } |
| 151 | }); | 169 | }); |
| 152 | - }, | 170 | + break; |
| 153 | - handleClose(){ | 171 | + case "two": |
| 154 | - this.dialogVisible = false | 172 | + console.log(111); |
| 173 | + ipcApi.send("satrt-download"); | ||
| 174 | + ipcApi.on("confirm-download", (event, arg) => { | ||
| 175 | + if (arg) { | ||
| 176 | + this.dialogVisible = true; | ||
| 177 | + } | ||
| 178 | + }); | ||
| 179 | + ipcApi.on("download-progress", (event, arg) => { | ||
| 180 | + this.percentage = Number(arg); | ||
| 181 | + }); | ||
| 182 | + ipcApi.on("download-error", (event, arg) => { | ||
| 183 | + if (arg) { | ||
| 184 | + this.progressStaus = "exception"; | ||
| 185 | + this.percentage = 40; | ||
| 186 | + this.colors = "#d81e06"; | ||
| 187 | + } | ||
| 188 | + }); | ||
| 189 | + ipcApi.on("download-done", (event, age) => { | ||
| 190 | + this.filePath = age.filePath; | ||
| 191 | + this.progressStaus = "success"; | ||
| 192 | + this.$alert("更新下载完成!", "提示", { | ||
| 193 | + confirmButtonText: "确定", | ||
| 194 | + callback: action => { | ||
| 195 | + this.$electron.shell.openItem(this.filePath); | ||
| 155 | } | 196 | } |
| 197 | + }); | ||
| 198 | + }); | ||
| 199 | + break; | ||
| 156 | 200 | ||
| 201 | + default: | ||
| 202 | + break; | ||
| 203 | + } | ||
| 204 | + }, | ||
| 205 | + handleClose() { | ||
| 206 | + this.dialogVisible = false; | ||
| 207 | + } | ||
| 157 | } | 208 | } |
| 158 | }; | 209 | }; |
| 159 | </script> | 210 | </script> | ... | ... |
-
请 注册 或 登录 后发表评论