qrlogin-nocookie.html 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <!DOCTYPE html>
  2. <html lang="zh">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <title>二维码登录</title>
  7. </head>
  8. <body>
  9. <img id="qrImg" />
  10. <div id="info" class="info"></div>
  11. <script src="https://cdn.jsdelivr.net/npm/axios@0.26.1/dist/axios.min.js
  12. "></script>
  13. <script>
  14. async function login() {
  15. let timer
  16. let timestamp = Date.now()
  17. const cookie = localStorage.getItem('cookie')
  18. getLoginStatus(cookie)
  19. const res = await axios({
  20. url: `/login/qr/key?timestamp=${Date.now()}`,
  21. })
  22. const key = res.data.data.unikey
  23. const res2 = await axios({
  24. url: `/login/qr/create?key=${key}&qrimg=true&timestamp=${Date.now()}`,
  25. })
  26. document.querySelector('#qrImg').src = res2.data.data.qrimg
  27. timer = setInterval(async () => {
  28. const statusRes = await checkStatus(key)
  29. if (statusRes.code === 800) {
  30. alert('二维码已过期,请重新获取')
  31. clearInterval(timer)
  32. }
  33. if (statusRes.code === 803) {
  34. // 这一步会返回cookie
  35. clearInterval(timer)
  36. alert('授权登录成功')
  37. await getLoginStatus(statusRes.cookie)
  38. localStorage.setItem('cookie', statusRes.cookie)
  39. }
  40. }, 3000)
  41. }
  42. login()
  43. async function checkStatus(key) {
  44. const res = await axios({
  45. url: `/login/qr/check?key=${key}&timestamp=${Date.now()}&noCookie=true`,
  46. })
  47. return res.data
  48. }
  49. async function getLoginStatus(cookie = '') {
  50. const res = await axios({
  51. url: `/login/status?timestamp=${Date.now()}`,
  52. method: 'post',
  53. data: {
  54. cookie,
  55. },
  56. })
  57. document.querySelector('#info').innerText = JSON.stringify(res.data, null, 2)
  58. }
  59. </script>
  60. <style>
  61. .info {
  62. white-space: pre;
  63. }
  64. </style>
  65. </body>
  66. </html>