150 lines
4.5 KiB
TypeScript
150 lines
4.5 KiB
TypeScript
import { printJson, printKeyValue, printTable, formatSourceLabel, formatExpiresAt } from "../printer.js";
|
|
import type { CliParsedArgs, CliResult, CliRuntime } from "../types.js";
|
|
|
|
export async function handleCorpsCommand(
|
|
parsed: CliParsedArgs,
|
|
runtime: CliRuntime
|
|
): Promise<CliResult> {
|
|
const action = parsed.action || "list";
|
|
|
|
switch (action) {
|
|
case "list":
|
|
return handleList(parsed, runtime);
|
|
case "switch":
|
|
case "use":
|
|
return handleSwitch(parsed, runtime);
|
|
case "current":
|
|
return handleCurrent(parsed, runtime);
|
|
default:
|
|
return {
|
|
stdout: "",
|
|
stderr: `未知 corps 子命令: ${action}。支持: list, switch, current\n`,
|
|
exitCode: 1,
|
|
};
|
|
}
|
|
}
|
|
|
|
async function handleList(parsed: CliParsedArgs, runtime: CliRuntime): Promise<CliResult> {
|
|
const corps = runtime.corpContext.listCorps();
|
|
|
|
if (runtime.jsonMode) {
|
|
return {
|
|
stdout: printJson(
|
|
{
|
|
activeCorpId: runtime.selectedCorp?.id,
|
|
corporations: corps,
|
|
},
|
|
runtime.outputOptions
|
|
),
|
|
stderr: "",
|
|
exitCode: 0,
|
|
};
|
|
}
|
|
|
|
if (corps.length === 0) {
|
|
return { stdout: "未找到任何企业配置,请先运行 hose auth login\n", stderr: "", exitCode: 0 };
|
|
}
|
|
|
|
const rows = corps.map((c, i) => [
|
|
c.current ? `* ${i + 1}` : ` ${i + 1}`,
|
|
c.id,
|
|
c.name,
|
|
c.staffId,
|
|
c.source || "manual",
|
|
formatExpiresAt(c.expiresAt),
|
|
c.accountId || "-",
|
|
]);
|
|
|
|
const table = printTable(
|
|
["#", "企业 ID", "企业名称", "员工工号", "来源", "Token 过期时间", "所属账号"],
|
|
rows
|
|
);
|
|
|
|
return {
|
|
stdout: `====== 已配置企业列表 (${corps.length} 家) ======\n${table}\n\n提示: 使用 hose corps switch <企业ID> 切换工作企业\n`,
|
|
stderr: "",
|
|
exitCode: 0,
|
|
};
|
|
}
|
|
|
|
async function handleSwitch(parsed: CliParsedArgs, runtime: CliRuntime): Promise<CliResult> {
|
|
const corpId = parsed.args[0];
|
|
if (!corpId) {
|
|
return { stdout: "", stderr: "用法: hose corps switch <企业ID>\n", exitCode: 1 };
|
|
}
|
|
|
|
const success = runtime.corpContext.switchCorp(corpId);
|
|
if (!success) {
|
|
return {
|
|
stdout: "",
|
|
stderr: `企业 ${corpId} 不存在或未配置。可运行 hose corps list 查看可用列表。\n`,
|
|
exitCode: 1,
|
|
};
|
|
}
|
|
|
|
const current = runtime.corpContext.getCurrent();
|
|
if (runtime.jsonMode) {
|
|
return { stdout: printJson(current, runtime.outputOptions), stderr: "", exitCode: 0 };
|
|
}
|
|
|
|
return {
|
|
stdout: `[SUCCESS] 已切换到企业: ${current?.name} (${current?.id})\n`,
|
|
stderr: "",
|
|
exitCode: 0,
|
|
};
|
|
}
|
|
|
|
async function handleCurrent(parsed: CliParsedArgs, runtime: CliRuntime): Promise<CliResult> {
|
|
const current = runtime.selectedCorp;
|
|
if (!current) {
|
|
return { stdout: "", stderr: "未选中任何企业\n", exitCode: 1 };
|
|
}
|
|
|
|
if (runtime.jsonMode) {
|
|
return { stdout: printJson(current, runtime.outputOptions), stderr: "", exitCode: 0 };
|
|
}
|
|
|
|
const pairs: Record<string, unknown> = {
|
|
"企业 ID": current.id,
|
|
"企业名称": current.name,
|
|
"员工工号": current.credentials.staffId,
|
|
"员工姓名": current.staffName || current.session?.staffName || "-",
|
|
"凭证来源": formatSourceLabel(current.source),
|
|
"Token 过期时间": formatExpiresAt(current.auth?.expiresAt || current.session?.expTime),
|
|
"所属账号": current.accountId || "-",
|
|
"主站地址": current.baseUrl || "https://app.ekuaibao.com",
|
|
};
|
|
|
|
if (current.session) {
|
|
if (current.session.loginTime) {
|
|
pairs["会话创建时间"] = new Date(current.session.loginTime).toLocaleString("zh-CN", { hour12: false });
|
|
}
|
|
if (current.session.platform) {
|
|
pairs["官方平台类型"] = current.session.platform;
|
|
}
|
|
if (current.session.isAssistPlatform) {
|
|
pairs["协助登录模式"] = "是 (Assist Platform)";
|
|
if (current.session.operationUserName || current.session.operationUserId) {
|
|
pairs["协助操作人员"] = `${current.session.operationUserName || ""} (${current.session.operationUserId || ""})`.trim();
|
|
}
|
|
if (current.session.assistId) {
|
|
pairs["协助会话 ID"] = current.session.assistId;
|
|
}
|
|
}
|
|
if (current.session.cellphone) {
|
|
pairs["绑定手机号码"] = current.session.cellphone;
|
|
}
|
|
if (current.session.isAdmin !== undefined) {
|
|
pairs["是否企业管理"] = current.session.isAdmin ? "是" : "否";
|
|
}
|
|
}
|
|
|
|
const output = printKeyValue(pairs);
|
|
|
|
return {
|
|
stdout: `====== 当前激活企业 ======\n${output}\n`,
|
|
stderr: "",
|
|
exitCode: 0,
|
|
};
|
|
}
|