Skip to content

Commit

Permalink
🎉 增加阿里云二维码生成和查询接口
Browse files Browse the repository at this point in the history
  • Loading branch information
itxve committed Apr 17, 2022
1 parent 0fd760f commit fb6a454
Show file tree
Hide file tree
Showing 13 changed files with 313 additions and 19 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
.history
node_modules
node_modules
.vercel
dist
3 changes: 3 additions & 0 deletions .vercelignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.history
node_modules
.vercel
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2022 itxve

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
106 changes: 106 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<script type="module" src="./index.ts"></script>
<script>
function copyIt() {
const u = document.querySelector("#user-Info")
u.select()
document.execCommand("copy")
alert("已复制")
}
</script>

<body>
<div id="app" class="main">
<img id="qrcode" class="qrcode-img" />
<div id="expire">
<p>二维码已失效</p>
<button class="refresh">刷新二维码</button>
</div>
</div>
<div class="main">
<textarea rows="5" cols="33" id="user-Info"></textarea>
<button class="copy" onclick="copyIt()">copy</button>
</div>
</body>

<style>
.main {
display: flex;
position: relative;
flex-direction: column;
justify-content: center;
align-items: center;
margin-top: 60px;
}

.qrcode-img {
box-shadow: 0px 0px 1px 1px rgb(28 28 32 / 5%), 0px 8px 24px rgb(28 28 32 / 12%);
border-radius: 10px;
width: 128px;
height: 128px;
display: flex;
justify-content: center;
align-items: center;
}

button.refresh {
cursor: pointer;
font-size: 12px;
color: #f0f0f0;
background: #637dff;
line-height: 24px;
height: 24px;
width: auto;
padding: 0px 10px;
border-radius: 30px;
border: 0;
}

#expire {
background: hsla(0, 0%, 100%, .9);
display: none;
flex-direction: column;
justify-content: center;
align-items: center;
position: absolute;
height: 100%;
top: 0;
height: 100%;
width: 100%;
}

#expire p {
font-size: 14px;
}

#token {
z-index: 100;
}

.show {
display: flex !important;
}

button.copy {
cursor: pointer;
font-size: 12px;
color: #f0f0f0;
background: #2f91dc;
line-height: 24px;
height: 24px;
width: auto;
padding: 0px 10px;
border-radius: 30px;
border: 0;
}
</style>

</html>
35 changes: 35 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
let qr: any = "";
let checkInterval: NodeJS.Timer;

document.querySelector(".refresh").addEventListener("click", getQrCode);
const userInfoDom = document.querySelector("#user-Info");
const expireDom = document.querySelector("#expire");

export function getQrCode() {
fetch("/api/qrcode-generate?img=true")
.then((res) => res.json())
.then((res) => {
qr = res;
const qrcode = document.getElementById("qrcode");
qrcode.setAttribute("src", res.codeContent);
checkInterval = setInterval(checkQrCode, 1000);
});
}

export function checkQrCode() {
fetch("/api/qrcode-state-query?ck=" + qr.ck + "&t=" + qr.t)
.then((res) => res.json())
.then((res) => {
if (["EXPIRED", "CANCELED"].includes(res.data.qrCodeStatus)) {
if (checkInterval) {
clearInterval(checkInterval);
}
expireDom.classList.add("show");
} else if (["CONFIRMED"].includes(res.data.qrCodeStatus)) {
console.log(res, "CONFIRMED");
userInfoDom.innerHTML = res.data.bizExt;
}
});
}

setTimeout(getQrCode, 1000);
14 changes: 11 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,19 @@
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"scripts": {},
"scripts": {
"dev": "vercel dev",
"serve": "vite",
"build": "tsc --noEmit && vite build",
"preview": "vite preview"
},
"devDependencies": {
"@types/qrcode": "^1.4.2",
"typescript": "^4.6.3",
"vite": "^2.9.5"
},
"dependencies": {
"@vercel/node": "^1.14.1"
"@vercel/node": "^1.14.1",
"qrcode": "^1.5.0"
}
}
}
9 changes: 9 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
## QR Code扫码获取阿里云盘refresh token

## Vercel 部署
<a href="https://vercel.com/new/import?s=https://github.com/itxve/aliyundriver-refresh-token"><img src="https://vercel.com/button" height="24"></a>


## License

[The MIT License (MIT)](https://github.com/itxve/aliyundriver-refresh-token/blob/master/LICENSE)
6 changes: 0 additions & 6 deletions src/hello.ts

This file was deleted.

50 changes: 50 additions & 0 deletions src/qrcode-generate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import type { VercelRequest, VercelResponse } from "@vercel/node";
import QRCode from "qrcode";
import https from "https";

export default function (req: VercelRequest, res: VercelResponse) {
res.setHeader("Access-Control-Allow-Origin", "*");
const { img } = req.query;
https
.get(
{
host: "passport.aliyundrive.com",
path:
"/newlogin/qrcode/generate.do?" +
"appName=aliyun_drive" +
"&fromSite=52" +
"&appName=aliyun_drive" +
"&appEntrance=web" +
"&isMobile=false" +
"&lang=zh_CN" +
"&returnUrl=" +
"&bizParams=" +
"&_bx-v=2.0.31",
timeout: 3000,
},
(response) => {
let data: any = "";
response.on("data", (d) => {
data += d;
});
response.on("end", async () => {
const json = JSON.parse(data);
const result = {
codeContent: json.content.data.codeContent,
ck: json.content.data.ck,
t: json.content.data.t,
};
if (img) {
const image = await QRCode.toDataURL(result.codeContent);
result.codeContent = image;
res.send(result);
} else {
res.send(result);
}
});
}
)
.on("error", (e) => {
res.status(500).send(e);
});
}
66 changes: 66 additions & 0 deletions src/qrcode-state-query.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import type { VercelRequest, VercelResponse } from "@vercel/node";
import https from "https";

export default function (req: VercelRequest, res: VercelResponse) {
res.setHeader("Access-Control-Allow-Origin", "*");
const { ck, t } = req.query;
const params: any = {
t,
ck,
appName: "aliyun_drive",
appEntrance: "web",
isMobile: "false",
lang: "zh_CN",
returnUrl: "",
fromSite: "52",
bizParams: "",
navlanguage: "zh-CN",
navPlatform: "MacIntel",
};

let body = "";
Object.keys(params).forEach((key) => {
body += "&" + key + "=" + params[key];
});

const status: any = {
NEW: "请用阿里云盘 App 扫码",
SCANED: "请在手机上确认",
EXPIRED: "二维码已过期",
CANCELED: "已取消",
CONFIRMED: "已确认",
};
https
.request(
{
method: "POST",
host: "passport.aliyundrive.com",
path:
"/newlogin/qrcode/query.do?appName=aliyun_drive&fromSite=52&_bx-v=2.0.31",
headers: {
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
},
timeout: 3000,
},
(response) => {
let data: any = "";
response.on("data", (d) => {
data += d;
});
response.on("end", () => {
const rt = JSON.parse(data).content;
if (rt.data.qrCodeStatus === "CONFIRMED") {
rt.data.bizExt = JSON.parse(atob(rt.data.bizExt));
} else {
//添加 一个tip
rt.data.tip = status[rt.data.qrCodeStatus];
}
res.send(rt);
});
}
)
.on("error", (e) => {
res.status(500).send(e);
})
.end(body);
}
8 changes: 1 addition & 7 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,8 @@
"resolveJsonModule": true,
"esModuleInterop": true,
"lib": ["esnext", "dom"],
"typeRoots": ["./src/types", "./node_modules/@types"],
"types": ["element-plus/global", "vite/client"],
"baseUrl": ".",
"paths": {
"@/*": ["src/*"]
},
"skipLibCheck": true
},
"include": ["src/**/*.ts", "src/**/*.d.ts", "src/**/*.tsx", "src/**/*.vue"],
"references": [{ "path": "./tsconfig.node.json" }]
"include": ["src/*.ts"]
}
4 changes: 2 additions & 2 deletions vercel.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
"version": 2,
"builds": [
{
"src": "./src/*.ts",
"src": "src/*.ts",
"use": "@vercel/node"
}
],
"routes": [
{
"src": "/(.*)",
"dest": "/"
"dest": "/src/$1.ts"
}
]
}
6 changes: 6 additions & 0 deletions vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ import { defineConfig } from "vite";
export default defineConfig({
server: {
port: 8000,
proxy: {
"/api": {
target: "http://localhost:3000/",
rewrite: (path) => path.replace(/^\/api/, ""),
},
},
},
build: {
minify: true,
Expand Down

0 comments on commit fb6a454

Please sign in to comment.