-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cjs
200 lines (187 loc) · 5.71 KB
/
main.cjs
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
const { app, Menu, ipcMain, dialog, BrowserWindow } = require('electron');
const isDev = require('electron-is-dev');
const menuTemplate = require('./src/menuTemplate.cjs');
const AppWindow = require('./src/AppWindow.cjs');
const Store = require('electron-store');
const QiniuManager = require('./src/utils/QiniuManager.cjs');
const path = require('path');
const url = require('url');
const settingsStore = new Store({ name: 'settings' });
let mainWindow, newWindow, splashWindow;
const createManager = () => {
const accessKey = settingsStore.get('accessKey');
const secretKey = settingsStore.get('secretKey');
const bucket = settingsStore.get('bucket');
return new QiniuManager(accessKey, secretKey, bucket);
};
// function showLoadingWindow() {
// splashWindow = new BrowserWindow({
// width: 1200,
// height: 800,
// frame: false,
// transparent: true,
// alwaysOnTop: true
// });
// splashWindow.loadURL(url.format({
// pathname: path.join(__dirname, './build/splash.html'),
// protocol: 'file:',
// slashes: true
// }));
// }
// 加载loading页面窗口
const showSplashWindow = () => {
return new Promise((resolve) => {
// loadingWindow = new BrowserWindow({
// width: 1200,
// height: 800,
// frame: false, // 无边框(窗口、工具栏等),只包含网页内容
// transparent: true, // 窗口是否支持透明,如果想做高级效果最好为true
// alwaysOnTop: true
// });
// loadingWindow.loadURL(url.format({
// pathname: path.join(__dirname, './build/splash.html'),
// protocol: 'file:',
// slashes: true
// }));
// loadingWindow.show()
const splasUrlLocation = isDev
? `http://localhost:3000/#/splash`
: url.format({
pathname: path.join(__dirname, '/build/index.html'),
protocol: 'file:',
slashes: true,
hash: 'splash',
});
splashWindow = new AppWindow(
{
width: 1200,
height: 800,
frame: false, // 无边框(窗口、工具栏等),只包含网页内容
// transparent: true, // 窗口是否支持透明,如果想做高级效果最好为true
// alwaysOnTop: false
},
splasUrlLocation
);
resolve();
});
};
const showMainWindow = () => {
return new Promise((resolve)=>{
const mainWindowConfig = {
width: 1200,
height: 800,
};
const mainUrlLocation = isDev
? 'http://localhost:3000/'
: url.format({
pathname: path.join(__dirname, './build/index.html'),
protocol: 'file:',
slashes: true,
});
mainWindow = new AppWindow(
mainWindowConfig,
mainUrlLocation,
()=>{
splashWindow.hide();
splashWindow.close();
},
5000
);
mainWindow.on('close', () => {
mainWindow = null;
});
resolve()
})
}
app.on('ready', async () => {
await showSplashWindow();
await showMainWindow();
// main event
ipcMain.on('open-new-window', (_, urlPath) => {
console.log('setting', urlPath);
if (newWindow) {
newWindow.close();
}
const newWindowConfig = {
width: 800,
height: 600,
parent: mainWindow,
};
const settingsUrlLocation = isDev
? `http://localhost:3000/#/${urlPath}`
: url.format({
pathname: path.join(__dirname, '/build/index.html'),
protocol: 'file:',
slashes: true,
hash: 'setting',
});
newWindow = new AppWindow(newWindowConfig, settingsUrlLocation);
if (urlPath === 'setting') {
newWindow.removeMenu();
}
newWindow.on('close', () => {
newWindow = null;
});
});
const menu = Menu.buildFromTemplate(menuTemplate);
Menu.setApplicationMenu(menu);
ipcMain.on('menu-config-uploaded', () => {
let qiniuMenu =
process.platform === 'darwin' ? menu.items[3] : menu.items[2];
const switchItems = (toggle) => {
[1, 2, 3].forEach((num) => {
qiniuMenu.submenu.items[num].enabled = toggle;
});
};
const qiniuIsConfiged = ['accessKey', 'secretKey', 'bucket'].every(
(key) => !!settingsStore.get(key)
);
switchItems(qiniuIsConfiged);
});
ipcMain.on('upload-file', (_, data) => {
const manager = createManager();
console.log('upload=====', data.key);
manager
.uploadFile(data.key, data.path)
.then((data) => {
console.log('上传成功.....');
mainWindow.webContents.send('active-file-uploaded');
})
.catch((err) => {
dialog.showErrorBox('同步失败', '请检查七牛云参数是否正确');
});
});
ipcMain.on('download-file', (_, data) => {
const manager = createManager();
console.log('download-file==ipcMain', data);
manager.getStat(data.key).then(
(resp) => {
console.log('download-file-resp', resp);
const qiniuUpdateTime = Math.floor(resp.putTime / 10000);
if (!data.updatedAt || data.updatedAt < qiniuUpdateTime) {
manager.downloadFile(data.key, data.path).then(() => {
mainWindow.webContents.send('file-downloaded', {
status: 'downloaded-success',
id: data.id,
});
});
} else {
mainWindow.webContents.send('file-downloaded', {
status: 'no-new-file',
id: data.id,
});
}
// todo 本地更新时间和七牛putTime比较
},
(err) => {
if (err.statusCode === 612) {
mainWindow.webContents.send('file-downloaded', {
status: 'no-file',
id: data.id,
});
}
console.log('download-file-error', err);
}
);
});
});