-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
181 lines (151 loc) · 4.88 KB
/
index.js
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
const { app, ipcMain, dialog, BrowserWindow } = require('electron')
const os = require('os')
const storage = require('./helpers/storage')
const path = require('path')
const { autoUpdater } = require('electron-differential-updater') //electron-updater
const log = require('electron-log')
const packageJSON = require('./package.json')
const isProduction = process.env.NODE_ENV !== 'development'
const isWindows = os.platform() === 'win32'
const rendererPageBaseURL = !isProduction
? 'http://localhost:8188'
: 'file://' + path.join(__dirname, './renderer/index.html')
let mainWindow = null
const updateCheckResponseData = {
error: { status: -1, msg: '更新出错,请稍后再试' },
checking: { status: 0, msg: '正在检查应用程序更新' },
updating: { status: 1, msg: '检测到新版本,正在下载,请稍后' },
latest: { status: 2, msg: '您现在使用的版本为最新版本,无需更新!' },
}
if (isProduction) {
Object.assign(console, log.functions)
}
function initialize() {
app.commandLine.appendSwitch('--autoplay-policy', 'no-user-gesture-required')
makeSingleInstance()
function createMainWindow() {
const windowOptions = {
show: false,
width: 440,
height: 680,
title: app.getName(),
backgroundColor: '#ffffff',
transparent: false,
resizable: false,
maximizable: false,
fullscreenable: false,
frame: false,
hasShadow: true,
icon: path.join(__dirname, 'assets/icon.png'),
titleBarStyle: isWindows ? 'customBottomOnHover' : 'hiddenInset',
// trafficLightPosition: { x: 10, y: 22 },
webPreferences: {
devTools: !isProduction,
webSecurity: false,
nodeIntegration: true,
experimentalFeatures: true,
},
}
mainWindow = new BrowserWindow(windowOptions)
mainWindow.loadURL(`${rendererPageBaseURL}`)
mainWindow.once('ready-to-show', () => {
mainWindow.show()
})
mainWindow.on('closed', () => {
mainWindow = null
})
mainWindow.webContents.openDevTools({
mode: 'detach',
})
}
function initializeUpdater() {
autoUpdater.setFeedURL('https://repic-1254052530.cos.ap-guangzhou.myqcloud.com/releases')
// 更新下载进度事件
autoUpdater.on('download-progress', function (data) {
sendUpdateMessage(data, 'update-download-progress')
})
//更新错误
autoUpdater.on('error', function (error) {
sendUpdateMessage(updateCheckResponseData.error)
})
//检查中
autoUpdater.on('checking-for-update', function () {
sendUpdateMessage(updateCheckResponseData.checking)
})
//发现新版本
autoUpdater.on('update-available', function (info) {
sendUpdateMessage(updateCheckResponseData.updating)
})
//当前版本为最新版本
autoUpdater.on('update-not-available', function (info) {
setTimeout(function () {
sendUpdateMessage(updateCheckResponseData.latest)
}, 1000)
})
autoUpdater.on('update-downloaded', (event, releaseNotes, releaseName) => {
const dialogOpts = {
type: 'info',
buttons: ['重启', '稍后'],
title: '软件更新',
message: process.platform === 'win32' ? releaseNotes : releaseName,
detail: '新版本已经下载完成是否重启应用并安装?',
}
dialog.showMessageBox(dialogOpts).then((returnValue) => {
if (returnValue.response === 0) {
autoUpdater.quitAndInstall()
} else if (returnValue.response === 1) {
sendUpdateMessage(
{
percent: 0,
},
'update-download-progress'
)
}
})
})
}
// 通过main进程发送事件给renderer进程,提示更新信息
function sendUpdateMessage(message, channel = 'update-event') {
log.info(message)
mainWindow.webContents.send(channel, message)
}
app.on('ready', () => {
storage.initializeAPPData().then(() => {
createMainWindow()
initializeUpdater()
})
})
app.on('window-all-closed', () => {
app.quit()
})
app.on('activate', () => {
mainWindow === null && createMainWindow()
})
ipcMain.on('checkForUpdate', (event, data) => {
autoUpdater.checkForUpdates()
})
}
// Make this app a single instance app.
//
// The main window will be restored and focused instead of a second window
// opened when a person attempts to launch a second instance.
//
// Returns true if the current version of the app should quit instead of
// launching.
function makeSingleInstance() {
if (process.mas) return
app.requestSingleInstanceLock()
app.on('second-instance', () => {
if (mainWindow) {
if (mainWindow.isMinimized()) mainWindow.restore()
mainWindow.focus()
}
})
}
initialize()
process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = 0
// 处理未捕获的错误
process.on('uncaughtException', (error) => {
console.log(error)
})
// TODO