fix: 修复旧版前端登录跳转
This commit is contained in:
+43
-10
@@ -5,6 +5,38 @@
|
||||
|
||||
// ==================== 工具函数 ====================
|
||||
|
||||
const LEGACY_TOKEN_KEY = 'token';
|
||||
const APP_TOKEN_KEY = 'capacity_report_token';
|
||||
|
||||
function getLegacyLoginPath() {
|
||||
const path = window.location.pathname;
|
||||
return path === '/old' || path.startsWith('/old/') ? '/old/login.html' : '/login.html';
|
||||
}
|
||||
|
||||
function getAuthToken() {
|
||||
return localStorage.getItem(APP_TOKEN_KEY) || localStorage.getItem(LEGACY_TOKEN_KEY) || '';
|
||||
}
|
||||
|
||||
function clearAuthToken() {
|
||||
localStorage.removeItem(APP_TOKEN_KEY);
|
||||
localStorage.removeItem(LEGACY_TOKEN_KEY);
|
||||
document.cookie = 'token=; path=/; max-age=0';
|
||||
}
|
||||
|
||||
function redirectToLogin() {
|
||||
const loginPath = getLegacyLoginPath();
|
||||
if (window.location.pathname !== loginPath) {
|
||||
window.location.replace(loginPath);
|
||||
}
|
||||
}
|
||||
|
||||
function requireAuthToken() {
|
||||
if (getAuthToken()) return true;
|
||||
clearAuthToken();
|
||||
redirectToLogin();
|
||||
return false;
|
||||
}
|
||||
|
||||
function $(selector) {
|
||||
return document.querySelector(selector);
|
||||
}
|
||||
@@ -114,7 +146,7 @@ async function api(endpoint, options = {}) {
|
||||
// 如果没有指定 method 且没有 body,默认使用 GET
|
||||
const method = options.method || (options.body ? 'POST' : 'GET');
|
||||
|
||||
const token = localStorage.getItem('token');
|
||||
const token = getAuthToken();
|
||||
|
||||
const fetchOptions = {
|
||||
method: method,
|
||||
@@ -134,7 +166,7 @@ async function api(endpoint, options = {}) {
|
||||
const response = await fetch(`/api${endpoint}`, fetchOptions);
|
||||
|
||||
if (response.status === 401 && endpoint !== '/login') {
|
||||
localStorage.removeItem('token');
|
||||
clearAuthToken();
|
||||
showLoginModal();
|
||||
throw new Error('未授权或登录已过期,请重新登录');
|
||||
}
|
||||
@@ -148,15 +180,13 @@ async function api(endpoint, options = {}) {
|
||||
}
|
||||
|
||||
function showLoginModal() {
|
||||
localStorage.removeItem('token');
|
||||
document.cookie = 'token=; path=/; max-age=0';
|
||||
window.location.href = '/';
|
||||
clearAuthToken();
|
||||
redirectToLogin();
|
||||
}
|
||||
|
||||
function logout() {
|
||||
localStorage.removeItem('token');
|
||||
document.cookie = 'token=; path=/; max-age=0';
|
||||
window.location.href = '/';
|
||||
clearAuthToken();
|
||||
window.location.href = getLegacyLoginPath();
|
||||
}
|
||||
|
||||
|
||||
@@ -651,7 +681,7 @@ class FileUploader {
|
||||
resolve({ success: true });
|
||||
}
|
||||
} else if (xhr.status === 401) {
|
||||
localStorage.removeItem('token');
|
||||
clearAuthToken();
|
||||
showLoginModal();
|
||||
reject(new Error('未授权或登录已过期,请重新登录'));
|
||||
} else {
|
||||
@@ -666,7 +696,7 @@ class FileUploader {
|
||||
|
||||
// 开始上传
|
||||
xhr.open('POST', '/api/upload');
|
||||
const token = localStorage.getItem('token');
|
||||
const token = getAuthToken();
|
||||
if (token) {
|
||||
xhr.setRequestHeader('Authorization', `Bearer ${token}`);
|
||||
}
|
||||
@@ -2208,6 +2238,9 @@ async function updateCacheSize() {
|
||||
}
|
||||
|
||||
document.addEventListener('DOMContentLoaded', () => {
|
||||
if (!requireAuthToken()) {
|
||||
return;
|
||||
}
|
||||
initApp();
|
||||
});
|
||||
|
||||
|
||||
+17
-4
@@ -161,6 +161,21 @@
|
||||
<div class="toast" id="toast"></div>
|
||||
|
||||
<script>
|
||||
const LEGACY_TOKEN_KEY = 'token';
|
||||
const APP_TOKEN_KEY = 'capacity_report_token';
|
||||
|
||||
function getLegacyHomePath() {
|
||||
const path = window.location.pathname;
|
||||
return path === '/old/login.html' || path.startsWith('/old/') ? '/old' : '/';
|
||||
}
|
||||
|
||||
function setAuthToken(token) {
|
||||
if (!token) return;
|
||||
localStorage.setItem(APP_TOKEN_KEY, token);
|
||||
localStorage.setItem(LEGACY_TOKEN_KEY, token);
|
||||
document.cookie = `token=${encodeURIComponent(token)}; path=/; max-age=${86400 * 30}; SameSite=Lax`;
|
||||
}
|
||||
|
||||
function showToast(msg, type) {
|
||||
const t = document.getElementById('toast');
|
||||
t.textContent = msg;
|
||||
@@ -197,11 +212,9 @@
|
||||
|
||||
const data = await res.json();
|
||||
if (data.success && data.token) {
|
||||
localStorage.setItem('token', data.token);
|
||||
// 设置 cookie 供后端 / 路由判断
|
||||
document.cookie = `token=${data.token}; path=/; max-age=${86400 * 30}; SameSite=Lax`;
|
||||
setAuthToken(data.token);
|
||||
showToast('登录成功', 'success');
|
||||
setTimeout(() => { window.location.href = '/'; }, 300);
|
||||
setTimeout(() => { window.location.href = getLegacyHomePath(); }, 300);
|
||||
}
|
||||
} catch (err) {
|
||||
showToast(err.message, 'error');
|
||||
|
||||
Reference in New Issue
Block a user