feat(platform,datalink,bill): add workflow job drilldown, safe record batch-edit, audit, and spec linkage diagnostics (v0.8.11)
This commit is contained in:
1 parent
2ec08033e0
commit
a0ec7f95a7
37 files changed
+3958
-674
No files matched your search
+79
-7
@@ -15,6 +15,7 @@ import {
|
||||
setActiveAccount,
|
||||
AccountEntry,
|
||||
} from "./account-store.js";
|
||||
import { clearCorpTokens } from "./token-store.js";
|
||||
import {
|
||||
CorporationItem,
|
||||
CorporationItemSchema,
|
||||
@@ -1124,6 +1125,59 @@ export function resolveLinkExpiresAt(
|
||||
return undefined;
|
||||
}
|
||||
|
||||
export function parseExpiresInSeconds(
|
||||
value?: string | number,
|
||||
fallbackSeconds = 7200
|
||||
): number {
|
||||
if (value === undefined || value === null || String(value).trim() === "") {
|
||||
return fallbackSeconds;
|
||||
}
|
||||
if (typeof value === "number") {
|
||||
if (!Number.isFinite(value) || value <= 0) {
|
||||
throw new AuthenticationError(`有效期秒数必须是大于 0 的数字: ${value}`);
|
||||
}
|
||||
return Math.round(value);
|
||||
}
|
||||
const raw = String(value).trim().toLowerCase();
|
||||
const match = raw.match(
|
||||
/^(\d+(?:\.\d+)?)\s*(s|sec|secs|second|seconds|m|min|mins|minute|minutes|h|hr|hrs|hour|hours|d|day|days)?$/
|
||||
);
|
||||
if (!match) {
|
||||
throw new AuthenticationError(
|
||||
`无效的有效期参数: ${value}(支持秒数如 86400,或带单位如 30m、24h、3d)`
|
||||
);
|
||||
}
|
||||
const amount = Number(match[1]);
|
||||
if (!Number.isFinite(amount) || amount <= 0) {
|
||||
throw new AuthenticationError(`有效期必须是大于 0 的数字: ${value}`);
|
||||
}
|
||||
const unit = match[2] || "s";
|
||||
if (["s", "sec", "secs", "second", "seconds"].includes(unit)) {
|
||||
return Math.round(amount);
|
||||
}
|
||||
if (["m", "min", "mins", "minute", "minutes"].includes(unit)) {
|
||||
return Math.round(amount * 60);
|
||||
}
|
||||
if (["h", "hr", "hrs", "hour", "hours"].includes(unit)) {
|
||||
return Math.round(amount * 3600);
|
||||
}
|
||||
return Math.round(amount * 86400);
|
||||
}
|
||||
|
||||
export function invalidateCorpSubdomainTokenCaches(corp: CorpConfig): void {
|
||||
try {
|
||||
clearCorpTokens(corp.id);
|
||||
if (corp.corpId && corp.corpId !== corp.id) {
|
||||
clearCorpTokens(corp.corpId);
|
||||
}
|
||||
if (corp.credentials?.corpId && corp.credentials.corpId !== corp.id) {
|
||||
clearCorpTokens(corp.credentials.corpId);
|
||||
}
|
||||
} catch {
|
||||
// Ignore cache cleanup errors in restricted environments
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析从 F12 抓包复制出来的 Request Headers、cURL 命令或 URL 字符串
|
||||
*/
|
||||
@@ -1675,7 +1729,8 @@ export async function elevateCorpWithOpenApiKey(
|
||||
export async function getCorpProvisionalAuthLink(
|
||||
corp: CorpConfig,
|
||||
options: {
|
||||
expiresIn?: number;
|
||||
expiresIn?: number | string;
|
||||
ttl?: number | string;
|
||||
pageType?: string;
|
||||
force?: boolean;
|
||||
syncLocalSession?: boolean;
|
||||
@@ -1693,7 +1748,8 @@ export async function getCorpProvisionalAuthLink(
|
||||
persistent: boolean;
|
||||
method: "provisional-openapi" | "session-token";
|
||||
}> {
|
||||
const expiresIn = options.expiresIn && options.expiresIn > 0 ? options.expiresIn : 7200;
|
||||
const explicitDuration = options.expiresIn ?? options.ttl;
|
||||
const expiresIn = parseExpiresInSeconds(explicitDuration, 7200);
|
||||
const pageType = options.pageType || "home";
|
||||
const corpId = corp.corpId || corp.credentials.corpId;
|
||||
const staffId = corp.staffId || corp.credentials.staffId;
|
||||
@@ -1737,6 +1793,7 @@ export async function getCorpProvisionalAuthLink(
|
||||
corp.session.expTime = provisional.expiresAt;
|
||||
}
|
||||
saveCorpConfig(corp, options.enterprisesDir);
|
||||
invalidateCorpSubdomainTokenCaches(corp);
|
||||
}
|
||||
|
||||
return {
|
||||
@@ -1760,13 +1817,25 @@ export async function getCorpProvisionalAuthLink(
|
||||
);
|
||||
}
|
||||
|
||||
const expMs = corp.auth?.expiresAt || corp.session?.expTime || Date.now() + expiresIn * 1000;
|
||||
const expMs =
|
||||
explicitDuration !== undefined
|
||||
? Date.now() + expiresIn * 1000
|
||||
: corp.auth?.expiresAt || corp.session?.expTime || Date.now() + expiresIn * 1000;
|
||||
if (expMs <= Date.now() && !canRefreshCorpAccessToken(corp)) {
|
||||
throw new AuthenticationError(
|
||||
`企业 [${corp.name}] 临时凭证已过期且未配置持久化密钥,请使用 hose-use corps add-link 重新导入。`
|
||||
);
|
||||
}
|
||||
|
||||
if (explicitDuration !== undefined && options.syncLocalSession !== false) {
|
||||
if (!corp.auth) corp.auth = { refreshTtl: 300 };
|
||||
corp.auth.expiresAt = expMs;
|
||||
if (corp.session) {
|
||||
corp.session.expTime = expMs;
|
||||
}
|
||||
saveCorpConfig(corp, options.enterprisesDir);
|
||||
}
|
||||
|
||||
const url = `${baseUrl}/web/thirdparty.html?accessToken=${encodeURIComponent(
|
||||
corp.credentials.accessToken
|
||||
)}&ekbCorpId=${encodeURIComponent(corpId)}&pageType=${encodeURIComponent(
|
||||
@@ -1780,7 +1849,7 @@ export async function getCorpProvisionalAuthLink(
|
||||
staffName: corp.staffName || corp.session?.staffName || undefined,
|
||||
url,
|
||||
token: corp.credentials.accessToken,
|
||||
expiresIn: Math.max(1, Math.floor((expMs - Date.now()) / 1000)),
|
||||
expiresIn: Math.max(1, Math.round((expMs - Date.now()) / 1000)),
|
||||
expiresAt: formatBeijingIso(expMs),
|
||||
persistent: canRefreshCorpAccessToken(corp),
|
||||
method: "session-token",
|
||||
@@ -1917,7 +1986,8 @@ export async function switchCorporationAndExchangeToken(
|
||||
export async function refreshSingleCorpAccessToken(
|
||||
corp: CorpConfig,
|
||||
businessCenterUrl: string = DEFAULT_BUSINESS_CENTER_URL,
|
||||
idpUrl: string = DEFAULT_IDP_URL
|
||||
idpUrl: string = DEFAULT_IDP_URL,
|
||||
enterprisesDir?: string
|
||||
): Promise<boolean> {
|
||||
if (!canRefreshCorpAccessToken(corp)) return false;
|
||||
|
||||
@@ -1951,7 +2021,8 @@ export async function refreshSingleCorpAccessToken(
|
||||
corp.auth.refreshToken = res.refreshToken;
|
||||
corp.auth.expiresAt = Date.now() + res.expiresIn * 1000;
|
||||
|
||||
saveCorpConfig(corp);
|
||||
saveCorpConfig(corp, enterprisesDir);
|
||||
invalidateCorpSubdomainTokenCaches(corp);
|
||||
return true;
|
||||
} catch (err) {
|
||||
if (!hasOpenApiPersistentCredentials(corp)) {
|
||||
@@ -1989,7 +2060,8 @@ export async function refreshSingleCorpAccessToken(
|
||||
corp.session.expTime = provisional.expiresAt;
|
||||
}
|
||||
|
||||
saveCorpConfig(corp);
|
||||
saveCorpConfig(corp, enterprisesDir);
|
||||
invalidateCorpSubdomainTokenCaches(corp);
|
||||
return true;
|
||||
} catch (err) {
|
||||
console.warn(
|
||||
|
||||
@@ -27,3 +27,11 @@ export class BusinessError extends EkbError {
|
||||
this.name = "BusinessError";
|
||||
}
|
||||
}
|
||||
|
||||
export class EkbApiError extends EkbError {
|
||||
constructor(message: string, code = "API_ERROR", status = 500) {
|
||||
super(message, code, status);
|
||||
this.name = "EkbApiError";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,6 +7,10 @@ import {
|
||||
fetchHoseToken,
|
||||
EKB_BASE_URL,
|
||||
} from "./unity-client.js";
|
||||
import {
|
||||
canRefreshCorpAccessToken,
|
||||
refreshSingleCorpAccessToken,
|
||||
} from "../auth/login.js";
|
||||
|
||||
export const SCRIPT_ENGINE_BASE_URL = "https://piper.ekuaibao.com";
|
||||
|
||||
@@ -16,9 +20,22 @@ export const PIPER_BASE_URL = SCRIPT_ENGINE_BASE_URL;
|
||||
// 内存缓存
|
||||
const scriptEngineMemoryCache = new Map<
|
||||
string,
|
||||
{ jwt: string; hoseToken: string; expiresAt: number }
|
||||
{ jwt: string; hoseToken: string; expiresAt: number; sourceAccessToken?: string }
|
||||
>();
|
||||
|
||||
export function clearScriptEngineClientCache(corpId?: string): void {
|
||||
if (!corpId) {
|
||||
scriptEngineMemoryCache.clear();
|
||||
return;
|
||||
}
|
||||
const prefix = `${corpId}:`;
|
||||
for (const key of scriptEngineMemoryCache.keys()) {
|
||||
if (key.startsWith(prefix)) {
|
||||
scriptEngineMemoryCache.delete(key);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function isExpired(entry: { expiresAt: number }, refreshTtl: number): boolean {
|
||||
return Date.now() >= entry.expiresAt - refreshTtl * 1000;
|
||||
}
|
||||
@@ -59,7 +76,7 @@ export async function getScriptEngineClient(
|
||||
|
||||
// 检查内存缓存
|
||||
const mem = scriptEngineMemoryCache.get(cacheKey);
|
||||
if (mem && !isExpired(mem, refreshTtl)) {
|
||||
if (mem && mem.sourceAccessToken === corp.credentials.accessToken && !isExpired(mem, refreshTtl)) {
|
||||
return createAuthClient({
|
||||
prefixUrl: SCRIPT_ENGINE_BASE_URL,
|
||||
getHeaders: () => ({
|
||||
@@ -72,6 +89,13 @@ export async function getScriptEngineClient(
|
||||
});
|
||||
}
|
||||
|
||||
if (canRefreshCorpAccessToken(corp)) {
|
||||
const expiresAt = corp.auth?.expiresAt || corp.session?.expTime;
|
||||
if (!corp.credentials.accessToken || (expiresAt && Date.now() >= expiresAt - refreshTtl * 1000)) {
|
||||
await refreshSingleCorpAccessToken(corp);
|
||||
}
|
||||
}
|
||||
|
||||
const corpId = corp.corpId || corp.credentials.corpId;
|
||||
const staffId = corp.staffId || corp.credentials.staffId;
|
||||
const environment = resolveCorpCluster(corp);
|
||||
@@ -94,6 +118,7 @@ export async function getScriptEngineClient(
|
||||
jwt: easoJwt,
|
||||
hoseToken,
|
||||
expiresAt: Date.now() + 2 * 60 * 60 * 1000, // 2小时
|
||||
sourceAccessToken: corp.credentials.accessToken,
|
||||
};
|
||||
|
||||
scriptEngineMemoryCache.set(cacheKey, entry);
|
||||
|
||||
@@ -6,6 +6,7 @@ import {
|
||||
canRefreshCorpAccessToken,
|
||||
getCorpProvisionalAuthLink,
|
||||
hasOpenApiPersistentCredentials,
|
||||
parseExpiresInSeconds,
|
||||
resolveLinkExpiresAt,
|
||||
selectOpenApiKey,
|
||||
} from "../auth/login.js";
|
||||
@@ -161,4 +162,11 @@ describe("core/auth/login link expiry", () => {
|
||||
expect(resolveLinkExpiresAt({ expires: "3" }, base)).toBe(base + 3 * 24 * 60 * 60 * 1000);
|
||||
expect(resolveLinkExpiresAt({}, base)).toBeUndefined();
|
||||
});
|
||||
|
||||
it("parses expires-in / ttl strings to seconds for provisional auth link", () => {
|
||||
expect(parseExpiresInSeconds("24h")).toBe(86400);
|
||||
expect(parseExpiresInSeconds("7d")).toBe(604800);
|
||||
expect(parseExpiresInSeconds("86400")).toBe(86400);
|
||||
expect(parseExpiresInSeconds(3600)).toBe(3600);
|
||||
});
|
||||
});
|
||||
@@ -33,14 +33,24 @@ export function resolveCorpCluster(corp: { cluster?: string; baseUrl?: string })
|
||||
// 内存缓存
|
||||
const easoMemoryCache = new Map<
|
||||
string,
|
||||
{ token: string; hoseToken: string; expiresAt: number }
|
||||
{ token: string; hoseToken: string; expiresAt: number; sourceAccessToken?: string }
|
||||
>();
|
||||
|
||||
const unityMemoryCache = new Map<
|
||||
string,
|
||||
{ token: string; expiresAt: number }
|
||||
{ token: string; expiresAt: number; sourceAccessToken?: string }
|
||||
>();
|
||||
|
||||
export function clearUnityClientCache(corpId?: string): void {
|
||||
if (!corpId) {
|
||||
easoMemoryCache.clear();
|
||||
unityMemoryCache.clear();
|
||||
return;
|
||||
}
|
||||
easoMemoryCache.delete(`${corpId}:easo`);
|
||||
unityMemoryCache.delete(`${corpId}:unity`);
|
||||
}
|
||||
|
||||
function isExpired(entry: { expiresAt: number }, refreshTtl: number): boolean {
|
||||
return Date.now() >= entry.expiresAt - refreshTtl * 1000;
|
||||
}
|
||||
@@ -120,7 +130,7 @@ export async function getEasoClient(corp: CorpConfig): Promise<{
|
||||
|
||||
// 检查内存缓存
|
||||
const mem = easoMemoryCache.get(cacheKey);
|
||||
if (mem && !isExpired(mem, refreshTtl)) {
|
||||
if (mem && mem.sourceAccessToken === corp.credentials.accessToken && !isExpired(mem, refreshTtl)) {
|
||||
return {
|
||||
client: createAuthClient({
|
||||
prefixUrl: EASO_BASE_URL,
|
||||
@@ -171,6 +181,7 @@ export async function getEasoClient(corp: CorpConfig): Promise<{
|
||||
token: easoToken,
|
||||
hoseToken,
|
||||
expiresAt: Date.now() + 2 * 60 * 60 * 1000, // 2小时
|
||||
sourceAccessToken: corp.credentials.accessToken,
|
||||
};
|
||||
|
||||
easoMemoryCache.set(cacheKey, entry);
|
||||
@@ -203,7 +214,7 @@ export async function getUnityClient(corp: CorpConfig): Promise<{
|
||||
const cacheKey = `${corp.id}:unity`;
|
||||
|
||||
const mem = unityMemoryCache.get(cacheKey);
|
||||
if (mem && !isExpired(mem, refreshTtl)) {
|
||||
if (mem && mem.sourceAccessToken === corp.credentials.accessToken && !isExpired(mem, refreshTtl)) {
|
||||
return {
|
||||
unityJwt: mem.token,
|
||||
client: createAuthClient({
|
||||
@@ -239,6 +250,7 @@ export async function getUnityClient(corp: CorpConfig): Promise<{
|
||||
const entry = {
|
||||
token: unityJwt,
|
||||
expiresAt: Date.now() + 2 * 60 * 60 * 1000,
|
||||
sourceAccessToken: corp.credentials.accessToken,
|
||||
};
|
||||
unityMemoryCache.set(cacheKey, entry);
|
||||
|
||||
|
||||
Reference in new issue
Block a user