修复管理员错误的问题

This commit is contained in:
CN-JS-HuiBai
2026-04-07 17:46:24 +08:00
parent 6467f17b30
commit 5b775622e1

View File

@@ -331,6 +331,10 @@
const ADMIN_HOME_URL = @json($adminHomeUrl); const ADMIN_HOME_URL = @json($adminHomeUrl);
const REFRESH_MS = 30000; const REFRESH_MS = 30000;
const TOKEN_KEYS = [ const TOKEN_KEYS = [
'auth_data',
'AUTH_DATA',
'admin_auth_data',
'ADMIN_AUTH_DATA',
'VUE_NAIVE_ACCESS_TOKEN', 'VUE_NAIVE_ACCESS_TOKEN',
'Vue_Naive_access_token', 'Vue_Naive_access_token',
'access_token', 'access_token',
@@ -361,6 +365,19 @@
return 'Bearer ' + raw; return 'Bearer ' + raw;
} }
function addCandidateToken(bucket, candidate, priority) {
const normalized = normalizeToken(candidate);
if (!normalized) return;
const existing = bucket.get(normalized);
if (!existing || priority > existing.priority) {
bucket.set(normalized, {
token: normalized,
priority: priority
});
}
}
function extractTokenFromParsedValue(input) { function extractTokenFromParsedValue(input) {
if (!input) return ''; if (!input) return '';
@@ -398,6 +415,42 @@
return ''; return '';
} }
function collectTokensFromUnknown(input, bucket, priority) {
if (!input) return;
if (typeof input === 'string') {
addCandidateToken(bucket, input, priority);
return;
}
if (Array.isArray(input)) {
input.forEach(function (item) {
collectTokensFromUnknown(item, bucket, priority - 1);
});
return;
}
if (!isObject(input)) {
return;
}
if (input.is_admin === true || input.isAdmin === true) {
addCandidateToken(bucket, input.auth_data || input.authData || input.authorization || input.token, priority + 20);
}
const directKeys = ['auth_data', 'authData', 'authorization', 'token', 'access_token', 'accessToken', 'value', 'data'];
directKeys.forEach(function (key) {
if (Object.prototype.hasOwnProperty.call(input, key)) {
collectTokensFromUnknown(input[key], bucket, priority - (key === 'data' ? 2 : 0));
}
});
Object.keys(input).forEach(function (key) {
if (directKeys.indexOf(key) !== -1) return;
collectTokensFromUnknown(input[key], bucket, priority - 3);
});
}
function parseStoredToken(rawValue) { function parseStoredToken(rawValue) {
if (!rawValue || typeof rawValue !== 'string') return ''; if (!rawValue || typeof rawValue !== 'string') return '';
@@ -416,21 +469,82 @@
return ''; return '';
} }
function readTokenFromStorage(storage) { function collectStorageCandidates(storage, bucket) {
if (!storage) return ''; if (!storage) return;
for (const key of TOKEN_KEYS) { for (const key of TOKEN_KEYS) {
const token = parseStoredToken(storage.getItem(key)); addCandidateToken(bucket, storage.getItem(key), 100);
if (token) return token; }
for (let index = 0; index < storage.length; index += 1) {
const key = storage.key(index);
if (!key) continue;
const rawValue = storage.getItem(key);
if (!rawValue) continue;
const normalizedKey = key.toLowerCase();
const priority = normalizedKey.indexOf('admin') !== -1
? 90
: normalizedKey.indexOf('auth') !== -1
? 80
: normalizedKey.indexOf('token') !== -1
? 70
: 20;
addCandidateToken(bucket, rawValue, priority);
const trimmed = rawValue.trim();
if (trimmed[0] === '{' || trimmed[0] === '[') {
try {
collectTokensFromUnknown(JSON.parse(trimmed), bucket, priority);
} catch (error) {
}
}
}
}
function buildAuthorizationCandidates() {
const bucket = new Map();
collectStorageCandidates(window.localStorage, bucket);
collectStorageCandidates(window.sessionStorage, bucket);
return Array.from(bucket.values())
.sort(function (left, right) {
return right.priority - left.priority;
})
.map(function (item) {
return item.token;
});
}
async function pickAuthorization() {
const candidates = buildAuthorizationCandidates();
for (const authorization of candidates) {
try {
const url = new URL(API_ENDPOINT, window.location.origin);
url.searchParams.set('current', '1');
url.searchParams.set('pageSize', '1');
const response = await fetch(url.toString(), {
method: 'GET',
headers: {
'Accept': 'application/json',
'Authorization': authorization
},
credentials: 'same-origin'
});
if (response.ok) {
return authorization;
}
} catch (error) {
}
} }
return ''; return '';
} }
function getAuthorization() {
return readTokenFromStorage(window.localStorage) || readTokenFromStorage(window.sessionStorage);
}
function formatTime(timestamp) { function formatTime(timestamp) {
if (!timestamp) return '-'; if (!timestamp) return '-';
@@ -543,10 +657,10 @@
} }
async function loadData() { async function loadData() {
const authorization = getAuthorization(); const authorization = await pickAuthorization();
if (!authorization) { if (!authorization) {
resetTable(); resetTable();
setError('No usable admin login token was found in Xboard browser storage. Please log in to the admin panel first, then refresh this page.'); setError('No usable admin login token was found in browser storage. Please log in to the Xboard admin panel first, then refresh this page.');
return; return;
} }