126 lines
3.4 KiB
JavaScript
126 lines
3.4 KiB
JavaScript
const LOGIN_TOKEN_KEY = 'LOGIN_STATUS_TOKEN';
|
|
|
|
function normalizeToken(token) {
|
|
return String(token || '').trim().replace(/^Bearer\s+/i, '');
|
|
}
|
|
|
|
function safeDecode(value) {
|
|
try {
|
|
return decodeURIComponent(String(value || '').replace(/\+/g, '%20'));
|
|
} catch (error) {
|
|
return String(value || '');
|
|
}
|
|
}
|
|
|
|
function pickTokenFromQuery(query) {
|
|
const tokenNames = ['token', 'remote_token', 'access_token', 'Authori-zation', 'Authorization'];
|
|
const queryString = String(query || '').replace(/^[?#&]/, '');
|
|
if (!queryString) return '';
|
|
|
|
const pairs = queryString.split('&');
|
|
for (let index = 0; index < pairs.length; index += 1) {
|
|
const pair = pairs[index];
|
|
const equalIndex = pair.indexOf('=');
|
|
const key = safeDecode(equalIndex >= 0 ? pair.slice(0, equalIndex) : pair);
|
|
const value = safeDecode(equalIndex >= 0 ? pair.slice(equalIndex + 1) : '');
|
|
if (tokenNames.indexOf(key) !== -1 && value) {
|
|
return normalizeToken(value);
|
|
}
|
|
}
|
|
|
|
return '';
|
|
}
|
|
|
|
export function extractTokenFromUrl(url) {
|
|
const fullUrl = String(url || '').trim();
|
|
if (!fullUrl) return '';
|
|
|
|
const questionIndex = fullUrl.indexOf('?');
|
|
if (questionIndex !== -1) {
|
|
const hashIndex = fullUrl.indexOf('#', questionIndex);
|
|
const query = fullUrl.slice(questionIndex + 1, hashIndex === -1 ? fullUrl.length : hashIndex);
|
|
const token = pickTokenFromQuery(query);
|
|
if (token) return token;
|
|
}
|
|
|
|
const hashIndex = fullUrl.indexOf('#');
|
|
if (hashIndex !== -1) {
|
|
const hash = fullUrl.slice(hashIndex + 1);
|
|
const hashQuestionIndex = hash.indexOf('?');
|
|
const hashQuery = hashQuestionIndex === -1 ? hash : hash.slice(hashQuestionIndex + 1);
|
|
const token = pickTokenFromQuery(hashQuery);
|
|
if (token) return token;
|
|
}
|
|
|
|
return pickTokenFromQuery(fullUrl);
|
|
}
|
|
|
|
export function saveTokenToStorage(token) {
|
|
const nextToken = normalizeToken(token);
|
|
if (!nextToken) return '';
|
|
|
|
uni.setStorageSync(LOGIN_TOKEN_KEY, nextToken);
|
|
return nextToken;
|
|
}
|
|
|
|
export function readTokenFromStorage() {
|
|
return normalizeToken(uni.getStorageSync(LOGIN_TOKEN_KEY));
|
|
}
|
|
|
|
export function getCurrentWebviewUrl() {
|
|
// #ifdef H5
|
|
if (typeof window !== 'undefined' && window.location) {
|
|
return window.location.href;
|
|
}
|
|
// #endif
|
|
|
|
// #ifdef APP-PLUS
|
|
if (typeof plus !== 'undefined' && plus.webview && plus.webview.currentWebview) {
|
|
const webview = plus.webview.currentWebview();
|
|
if (webview && typeof webview.getURL === 'function') {
|
|
return webview.getURL();
|
|
}
|
|
}
|
|
// #endif
|
|
|
|
return '';
|
|
}
|
|
|
|
export function getH5ReferrerUrl() {
|
|
// #ifdef H5
|
|
if (typeof document !== 'undefined') {
|
|
return document.referrer || '';
|
|
}
|
|
// #endif
|
|
|
|
return '';
|
|
}
|
|
|
|
export function refreshCurrentWebviewToken(url) {
|
|
const candidateUrls = [];
|
|
const inputUrl = String(url || '').trim();
|
|
const currentUrl = String(getCurrentWebviewUrl() || '').trim();
|
|
const referrerUrl = getH5ReferrerUrl();
|
|
|
|
if (inputUrl) {
|
|
candidateUrls.push(inputUrl);
|
|
}
|
|
|
|
if (currentUrl && candidateUrls.indexOf(currentUrl) === -1) {
|
|
candidateUrls.push(currentUrl);
|
|
}
|
|
|
|
if (referrerUrl && candidateUrls.indexOf(referrerUrl) === -1) {
|
|
candidateUrls.push(referrerUrl);
|
|
}
|
|
|
|
for (let index = 0; index < candidateUrls.length; index += 1) {
|
|
const nextToken = extractTokenFromUrl(candidateUrls[index]);
|
|
if (nextToken) {
|
|
return saveTokenToStorage(nextToken);
|
|
}
|
|
}
|
|
|
|
return readTokenFromStorage();
|
|
}
|