张恒

加入不使用updater进行全量更新的办法

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,53 +122,89 @@ export default { ...@@ -107,53 +122,89 @@ export default {
107 // } 122 // }
108 // api.ErrorMessageBox(dialog,data) 123 // api.ErrorMessageBox(dialog,data)
109 }, 124 },
110 - CheckUpdate() { 125 + CheckUpdate(data) {
111 - const dialog = this.$electron.remote.dialog; 126 + switch (data) {
112 - ipcApi.send("check-update"); 127 + case "one":
113 - console.log("启动检查"); 128 + const dialog = this.$electron.remote.dialog;
114 - ipcApi.on("UpdateMsg", (event, data) => { 129 + ipcApi.send("check-update");
115 - console.log(data) 130 + console.log("启动检查");
116 - switch (data.state) { 131 + ipcApi.on("UpdateMsg", (event, data) => {
117 - case -1: 132 + console.log(data);
118 - const msgdata = { 133 + switch (data.state) {
119 - title: data.msg, 134 + case -1:
120 - }; 135 + const msgdata = {
121 - api.MessageBox(dialog, msgdata); 136 + title: data.msg
122 - break; 137 + };
123 - case 0: 138 + api.MessageBox(dialog, msgdata);
124 - this.$message("正在检查更新"); 139 + break;
125 - break; 140 + case 0:
126 - case 1: 141 + this.$message("正在检查更新");
127 - this.$message({ 142 + break;
128 - type: "success", 143 + case 1:
129 - message: "已检查到新版本,开始下载" 144 + this.$message({
130 - }); 145 + type: "success",
131 - this.dialogVisible = true 146 + message: "已检查到新版本,开始下载"
132 - break; 147 + });
133 - case 2: 148 + this.dialogVisible = true;
134 - this.$message({ type: "success", message: "无新版本" }); 149 + break;
135 - break; 150 + case 2:
136 - case 3: 151 + this.$message({ type: "success", message: "无新版本" });
137 - this.percentage = data.msg.percent.toFixed(1) 152 + break;
138 - break; 153 + case 3:
139 - case 4: 154 + this.percentage = data.msg.percent.toFixed(1);
155 + break;
156 + case 4:
157 + this.progressStaus = "success";
158 + this.$alert("更新下载完成!", "提示", {
159 + confirmButtonText: "确定",
160 + callback: action => {
161 + ipcApi.send("confirm-update");
162 + }
163 + });
164 + break;
165 +
166 + default:
167 + break;
168 + }
169 + });
170 + break;
171 + case "two":
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";
140 this.$alert("更新下载完成!", "提示", { 192 this.$alert("更新下载完成!", "提示", {
141 confirmButtonText: "确定", 193 confirmButtonText: "确定",
142 callback: action => { 194 callback: action => {
143 - ipcApi.send("confirm-update"); 195 + this.$electron.shell.openItem(this.filePath);
144 } 196 }
145 }); 197 });
146 - break; 198 + });
199 + break;
147 200
148 - default: 201 + default:
149 - break; 202 + break;
150 - } 203 + }
151 - });
152 }, 204 },
153 - handleClose(){ 205 + handleClose() {
154 - this.dialogVisible = false 206 + this.dialogVisible = false;
155 } 207 }
156 -
157 } 208 }
158 }; 209 };
159 </script> 210 </script>
......