-
Notifications
You must be signed in to change notification settings - Fork 71
/
index.ts
63 lines (56 loc) · 2 KB
/
index.ts
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
let qr: any = "";
let checkInterval: NodeJS.Timeout;
const host = "";
document.querySelector(".refresh")!.addEventListener("click", getQrCode);
document.querySelector("#sign")!.addEventListener("click", sign);
const userInfoDom = document.querySelector("#user-info")!;
const expireDom = document.querySelector("#expire")!;
const tipDom = document.querySelector("#tip")!;
const nickNameDom = document.querySelector("#nick-name")!;
const userImgDom = document.querySelector("#user-img")!;
const userDom = document.querySelector("#user")!;
export function getQrCode() {
tipDom.innerHTML = "请用阿里云盘 App 扫码";
expireDom.classList.remove("show");
userDom.classList.add("hidden");
fetch(host + "/api/generate?img=true")
.then((res) => res.json())
.then((res) => {
qr = res;
const qrcode = document.getElementById("qrcode")!;
qrcode.setAttribute("src", res.codeContent);
checkInterval = setInterval(checkQrCode, 2500);
});
}
export function checkQrCode() {
fetch(host + "/api/state-query?ck=" + qr.ck + "&t=" + qr.t)
.then((res) => res.json())
.then((res) => {
if (["EXPIRED", "CANCELED"].includes(res.data.qrCodeStatus)) {
clearInterval(checkInterval);
expireDom.classList.add("show");
} else if (["CONFIRMED"].includes(res.data.qrCodeStatus)) {
const {
nickName,
avatar,
refreshToken,
} = res.data.bizExt.pds_login_result;
(userInfoDom as HTMLElement).innerText = refreshToken;
nickNameDom.innerHTML = nickName;
userImgDom.setAttribute("src", avatar);
userDom.classList.remove("hidden");
expireDom.classList.add("show");
clearInterval(checkInterval);
}
tipDom.innerHTML = res.data.tip;
});
}
export function sign() {
const refreshToken = (userInfoDom as HTMLElement).innerText!;
fetch(host + "/api/sign?refreshToken=" + refreshToken)
.then((res) => res.text())
.then((res) => {
alert(res);
});
}
setTimeout(getQrCode, 200);