feat(auth): 实现全自动无感续杯 (401/即将过期自动重试换票) 与企业多身份唯一标识分层

This commit is contained in:
gitea committed 2026-08-27 11:04:41 +08:00
1 parent 189415cdac
commit b7a3bf2c03
7 files changed
+122 -17

No files matched your search

+48
View File
@@ -391,6 +391,9 @@ export async function switchCorporationAndExchangeToken(
const corpConfig: CorpConfig = {
id: corp.corporationId,
name: corp.corporationName,
corpId: corp.corporationId,
userId: corp.userId,
staffId: corp.staffId,
baseUrl: corp.baseUrl || "https://app.ekuaibao.com",
source: "account",
accountId,
@@ -414,6 +417,51 @@ export async function switchCorporationAndExchangeToken(
};
}
/**
* 针对单个企业凭证进行静默无感自动续杯
*/
export async function refreshSingleCorpAccessToken(
corp: CorpConfig,
businessCenterUrl: string = DEFAULT_BUSINESS_CENTER_URL,
idpUrl: string = DEFAULT_IDP_URL
): Promise<boolean> {
const account = corp.accountId ? getAccount(corp.accountId) : getActiveAccount();
if (!account?.cloudAuthToken) return false;
try {
const corpItem: CorporationItem = {
corporationId: corp.corpId || corp.credentials.corpId,
corporationName: corp.name,
cluster: "app",
clientId: "whispered-app",
staffId: corp.staffId || corp.credentials.staffId,
userId: corp.userId || account.userId || "",
baseUrl: corp.baseUrl || "https://app.ekuaibao.com",
external: false,
needAuth: false,
};
const res = await switchCorporationAndExchangeToken(
account.cloudAuthToken,
corpItem,
corp.accountId,
idpUrl,
businessCenterUrl
);
corp.credentials.accessToken = res.accessToken;
if (!corp.auth) corp.auth = { refreshTtl: 300 };
corp.auth.refreshToken = res.refreshToken;
corp.auth.expiresAt = Date.now() + res.expiresIn * 1000;
saveCorpConfig(corp);
return true;
} catch (err) {
console.warn(`[auth] 自动续杯刷新企业 ${corp.name} 失败:`, err instanceof Error ? err.message : String(err));
return false;
}
}
// ── 全流程一站式高阶函数 ─────────────────────────────────────────────────────
export interface FullLoginOptions {