-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
121 lines (103 loc) · 3.03 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
import { app, BrowserWindow, ipcMain, dialog } from 'electron'
import { join, dirname } from 'path'
import { fileURLToPath } from 'url'
import isDev from 'electron-is-dev'
import Store from 'electron-store'
import fetch from 'node-fetch'
import { setBaiduToken, setIsDev } from './server/dist/server.js'
const __filename = fileURLToPath(import.meta.url)
const __dirname = dirname(__filename)
const store = new Store()
let mainWindow
function createWindow() {
mainWindow = new BrowserWindow({
width: 1200,
height: 1000,
webPreferences: {
nodeIntegration: true,
contextIsolation: true,
preload: join(__dirname, 'preload.cjs'),
},
})
const startUrl = isDev
? 'http://localhost:3000'
: `file://${join(__dirname, 'client/build/index.html')}`
mainWindow.loadURL(startUrl)
mainWindow.on('closed', () => {
mainWindow = null
})
setIsDev(isDev)
if (isDev) {
mainWindow.webContents.openDevTools()
}
}
app.on('ready', () => {
createWindow()
})
const defaultNoteDir = join(app.getPath('home'), 'loginote')
ipcMain.on('get-note-dir', event => {
event.returnValue = store.get('noteDir', defaultNoteDir)
})
ipcMain.on('set-note-dir', (_, newDir) => {
store.set('noteDir', newDir)
})
ipcMain.on('get-baidu-keys', event => {
event.returnValue = store.get('baiduApiKeys', {
baiduApiKey: '',
baiduSecretKey: '',
})
})
ipcMain.on('set-baidu-keys', (_, value) => {
store.set('baiduApiKeys', value)
getAccessToken(value.baiduApiKey, value.baiduSecretKey)
})
ipcMain.handle('open-directory-dialog', async () => {
const result = await dialog.showOpenDialog(mainWindow, {
properties: ['openDirectory', 'createDirectory', 'promptToCreate'],
})
if (result.canceled) return ''
return result.filePaths[0]
})
ipcMain.handle('select-file-dialog', async () => {
const result = await dialog.showOpenDialog(mainWindow, {
properties: ['openFile'],
})
if (result.canceled) return ''
return result.filePaths[0]
})
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit()
}
})
app.on('activate', () => {
if (mainWindow === null) {
createWindow()
}
})
function getAccessToken(apiKey, secretKey) {
if (apiKey === '' || secretKey === '') return
fetch(
`https://aip.baidubce.com/oauth/2.0/token?client_id=${apiKey}&client_secret=${secretKey}&grant_type=client_credentials`,
{
method: 'POST',
headers: {
'Content-Type': 'application/json',
Accept: 'application/json',
},
}
)
.then(v => {
v.json()
.then(r => {
store.set('accessToken', r.access_token)
setBaiduToken(r.access_token)
})
.catch(e => {
console.error(e)
})
})
.catch(e => {
console.error(e)
})
}