|
14 | 14 | from maxkb.const import CONFIG |
15 | 15 |
|
16 | 16 | content = """ |
17 | | -<!doctype html> |
| 17 | +<!DOCTYPE html> |
18 | 18 | <html lang="en"> |
19 | 19 | <head> |
20 | 20 | <meta charset="UTF-8" /> |
21 | 21 | <meta http-equiv="X-UA-Compatible" content="IE=edge" /> |
22 | 22 | <meta name="viewport" content="width=device-width, initial-scale=1.0" /> |
23 | 23 | <title>Document</title> |
24 | | - <script> |
25 | | - function setCookie(name, value, days) { |
26 | | - var expires = ""; |
27 | | - if (days) { |
28 | | - var date = new Date(); |
29 | | - date.setTime(date.getTime() + (days*2)); |
30 | | - expires = "; expires=" + date.toUTCString(); |
| 24 | + </head> |
| 25 | + <style> |
| 26 | + /* 弹框内容样式 */ |
| 27 | + .modal-content { |
| 28 | + background-color: #fefefe; |
| 29 | + margin: 15% auto; /* 15% 从顶部和自动水平居中 */ |
| 30 | + padding: 20px; |
| 31 | + border: 1px solid #888; |
| 32 | + width: 80%; /* 宽度 */ |
31 | 33 | } |
32 | | - document.cookie = name + "=" + (value || "") + expires + "; path=/"; |
33 | | -} |
34 | | - window.onload = () => { |
35 | | - var xhr = new XMLHttpRequest() |
36 | | - xhr.open('GET', '/api/user/profile', true) |
| 34 | + </style> |
| 35 | + <body> |
| 36 | + <div class="modal-content"> |
| 37 | + <input type="text" id="auth-input" /> |
| 38 | + <button id="auth">认证</button> |
| 39 | + <button id="goLogin">去登录</button> |
| 40 | + </div> |
| 41 | + <script> |
| 42 | + const setCookie = (name, value, days) => { |
| 43 | + var expires = ""; |
| 44 | + if (days) { |
| 45 | + var date = new Date(); |
| 46 | + date.setTime(date.getTime() + days * 2); |
| 47 | + expires = "; expires=" + date.toUTCString(); |
| 48 | + } |
| 49 | + document.cookie = name + "=" + (value || "") + expires + "; path=/"; |
| 50 | + }; |
| 51 | + const authToken = (token) => { |
| 52 | + return new Promise((resolve, reject) => { |
| 53 | + try { |
| 54 | + var xhr = new XMLHttpRequest(); |
| 55 | + xhr.open("GET", "/api/user/profile", true); |
| 56 | + xhr.setRequestHeader("Content-Type", "application/json"); |
| 57 | + const pathname = window.location.pathname; |
| 58 | + if (token) { |
| 59 | + xhr.setRequestHeader("Authorization", "Bearer " + token); |
| 60 | + xhr.onreadystatechange = function () { |
| 61 | + if (xhr.readyState === 4) { |
| 62 | + if (xhr.status === 200) { |
| 63 | + resolve(true); |
| 64 | + } else { |
| 65 | + reject(true); |
| 66 | + } |
| 67 | + } |
| 68 | + }; |
37 | 69 |
|
38 | | - xhr.setRequestHeader('Content-Type', 'application/json') |
39 | | - const token = localStorage.getItem('token') |
40 | | - const pathname = window.location.pathname |
41 | | - if (token) { |
42 | | - xhr.setRequestHeader('Authorization', 'Bearer '+token) |
43 | | - xhr.onreadystatechange = function () { |
44 | | - if (xhr.readyState === 4) { |
45 | | - if (xhr.status === 200) { |
46 | | - setCookie("Authorization",'Bearer '+token) |
47 | | - window.location.href = pathname |
48 | | - } |
49 | | - if (xhr.status === 401) { |
50 | | - window.location.href = '/admin/login' |
51 | | - } |
| 70 | + xhr.send(); |
52 | 71 | } |
| 72 | + } catch (e) { |
| 73 | + reject(false); |
53 | 74 | } |
| 75 | + }); |
| 76 | + }; |
| 77 | + window.onload = () => { |
| 78 | + const token = localStorage.getItem("token"); |
| 79 | + authToken(token) |
| 80 | + .then(() => { |
| 81 | + setCookie("Authorization", "Bearer " + token); |
| 82 | + window.location.href = window.location.pathname; |
| 83 | + }) |
| 84 | + .catch((e) => {}); |
| 85 | + }; |
| 86 | + // 获取元素 |
| 87 | + const auth = document.getElementById("auth"); |
| 88 | + const goLogin = document.getElementById("goLogin"); |
54 | 89 |
|
55 | | - xhr.send() |
56 | | - } else { |
57 | | - window.location.href = '/admin/login' |
58 | | - } |
59 | | - } |
| 90 | + // 打开弹框函数 |
| 91 | + auth.onclick = ()=> { |
| 92 | + const authInput = document.getElementById("auth-input"); |
| 93 | + const token = authInput.value |
| 94 | + authToken(token) |
| 95 | + .then(() => { |
| 96 | + setCookie("Authorization", "Bearer " + token); |
| 97 | + window.location.href = window.location.pathname; |
| 98 | + }) |
| 99 | + .catch((e) => { |
| 100 | + alert("令牌错误"); |
| 101 | + }); |
| 102 | + }; |
| 103 | +
|
| 104 | + // 去系统的登录页面 |
| 105 | + goLogin.onclick = ()=> { |
| 106 | + window.location.href = "/admin/login"; |
| 107 | + }; |
60 | 108 | </script> |
61 | | - </head> |
62 | | - <body></body> |
| 109 | + </body> |
63 | 110 | </html> |
64 | 111 |
|
65 | 112 | """.replace("/api/user/profile", CONFIG.get_admin_path() + '/api/user/profile').replace('/admin/login', |
|
0 commit comments