bug修复
Some checks failed
Node CI / build (14.x, macOS-latest) (push) Has been cancelled
Node CI / build (14.x, ubuntu-latest) (push) Has been cancelled
Node CI / build (14.x, windows-latest) (push) Has been cancelled
Node CI / build (16.x, macOS-latest) (push) Has been cancelled
Node CI / build (16.x, ubuntu-latest) (push) Has been cancelled
Node CI / build (16.x, windows-latest) (push) Has been cancelled
CodeQL / Analyze (javascript) (push) Has been cancelled
coverage CI / build (push) Has been cancelled
Node pnpm CI / build (16.x, macOS-latest) (push) Has been cancelled
Node pnpm CI / build (16.x, ubuntu-latest) (push) Has been cancelled
Node pnpm CI / build (16.x, windows-latest) (push) Has been cancelled
Some checks failed
Node CI / build (14.x, macOS-latest) (push) Has been cancelled
Node CI / build (14.x, ubuntu-latest) (push) Has been cancelled
Node CI / build (14.x, windows-latest) (push) Has been cancelled
Node CI / build (16.x, macOS-latest) (push) Has been cancelled
Node CI / build (16.x, ubuntu-latest) (push) Has been cancelled
Node CI / build (16.x, windows-latest) (push) Has been cancelled
CodeQL / Analyze (javascript) (push) Has been cancelled
coverage CI / build (push) Has been cancelled
Node pnpm CI / build (16.x, macOS-latest) (push) Has been cancelled
Node pnpm CI / build (16.x, ubuntu-latest) (push) Has been cancelled
Node pnpm CI / build (16.x, windows-latest) (push) Has been cancelled
This commit is contained in:
47
src/utils/thirdPartyLogin.ts
Normal file
47
src/utils/thirdPartyLogin.ts
Normal file
@@ -0,0 +1,47 @@
|
||||
import { setSessionToken } from '@/access';
|
||||
import { getTjmhToken, getWwTjmHlwToken } from '@/services/jobportal/auth';
|
||||
import { saveTokenFromUrl } from '@/utils/tokenCache';
|
||||
|
||||
const SESSION_DURATION_MS = 1000 * 12 * 60 * 60;
|
||||
|
||||
export function isThirdPartyTransitionPage(pathname: string): boolean {
|
||||
return (
|
||||
pathname === '/' ||
|
||||
pathname === '/login-tow' ||
|
||||
pathname === '/shihezi/' ||
|
||||
pathname === '/shihezi/login-tow'
|
||||
);
|
||||
}
|
||||
|
||||
/** 第三方 SSO 换票,须在 getRouters 之前完成 */
|
||||
export async function exchangeThirdPartyCredential(options: {
|
||||
code?: string | null;
|
||||
token?: string | null;
|
||||
}): Promise<boolean> {
|
||||
const { code, token } = options;
|
||||
|
||||
if (code) {
|
||||
const tokenResponse = await getTjmhToken(code);
|
||||
const sessionToken = tokenResponse?.data?.token;
|
||||
const lcToken = tokenResponse?.data?.lcToken;
|
||||
if (tokenResponse.code === 200 && sessionToken) {
|
||||
setSessionToken(sessionToken, sessionToken, Date.now() + SESSION_DURATION_MS, lcToken);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
if (token) {
|
||||
saveTokenFromUrl();
|
||||
const tokenResponse = await getWwTjmHlwToken(token);
|
||||
const sessionToken = tokenResponse?.data?.token;
|
||||
const lcToken = tokenResponse?.data?.lcToken;
|
||||
if (tokenResponse.code === 200 && sessionToken) {
|
||||
setSessionToken(sessionToken, sessionToken, Date.now() + SESSION_DURATION_MS, lcToken);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
69
src/utils/tokenCache.ts
Normal file
69
src/utils/tokenCache.ts
Normal file
@@ -0,0 +1,69 @@
|
||||
/**
|
||||
* Token缓存工具类
|
||||
* 用于管理互联网端登录的token缓存和URL拼接
|
||||
*/
|
||||
|
||||
// 缓存键名
|
||||
const TOKEN_CACHE_KEY = 'internet_token';
|
||||
|
||||
/**
|
||||
* 从URL参数中获取token并保存到缓存
|
||||
*/
|
||||
export function saveTokenFromUrl(): void {
|
||||
const urlParams = new URLSearchParams(window.location.search);
|
||||
const token = urlParams.get('token');
|
||||
|
||||
if (token) {
|
||||
localStorage.setItem(TOKEN_CACHE_KEY, token);
|
||||
console.log('Token已保存到缓存:', token);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 从缓存中获取token
|
||||
*/
|
||||
export function getTokenFromCache(): string | null {
|
||||
return localStorage.getItem(TOKEN_CACHE_KEY);
|
||||
}
|
||||
|
||||
/**
|
||||
* 清除缓存中的token
|
||||
*/
|
||||
export function clearTokenCache(): void {
|
||||
localStorage.removeItem(TOKEN_CACHE_KEY);
|
||||
}
|
||||
|
||||
/**
|
||||
* 为URL添加token参数
|
||||
* @param url 原始URL
|
||||
* @param token token值
|
||||
* @returns 添加了token参数的URL
|
||||
*/
|
||||
export function appendTokenToUrl(url: string, token: string | null): string {
|
||||
if (!token) {
|
||||
return url;
|
||||
}
|
||||
|
||||
// 检查URL是否已经有查询参数
|
||||
const hasQuery = url.includes('?');
|
||||
const separator = hasQuery ? '&' : '?';
|
||||
|
||||
return `${url}${separator}token=${encodeURIComponent(token)}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理导航跳转,自动添加token参数
|
||||
* @param url 目标URL
|
||||
*/
|
||||
export function navigateWithToken(url: string): void {
|
||||
const token = getTokenFromCache();
|
||||
const finalUrl = appendTokenToUrl(url, token);
|
||||
|
||||
console.log('跳转URL:', finalUrl);
|
||||
window.open(finalUrl, '_blank');
|
||||
// 对于需要确保新标签页被浏览器允许打开的情况,可以使用以下方式:
|
||||
// const newWindow = window.open();
|
||||
// if (newWindow) {
|
||||
// newWindow.location.assign(finalUrl);
|
||||
// }
|
||||
}
|
||||
Reference in New Issue
Block a user