-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
146 lines (127 loc) · 3.92 KB
/
index.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
const { app, BrowserWindow, ipcMain, dialog } = require('electron')
const path = require('path')
const header = require('./header')
const createWindowMain = () => {
const mainWindow = new BrowserWindow({
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
},
icon: header.getSingleResource('window.icon'),
})
ipcMain.on('log', (event, msg) => {
console.log(msg)
})
ipcMain.handle('get-resource', async function (event, key) {
return header.getSingleResource(key)
})
ipcMain.handle('get-messages', async function (event) {
// console.log(header.getMessages())
return header.getMessages()
})
ipcMain.handle('send-message', async function (event, args) {
return header.sendMessage(args[0], args[1])
})
ipcMain.handle('get-user-info', async function (event, args) {
return header.getUserInfo(args)
})
ipcMain.handle('get-chat-info', async function (event, cid) {
return header.getChatInfo(cid)
})
ipcMain.on('msg-box-rst', () => {
header.resetMessageBox()
})
ipcMain.on('show-img', (e, s) => {
let { shell } = require('electron')
shell.openExternal(s)
})
mainWindow.loadFile('index.html')
}
const createWindowLogin = () => {
const loginWindow = new BrowserWindow({
webPreferences: {
preload: path.join(__dirname, 'preload_login.js'),
},
icon: header.getSingleResource('window.icon'),
})
ipcMain.on('log', (event, msg) => {
console.log(msg)
})
ipcMain.handle('get-server', async (e) => {
return header.getConfig()['server']
})
const login = async function (e, args) {
u = args[0]
p = args[1]
s = args[2]
let config = header.getConfig()
if (config['server'] != s) {
config['server'] = s
header.setConfig(config)
}
let axios = require('axios')
let data = new FormData()
let sha256 = require('crypto-js/sha256')
data.append('user_id', u)
data.append('password', sha256(p))
let rsp = undefined
await axios
.post(s + '/login.php', data)
.then((response) => {
rsp = response['data']
console.log(rsp)
if (rsp['success']) {
header.setToken(u, rsp['token'])
createWindowMain()
loginWindow.destroy()
}
})
.catch((err) => {
console.log(err)
})
return rsp
}
ipcMain.handle('login', login)
ipcMain.handle('sign-up', async (ev, args) => {
u = args[0]
p = args[1]
e = args[2]
s = args[3]
let config = header.getConfig()
if (config['server'] != s) {
config['server'] = s
header.setConfig(config)
}
let axios = require('axios')
let data = new FormData()
let sha256 = require('crypto-js/sha256')
data.append('username', u)
data.append('password', sha256(p))
data.append('email', e)
let rsp = undefined
await axios
.post(s + '/signup.php', data)
.then((response) => {
rsp = response['data']
console.log(rsp)
if (rsp['status'] == 'ok') {
login(undefined, [rsp['id'], p, s])
}
})
.catch((err) => {
console.log(err)
})
return rsp
})
loginWindow.loadFile(path.join(__dirname, 'login.html'))
}
app.whenReady().then(() => {
createWindowLogin()
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindowMain()
}
})
})
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') app.quit()
})