fix(corps): resolve whoami staff names online by id

- whoami 默认在线核验 staffs/me,配置只依赖工号
- --offline 退回本地缓存
- 并发 5、单请求 8s 超时
This commit is contained in:
gitea committed 2026-10-23 17:58:24 +08:00
1 parent a797196caa
commit af7362a346
3 files changed
+307 -254

No files matched your search

+1 -1
View File
@@ -166,7 +166,7 @@ export const COMMAND_METADATA: Record<string, CliCommandMetadata> = {
"sync-from-seal": "sync-seal",
"import-seal": "sync-seal"
},
flags: { ...flagList("a add all as-hose demo elevate elevate-hose f force migrate-all no-elevate overwrite p persist plain prod production r refresh reveal save save-staff t test upgrade-all verify", "boolean"), ...flagList("app-key app-security appKey appSecurity base-url baseUrl corp corp-id corpId data days domain e env expire expires expires-in expiresIn filter id k key key-id keyId keyword link name operator page-type pageType password proxyStaffBizId q query role roleDefId seal-dir sealDir secret staff staff-id staff-name staff-phone staffId staffName staffPhone ttl url user", "value") }
flags: { ...flagList("a add all as-hose demo elevate elevate-hose f force migrate-all no-elevate overwrite p persist plain prod production r refresh reveal save save-staff t test upgrade-all verify offline cached", "boolean"), ...flagList("app-key app-security appKey appSecurity base-url baseUrl corp corp-id corpId data days domain e env expire expires expires-in expiresIn filter id k key key-id keyId keyword link name operator page-type pageType password proxyStaffBizId q query role roleDefId seal-dir sealDir secret staff staff-id staff-name staff-phone staffId staffName staffPhone ttl url user", "value") }
},
"config": {
"summary": "全局运行模式与路径配置",
+64 -11
View File
@@ -1,26 +1,79 @@
import { formatSourceLabel } from "../../printer.js";
import { printJson, printTable } from "../../printer.js";
import type { CliResult, CliRuntime } from "../../types.js";
import { loadCorpConfigs } from "../../../core/config/loader.js";
import { createSessionClient } from "../../../core/http/session-client.js";
import { booleanOption } from "../../parser.js";
import { formatSourceLabel, printJson, printTable } from "../../printer.js";
import type { CliParsedArgs, CliResult, CliRuntime } from "../../types.js";
/**
* `hose-use whoami` — 按企业逐行显示当前执行身份(员工姓名 + 工号)。
* `hose-use whoami` — 按企业逐行显示当前执行身份。
*
* 只读本地配置,不发网络请求;写操作前可快速核对“我会以谁的身份执行”。
* 名称以在线 `staffs/me` 为准(配置里只存工号即可);
* `--offline` 时退回本地缓存的 staffName(没有则显示 -)。
*/
export async function handleWhoami(_parsed: unknown, runtime: CliRuntime): Promise<CliResult> {
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 corps = runtime.corpContext.listCorps();
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,
corporations: corps.map((corp: any) => ({
resolvedOnline: !offline,
corporations: corps.map((corp: any, index: number) => ({
id: corp.id,
name: corp.name,
env: corp.env,
staffId: corp.staffId,
staffName: corp.staffName || null,
staffName: resolved[index]?.name ?? null,
staffNameResolved: resolved[index]?.online ?? false,
source: corp.source,
current: Boolean(corp.current),
})),
@@ -40,7 +93,7 @@ export async function handleWhoami(_parsed: unknown, runtime: CliRuntime): Promi
corp.current ? `* ${index + 1}` : ` ${index + 1}`,
corp.name,
corp.id,
corp.staffName || "-",
resolved[index]?.name || "-",
corp.staffId || "-",
formatSourceLabel(corp.source, corp.hasOpenApi),
]);
@@ -52,8 +105,8 @@ export async function handleWhoami(_parsed: unknown, runtime: CliRuntime): Promi
return {
stdout:
`====== 当前执行身份(共 ${corps.length} 家企业)======\n${table}\n` +
`\n提示: 标记 * 为当前激活企业;写操作前请核对「执行身份」是否为你本人。\n`,
`====== 当前执行身份(共 ${corps.length} 家企业${offline ? ",离线缓存" : ",在线核验"})======\n${table}\n` +
`\n提示: 标记 * 为当前激活企业;追加 --offline 可只看本地缓存、不发起网络请求。\n`,
stderr: "",
exitCode: 0,
};