forked from itxve/aliyundriver-refresh-token
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
55 lines (48 loc) · 1.76 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
let qr: any = "";
let checkInterval: NodeJS.Timer;
const host = "";
document.querySelector(".refresh")!.addEventListener("click", getQrCode);
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)) {
console.log(res, "CONFIRMED");
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;
});
}
setTimeout(getQrCode, 200);