109 lines
3.7 KiB
TypeScript
109 lines
3.7 KiB
TypeScript
import { createClientsForCorp } from "../helpers.js";
|
|
import * as api from "../../domains/platform/api.js";
|
|
import { printJson, printKeyValue, formatSourceLabel, formatExpiresAt } from "../printer.js";
|
|
import type { CliParsedArgs, CliResult, CliRuntime } from "../types.js";
|
|
|
|
export async function handleProbeCommand(
|
|
parsed: CliParsedArgs,
|
|
runtime: CliRuntime
|
|
): Promise<CliResult> {
|
|
const currentCorp = runtime.selectedCorp;
|
|
if (!currentCorp) {
|
|
return {
|
|
stdout: "",
|
|
stderr: "未配置任何企业,请先运行 hose auth login 或使用 --corp 指定\n",
|
|
exitCode: 1,
|
|
};
|
|
}
|
|
|
|
const results: Record<string, unknown> = {
|
|
enterprise: {
|
|
id: currentCorp.id,
|
|
name: currentCorp.name,
|
|
staffId: currentCorp.credentials.staffId,
|
|
baseUrl: currentCorp.baseUrl,
|
|
source: currentCorp.source,
|
|
accountId: currentCorp.accountId,
|
|
},
|
|
ekbSession: { status: "pending" },
|
|
easoToken: { status: "pending" },
|
|
platformAccess: { status: "pending" },
|
|
};
|
|
|
|
try {
|
|
const clients = await createClientsForCorp(currentCorp);
|
|
|
|
// 1. 测试 EKB 主站 Session
|
|
try {
|
|
const sessionRes = await clients.ekbClient
|
|
.get("api/account/v2/session")
|
|
.json<{ value?: { userId?: string; loginStaffId?: string; expTime?: number } }>();
|
|
|
|
results.ekbSession = {
|
|
status: "ok",
|
|
userId: sessionRes.value?.userId,
|
|
loginStaffId: sessionRes.value?.loginStaffId,
|
|
expTime: sessionRes.value?.expTime ? new Date(sessionRes.value.expTime).toLocaleString() : undefined,
|
|
};
|
|
} catch (e) {
|
|
results.ekbSession = {
|
|
status: "error",
|
|
error: e instanceof Error ? e.message : String(e),
|
|
};
|
|
}
|
|
|
|
// 2. 测试 EASO Token 换取
|
|
try {
|
|
const platforms = await api.listPlatforms(clients.ekbClient, currentCorp.credentials.corpId);
|
|
results.platformAccess = {
|
|
status: "ok",
|
|
totalPlatforms: platforms.length,
|
|
};
|
|
|
|
const stats = await api.getWorkflowStats(clients.easoClient, platforms[0]?.id || "platform");
|
|
results.easoToken = {
|
|
status: "ok",
|
|
message: "EASO 四步换票握手成功",
|
|
};
|
|
} catch (e) {
|
|
results.easoToken = {
|
|
status: "error",
|
|
error: e instanceof Error ? e.message : String(e),
|
|
};
|
|
}
|
|
} catch (error) {
|
|
results.error = error instanceof Error ? error.message : String(error);
|
|
}
|
|
|
|
if (runtime.jsonMode) {
|
|
return { stdout: printJson(results), stderr: "", exitCode: 0 };
|
|
}
|
|
|
|
const probePairs: Record<string, unknown> = {
|
|
"企业名称": currentCorp.name,
|
|
"企业 ID": currentCorp.id,
|
|
"员工工号": currentCorp.credentials.staffId,
|
|
"员工姓名": currentCorp.staffName || currentCorp.session?.staffName || "-",
|
|
"凭证来源": formatSourceLabel(currentCorp.source),
|
|
"Token 过期时间": formatExpiresAt(currentCorp.auth?.expiresAt || currentCorp.session?.expTime),
|
|
"主站 Session 连通": (results.ekbSession as any).status === "ok" ? "🟢 正常" : "🔴 异常",
|
|
"EASO 引擎鉴权换票": (results.easoToken as any).status === "ok" ? "🟢 握手成功" : "🔴 失败",
|
|
"自建应用扫描": (results.platformAccess as any).status === "ok" ? `🟢 可访问 (${(results.platformAccess as any).totalPlatforms} 个自建应用)` : "🔴 失败",
|
|
};
|
|
|
|
if (currentCorp.session?.isAssistPlatform) {
|
|
probePairs["协助操作人"] = `${currentCorp.session.operationUserName || ""} (${currentCorp.session.operationUserId || ""})`.trim();
|
|
if (currentCorp.session.assistId) {
|
|
probePairs["协助会话 ID"] = currentCorp.session.assistId;
|
|
}
|
|
}
|
|
|
|
const summary = printKeyValue(probePairs);
|
|
|
|
return {
|
|
stdout: `====== 诊断结果 (Enterprise Probe) ======\n${summary}\n`,
|
|
stderr: "",
|
|
exitCode: 0,
|
|
};
|
|
}
|