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 { try { const client = await Promise.race([ createSessionClient(config), new Promise((_, 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; const staff = json.value?.staff ?? json; const name = typeof staff?.name === "string" ? staff.name.trim() : ""; return name || undefined; } catch { return undefined; } } async function mapLimit(items: T[], limit: number, fn: (item: T) => Promise): Promise { 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 { 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(); 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, }; }