-
Notifications
You must be signed in to change notification settings - Fork 15
/
main.js
137 lines (124 loc) · 4.25 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
const { app, BrowserWindow, ipcMain, Menu, MenuItem, dialog, shell } = require('electron');
const path = require('node:path');
const { ipcHandle } = require("./server/ipcHandle");
const { autoUpdater } = require("electron-updater");
const consoleLogUtil = require('./server/utils/consoleLogUtil');
const { getUserDataProperty } = require("./server/utils/storeUtil");
const Constants = require("./constant/constants");
function createWindow() {
const iconPath = path.join(__dirname, 'public/favicon.png');
// 创建浏览器窗口
const win = new BrowserWindow({
width: 1350,
height: 800,
webPreferences: {
nodeIntegration: false,
contextIsolation: true,
enableRemoteModule: false,
preload: path.join(__dirname, "preload.js") // use a preload script
},
icon: iconPath
});
const menuItems = Menu.getApplicationMenu().items;
const helpMenuItem = menuItems.find(item => item.label === "Help");
if (helpMenuItem) {
helpMenuItem.submenu.append(new MenuItem({
label: 'Github',
click: () => {
shell.openExternal("https://github.com/GilHogan/JDTreasureGrabber");
}
}));
helpMenuItem.submenu.append(new MenuItem({
label: 'License',
click: () => {
shell.openExternal("https://www.gnu.org/licenses/agpl-3.0.en.html");
}
}));
helpMenuItem.submenu.append(new MenuItem({
label: 'About',
click: () => {
// 在这里添加处理 "About" 点击事件的代码
const version = app.getVersion();
const message = `Version: ${version}\r\nAuthor: hogan \r\nLicense: AGPL-3.0`;
dialog.showMessageBox({
icon: iconPath,
title: '京东夺宝岛助手',
message: message,
buttons: ['OK'],
type: 'none'
});
}
}));
const menu = Menu.buildFromTemplate(menuItems);
// 重新设置修改后的菜单
Menu.setApplicationMenu(menu);
}
// 并且为你的应用加载index.html
win.loadFile(path.join(__dirname, "pages/index.html"));
// 打开开发者工具
// win.webContents.openDevTools();
}
// ipcRenderer.invoke 处理
ipcMain.handle("toMain", async (e, args) => {
return await ipcHandle(e, args);
})
// ipcRenderer.on 处理
ipcMain.on("toMain", async (e, args) => {
if (!args || !args.event) {
return;
}
const data = await ipcHandle(e, args);
const webContents = e.sender;
const win = BrowserWindow.fromWebContents(webContents)
win.webContents.send("fromMain", { event: args.event, data: data });
});
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// 部分 API 在 ready 事件触发后才能使用。
app.whenReady().then(function () {
createWindow();
// 查询是否启用自动更新,未查到时,默认自动更新
const options = getUserDataProperty(Constants.StoreKeys.OPTIONS_KEY) || {};
const enableAutoUpdate = options.enableAutoUpdate;
if (enableAutoUpdate === undefined || enableAutoUpdate === null || enableAutoUpdate) {
// 检查更新
autoUpdater.checkForUpdatesAndNotify();
}
});
// Quit when all windows are closed.
app.on('window-all-closed', () => {
// 在 macOS 上,除非用户用 Cmd + Q 确定地退出,
// 否则绝大部分应用及其菜单栏会保持激活。
if (process.platform !== 'darwin') {
app.quit()
}
});
app.on('activate', () => {
// 在macOS上,当单击dock图标并且没有其他窗口打开时,
// 通常在应用程序中重新创建一个窗口。
if (BrowserWindow.getAllWindows().length === 0) {
createWindow()
}
});
// 应用更新
autoUpdater.on('checking-for-update', () => {
consoleLogUtil.log('Checking for update...');
})
autoUpdater.on('update-available', (info) => {
consoleLogUtil.log('Update available.');
})
autoUpdater.on('update-not-available', (info) => {
consoleLogUtil.log('Update not available.');
})
autoUpdater.on('error', (err) => {
consoleLogUtil.log('Error in auto-updater. ' + err);
})
autoUpdater.on('download-progress', (progressObj) => {
let log_message = "Download speed: " + (progressObj.bytesPerSecond / 1024).toFixed(2) + " KB/s";
log_message = log_message + ' - Downloaded ' + progressObj.percent.toFixed(2) + '%';
log_message = log_message + ' (' + progressObj.transferred + "/" + progressObj.total + ')';
consoleLogUtil.log(log_message);
})
autoUpdater.on('update-downloaded', (info) => {
consoleLogUtil.log('Update downloaded');
});