import { canRefreshCorpAccessToken, hasOpenApiPersistentCredentials } from "../../../core/auth/login.js"; import { isDryRun, mutationPreview, requireMutationConfirmation } from "../../mutation.js"; import { printJson, printKeyValue, formatSourceLabel, formatExpiresAt } from "../../printer.js"; import type { CliParsedArgs, CliResult, CliRuntime } from "../../types.js"; export async function handleSwitch(parsed: CliParsedArgs, runtime: CliRuntime): Promise { const corpId = parsed.args[0]; if (!corpId) { return { stdout: "", stderr: "用法: hose-use corps switch <企业ID>\n", exitCode: 1 }; } if (isDryRun(parsed, runtime)) { return mutationPreview(parsed, runtime, "切换当前工作企业", corpId, { defaultCorpId: corpId }); } requireMutationConfirmation(parsed, runtime, "切换当前工作企业", `hose-use corps switch ${corpId} --yes`); 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, }; } export async function handleCurrent(parsed: CliParsedArgs, runtime: CliRuntime): Promise { 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 hasOpenApi = hasOpenApiPersistentCredentials(current); const isPersistent = canRefreshCorpAccessToken(current); const pairs: Record = { "企业 ID": current.id, "企业名称": current.name, "员工工号": current.credentials.staffId, "员工姓名": current.staffName || current.session?.staffName || "-", "凭证来源": formatSourceLabel(current.source, hasOpenApi), "持久化状态": isPersistent ? (hasOpenApi ? `✅ 已配置 OpenAPI 持久化密钥 (${current.credentials.appKey})` : "✅ 已绑定统一认证主账号") : "⚪ 短期临时会话", "Token 过期时间": formatExpiresAt(current.auth?.expiresAt || current.session?.expTime, isPersistent), "所属账号": 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, }; }