-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
400 lines (360 loc) · 11.9 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
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
/**
*
观星记 Starte
Copyright (c) 2022-2023, zestela.co.
网站: https://zestela.co/starte/
基于 MIT License 开源
任何根据 MIT License 修改和研究的版本都必须保留本注释, 否则视为未遵守开源协议
*/
require('v8-compile-cache');
const {
app,
BrowserWindow,
Menu,
shell,
ipcMain,
dialog,
Tray,
nativeImage
} = require('electron');
const path = require('path');
const fs = require('fs');
const os = require('os');
const axios = require('axios');
const starte = require("./packages/starte/index.js");
let mainWindow;
let popupWindow;
let mainpageRendererData = {};
let appTray = null;
let popupMsg;
process.on('unhandledRejection', (reason, p) => {
console.log('Unhandled Rejection at: Promise', p, 'reason:', reason);
// application specific logging, throwing an error, or other logic here
});
function reportError(errorMsg) {
popupMsg = errorMsg;
popupWindow = new BrowserWindow({
width: 300,
height: 200,
frame: false,
parent: mainWindow,
modal: true,
hasShadow: true,
webPreferences: {
preload: path.join(__dirname, 'preload.js')
},
resizable: false,
show: false
});
popupWindow.loadFile('src/popup.html');
popupWindow.show();
}
async function infoToServer() {
const errorMsg = "向服务器存储数据时出现错误,请<a href='https://zestela.co/support/' target='_blank'>点击此处反馈</a><br>错误信息:";
const userVersion = require("./package.json").version;
const userOS = os.version().replace(/ /g, '%20') + "%20" + os.release().replace(/ /g, '%20'); //获取电脑系统版本,replace是为了把空格替换成%20,否则api链接会在空格处断开
const getipAddress = await axios.get('https://ipapi.co/json/', { timeout: 20000 })
.catch(function (error) {
reportError(errorMsg + error);
});
const ipAddress = getipAddress.data.ip;
const timestamp = Math.round(new Date().getTime() / 1000);
const getUrl = `https://api.zestela.co/info/analysis.php?getip=${ipAddress}&getuseTime=${timestamp}&getdeviceId=${require("node-machine-id").machineIdSync({ original: true })}&getuseSystem=${userOS}&getuseVersion=${userVersion}`;
let sendInfoResult = await axios.get(getUrl, {
timeout: 30000
}).catch(function (error) {
reportError(errorMsg + error);
});
sendInfoResult = sendInfoResult.data;
if (sendInfoResult.code == 1) return 0;
else reportError(errorMsg + sendInfoResult.msg);
}
async function createWindow() {
mainWindow = new BrowserWindow({
minWidth: 900,
minHeight: 600,
width: 1280,
height: 720,
icon: "./src/icons/dock.ico",
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
},
frame: false,
show: false
});
Menu.setApplicationMenu(null);
mainWindow.loadFile('src/loading.html');
mainWindow.show();
if (!app.isPackaged) mainWindow.webContents.openDevTools();
let trayMenuTemplate = [{
label: '打开主界面',
icon: nativeImage.createFromPath(path.join(__dirname, "src/icons/toHome.png")),
click: function () {
mainWindow.loadFile("src/index.html");
mainWindow.show();
}
},
{
label: '投稿',
icon: nativeImage.createFromPath(path.join(__dirname, "src/icons/toSubmission.png")),
click: function () {
mainWindow.loadFile("src/submission.html");
mainWindow.show();
}
},
{
label: '退出观星记',
icon: nativeImage.createFromPath(path.join(__dirname, "src/icons/toExit.png")),
click: function () {
app.quit();
app.quit(); // 因为程序设定关闭为最小化,所以调用两次关闭,防止最大化时一次不能关闭的情况
}
}
];
appTray = new Tray(nativeImage.createFromPath(path.join(__dirname, "src/icons/dock.ico")));
const contextMenu = Menu.buildFromTemplate(trayMenuTemplate);
appTray.setToolTip('观星记 Starte');
appTray.setContextMenu(contextMenu);
appTray.on('click', function () {
mainWindow.show();
});
appTray.on('right-click', () => {
appTray.popUpContextMenu(trayMenuTemplate);
});
}
app.whenReady().then(async () => {
ipcMain.handle('get-cwd', () => {
return process.cwd().replaceAll("\\", "/");
});
ipcMain.handle('get-appdata', () => {
return process.env.APPDATA.replaceAll("\\", "/");
});
ipcMain.handle('get-shareId', () => {
return shareId;
});
ipcMain.handle('get-shareType', () => {
return shareType;
});
ipcMain.handle('get-version', () => {
return require("./package.json").version;
});
ipcMain.handle('get-mainpage-data', () => {
return mainpageRendererData;
});
ipcMain.handle('get-setting', async (event, configName) => {
return await starte.getSetting(configName);
});
ipcMain.handle('get-if-args', () => {
if (typeof (pageAnchor) == "undefined") {
return "114514";
} else {
return pageAnchor;
}
});
ipcMain.handle('get-vici-detail', () => {
return viciId;
});
ipcMain.handle('get-machine-id', () => {
return require("node-machine-id").machineIdSync({ original: true });
});
ipcMain.handle('get-popup-msg', () => {
return popupMsg;
});
createWindow();
app.on('activate', function () {
if (BrowserWindow.getAllWindows().length === 0) createWindow();
});
if (app.isPackaged) { infoToServer(); };
});
app.on('window-all-closed', function () {
if (process.platform !== 'darwin') app.quit();
});
app.on('web-contents-created', (e, webContents) => {
webContents.setWindowOpenHandler(({ url }) => {
shell.openExternal(url);
return { action: 'deny' };
});
});
ipcMain.on('init', async () => {
if (!fs.existsSync(path.join(process.env.APPDATA, "starte-cache")))
fs.mkdirSync(path.join(process.env.APPDATA, "starte-cache"));
if (!fs.existsSync(path.join(process.env.APPDATA, "starte-cache", "config.json")))
fs.writeFileSync(path.join(process.env.APPDATA, "starte-cache", "config.json"), JSON.stringify({}));
if (!fs.existsSync(path.join(process.env.APPDATA, "starte-cache", "mainpage-cache.json")))
fs.writeFileSync(path.join(process.env.APPDATA, "starte-cache", "mainpage-cache.json"), JSON.stringify({}));
const ifOpenConfig = await starte.getSetting("isSelfopen");
let mainpageCache = JSON.parse(fs.readFileSync(path.join(process.env.APPDATA, "starte-cache", "mainpage-cache.json")));
let mainpageData = await axios.get("https://api.zestela.co/new-mainpage/get-mainpage.php", {
timeout: 30000
})
.catch(function (error) {
console.log('Error', error.message);
mainWindow.loadFile('src/timeout.html');
});
mainpageData = mainpageData.data;
mainpageRendererData = mainpageData.data;
if (mainpageData.code !== 0) {
let filename = path.join(process.env.APPDATA, "starte-cache", mainpageData.data.id + ".png");
if (mainpageCache.date != mainpageData.data.date || !fs.existsSync(filename) || (mainpageCache.size != fs.statSync(filename).size)) {
console.log("Log: start downloading");
let https = require('https');
let mainpageReq = https.request(mainpageData.data.url, { method: 'HEAD' }, function (res) {
mainpageCache.date = mainpageData.data.date;
mainpageCache.size = JSON.parse(res.headers["content-length"]);
fs.writeFileSync(path.join(process.env.APPDATA, "starte-cache", "mainpage-cache.json"), JSON.stringify(mainpageCache));
starte.downloadImage(mainpageData.data.url, mainpageData.data.id + ".png")
.finally(() => {
console.log("Log: download successfully");
mainWindow.loadFile('src/index.html');
});
if (ifOpenConfig == true) starte.setWallpaper(filename);
});
mainpageReq.end();
} else {
console.log("Log: load cache successfully");
mainWindow.loadFile('src/index.html');
}
} else {
console.log("Log: there is no data of this month");
mainWindow.loadFile('src/timeout.html');
}
});
ipcMain.on('window-events', (event, type) => {
if (type === 1)
mainWindow.minimize();
else if (type === 2) {
if (mainWindow.isMaximized())
mainWindow.unmaximize();
else
mainWindow.maximize();
} else if (type === 3) mainWindow.hide();
});
ipcMain.on('pop-up-close', () => {
popupWindow.close();
});
let shareId = 0;
let shareType = 0;
ipcMain.on('share', async (event, id, type) => {
shareId = id;
shareType = type;
let shareData = await axios.get("https://api.zestela.co/new-mainpage/get-photo-title-describe-links.php?id=" + id, {
timeout: 5000
})
.catch(function (error) {
console.log('Error', error.message);
mainWindow.loadFile('src/timeout.html');
});
shareData = shareData.data;
let https = require('https');
let shareReq = https.request(shareData.data.url, { method: 'HEAD' }, function (res) {
let fileSize = JSON.parse(res.headers["content-length"]);
if (shareData.code !== 0) {
let filename = path.join(process.env.APPDATA, "starte-cache", shareId + ".png");
if (!fs.existsSync(filename) || (fileSize != fs.statSync(filename).size)) {
console.log("Log: start downloading");
starte.downloadImage(shareData.data.url, shareId + ".png")
.finally(async () => {
console.log("Log: download successfully");
await mainWindow.loadFile('src/share.html');
});
} else {
console.log("Log: load cache successfully");
mainWindow.loadFile('src/share.html');
}
}
});
shareReq.end();
});
ipcMain.on('set-wallpaper', async (event, id) => {
starte.setWallPaperOut(id);
});
ipcMain.on('save-share', async (event, data) => {
let dataBuffer = Buffer.from(data.replace(/^data:image\/\w+;base64,/, ""), 'base64');
let filePath = dialog.showSaveDialogSync({
filters: [{
name: 'img',
extensions: ['jpeg']
}]
});
if (filePath != undefined)
fs.writeFile(filePath, dataBuffer, () => { });
});
ipcMain.on("go-to-page", async (event, pageId) => {
switch (pageId) {
case 1:
await mainWindow.loadFile("src/index.html");
break;
case 2:
await mainWindow.loadFile("src/wallpaper-list.html");
break;
case 3:
await mainWindow.loadFile("src/settings.html");
break;
case 4:
await mainWindow.loadFile("src/settings-about.html");
break;
case 5:
await mainWindow.loadFile("src/timeout.html");
break;
case 6:
await mainWindow.loadFile("src/star-watching.html");
break;
case 7:
await mainWindow.loadFile("src/submission.html");
break;
case 8:
await mainWindow.loadFile("src/check-new.html");
break;
case 9:
await mainWindow.loadFile("src/donate.html");
break;
case 10:
await mainWindow.loadFile("src/vicissitudes.html");
break;
case 11:
await mainWindow.loadFile("src/search.html");
break;
}
});
ipcMain.on("go-to-page-with-args", async (event, page, id) => {
pageAnchor = id;
if (page == "wallpaper-list") {
await mainWindow.loadFile("src/wallpaper-list.html");
} else if (page == "sentence") {
await mainWindow.loadFile("src/star-watching.html");
}
});
ipcMain.on("out-alert", async (event, str) => {
reportError(str);
});
ipcMain.on("set-setting", async (event, configName, value) => {
starte.setSetting(configName, value);
const ifOpenConfig = starte.getSetting("isSelfopen");
if (ifOpenConfig == true) {
try {
const exeName = path.basename(process.execPath);
app.setLoginItemSettings({
openAtLogin: true,
openAsHidden: true,
path: process.execPath,
args: [
'--processStart', `"${exeName}"`,
]
});
} catch (err) {
console.log(err);
}
} else if (ifOpenConfig == false) {
try {
app.setLoginItemSettings({
openAtLogin: false
});
} catch (err) {
console.log(err);
}
}
});
ipcMain.on("go-vici-detail", async (event, Id) => {
viciId = Id;
await mainWindow.loadFile("src/vicissitudes-detail.html");
});