umbrella22

将主进程修改为ts,主进程添加__lib文件路径,更新依赖

...@@ -2,13 +2,15 @@ ...@@ -2,13 +2,15 @@
2 2
3 process.env.BABEL_ENV = 'main' 3 process.env.BABEL_ENV = 'main'
4 4
5 -const os = require('os')
6 const path = require('path') 5 const path = require('path')
7 const { dependencies } = require('../package.json') 6 const { dependencies } = require('../package.json')
8 const webpack = require('webpack') 7 const webpack = require('webpack')
9 const MinifyPlugin = require("babel-minify-webpack-plugin"); 8 const MinifyPlugin = require("babel-minify-webpack-plugin");
10 -const HappyPack = require('happypack') 9 +const config = require('../config')
11 -const HappyThreadPool = HappyPack.ThreadPool({ size: os.cpus().length > 4 ? 4 : os.cpus().length }) 10 +
11 +// const os = require('os')
12 +// const HappyPack = require('happypack')
13 +// const HappyThreadPool = HappyPack.ThreadPool({ size: os.cpus().length > 4 ? 4 : os.cpus().length })
12 14
13 function resolve(dir) { 15 function resolve(dir) {
14 return path.join(__dirname, '..', dir) 16 return path.join(__dirname, '..', dir)
...@@ -16,7 +18,7 @@ function resolve(dir) { ...@@ -16,7 +18,7 @@ function resolve(dir) {
16 18
17 let mainConfig = { 19 let mainConfig = {
18 entry: { 20 entry: {
19 - main: path.join(__dirname, '../src/main/index.js') 21 + main: path.join(__dirname, '../src/main/index.ts')
20 }, 22 },
21 externals: [ 23 externals: [
22 ...Object.keys(dependencies || {}) 24 ...Object.keys(dependencies || {})
...@@ -34,10 +36,20 @@ let mainConfig = { ...@@ -34,10 +36,20 @@ let mainConfig = {
34 // } 36 // }
35 // } 37 // }
36 // }, 38 // },
39 + // {
40 + // test: /\.js$/,
41 + // use: 'happypack/loader?id=MainHappyBabel',
42 + // exclude: /node_modules/
43 + // },
37 { 44 {
38 - test: /\.js$/, 45 + test: /\.ts$/,
39 - use: 'happypack/loader?id=MainHappyBabel', 46 + use: [{
40 - exclude: /node_modules/ 47 + loader: 'babel-loader',
48 + options: {
49 + cacheDirectory: true
50 + }
51 + }, 'ts-loader'],
52 +
41 }, 53 },
42 { 54 {
43 test: /\.node$/, 55 test: /\.node$/,
...@@ -56,16 +68,16 @@ let mainConfig = { ...@@ -56,16 +68,16 @@ let mainConfig = {
56 }, 68 },
57 plugins: [ 69 plugins: [
58 new webpack.NoEmitOnErrorsPlugin(), 70 new webpack.NoEmitOnErrorsPlugin(),
59 - new HappyPack({ 71 + // new HappyPack({
60 - id: "MainHappyBabel", 72 + // id: "MainHappyBabel",
61 - loaders: [{ 73 + // loaders: [{
62 - loader: 'babel-loader', 74 + // loader: 'babel-loader',
63 - options: { 75 + // options: {
64 - cacheDirectory: true 76 + // cacheDirectory: true
65 - } 77 + // }
66 - }], 78 + // }],
67 - threadPool: HappyThreadPool 79 + // threadPool: HappyThreadPool
68 - }) 80 + // })
69 ], 81 ],
70 resolve: { 82 resolve: {
71 alias: { 83 alias: {
...@@ -82,7 +94,8 @@ let mainConfig = { ...@@ -82,7 +94,8 @@ let mainConfig = {
82 if (process.env.NODE_ENV !== 'production') { 94 if (process.env.NODE_ENV !== 'production') {
83 mainConfig.plugins.push( 95 mainConfig.plugins.push(
84 new webpack.DefinePlugin({ 96 new webpack.DefinePlugin({
85 - '__static': `"${path.join(__dirname, '../static').replace(/\\/g, '\\\\')}"` 97 + '__static': `"${path.join(__dirname, '../static').replace(/\\/g, '\\\\')}"`,
98 + '__lib': `"${path.join(__dirname, `../${config.DllFolder}`).replace(/\\/g, '\\\\')}"`
86 }) 99 })
87 ) 100 )
88 } 101 }
......
...@@ -9,7 +9,7 @@ module.exports = { ...@@ -9,7 +9,7 @@ module.exports = {
9 chineseLog: false, 9 chineseLog: false,
10 port: 9080 10 port: 9080
11 }, 11 },
12 - UseStartupChart: false, 12 + UseStartupChart: true,
13 IsUseSysTitle: true, 13 IsUseSysTitle: true,
14 DllFolder: '', 14 DllFolder: '',
15 BuiltInServerPort: 25565 15 BuiltInServerPort: 25565
......
...@@ -81,11 +81,11 @@ ...@@ -81,11 +81,11 @@
81 "js-cookie": "^2.2.1", 81 "js-cookie": "^2.2.1",
82 "nprogress": "^0.2.0", 82 "nprogress": "^0.2.0",
83 "vue": "^2.6.11", 83 "vue": "^2.6.11",
84 - "vue-router": "^3.3.2", 84 + "vue-router": "^3.4.3",
85 "vuex": "^3.5.1" 85 "vuex": "^3.5.1"
86 }, 86 },
87 "devDependencies": { 87 "devDependencies": {
88 - "@babel/core": "^7.11.0", 88 + "@babel/core": "^7.11.1",
89 "@babel/plugin-proposal-class-properties": "^7.10.4", 89 "@babel/plugin-proposal-class-properties": "^7.10.4",
90 "@babel/plugin-proposal-decorators": "^7.10.5", 90 "@babel/plugin-proposal-decorators": "^7.10.5",
91 "@babel/plugin-proposal-do-expressions": "^7.10.4", 91 "@babel/plugin-proposal-do-expressions": "^7.10.4",
...@@ -106,7 +106,9 @@ ...@@ -106,7 +106,9 @@
106 "@babel/polyfill": "^7.10.4", 106 "@babel/polyfill": "^7.10.4",
107 "@babel/preset-env": "^7.11.0", 107 "@babel/preset-env": "^7.11.0",
108 "@babel/register": "^7.10.5", 108 "@babel/register": "^7.10.5",
109 - "@babel/runtime": "^7.11.0", 109 + "@babel/runtime": "^7.11.2",
110 + "@types/fs-extra": "^9.0.1",
111 + "@types/node": "^14.0.27",
110 "ajv": "^6.12.3", 112 "ajv": "^6.12.3",
111 "babel-eslint": "^9.0.0", 113 "babel-eslint": "^9.0.0",
112 "babel-loader": "^8.1.0", 114 "babel-loader": "^8.1.0",
...@@ -116,16 +118,16 @@ ...@@ -116,16 +118,16 @@
116 "chalk": "^4.0.0", 118 "chalk": "^4.0.0",
117 "copy-webpack-plugin": "^6.0.3", 119 "copy-webpack-plugin": "^6.0.3",
118 "cross-env": "^7.0.2", 120 "cross-env": "^7.0.2",
119 - "css-loader": "^4.2.0", 121 + "css-loader": "^4.2.1",
120 "del": "^5.1.0", 122 "del": "^5.1.0",
121 - "electron": "^9.1.2", 123 + "electron": "^9.2.0",
122 - "electron-builder": "^22.6.1", 124 + "electron-builder": "^22.8.0",
123 "electron-devtools-installer": "^3.1.1", 125 "electron-devtools-installer": "^3.1.1",
124 "eslint": "^6.8.0", 126 "eslint": "^6.8.0",
125 "eslint-config-standard": "^14.1.1", 127 "eslint-config-standard": "^14.1.1",
126 "eslint-friendly-formatter": "^4.0.1", 128 "eslint-friendly-formatter": "^4.0.1",
127 "eslint-loader": "^3.0.4", 129 "eslint-loader": "^3.0.4",
128 - "eslint-plugin-html": "^6.0.2", 130 + "eslint-plugin-html": "^6.0.3",
129 "eslint-plugin-import": "^2.22.0", 131 "eslint-plugin-import": "^2.22.0",
130 "eslint-plugin-node": "^9.2.0", 132 "eslint-plugin-node": "^9.2.0",
131 "eslint-plugin-promise": "^4.2.1", 133 "eslint-plugin-promise": "^4.2.1",
...@@ -133,17 +135,19 @@ ...@@ -133,17 +135,19 @@
133 "file-loader": "^6.0.0", 135 "file-loader": "^6.0.0",
134 "happypack": "^5.0.1", 136 "happypack": "^5.0.1",
135 "html-webpack-plugin": "^4.3.0", 137 "html-webpack-plugin": "^4.3.0",
136 - "mini-css-extract-plugin": "0.9.0", 138 + "mini-css-extract-plugin": "0.10.0",
137 "multispinner": "^0.2.1", 139 "multispinner": "^0.2.1",
138 - "node-loader": "^1.0.0", 140 + "node-loader": "^1.0.1",
139 "portfinder": "^1.0.28", 141 "portfinder": "^1.0.28",
140 "sass": "^1.26.10", 142 "sass": "^1.26.10",
141 - "sass-loader": "^9.0.2", 143 + "sass-loader": "^9.0.3",
142 "split2": "^3.1.1", 144 "split2": "^3.1.1",
143 "style-loader": "^1.2.1", 145 "style-loader": "^1.2.1",
144 "svg-sprite-loader": "^5.0.0", 146 "svg-sprite-loader": "^5.0.0",
145 - "terser-webpack-plugin": "^3.0.8", 147 + "terser-webpack-plugin": "^4.1.0",
146 "through2-filter": "^3.0.0", 148 "through2-filter": "^3.0.0",
149 + "ts-loader": "^8.0.2",
150 + "typescript": "^3.9.7",
147 "url-loader": "^4.1.0", 151 "url-loader": "^4.1.0",
148 "vue-html-loader": "^1.2.4", 152 "vue-html-loader": "^1.2.4",
149 "vue-loader": "^15.9.3", 153 "vue-loader": "^15.9.3",
...@@ -153,6 +157,6 @@ ...@@ -153,6 +157,6 @@
153 "webpack-cli": "^3.3.12", 157 "webpack-cli": "^3.3.12",
154 "webpack-dev-server": "^3.11.0", 158 "webpack-dev-server": "^3.11.0",
155 "webpack-hot-middleware": "^2.25.0", 159 "webpack-hot-middleware": "^2.25.0",
156 - "webpack-merge": "^5.0.9" 160 + "webpack-merge": "^5.1.1"
157 } 161 }
158 } 162 }
......
1 import { globalShortcut } from 'electron' 1 import { globalShortcut } from 'electron'
2 -import config from '@config' 2 +import config from '@config/index'
3 3
4 export default { 4 export default {
5 Disablef12 () { 5 Disablef12 () {
......
1 -// 这里定义了静态文件路径的位置
2 -import path from 'path'
3 -
4 -/**
5 - * Set `__static` path to static files in production
6 - * https://simulatedgreg.gitbooks.io/electron-vue/content/en/using-static-assets.html
7 - */
8 -// 这个瓜皮全局变量只能在单个js中生效,而并不是整个主进程中
9 -if (process.env.NODE_ENV !== 'development') {
10 - global.__static = path.join(__dirname, '/static').replace(/\\/g, '\\\\')
11 -}
12 -
13 -export const winURL = process.env.NODE_ENV === 'development' ? `http://localhost:${process.env.PORT}` : `file://${__dirname}/index.html`
14 -export const loadingURL = process.env.NODE_ENV === 'development' ? `http://localhost:${process.env.PORT}/static/loader.html` : `file://${__static}/loader.html`
1 +// 这里定义了静态文件路径的位置
2 +import { join } from 'path'
3 +import { DllFolder } from '@config/index'
4 +
5 +var __static: string
6 +var __lib: string
7 +if (process.env.NODE_ENV !== 'development') {
8 + __static = join(__dirname, '/static').replace(/\\/g, '\\\\');
9 + __lib = join(__dirname, '..', '..', '..', '..', `${DllFolder}`).replace(/\\/g, '\\\\')
10 +}
11 +
12 +
13 +export const winURL = process.env.NODE_ENV === 'development' ? `http://localhost:${process.env.PORT}` : `file://${__dirname}/index.html`
14 +export const loadingURL = process.env.NODE_ENV === 'development' ? `http://localhost:${process.env.PORT}/static/loader.html` : `file://${__static}/loader.html`
15 +export const lib = __lib
...\ No newline at end of file ...\ No newline at end of file
1 // 这里是定义菜单的地方,详情请查看 https://electronjs.org/docs/api/menu 1 // 这里是定义菜单的地方,详情请查看 https://electronjs.org/docs/api/menu
2 -const { dialog } = require('electron') 2 +import { dialog } from 'electron'
3 -const os = require('os') 3 +import { type, arch, release } from 'os'
4 -const version = require('../../../package.json').version 4 +import { version } from '../../../package.json'
5 +
5 const menu = [ 6 const menu = [
6 { 7 {
7 label: '设置', 8 label: '设置',
...@@ -24,14 +25,15 @@ const menu = [ ...@@ -24,14 +25,15 @@ const menu = [
24 } 25 }
25 }] 26 }]
26 }] 27 }]
27 -function info () { 28 +function info() {
28 dialog.showMessageBox({ 29 dialog.showMessageBox({
29 title: '关于', 30 title: '关于',
30 type: 'info', 31 type: 'info',
31 message: 'electron-Vue框架', 32 message: 'electron-Vue框架',
32 - detail: `版本信息:${version}\n引擎版本:${process.versions.v8}\n当前系统:${os.type()} ${os.arch()} ${os.release()}`, 33 + detail: `版本信息:${version}\n引擎版本:${process.versions.v8}\n当前系统:${type()} ${arch()} ${release()}`,
33 noLink: true, 34 noLink: true,
34 buttons: ['查看github', '确定'] 35 buttons: ['查看github', '确定']
35 }) 36 })
36 } 37 }
38 +
37 export default menu 39 export default menu
......
...@@ -5,7 +5,7 @@ import initWindow from './services/windowManager' ...@@ -5,7 +5,7 @@ import initWindow from './services/windowManager'
5 import DisableButton from './config/DisableButton' 5 import DisableButton from './config/DisableButton'
6 6
7 function onAppReady () { 7 function onAppReady () {
8 - initWindow() 8 + new initWindow().initWindow()
9 DisableButton.Disablef12() 9 DisableButton.Disablef12()
10 } 10 }
11 11
......
1 /* eslint-disable prefer-promise-reject-errors */ 1 /* eslint-disable prefer-promise-reject-errors */
2 import app from './server' 2 import app from './server'
3 -import http from 'http' 3 +import { createServer } from 'http'
4 -import config from '@config' 4 +import config from '@config/index'
5 const port = config.BuiltInServerPort 5 const port = config.BuiltInServerPort
6 var server = null 6 var server = null
7 app.set('port', port) 7 app.set('port', port)
8 8
9 export default { 9 export default {
10 - StatrServer () { 10 + StatrServer() {
11 return new Promise((resolve, reject) => { 11 return new Promise((resolve, reject) => {
12 - server = http.createServer(app) 12 + server = createServer(app)
13 server.listen(port) 13 server.listen(port)
14 server.on('error', (error) => { 14 server.on('error', (error) => {
15 switch (error.code) { 15 switch (error.code) {
...@@ -28,9 +28,8 @@ export default { ...@@ -28,9 +28,8 @@ export default {
28 }) 28 })
29 }) 29 })
30 }, 30 },
31 - StopServer () { 31 + StopServer() {
32 return new Promise((resolve, reject) => { 32 return new Promise((resolve, reject) => {
33 - console.log(server)
34 if (server) { 33 if (server) {
35 server.close() 34 server.close()
36 server.on('close', () => { 35 server.on('close', () => {
......
1 import { autoUpdater } from 'electron-updater' 1 import { autoUpdater } from 'electron-updater'
2 -import { ipcMain } from 'electron' 2 +import { ipcMain, BrowserWindow } from 'electron'
3 /** 3 /**
4 * -1 检查更新失败 0 正在检查更新 1 检测到新版本,准备下载 2 未检测到新版本 3 下载中 4 下载完成 4 * -1 检查更新失败 0 正在检查更新 1 检测到新版本,准备下载 2 未检测到新版本 3 下载中 4 下载完成
5 **/ 5 **/
6 -function Message (mainWindow, type, data) { 6 +class Update {
7 - const senddata = { 7 + public mainWindow: BrowserWindow
8 - state: type, 8 + constructor(mainWindow: BrowserWindow) {
9 - msg: data
10 - }
11 - mainWindow.webContents.send('UpdateMsg', senddata)
12 -}
13 -
14 -export default {
15 - Update (mainWindow) {
16 - // 设置地址要和package中的一样
17 autoUpdater.setFeedURL('http://127.0.0.1:25565/') 9 autoUpdater.setFeedURL('http://127.0.0.1:25565/')
10 + this.mainWindow = mainWindow
11 + // 注册事件
12 + this.checkUpdate()
13 + this.start()
14 + this.hasData()
15 + this.noData()
16 + this.listen()
17 + this.done()
18 + this.quitInstall()
19 + this.error()
20 + }
21 + // 负责向渲染进程发送信息
22 + Message(mainWindow: BrowserWindow, type: Number, data?: String) {
23 + const senddata = {
24 + state: type,
25 + msg: data || ''
26 + }
27 + mainWindow.webContents.send('UpdateMsg', senddata)
28 + }
18 29
19 - // 当更新发生错误的时候触发。 30 + // 当更新发生错误的时候触发。
31 + error() {
20 autoUpdater.on('error', (err) => { 32 autoUpdater.on('error', (err) => {
21 - console.log('更新出现错误') 33 + console.log('更新出现错误', err.message)
22 - console.log(err.message)
23 if (err.message.includes('sha512 checksum mismatch')) { 34 if (err.message.includes('sha512 checksum mismatch')) {
24 - Message(mainWindow, -1, 'sha512校验失败') 35 + this.Message(this.mainWindow, -1, 'sha512校验失败')
25 } 36 }
26 }) 37 })
38 + }
39 + // 当开始检查更新的时候触发
27 40
28 - // 当开始检查更新的时候触发 41 + start() {
29 autoUpdater.on('checking-for-update', (event, arg) => { 42 autoUpdater.on('checking-for-update', (event, arg) => {
30 console.log('开始检查更新') 43 console.log('开始检查更新')
31 - Message(mainWindow, 0) 44 + this.Message(this.mainWindow, 0)
32 }) 45 })
46 + }
33 47
34 - // 发现可更新数据时 48 + // 发现可更新数据时
49 + hasData() {
35 autoUpdater.on('update-available', (event, arg) => { 50 autoUpdater.on('update-available', (event, arg) => {
36 console.log('有更新') 51 console.log('有更新')
37 - Message(mainWindow, 1) 52 + this.Message(this.mainWindow, 1)
38 }) 53 })
54 + }
39 55
40 - // 没有可更新数据时 56 + // 没有可更新数据时
57 + noData() {
41 autoUpdater.on('update-not-available', (event, arg) => { 58 autoUpdater.on('update-not-available', (event, arg) => {
42 console.log('没有更新') 59 console.log('没有更新')
43 - Message(mainWindow, 2) 60 + this.Message(this.mainWindow, 2)
44 }) 61 })
62 + }
45 63
46 - // 下载监听 64 + // 下载监听
65 + listen() {
47 autoUpdater.on('download-progress', (progressObj) => { 66 autoUpdater.on('download-progress', (progressObj) => {
48 - Message(mainWindow, 3, progressObj) 67 + this.Message(this.mainWindow, 3, progressObj)
49 }) 68 })
69 + }
50 70
51 - // 下载完成 71 + // 下载完成
72 + done() {
52 autoUpdater.on('update-downloaded', () => { 73 autoUpdater.on('update-downloaded', () => {
53 console.log('下载完成') 74 console.log('下载完成')
54 - Message(mainWindow, 4) 75 + this.Message(this.mainWindow, 4)
55 }) 76 })
56 - // 执行自动更新检查 77 + }
78 +
79 + // 执行自动更新检查
80 + checkUpdate() {
57 ipcMain.handle('check-update', () => { 81 ipcMain.handle('check-update', () => {
58 autoUpdater.checkForUpdates().catch(err => { 82 autoUpdater.checkForUpdates().catch(err => {
59 console.log('网络连接问题', err) 83 console.log('网络连接问题', err)
60 }) 84 })
61 }) 85 })
62 - // 渲染进程执行更新操作 86 + }
87 +
88 + // 退出并安装
89 + quitInstall() {
63 ipcMain.handle('confirm-update', () => { 90 ipcMain.handle('confirm-update', () => {
64 autoUpdater.quitAndInstall() 91 autoUpdater.quitAndInstall()
65 }) 92 })
66 } 93 }
67 } 94 }
95 +
96 +export default Update
......
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 defaultDownloadUrL = null
11 -// 识别操作系统位数D
12 -os.arch().includes('64') ? Sysarch = 'win64' : Sysarch = 'win32'
13 -// 识别操作系统
14 -// linux自己修改后缀名哦,我没有linux就没有测试了
15 -if (os.platform().includes('win32')) {
16 - defaultDownloadUrL = baseUrl + `electron_${version}_${Sysarch}.exe?${new Date().getTime()}`
17 -} else if (os.platform().includes('linux')) {
18 - defaultDownloadUrL = baseUrl + `electron_${version}_${Sysarch}?${new Date().getTime()}`
19 -} else {
20 - defaultDownloadUrL = baseUrl + `electron_${version}_mac.dmg?${new Date().getTime()}`
21 -}
22 -export default {
23 - download (mainWindow) {
24 - ipcMain.handle('start-download', (event, msg) => {
25 - mainWindow.webContents.downloadURL(msg.downloadUrL || defaultDownloadUrL)
26 - mainWindow.webContents.session.on('will-download', (event, item, webContents) => {
27 - // 将文件保存在系统的下载目录
28 - const filePath = path.join(app.getPath('downloads'), item.getFilename())
29 - // 自动保存
30 - item.setSavePath(filePath)
31 - // 下载进度
32 - item.on('updated', (event, state) => {
33 - switch (state) {
34 - case 'progressing':
35 - mainWindow.webContents.send('download-progress', (item.getReceivedBytes() / item.getTotalBytes() * 100).toFixed(0))
36 - break
37 - case 'interrupted ':
38 - mainWindow.webContents.send('download-paused', true)
39 - break
40 - default:
41 -
42 - break
43 - }
44 - })
45 - // 下载完成或失败
46 - item.once('done', (event, state) => {
47 - switch (state) {
48 - case 'completed':
49 - const data = {
50 - filePath
51 - }
52 - mainWindow.webContents.send('download-done', data)
53 - break
54 - case 'interrupted':
55 - mainWindow.webContents.send('download-error', true)
56 - dialog.showErrorBox('下载出错', '由于网络或其他未知原因导致客户端下载出错,请前往官网进行重新安装')
57 - break
58 - default:
59 - break
60 - }
61 - })
62 - })
63 - })
64 - }
65 -}
1 +import { app, ipcMain, BrowserWindow, dialog } from 'electron'
2 +import { join } from 'path'
3 +import { arch, platform } from 'os'
4 +import { exists, remove } from 'fs-extra'
5 +import { version } from '../../../package.json'
6 +
7 +
8 +/**
9 + *
10 + * @description
11 + * @returns {void} 下载类
12 + * @param {mainWindow} 主窗口
13 + * @param {downloadUrl} 下载地址,当未传入时则会使用预先设置好的baseUrl拼接名称
14 + * @author Sky
15 + * @date 2020-08-12
16 + */
17 +
18 +class Main {
19 +
20 + public mainWindow: BrowserWindow = null
21 + public downloadUrl: string = ""
22 + public version: string = version
23 + public baseUrl: string = ''
24 + public Sysarch: string = arch().includes('64') ? 'win64' : 'win32'
25 + public HistoryFilePath = join(app.getPath('downloads'), platform().includes('win32') ? `electron_${this.version}_${this.Sysarch}.exe` : `electron_${this.version}_mac.dmg`)
26 +
27 +
28 + constructor(mainWindow: BrowserWindow, downloadUrl?: string) {
29 + this.mainWindow = mainWindow
30 + this.downloadUrl = downloadUrl || platform().includes('win32') ? this.baseUrl + `electron_${this.version}_${this.Sysarch}.exe?${new Date().getTime()}` : this.baseUrl + `electron_${this.version}_mac.dmg?${new Date().getTime()}`
31 + }
32 +
33 + start() {
34 + ipcMain.on('satrt-download', (event, msg) => {
35 + // 更新时检查有无同名文件,若有就删除,若无就开始下载
36 + exists(this.HistoryFilePath, async (e) => {
37 + try {
38 + if (e) {
39 + await remove(this.HistoryFilePath)
40 + }
41 + this.mainWindow.webContents.downloadURL(this.downloadUrl)
42 + } catch (error) { console.log(error) }
43 + })
44 + this.mainWindow.webContents.session.on('will-download', (event: any, item: any, webContents: any) => {
45 + const filePath = join(app.getPath('downloads'), item.getFilename())
46 + item.setSavePath(filePath)
47 + item.on('updated', (event: any, state: String) => {
48 + switch (state) {
49 + case 'progressing':
50 + this.mainWindow.webContents.send('download-progress', (item.getReceivedBytes() / item.getTotalBytes() * 100).toFixed(0))
51 + break
52 + default:
53 + this.mainWindow.webContents.send('download-error', true)
54 + dialog.showErrorBox('下载出错', '由于网络或其他未知原因导致客户端下载出错,请前往官网进行重新安装')
55 + break
56 + }
57 + })
58 + item.once('done', (event: any, state: String) => {
59 + switch (state) {
60 + case 'completed':
61 + const data = {
62 + filePath
63 + }
64 + this.mainWindow.webContents.send('download-done', data)
65 + break
66 + case 'interrupted':
67 + this.mainWindow.webContents.send('download-error', true)
68 + dialog.showErrorBox('下载出错', '由于网络或其他未知原因导致客户端下载出错,请前往官网进行重新安装')
69 + break
70 + default:
71 + break
72 + }
73 + })
74 + })
75 + })
76 + }
77 +}
78 +
79 +export default Main
1 import { ipcMain, dialog, BrowserWindow } from 'electron' 1 import { ipcMain, dialog, BrowserWindow } from 'electron'
2 -import Server from '../server/index' 2 +import Server from '../server'
3 import { winURL } from '../config/StaticPath' 3 import { winURL } from '../config/StaticPath'
4 4
5 export default { 5 export default {
6 - Mainfunc (mainWindow, IsUseSysTitle) { 6 + Mainfunc(mainWindow: BrowserWindow, IsUseSysTitle: Boolean) {
7 ipcMain.handle('IsUseSysTitle', async () => { 7 ipcMain.handle('IsUseSysTitle', async () => {
8 return IsUseSysTitle 8 return IsUseSysTitle
9 }) 9 })
......
1 -import { BrowserWindow, Menu } from 'electron'
2 -import menuconfig from '../config/menu'
3 -import config from '@config'
4 -import setIpc from './ipcMain'
5 -import electronDevtoolsInstaller, { VUEJS_DEVTOOLS } from 'electron-devtools-installer'
6 -import upload from './checkupdate'
7 -import DownloadUpdate from './downloadFile'
8 -import { winURL, loadingURL } from '../config/StaticPath'
9 -
10 -var loadWindow = null
11 -var mainWindow = null
12 -
13 -function createMainWindow () {
14 - /**
15 - * Initial window options
16 - */
17 - mainWindow = new BrowserWindow({
18 - height: 800,
19 - useContentSize: true,
20 - width: 1700,
21 - minWidth: 1366,
22 - show: false,
23 - frame: config.IsUseSysTitle,
24 - titleBarStyle: 'hidden',
25 - webPreferences: {
26 - nodeIntegration: true,
27 - webSecurity: false,
28 - // 如果是开发模式可以使用devTools
29 - devTools: process.env.NODE_ENV === 'development',
30 - // devTools: true,
31 - // 在macos中启用橡皮动画
32 - scrollBounce: process.platform === 'darwin'
33 - }
34 - })
35 - // 这里设置只有开发环境才注入显示开发者模式
36 - if (process.env.NODE_ENV === 'development') {
37 - menuconfig.push({
38 - label: '开发者设置',
39 - submenu: [{
40 - label: '切换到开发者模式',
41 - accelerator: 'CmdOrCtrl+I',
42 - role: 'toggledevtools'
43 - }]
44 - })
45 - }
46 - // 载入菜单
47 - const menu = Menu.buildFromTemplate(menuconfig)
48 - Menu.setApplicationMenu(menu)
49 - mainWindow.loadURL(winURL)
50 -
51 - setIpc.Mainfunc(mainWindow, config.IsUseSysTitle)
52 - upload.Update(mainWindow)
53 - DownloadUpdate.download(mainWindow)
54 -
55 - if (process.env.NODE_ENV === 'development') {
56 - mainWindow.webContents.once('dom-ready', () => {
57 - mainWindow.show()
58 - electronDevtoolsInstaller(VUEJS_DEVTOOLS)
59 - .then((name) => console.log(`installed: ${name}`))
60 - .catch(err => console.log('Unable to install `vue-devtools`: \n', err))
61 - })
62 - if (config.UseStartupChart) loadWindow.destroy()
63 -
64 - mainWindow.webContents.openDevTools(true)
65 - } else {
66 - mainWindow.webContents.once('dom-ready', () => {
67 - mainWindow.show()
68 - if (config.UseStartupChart) loadWindow.destroy()
69 - })
70 - }
71 -
72 - mainWindow.on('closed', () => {
73 - mainWindow = null
74 - })
75 -}
76 -
77 -function loadindWindow () {
78 - loadWindow = new BrowserWindow({
79 - width: 400,
80 - height: 600,
81 - frame: false,
82 - backgroundColor: '#222',
83 - skipTaskbar: true,
84 - transparent: true,
85 - resizable: false,
86 - webPreferences: { experimentalFeatures: true }
87 - })
88 -
89 - loadWindow.loadURL(loadingURL)
90 -
91 - loadWindow.show()
92 -
93 - setTimeout(() => {
94 - createMainWindow()
95 - }, 2000)
96 -
97 - loadWindow.on('closed', () => {
98 - loadWindow = null
99 - })
100 -}
101 -
102 -function initWindow () {
103 - if (config.UseStartupChart) {
104 - return loadindWindow()
105 - } else {
106 - return createMainWindow()
107 - }
108 -}
109 -export default initWindow
1 +import setIpc from './ipcMain'
2 +import config from '@config/index'
3 +import menuconfig from '../config/menu'
4 +import DownloadUpdate from './downloadFile'
5 +import Update from './checkupdate';
6 +import { app, BrowserWindow, Menu, dialog } from 'electron'
7 +import { winURL, loadingURL } from '../config/StaticPath'
8 +import electronDevtoolsInstaller, { VUEJS_DEVTOOLS } from 'electron-devtools-installer'
9 +
10 +class MainInit {
11 +
12 + public winURL: string = ''
13 + public shartURL: string = ''
14 + public loadWindow: BrowserWindow = null
15 + public mainWindow: BrowserWindow = null
16 +
17 + constructor() {
18 + this.winURL = winURL
19 + this.shartURL = loadingURL
20 + if (process.env.NODE_ENV === 'development') {
21 + menuconfig.push({
22 + label: '开发者设置',
23 + submenu: [{
24 + label: '切换到开发者模式',
25 + accelerator: 'CmdOrCtrl+I',
26 + role: 'toggledevtools'
27 + }]
28 + })
29 + }
30 + }
31 + // 主窗口函数
32 + createMainWindow() {
33 + this.mainWindow = new BrowserWindow({
34 + height: 800,
35 + useContentSize: true,
36 + width: 1700,
37 + minWidth: 1366,
38 + show: false,
39 + frame: config.IsUseSysTitle,
40 + titleBarStyle: 'hidden',
41 + webPreferences: {
42 + nodeIntegration: true,
43 + webSecurity: false,
44 + // 如果是开发模式可以使用devTools
45 + devTools: process.env.NODE_ENV === 'development',
46 + // devTools: true,
47 + // 在macos中启用橡皮动画
48 + scrollBounce: process.platform === 'darwin'
49 + }
50 + })
51 + // 赋予模板
52 + const menu = Menu.buildFromTemplate(menuconfig as any)
53 + // 加载模板
54 + Menu.setApplicationMenu(menu)
55 + // 加载主窗口
56 + this.mainWindow.loadURL(this.winURL)
57 + // 下载文件
58 + new DownloadUpdate(this.mainWindow).start()
59 + // electron-update注册
60 + new Update(this.mainWindow)
61 + // 启用协议,这里暂时只用于自定义头部的时候使用
62 + setIpc.Mainfunc(this.mainWindow, config.IsUseSysTitle)
63 + // 安装devtools
64 + if (process.env.NODE_ENV === 'development') {
65 + this.mainWindow.webContents.once('dom-ready', () => {
66 + this.mainWindow.show()
67 + electronDevtoolsInstaller(VUEJS_DEVTOOLS)
68 + .then((name) => console.log(`已安装: ${name}`))
69 + .catch(err => console.log('无法安装 `vue-devtools`: \n 可能发生得错误:网络连接问题 \n', err))
70 + if (config.UseStartupChart) this.loadWindow.destroy()
71 + this.mainWindow.webContents.openDevTools({ mode: 'undocked', activate: true })
72 + })
73 + } else {
74 + this.mainWindow.webContents.once('dom-ready', () => {
75 + this.mainWindow.show()
76 + if (config.UseStartupChart) this.loadWindow.destroy()
77 + })
78 + }
79 + // 当确定渲染进程卡死时
80 + this.mainWindow.webContents.on('crashed', () => {
81 + dialog.showMessageBox(this.mainWindow, {
82 + type: 'warning',
83 + title: '警告',
84 + buttons: ['重载', '退出'],
85 + message: '图形化进程失去响应,是否等待其恢复?',
86 + noLink: true
87 + }).then(res => {
88 + if (res.response === 0) this.mainWindow.reload()
89 + else this.mainWindow.close()
90 + })
91 + })
92 + // 不知道什么原因,反正就是这个窗口里的页面触发了假死时执行
93 + this.mainWindow.on('unresponsive', () => {
94 + dialog.showMessageBox(this.mainWindow, {
95 + type: 'warning',
96 + title: '警告',
97 + buttons: ['重载', '退出'],
98 + message: '图形化进程失去响应,是否等待其恢复?',
99 + noLink: true
100 + }).then(res => {
101 + if (res.response === 0) this.mainWindow.reload()
102 + else this.mainWindow.close()
103 + })
104 + })
105 + app.on('gpu-process-crashed', () => {
106 + dialog.showMessageBox(this.mainWindow, {
107 + type: 'warning',
108 + title: '',
109 + buttons: ['切换', '退出'],
110 + message: '侦测到图形处理设备丢失,若继续则会导致软件出现意外故障,是否切换到通用模式,若切换后仍然黑屏请联系我们。',
111 + noLink: true
112 + }).then(res => {
113 + // 当显卡出现崩溃现象时使用该设置禁用显卡加速模式。
114 + if (res.response === 0) {
115 + app.disableHardwareAcceleration()
116 + this.mainWindow.reload()
117 + } else {
118 + this.mainWindow.close()
119 + }
120 + })
121 + })
122 + this.mainWindow.on('closed', () => {
123 + this.mainWindow = null
124 + })
125 + }
126 + // 加载窗口函数
127 + loadindWindow(loadingURL: string) {
128 + this.loadWindow = new BrowserWindow({
129 + width: 400,
130 + height: 600,
131 + frame: false,
132 + skipTaskbar: true,
133 + transparent: true,
134 + resizable: false,
135 + webPreferences: { experimentalFeatures: true }
136 + })
137 +
138 + this.loadWindow.loadURL(loadingURL)
139 + this.loadWindow.show()
140 + this.loadWindow.setAlwaysOnTop(true)
141 + // 延迟两秒可以根据情况后续调快,= =,就相当于个,sleep吧,就那种。 = =。。。
142 + setTimeout(() => {
143 + this.createMainWindow()
144 + }, 1500)
145 + }
146 + // 初始化窗口函数
147 + initWindow() {
148 + if (config.UseStartupChart) {
149 + return this.loadindWindow(this.shartURL)
150 + } else {
151 + return this.createMainWindow()
152 + }
153 +
154 + }
155 +}
156 +export default MainInit
1 import { ipcRenderer } from 'electron' 1 import { ipcRenderer } from 'electron'
2 export default { 2 export default {
3 - send (name, data = {}) { 3 + send (name, data) {
4 return new Promise((resolve, reject) => { 4 return new Promise((resolve, reject) => {
5 ipcRenderer.invoke(name, data).then(res => { 5 ipcRenderer.invoke(name, data).then(res => {
6 resolve(res) 6 resolve(res)
......
1 +{
2 + "compileOnSave": false,
3 + "compilerOptions": {
4 + "resolveJsonModule": true,
5 + "baseUrl": ".",
6 + "outDir": "./dist/electron",
7 + "sourceMap": true,
8 + "declaration": false,
9 + "module": "es2015",
10 + "moduleResolution": "node",
11 + "allowSyntheticDefaultImports": true,
12 + "emitDecoratorMetadata": true,
13 + "experimentalDecorators": true,
14 + "importHelpers": true,
15 + "target": "es5",
16 + "paths": {
17 + "@config/*": [
18 + "config/*"
19 + ]
20 + },
21 + "typeRoots": [
22 + "node_modules/@types"
23 + ],
24 + "lib": [
25 + "es2018",
26 + "dom"
27 + ]
28 + }
29 +}
...\ No newline at end of file ...\ No newline at end of file