feat(auth): 完善多账号与企业分层管理模型,全面支持 F12/cURL 临时凭证导入与一键保活
This commit is contained in:
1 parent
255c7aee8d
commit
3d549e2e2f
8 files changed
+749
-155
No files matched your search
@@ -1,15 +1,30 @@
|
||||
import { CorpConfig } from "./types";
|
||||
import { CorpConfig } from "./types.js";
|
||||
|
||||
export interface CorpSummary {
|
||||
id: string;
|
||||
name: string;
|
||||
source?: string;
|
||||
accountId?: string;
|
||||
staffId: string;
|
||||
baseUrl?: string;
|
||||
current: boolean;
|
||||
}
|
||||
|
||||
export class CorpContext {
|
||||
private current: CorpConfig | null = null;
|
||||
private corps: Map<string, CorpConfig> = new Map();
|
||||
|
||||
constructor(corps: CorpConfig[]) {
|
||||
constructor(corps: CorpConfig[] = []) {
|
||||
this.reload(corps);
|
||||
}
|
||||
|
||||
reload(corps: CorpConfig[]): void {
|
||||
this.corps.clear();
|
||||
for (const corp of corps) {
|
||||
this.corps.set(corp.id, corp);
|
||||
}
|
||||
if (corps.length > 0) {
|
||||
this.current = corps[0];
|
||||
if (this.corps.size > 0 && (!this.current || !this.corps.has(this.current.id))) {
|
||||
this.current = Array.from(this.corps.values())[0];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,6 +32,17 @@ export class CorpContext {
|
||||
return this.current;
|
||||
}
|
||||
|
||||
getCorp(corpId: string): CorpConfig | undefined {
|
||||
return this.corps.get(corpId);
|
||||
}
|
||||
|
||||
upsertCorp(corp: CorpConfig, setCurrent: boolean = false): void {
|
||||
this.corps.set(corp.id, corp);
|
||||
if (setCurrent || !this.current) {
|
||||
this.current = corp;
|
||||
}
|
||||
}
|
||||
|
||||
switchCorp(corpId: string): boolean {
|
||||
const corp = this.corps.get(corpId);
|
||||
if (!corp) return false;
|
||||
@@ -24,10 +50,14 @@ export class CorpContext {
|
||||
return true;
|
||||
}
|
||||
|
||||
listCorps(): Array<{ id: string; name: string; current: boolean }> {
|
||||
listCorps(): CorpSummary[] {
|
||||
return Array.from(this.corps.values()).map((corp) => ({
|
||||
id: corp.id,
|
||||
name: corp.name,
|
||||
source: corp.source || "manual",
|
||||
accountId: corp.accountId,
|
||||
staffId: corp.credentials.staffId,
|
||||
baseUrl: corp.baseUrl,
|
||||
current: corp.id === this.current?.id,
|
||||
}));
|
||||
}
|
||||
|
||||
@@ -1,19 +1,26 @@
|
||||
import { z } from "zod";
|
||||
|
||||
export const CredentialSourceSchema = z.enum(["account", "manual", "browser", "f12"]);
|
||||
export type CredentialSource = z.infer<typeof CredentialSourceSchema>;
|
||||
|
||||
export const CorpCredentialsSchema = z.object({
|
||||
accessToken: z.string(),
|
||||
corpId: z.string(),
|
||||
staffId: z.string(),
|
||||
accessToken: z.string().describe("主站访问令牌"),
|
||||
corpId: z.string().describe("企业 ID"),
|
||||
staffId: z.string().describe("员工 ID"),
|
||||
});
|
||||
|
||||
export const CorpAuthConfigSchema = z.object({
|
||||
refreshTtl: z.number().default(300), // 提前多少秒刷新,默认 5 分钟
|
||||
refreshTtl: z.number().default(300).describe("提前多少秒刷新,默认 5 分钟"),
|
||||
refreshToken: z.string().optional().describe("OAuth2 Refresh Token"),
|
||||
expiresAt: z.number().optional().describe("AccessToken 到期时间戳 (ms)"),
|
||||
});
|
||||
|
||||
export const CorpConfigSchema = z.object({
|
||||
id: z.string(),
|
||||
name: z.string(),
|
||||
baseUrl: z.string().url().optional(), // 默认 https://app.ekuaibao.com
|
||||
id: z.string().describe("企业 ID"),
|
||||
name: z.string().describe("企业名称"),
|
||||
baseUrl: z.string().url().optional().default("https://app.ekuaibao.com"),
|
||||
source: CredentialSourceSchema.optional().default("manual").describe("凭证来源: account/manual/browser/f12"),
|
||||
accountId: z.string().optional().describe("所属主账号标识(如手机号)"),
|
||||
credentials: CorpCredentialsSchema,
|
||||
auth: CorpAuthConfigSchema.optional(),
|
||||
});
|
||||
|
||||
Reference in new issue
Block a user