feat(auth): 完善多账号与企业分层管理模型,全面支持 F12/cURL 临时凭证导入与一键保活
This commit is contained in:
1 parent
255c7aee8d
commit
3d549e2e2f
8 files changed
+749
-155
No files matched your search
@@ -0,0 +1,175 @@
|
||||
import { z } from "zod";
|
||||
import { CorpContext } from "../config/manager.js";
|
||||
import { loadCorpConfigs } from "../config/loader.js";
|
||||
import {
|
||||
importF12Credentials,
|
||||
parseHeadersOrCurl,
|
||||
refreshAccountCorporations,
|
||||
} from "./login.js";
|
||||
import { listAccounts, getActiveAccount } from "./account-store.js";
|
||||
import { createEkbClient } from "./ekb.js";
|
||||
|
||||
// ── Auth Tools 定义 ──────────────────────────────────────────────────────────
|
||||
|
||||
export const authTools = [
|
||||
{
|
||||
name: "list_corps",
|
||||
description: "列出当前所有已配置的企业(包括账号关联企业与手动导入企业)",
|
||||
parameters: z.object({}),
|
||||
handler: async ({ corpContext }: { corpContext: CorpContext }) => {
|
||||
return {
|
||||
activeCorp: corpContext.getCurrent()?.id,
|
||||
corporations: corpContext.listCorps(),
|
||||
};
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "switch_corp",
|
||||
description: "切换当前活跃企业",
|
||||
parameters: z.object({
|
||||
corpId: z.string().describe("目标企业 ID"),
|
||||
}),
|
||||
handler: async (
|
||||
{ corpContext }: { corpContext: CorpContext },
|
||||
params: { corpId: string }
|
||||
) => {
|
||||
const success = corpContext.switchCorp(params.corpId);
|
||||
if (!success) {
|
||||
throw new Error(`企业 ${params.corpId} 不存在或未配置`);
|
||||
}
|
||||
const current = corpContext.getCurrent();
|
||||
return {
|
||||
success: true,
|
||||
message: `已切换到企业: ${current?.name} (${current?.id})`,
|
||||
corp: current,
|
||||
};
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "whoami",
|
||||
description: "查看当前操作员身份、当前活跃企业与账号状态",
|
||||
parameters: z.object({}),
|
||||
handler: async ({ corpContext }: { corpContext: CorpContext }) => {
|
||||
const current = corpContext.getCurrent();
|
||||
if (!current) {
|
||||
return {
|
||||
authenticated: false,
|
||||
message: "未配置任何企业",
|
||||
activeAccount: getActiveAccount(),
|
||||
};
|
||||
}
|
||||
|
||||
// 测试当前 Session
|
||||
let sessionData: unknown = null;
|
||||
let sessionValid = false;
|
||||
try {
|
||||
const client = createEkbClient(current);
|
||||
const res = await client.get("api/account/v2/session").json<{ value?: unknown }>();
|
||||
sessionData = res.value;
|
||||
sessionValid = !!res.value;
|
||||
} catch (e) {
|
||||
sessionValid = false;
|
||||
}
|
||||
|
||||
return {
|
||||
authenticated: sessionValid,
|
||||
currentCorp: {
|
||||
id: current.id,
|
||||
name: current.name,
|
||||
staffId: current.credentials.staffId,
|
||||
source: current.source,
|
||||
accountId: current.accountId,
|
||||
baseUrl: current.baseUrl,
|
||||
},
|
||||
session: sessionData,
|
||||
};
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "import_f12_corp",
|
||||
description: "从网页 F12 抓包(Network / Headers / cURL)直接导入单企业登录凭证",
|
||||
parameters: z.object({
|
||||
accessToken: z.string().optional().describe("主站 accessToken"),
|
||||
corpId: z.string().optional().describe("企业 ID"),
|
||||
staffId: z.string().optional().describe("员工 ID"),
|
||||
name: z.string().optional().describe("企业名称(可选)"),
|
||||
rawHeadersOrCurl: z.string().optional().describe("F12 复制的原始 Headers 文本或 cURL 命令行字符串(自动解析)"),
|
||||
baseUrl: z.string().optional().describe("站点基础地址,默认 https://app.ekuaibao.com"),
|
||||
}),
|
||||
handler: async (
|
||||
{ corpContext }: { corpContext: CorpContext },
|
||||
params: {
|
||||
accessToken?: string;
|
||||
corpId?: string;
|
||||
staffId?: string;
|
||||
name?: string;
|
||||
rawHeadersOrCurl?: string;
|
||||
baseUrl?: string;
|
||||
}
|
||||
) => {
|
||||
let accessToken = params.accessToken;
|
||||
let corpId = params.corpId;
|
||||
let staffId = params.staffId;
|
||||
|
||||
if (params.rawHeadersOrCurl) {
|
||||
const parsed = parseHeadersOrCurl(params.rawHeadersOrCurl);
|
||||
if (parsed.accessToken) accessToken = parsed.accessToken;
|
||||
if (parsed.corpId) corpId = parsed.corpId;
|
||||
if (parsed.staffId) staffId = parsed.staffId;
|
||||
}
|
||||
|
||||
if (!accessToken || !corpId || !staffId) {
|
||||
throw new Error("导入失败:必须提供 accessToken, corpId, staffId 或有效的 rawHeadersOrCurl 抓包内容");
|
||||
}
|
||||
|
||||
const importedConfig = await importF12Credentials({
|
||||
accessToken,
|
||||
corpId,
|
||||
staffId,
|
||||
name: params.name,
|
||||
baseUrl: params.baseUrl,
|
||||
});
|
||||
|
||||
corpContext.upsertCorp(importedConfig, true);
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: `成功导入并保存企业凭证: ${importedConfig.name} (${importedConfig.id})`,
|
||||
corp: importedConfig,
|
||||
};
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "refresh_corps",
|
||||
description: "通过主账号登录态一键刷新名下所有企业的最新 AccessToken 并重新加载",
|
||||
parameters: z.object({
|
||||
accountId: z.string().optional().describe("指定刷新的账号手机号,为空时刷新当前活跃账号"),
|
||||
}),
|
||||
handler: async (
|
||||
{ corpContext }: { corpContext: CorpContext },
|
||||
params: { accountId?: string }
|
||||
) => {
|
||||
const result = await refreshAccountCorporations(params.accountId, { saveAll: true });
|
||||
const freshCorps = loadCorpConfigs();
|
||||
corpContext.reload(freshCorps);
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: `成功刷新账号 ${result.accountId} 下的所有企业凭证`,
|
||||
refreshedCount: result.allCorps.length,
|
||||
corporations: corpContext.listCorps(),
|
||||
};
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "list_accounts",
|
||||
description: "查看本地已保存的所有登录主账号",
|
||||
parameters: z.object({}),
|
||||
handler: async () => {
|
||||
return {
|
||||
activeAccount: getActiveAccount()?.accountId,
|
||||
accounts: listAccounts(),
|
||||
};
|
||||
},
|
||||
},
|
||||
];
|
||||
Reference in new issue
Block a user