150 lines
5.1 KiB
TypeScript
150 lines
5.1 KiB
TypeScript
import { fetchWorkbenchAppsList } from "../../domains/menu/tools.js";
|
|
import { getPlatform } from "../../domains/datalink/services.js";
|
|
import { createClientsForCorp } from "../helpers.js";
|
|
import { stringOption, booleanOption } from "../parser.js";
|
|
import { printJson, printTable } from "../printer.js";
|
|
import type { CliParsedArgs, CliResult, CliRuntime } from "../types.js";
|
|
|
|
function inferCategory(app: { id: string; name: string; type?: string | null }): string {
|
|
if (app.id.startsWith("SCGY:") || app.type === "PRIVATE_CAR") return "用车/商旅套件";
|
|
if (app.type === "REGULATION") return "制度与规约";
|
|
if (app.type === "VCM") return "商旅卡与金融";
|
|
if (app.name.includes("[模板]") || app.name.includes("模板")) return "预置模板";
|
|
return "自建业务应用";
|
|
}
|
|
|
|
export async function handleAppCommand(
|
|
parsed: CliParsedArgs,
|
|
runtime: CliRuntime
|
|
): Promise<CliResult> {
|
|
if (!runtime.selectedCorp) {
|
|
return {
|
|
stdout: "",
|
|
stderr: "未选中企业,请先通过 hose-use corps switch 或 --corp 指定企业\n",
|
|
exitCode: 1,
|
|
};
|
|
}
|
|
|
|
const action = parsed.action || "list";
|
|
|
|
switch (action) {
|
|
case "list":
|
|
case "ls":
|
|
case "apps":
|
|
case "workbench": {
|
|
const keyword = stringOption(parsed.options, ["keyword", "k", "q", "query"])?.toLowerCase();
|
|
const apps = await fetchWorkbenchAppsList(runtime.selectedCorp);
|
|
|
|
const filtered = keyword
|
|
? apps.filter((app) =>
|
|
[app.id, app.name, app.code, app.href]
|
|
.filter((val): val is string => Boolean(val))
|
|
.some((val) => val.toLowerCase().includes(keyword))
|
|
)
|
|
: apps;
|
|
|
|
if (runtime.jsonMode) {
|
|
return {
|
|
stdout: printJson(
|
|
{
|
|
corpId: runtime.selectedCorp.credentials.corpId,
|
|
corpName: runtime.selectedCorp.name,
|
|
total: apps.length,
|
|
count: filtered.length,
|
|
keyword: keyword ?? null,
|
|
apps: filtered,
|
|
},
|
|
runtime.outputOptions
|
|
),
|
|
stderr: "",
|
|
exitCode: 0,
|
|
};
|
|
}
|
|
|
|
if (filtered.length === 0) {
|
|
const msg = keyword
|
|
? `企业 [${runtime.selectedCorp.name}] 下未找到匹配 "${keyword}" 的工作台应用\n`
|
|
: `企业 [${runtime.selectedCorp.name}] 下暂无工作台应用配置\n`;
|
|
return { stdout: msg, stderr: "", exitCode: 0 };
|
|
}
|
|
|
|
const headers = ["#", "应用 ID", "应用名称", "应用分类", "功能代码", "Web 直达路由"];
|
|
const rows = filtered.map((app, i) => [
|
|
i + 1,
|
|
app.id,
|
|
app.name,
|
|
inferCategory(app),
|
|
app.code || "dataLinkItem",
|
|
app.href,
|
|
]);
|
|
|
|
const title = `====== [${runtime.selectedCorp.name}] 工作台应用管理列表 (${filtered.length} 个应用) ======\n`;
|
|
return {
|
|
stdout: title + printTable(headers, rows) + "\n",
|
|
stderr: "",
|
|
exitCode: 0,
|
|
};
|
|
}
|
|
|
|
case "get":
|
|
case "show": {
|
|
const target = parsed.args[0] || stringOption(parsed.options, ["id", "name", "app"]);
|
|
if (!target) {
|
|
return {
|
|
stdout: "",
|
|
stderr: "请提供应用 ID 或名称: hose-use app get <应用 ID|名称>\n",
|
|
exitCode: 1,
|
|
};
|
|
}
|
|
|
|
const apps = await fetchWorkbenchAppsList(runtime.selectedCorp);
|
|
const matched = apps.find(
|
|
(a) =>
|
|
a.id.toLowerCase() === target.toLowerCase() ||
|
|
a.name.toLowerCase().includes(target.toLowerCase())
|
|
);
|
|
|
|
const appId = matched ? matched.id : target;
|
|
const appName = matched ? matched.name : target;
|
|
|
|
const clients = await createClientsForCorp(runtime.selectedCorp);
|
|
const corpId = runtime.selectedCorp.credentials.corpId;
|
|
|
|
try {
|
|
const detail = await getPlatform(clients.ekbClient, corpId, appId);
|
|
if (runtime.jsonMode) {
|
|
return { stdout: printJson(detail, runtime.outputOptions), stderr: "", exitCode: 0 };
|
|
}
|
|
|
|
const infoRows = [
|
|
["应用 ID", detail.id],
|
|
["应用名称", detail.name],
|
|
["应用分类", inferCategory(detail)],
|
|
["启用状态", detail.active ? "🟢 已启用" : "⚪ 未启用"],
|
|
["Web 路由", matched?.href || `/third-party-item/${detail.id}`],
|
|
["管理员数", detail.adminIds.length],
|
|
["可见人员数", detail.staffIds.length],
|
|
["可见角色数", detail.roleIds.length],
|
|
["可见部门数", detail.departmentIds.length],
|
|
];
|
|
|
|
const text = `====== 工作台应用详情: ${appName} ======\n${printTable(["属性", "信息"], infoRows)}\n`;
|
|
return { stdout: text, stderr: "", exitCode: 0 };
|
|
} catch (err) {
|
|
return {
|
|
stdout: "",
|
|
stderr: `获取应用详情失败: ${err instanceof Error ? err.message : String(err)}\n`,
|
|
exitCode: 1,
|
|
};
|
|
}
|
|
}
|
|
|
|
default:
|
|
return {
|
|
stdout: "",
|
|
stderr: `未知 app 子命令: ${action}。支持: list, get\n用法: hose-use app list [--keyword <关键词>]\n`,
|
|
exitCode: 1,
|
|
};
|
|
}
|
|
}
|