update: 二维码登录例子更新

This commit is contained in:
binaryify 2022-04-06 16:55:12 +08:00
parent 22fbe0e3ed
commit 81a218a2b1

View File

@ -1,12 +1,13 @@
<!DOCTYPE html> <!DOCTYPE html>
<html lang="zh"> <html lang="zh">
<head>
<head>
<meta charset="UTF-8" /> <meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>二维码登录</title> <title>二维码登录</title>
</head> </head>
<body> <body>
<img id="qrImg" /> <img id="qrImg" />
<div id="info" class="info"></div> <div id="info" class="info"></div>
<script src="https://cdn.jsdelivr.net/npm/axios@0.26.1/dist/axios.min.js <script src="https://cdn.jsdelivr.net/npm/axios@0.26.1/dist/axios.min.js
@ -15,29 +16,30 @@
async function checkStatus(key) { async function checkStatus(key) {
const res = await axios({ const res = await axios({
url: `/login/qr/check?key=${key}&timerstamp=${Date.now()}`, url: `/login/qr/check?key=${key}&timerstamp=${Date.now()}`,
withCredentials: true, //关键
}) })
return res.data return res.data
} }
async function getLoginStatus() { async function getLoginStatus(cookie = '') {
const res = await axios({ const res = await axios({
url: `/login/status?timerstamp=${Date.now()}`, url: `/login/status?timerstamp=${Date.now()}`,
withCredentials: true, //关键 method: 'post',
data: {
cookie,
},
}) })
document.querySelector('#info').innerText = JSON.stringify(res.data, null, 2) document.querySelector('#info').innerText = JSON.stringify(res.data, null, 2)
} }
async function login() { async function login() {
let timer let timer
let timestamp = Date.now() let timestamp = Date.now()
this.getLoginStatus() const cookie = localStorage.getItem('cookie')
this.getLoginStatus(cookie)
const res = await axios({ const res = await axios({
url: `/login/qr/key?timerstamp=${Date.now()}`, url: `/login/qr/key?timerstamp=${Date.now()}`,
withCredentials: true, //关键
}) })
const key = res.data.data.unikey const key = res.data.data.unikey
const res2 = await axios({ const res2 = await axios({
url: `/login/qr/create?key=${key}&qrimg=true&timerstamp=${Date.now()}`, url: `/login/qr/create?key=${key}&qrimg=true&timerstamp=${Date.now()}`,
withCredentials: true, //关键
}) })
document.querySelector('#qrImg').src = res2.data.data.qrimg document.querySelector('#qrImg').src = res2.data.data.qrimg
@ -51,16 +53,18 @@
// 这一步会返回cookie // 这一步会返回cookie
clearInterval(timer) clearInterval(timer)
alert('授权登录成功') alert('授权登录成功')
await this.getLoginStatus() await this.getLoginStatus(statusRes.cookie)
localStorage.setItem('cookie', statusRes.cookie)
} }
}, 3000) }, 3000)
} }
login() login()
</script> </script>
<style> <style>
.info{ .info {
white-space: pre; white-space: pre;
} }
</style> </style>
</body> </body>
</html> </html>