- Schema 扩展: CorpConfigSchema 增加 env ('prod' | 'test' | 'demo') 与 isTest 字段,未知企业保守兜底为 prod
- 智能初判与归一化: inferCorpEnvFromName 依据名称初判环境,normalizeCorpConfig 确保配置持久化时环境对齐
- CLI 视图升级: corps list 与 search 增加「环境」列 (🏭 正式 / 🧪 测试 / 🎪 演示),支持 --test/--prod/--env 筛选
- 环境打标与迁移: 新增 hose-use corps tag 命令,支持单个企业标记与 --migrate-all 批量显式升级持久化
- 生产安全阻断: workflow test 等敏感调试在正式生产环境 (prod) 阻断执行,需显式附加 --force 确认生产风险
- 上下文与探针增强: context、probe、corps current 全面输出企业环境属性
- 测试与文档补全: 覆盖 489 项全量测试并通过 tsc 类型检查
98 lines
3.9 KiB
TypeScript
98 lines
3.9 KiB
TypeScript
import { canRefreshCorpAccessToken, hasOpenApiPersistentCredentials } from "../../../core/auth/login.js";
|
|
import { isDryRun, mutationPreview, requireMutationConfirmation } from "../../mutation.js";
|
|
import { printJson, printKeyValue, formatSourceLabel, formatExpiresAt, formatCorpEnvLabel } from "../../printer.js";
|
|
import type { CliParsedArgs, CliResult, CliRuntime } from "../../types.js";
|
|
|
|
export 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 };
|
|
}
|
|
|
|
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 };
|
|
}
|
|
|
|
const envLabel = formatCorpEnvLabel(current?.env);
|
|
return {
|
|
stdout: `[SUCCESS] 已切换到企业: ${current?.name} (${current?.id}) [${envLabel}]\n`,
|
|
stderr: "",
|
|
exitCode: 0,
|
|
};
|
|
}
|
|
|
|
export 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 hasOpenApi = hasOpenApiPersistentCredentials(current);
|
|
const isPersistent = canRefreshCorpAccessToken(current);
|
|
|
|
const pairs: Record<string, unknown> = {
|
|
"企业 ID": current.id,
|
|
"企业名称": current.name,
|
|
"环境属性": formatCorpEnvLabel(current.env),
|
|
"员工工号": 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, hasOpenApi ? "openapi" : current.source),
|
|
"所属账号": 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,
|
|
};
|
|
}
|