-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
220 lines (208 loc) · 5.14 KB
/
main.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
const { app, BrowserWindow, Tray, Menu, dialog, Notification } = require('electron')
const AutoLaunch = require('auto-launch')
const path = require('path')
const { checkNeddLogSend } = require(path.join(__dirname, 'render/common/common.js'))
const { computedHoursStart } = require(path.join(__dirname, 'utils/utils.js'))
const reloadApp = require(path.join(__dirname, 'main/job/reload.js'))
const isSupporteNotification = Notification.isSupported()
let win = null
let timer = null
let jobTimer = null
let appIcon = null
let neddLogSend = true
let aboutWin = null
let notifi = null
let setWin = null
let historyWin = null
const autoLogSend = new AutoLaunch({
name: 'logSend'
})
reloadApp(app)
function createWindow () {
win = new BrowserWindow({
width: 800,
height: 670,
icon: path.join(__dirname, 'img/logo/logo.png'),
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
nodeIntegration: true,
enableRemoteModule: true,
contextIsolation: false
},
})
win.setSkipTaskbar(true)
win.loadFile('index.html')
win.on('close', event => {
event.preventDefault()
win && win.hide()
})
}
function createSendLogNotification () {
notifi = new Notification({
title: '填写日报时间到了',
subtitle: '每日任务',
body: '请准时提交日报, 配合好管理的工作,谢谢同学!',
silent: true,
timeoutType: 'never'
})
notifi.on('click', () => {
win && win.show()
})
}
// 循环让用户填写日志
function loopSendLog () {
timer = setInterval(() => {
if (neddLogSend && !win.isVisible()) {
if (isSupporteNotification) {
notifi.show()
} else {
win.show()
}
} else if (!neddLogSend) {
clearInterval(timer)
clearTimeout(jobTimer)
jobTimer = null
timer = null
// 设置任务计划 下午六点开始执行循环提醒用户发日志
jobTimer = computedHoursStart({ hours: 18 }, loopSendLog)
}
}, 30000)
}
// 创建系统托盘
async function createTray () {
// 获取当前开机自启状态
const isEnabled = await autoLogSend.isEnabled().catch(err => console.log(err))
const menuList = [
{
label: '发送日报',
click () {
win.show()
}
},
{
label: '本地日报历史',
click () {
if (historyWin) {
historyWin.show()
return
}
historyWin = new BrowserWindow({
width: 420,
height: 320,
webPreferences: {
nodeIntegration: true,
enableRemoteModule: true,
contextIsolation: false
}
})
historyWin.loadFile('history.html')
historyWin.on('closed', () => {
historyWin = null
})
}
},
{
label: '开机启动',
type: 'checkbox',
checked: isEnabled,
click () {
if (isEnabled) {
autoLogSend.disable()
} else {
autoLogSend.enable()
}
}
},
{
label: '日报发送配置',
click () {
if (setWin) {
setWin.show()
return
}
setWin = new BrowserWindow({
width: 420,
height: 320,
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
enableRemoteModule: true
}
})
setWin.loadFile('author.html')
setWin.on('closed', () => {
setWin = null
})
}
},
{
label: '关于作者',
click() {
if (aboutWin) {
aboutWin.show()
return
}
aboutWin = new BrowserWindow({
width: 300,
height: 300,
webPreferences: {
nodeIntegration: true,
enableRemoteModule: true,
contextIsolation: false
}
})
aboutWin.loadFile('about.html')
aboutWin.on('closed', () => {
aboutWin = null
})
}
},
{
label: '退出',
click () {
win = null
app.exit()
}
}
]
const contextMenu = Menu.buildFromTemplate(menuList)
appIcon = new Tray(path.join(__dirname, 'img/logo/logo-16.png'))
appIcon.on('click', () => {
if (win.isFocused()) {
win.hide()
} else {
win.show()
}
})
appIcon.on('right-click', () => {
appIcon.popUpContextMenu(contextMenu)
})
}
if (process.platform === 'darwin' && app.isPackaged) {
app.dock.hide()
}
app.whenReady().then(async () => {
createWindow()
createSendLogNotification()
// 检查今日是否已经发送日报
neddLogSend = await checkNeddLogSend()
// 设置任务计划 下午六点开始执行循环提醒用户发日志
jobTimer = computedHoursStart({ hours: 18 }, loopSendLog)
// setInterval(computedHoursStart, 1000 * 60 * 60)
createTray()
})
app.on('activate', () => {
console.log('activate')
if (process.platform !== 'darwin') {
win.show()
} else if (BrowserWindow.getAllWindows().length === 0) {
createWindow()
}
})
app.on('will-quite', () => {
dialog.showMessageBox({
title: 'test',
message: '要退出了啊!'
})
})
app.on('window-all-closed', event => {})