-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.js
57 lines (52 loc) · 1.35 KB
/
user.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
'use strict'
let user = {
host: 'http://electsys.sjtu.edu.cn',
url: new Proxy({
login: '/edu/login.aspx',
logout: '/edu/logout.aspx',
sdtleft: '/edu/student/sdtleft.aspx',
testLogin: '/edu/changePwd.aspx',
}, {
get (target, key) {
return user.host + target[key]
}
}),
isLogin () {
return fetch(this.url.testLogin, {credentials: 'include'})
.then(response => !response.url.endsWith('outTimePage.aspx'))
},
login () {
return user.login.open().then(user.login.wait)
},
logout () {
return fetch(this.url.logout, {credentials: 'include'})
},
}
user.login.open = function openLogin () {
return fetch(user.url.login, {credentials: 'include'}).then(response => {
let responseURL = response.url
if (responseURL.includes('sdtMain')) {
return
}
if (!responseURL.includes('login')) {
throw new TypeError
}
return window.open(responseURL, 'login', 'width=436, height=527')
})
}
user.login.wait = async function waitLogin (loginWindow) {
if (loginWindow === undefined) {
window.dispatchEvent(new Event('login'))
return true
}
while (true) {
await new Promise(resolve => setTimeout(resolve, 1000))
if (loginWindow.closed) {
if (await user.isLogin()) {
window.dispatchEvent(new Event('login'))
return true
}
return false
}
}
}