Files
hose-use/src/cli/commands/corps/whoami.ts
T
gitea 42bf0bfb0b refactor(core/http): 7大通信底座客户端语义化全量重构与合思开放文档客户端纳管
- 彻底移除旧内部狭隘命名与垫片包袱,一步到位全仓整体重命名:
  * session-client.ts ➔ hose-web-session-client.ts (合思 Web 主站会话客户端)
  * script-engine-client.ts ➔ piper-workflow-client.ts (Piper 工作流引擎客户端)
  * unity-client.ts ➔ unity-easo-client.ts (Unity / EASO 运营中台与动态集群客户端)
  * sso-client.ts ➔ sso-auth-client.ts (SSO 与扫码鉴权客户端)
- 新增 hose-docs-client.ts 纳为第 7 大通信底座客户端,收口 docs.ekuaibao.com 文档站点通信
- 端正 Piper 工作流引擎定位,完整覆盖工作流创建、保存、发布、108 类节点编排与 Job 流转
- 彻底整体替换全仓 import 引用与测试套件,新增 clients-semantics.test.ts 自动化校验
- 全量自动化测试 150 files 1155 tests 全部通过,类型检查零错误
2026-11-16 17:15:29 +08:00

118 lines
4.3 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { loadCorpConfigs } from "../../../core/config/loader.js";
import { createSessionClient } from "../../../core/http/hose-web-session-client.js";
import { booleanOption, stringOption } from "../../parser.js";
import { NotFoundError } from "../../../core/errors.js";
import { formatSourceLabel, printJson, printTable } from "../../printer.js";
import type { CliParsedArgs, CliResult, CliRuntime } from "../../types.js";
/**
* `hose-use whoami` — 按企业逐行显示当前执行身份。
*
* 名称以在线 `staffs/me` 为准(配置里只存工号即可);
* `--offline` 时退回本地缓存的 staffName(没有则显示 -)。
*/
async function resolveStaffName(config: any): Promise<string | undefined> {
try {
const client = await Promise.race([
createSessionClient(config),
new Promise<never>((_, reject) => setTimeout(() => reject(new Error("timeout")), 8000)),
]);
const corpId = config.corpId || config.id;
const response = await (client as any).get("api/v1/organization/staffs/me", {
searchParams: { corpId },
timeout: 8000,
});
const json = (await response.json()) as Record<string, any>;
const staff = json.value?.staff ?? json;
const name = typeof staff?.name === "string" ? staff.name.trim() : "";
return name || undefined;
} catch {
return undefined;
}
}
async function mapLimit<T, R>(items: T[], limit: number, fn: (item: T) => Promise<R>): Promise<R[]> {
const results: R[] = new Array(items.length);
let cursor = 0;
const workers = Array.from({ length: Math.min(limit, items.length) }, async () => {
while (cursor < items.length) {
const index = cursor++;
results[index] = await fn(items[index]);
}
});
await Promise.all(workers);
return results;
}
export async function handleWhoami(parsed: CliParsedArgs, runtime: CliRuntime): Promise<CliResult> {
const offline = booleanOption(parsed.options, ["offline", "cached"]);
const allCorps = runtime.corpContext.listCorps();
const explicitCorp = stringOption(parsed.options, ["corp"]);
if (explicitCorp && !runtime.selectedCorp) throw new NotFoundError(`未找到企业: ${explicitCorp}`);
const corps = explicitCorp ? allCorps.filter((corp) => corp.id === runtime.selectedCorp!.id) : allCorps;
const configs = new Map<string, any>();
for (const config of loadCorpConfigs()) {
configs.set(config.id, config);
}
const resolved: Array<{ name?: string; online: boolean }> = offline
? corps.map((corp: any) => ({ name: corp.staffName || undefined, online: false }))
: await mapLimit(corps, 5, async (corp: any) => {
const config = configs.get(corp.id);
if (!config) return { name: corp.staffName || undefined, online: false };
const name = await resolveStaffName(config);
return { name: name ?? corp.staffName ?? undefined, online: Boolean(name) };
});
if (runtime.jsonMode) {
return {
stdout: printJson(
{
activeCorpId: runtime.selectedCorp?.id,
resolvedOnline: !offline,
corporations: corps.map((corp: any, index: number) => ({
id: corp.id,
name: corp.name,
env: corp.env,
staffId: corp.staffId,
staffName: resolved[index]?.name ?? null,
staffNameResolved: resolved[index]?.online ?? false,
source: corp.source,
current: Boolean(corp.current),
})),
},
runtime.outputOptions
),
stderr: "",
exitCode: 0,
};
}
if (corps.length === 0) {
return { stdout: "未配置任何企业,请先运行 hose-use auth login\n", stderr: "", exitCode: 0 };
}
const rows = corps.map((corp: any, index: number) => [
corp.current ? `* ${index + 1}` : ` ${index + 1}`,
corp.name,
corp.id,
resolved[index]?.name || "-",
corp.staffId || "-",
formatSourceLabel(corp.source, corp.hasOpenApi),
]);
const table = printTable(
["#", "企业名称", "企业 ID", "执行身份", "员工工号", "来源"],
rows
);
return {
stdout:
`====== 当前执行身份(共 ${corps.length} 家企业${offline ? ",离线缓存" : ",在线核验"})======\n${table}\n` +
`\n提示: 标记 * 为当前激活企业;追加 --offline 可只看本地缓存、不发起网络请求。\n`,
stderr: "",
exitCode: 0,
};
}