This repository has been archived by the owner on Feb 11, 2023. It is now read-only.
/
dingtalk.js
254 lines (232 loc) · 5.13 KB
/
dingtalk.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
import { app, Menu, ipcMain, BrowserWindow } from 'electron'
import { initSetting, readSetting, writeSetting } from './setting'
import online from './online'
import Notify from './notify'
import mainWin from './mainWin'
import emailWin from './emailWin'
import errorWin from './errorWin'
import aboutWin from './aboutWin'
import shortcut from './shortcut'
import autoUpdate from './autoUpdate'
import settingWin from './settingWin'
import DingtalkTray from './dingtalkTray'
import ShortcutCapture from 'shortcut-capture'
export default class DingTalk {
// app对象是否ready
_ready = null
// 托盘图标
$tray = null
// 主窗口
$mainWin = null
// 邮箱窗口
$emailWin = null
// 错误窗口
$errorWin = null
// 设置窗口
$settingWin = null
// 关于窗口
$aboutWin = null
// 截图对象
$shortcutCapture = null
// 网络情况,默认为null,必须等到页面报告状态
online = null
// 默认配置
setting = {
autoupdate: true,
keymap: {
'shortcut-capture': ['Control', 'Alt', 'A']
}
}
constructor () {
if (!this.requestSingleInstanceLock()) {
this.init().then(() => {
app.setAppUserModelId('com.electron.dingtalk')
// 移除窗口菜单
Menu.setApplicationMenu(null)
this.initMainWin()
this.initTray()
this.initShortcutCapture()
this.initNotify()
this.autoUpdate()
this.bindShortcut()
})
}
}
/**
* 初始化
* @return {Promise} setting
*/
async init () {
online(this)()
this.setting = await initSetting(this)()
return this.whenReady()
}
/**
* 使应用程序成为单个实例应用程序
* 而不是允许应用程序的多个实例运行
* 这将确保只有一个应用程序的实例正在运行
* 其余的实例全部会被终止并退出
*/
requestSingleInstanceLock () {
// 同时只能运行一个人实例
const isSecond = app.makeSingleInstance(() => this.showMainWin())
if (isSecond) app.quit()
return isSecond
}
/**
* 初始化主窗口
*/
initMainWin () {
this.$mainWin = mainWin(this)()
}
/**
* 初始化托盘图标
*/
initTray () {
this.$tray = new DingtalkTray({ dingtalk: this })
}
/**
* 初始化截图
*/
initShortcutCapture () {
this.$shortcutCapture = new ShortcutCapture()
}
/**
* 初始化消息提示
*/
initNotify () {
this.$notify = new Notify()
ipcMain.on('notify', (e, body) => this.$notify.show(body))
this.$notify.on('click', () => this.showMainWin())
}
/**
* 从文件中读取设置信息
* @return {Promise} setting
*/
readSetting () {
return readSetting(this)()
}
/**
* 写入设置到文件
* @return {Promise} setting
*/
writeSetting () {
return writeSetting(this)()
}
/**
* app是否ready
* @return {Promise} setting
*/
whenReady () {
if (this._ready) return this._ready
this._ready = new Promise((resolve, reject) => {
if (app.isReady()) return resolve()
app.once('ready', () => resolve())
// 所有窗口关闭之后退出应用
app.once('window-all-closed', () => {
if (process.platform !== 'darwin') {
if (this.$tray && !this.$tray.isDestroyed()) {
this.$tray.destroy()
this.$tray = null
}
app.quit()
}
})
})
return this._ready
}
/**
* 退出应用
*/
quit () {
const windows = BrowserWindow.getAllWindows()
windows.forEach(item => item.destroy())
if (process.platform !== 'darwin') {
if (this.$tray && !this.$tray.isDestroyed()) {
this.$tray.destroy()
this.$tray = null
}
app.quit()
}
}
/**
* 绑定快捷键
*/
bindShortcut () {
shortcut(this)()
}
/**
* 显示主窗口
*/
showMainWin () {
if (this.$mainWin) {
if (this.online) {
if (this.$mainWin.isMinimized()) this.$mainWin.restore()
this.$mainWin.show()
this.$mainWin.focus()
} else if (this.online === false) {
/**
* this.online === null不显示
* 因为可能此时还没有初始化online
* 即$mainWin还没有触发dom-ready
*/
this.showErrorWin()
}
}
}
/**
* 截图
*/
shortcutCapture () {
if (this.shortcutCapture) {
this.$shortcutCapture.shortcutCapture()
}
}
/**
* 显示邮箱窗口
* @param {Object} storage
*/
showEmailWin (storage) {
this.$emailWin = emailWin(this)(storage)
}
/**
* 显示错误窗口
*/
showErrorWin () {
this.$errorWin = errorWin(this)()
}
/**
* 隐藏错误窗口
*/
hideErrorWin () {
if (this.$errorWin) {
this.$errorWin.close()
}
}
/**
* 显示设置窗口
*/
showSettingWin () {
this.$settingWin = settingWin(this)()
}
/**
* 关闭设置窗口
*/
hideSettingWin () {
if (this.$settingWin) {
this.$settingWin.close()
}
}
/**
* 显示关于窗口
*/
showAboutWin () {
this.$aboutWin = aboutWin(this)()
}
/**
* 检查更新
*/
autoUpdate () {
autoUpdate(this)()
}
}