232 lines
7.6 KiB
TypeScript
232 lines
7.6 KiB
TypeScript
import { join } from "node:path";
|
|
import { importFromLink } from "../../core/auth/login.js";
|
|
import { resolveWritableEnterprisesDir } from "../../core/config/loader.js";
|
|
import { stringOption, booleanOption } from "../parser.js";
|
|
import { prompt } from "../helpers.js";
|
|
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);
|
|
case "add-link":
|
|
return handleAddLink(parsed, runtime);
|
|
default:
|
|
return {
|
|
stdout: "",
|
|
stderr: `未知 corps 子命令: ${action}。支持: list, switch, current, add-link\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-use 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提示:\n - 标记 * 为当前激活企业(同时作为 hose-use 与 hose-aipro 的统一默认执行上下文)\n - 使用 hose-use 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-use corps switch <企业ID>\n", exitCode: 1 };
|
|
}
|
|
|
|
const success = runtime.corpContext.switchCorp(corpId);
|
|
if (!success) {
|
|
return {
|
|
stdout: "",
|
|
stderr: `企业 ${corpId} 不存在或未配置。可运行 hose-use 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",
|
|
"适用上下文": "同时作为 hose-use 与 hose-aipro 的统一执行环境",
|
|
};
|
|
|
|
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,
|
|
};
|
|
}
|
|
|
|
async function handleAddLink(parsed: CliParsedArgs, runtime: CliRuntime): Promise<CliResult> {
|
|
let linkUrl = parsed.args[0] || stringOption(parsed.options, ["url", "link"]);
|
|
const name = stringOption(parsed.options, ["name"]);
|
|
const overwrite = booleanOption(parsed.options, ["overwrite", "force", "f"], false);
|
|
const ttl = stringOption(parsed.options, ["ttl"]);
|
|
const expires = stringOption(parsed.options, ["expires", "days"]);
|
|
|
|
if (!linkUrl && process.stdin.isTTY) {
|
|
linkUrl = await prompt("请输入第三方协助/免密登录链接 URL: ");
|
|
}
|
|
|
|
if (!linkUrl) {
|
|
return {
|
|
stdout: "",
|
|
stderr: "错误:必须提供协助/免密链接 URL!用法: hose-use corps add-link <url> [--name <企业别名>] [--ttl 3d|--expires 3] [--overwrite]\n",
|
|
exitCode: 1,
|
|
};
|
|
}
|
|
|
|
try {
|
|
const corp = await importFromLink(linkUrl, {
|
|
name,
|
|
overwrite,
|
|
ttl,
|
|
expires,
|
|
});
|
|
|
|
if (runtime.jsonMode) {
|
|
return { stdout: printJson(corp, runtime.outputOptions), stderr: "", exitCode: 0 };
|
|
}
|
|
|
|
const targetDir = resolveWritableEnterprisesDir();
|
|
const savedPath = join(targetDir, `${corp.id}.json`);
|
|
|
|
const summaryPairs: Record<string, unknown> = {
|
|
"企业名称": corp.name,
|
|
"企业 ID": corp.id,
|
|
"集群环境": corp.cluster || "app",
|
|
"员工工号": corp.credentials.staffId,
|
|
"员工姓名": corp.staffName || corp.session?.staffName || "-",
|
|
"手机号码": corp.session?.cellphone || "-",
|
|
"是否管理": corp.session?.isAdmin ? "是" : "否",
|
|
"凭证来源": formatSourceLabel(corp.source),
|
|
"服务节点": corp.baseUrl,
|
|
"过期时间": formatExpiresAt(corp.auth?.expiresAt || corp.session?.expTime),
|
|
"保存路径": savedPath,
|
|
};
|
|
|
|
if (corp.session?.isAssistPlatform) {
|
|
summaryPairs["协助平台"] = "是 (Assist Platform 调试授权)";
|
|
if (corp.session.operationUserName || corp.session.operationUserId) {
|
|
summaryPairs["协助操作人"] = `${corp.session.operationUserName || ""} (${corp.session.operationUserId || ""})`.trim();
|
|
}
|
|
if (corp.session.assistId) {
|
|
summaryPairs["协助会话 ID"] = corp.session.assistId;
|
|
}
|
|
}
|
|
|
|
const summaryText = printKeyValue(summaryPairs);
|
|
|
|
return {
|
|
stdout: `[SUCCESS] 成功导入并保存企业凭证!\n${summaryText}\n`,
|
|
stderr: "",
|
|
exitCode: 0,
|
|
};
|
|
} catch (error) {
|
|
return {
|
|
stdout: "",
|
|
stderr: `[ERROR] 导入协助链接失败: ${error instanceof Error ? error.message : String(error)}\n`,
|
|
exitCode: 1,
|
|
};
|
|
}
|
|
}
|