240 lines
6.4 KiB
TypeScript
240 lines
6.4 KiB
TypeScript
import {
|
|
fetchClientGoods,
|
|
searchGoods,
|
|
fetchGoodsDetail,
|
|
} from "../../domains/store/tools.js";
|
|
import { booleanOption, stringOption } from "../parser.js";
|
|
import { printJson, printKeyValue, printTable } from "../printer.js";
|
|
import type { CliParsedArgs, CliResult, CliRuntime } from "../types.js";
|
|
import { handleKeysCommand } from "./keys.js";
|
|
|
|
function formatDate(ts: number | null | undefined): string {
|
|
if (!ts) return "长期有效";
|
|
const d = new Date(ts);
|
|
if (isNaN(d.getTime())) return "-";
|
|
const Y = d.getFullYear();
|
|
const M = String(d.getMonth() + 1).padStart(2, "0");
|
|
const D = String(d.getDate()).padStart(2, "0");
|
|
return `${Y}/${M}/${D}`;
|
|
}
|
|
|
|
function formatStatus(status: string | null | undefined): string {
|
|
if (!status) return "-";
|
|
switch (status.toUpperCase()) {
|
|
case "OPENED":
|
|
return "🟢 已开通";
|
|
case "EXPIRE":
|
|
return "🔴 已过期";
|
|
case "APPLYING":
|
|
return "🟡 申请中";
|
|
case "CLOSED":
|
|
return "⚪ 已关闭";
|
|
default:
|
|
return status;
|
|
}
|
|
}
|
|
|
|
export async function handleStoreCommand(
|
|
parsed: CliParsedArgs,
|
|
runtime: CliRuntime
|
|
): Promise<CliResult> {
|
|
const currentCorp = runtime.selectedCorp;
|
|
if (!currentCorp) {
|
|
return {
|
|
stdout: "",
|
|
stderr: "未选中企业,请先通过 hose-use corps switch 或 --corp 指定企业\n",
|
|
exitCode: 1,
|
|
};
|
|
}
|
|
|
|
const action = (parsed.action || "list").toLowerCase();
|
|
|
|
switch (action) {
|
|
case "keys":
|
|
case "key":
|
|
return handleKeysCommand(parsed, runtime);
|
|
|
|
case "list":
|
|
case "ls":
|
|
return handleList(parsed, runtime);
|
|
|
|
case "search":
|
|
case "find":
|
|
return handleSearch(parsed, runtime);
|
|
|
|
case "get":
|
|
case "info":
|
|
case "detail":
|
|
return handleGet(parsed, runtime);
|
|
|
|
default:
|
|
return {
|
|
stdout: "",
|
|
stderr: `未知 store 子命令: ${action}。支持: list, search, get, keys\n`,
|
|
exitCode: 1,
|
|
};
|
|
}
|
|
}
|
|
|
|
async function handleList(parsed: CliParsedArgs, runtime: CliRuntime): Promise<CliResult> {
|
|
const currentCorp = runtime.selectedCorp!;
|
|
const keyword = parsed.args[0] || stringOption(parsed.options, ["name", "keyword", "k"]);
|
|
const isAll = booleanOption(parsed.options, ["all", "a"]);
|
|
|
|
try {
|
|
const items = await fetchClientGoods(currentCorp, { keyword, isAll });
|
|
|
|
if (runtime.jsonMode) {
|
|
return {
|
|
stdout: printJson(items, runtime.outputOptions),
|
|
stderr: "",
|
|
exitCode: 0,
|
|
};
|
|
}
|
|
|
|
if (items.length === 0) {
|
|
return {
|
|
stdout: `企业 [${currentCorp.name}] 暂无匹配的已购应用${keyword ? ` (搜索: ${keyword})` : ""}\n`,
|
|
stderr: "",
|
|
exitCode: 0,
|
|
};
|
|
}
|
|
|
|
const rows = items.map((item, i) => {
|
|
const powersStr = (item.powers || [])
|
|
.map((p) => (typeof p === "string" ? p : p.powerName || p.powerCode || ""))
|
|
.filter(Boolean)
|
|
.join(", ");
|
|
|
|
return [
|
|
i + 1,
|
|
item.id,
|
|
item.name,
|
|
formatStatus(item.status),
|
|
formatDate(item.expireTime),
|
|
item.hasConfig ? "支持配置" : "-",
|
|
powersStr || "-",
|
|
];
|
|
});
|
|
|
|
const table = printTable(
|
|
["#", "应用 ID", "应用名称", "开通状态", "有效期限", "配置支持", "绑定能力 (Powers)"],
|
|
rows
|
|
);
|
|
|
|
const title = `====== [${currentCorp.name}] 企业已开通/已购应用列表 (共 ${items.length} 个) ======`;
|
|
|
|
return {
|
|
stdout: `${title}\n\n${table}\n`,
|
|
stderr: "",
|
|
exitCode: 0,
|
|
};
|
|
} catch (err: any) {
|
|
return {
|
|
stdout: "",
|
|
stderr: `查询企业已购应用失败: ${err?.message || err}\n`,
|
|
exitCode: 1,
|
|
};
|
|
}
|
|
}
|
|
|
|
async function handleSearch(parsed: CliParsedArgs, runtime: CliRuntime): Promise<CliResult> {
|
|
const currentCorp = runtime.selectedCorp!;
|
|
const keyword = parsed.args[0] || stringOption(parsed.options, ["name", "keyword", "k"]) || "";
|
|
|
|
try {
|
|
const result = await searchGoods(currentCorp, { keyword, count: 20 });
|
|
|
|
if (runtime.jsonMode) {
|
|
return {
|
|
stdout: printJson(result, runtime.outputOptions),
|
|
stderr: "",
|
|
exitCode: 0,
|
|
};
|
|
}
|
|
|
|
if (result.items.length === 0) {
|
|
return {
|
|
stdout: `应用商店未搜索到匹配应用${keyword ? ` (关键词: ${keyword})` : ""}\n`,
|
|
stderr: "",
|
|
exitCode: 0,
|
|
};
|
|
}
|
|
|
|
const rows = result.items.map((item, i) => [
|
|
i + 1,
|
|
item.id,
|
|
item.name,
|
|
formatStatus(item.status),
|
|
item.summary || "-",
|
|
]);
|
|
|
|
const table = printTable(["#", "商品 ID", "应用名称", "企业状态", "应用简介"], rows);
|
|
const title = `====== 合思应用商店搜索结果 (共 ${result.total} 条,当前展示 ${result.items.length} 条) ======`;
|
|
|
|
return {
|
|
stdout: `${title}\n\n${table}\n`,
|
|
stderr: "",
|
|
exitCode: 0,
|
|
};
|
|
} catch (err: any) {
|
|
return {
|
|
stdout: "",
|
|
stderr: `搜索应用商店失败: ${err?.message || err}\n`,
|
|
exitCode: 1,
|
|
};
|
|
}
|
|
}
|
|
|
|
async function handleGet(parsed: CliParsedArgs, runtime: CliRuntime): Promise<CliResult> {
|
|
const currentCorp = runtime.selectedCorp!;
|
|
const goodsId = parsed.args[0] || stringOption(parsed.options, ["id"]);
|
|
|
|
if (!goodsId) {
|
|
return {
|
|
stdout: "",
|
|
stderr: "请提供应用/商品 ID,例如: hose-use store get O1w2Hg000000qw\n",
|
|
exitCode: 1,
|
|
};
|
|
}
|
|
|
|
try {
|
|
const detail = await fetchGoodsDetail(currentCorp, goodsId);
|
|
|
|
if (runtime.jsonMode) {
|
|
return {
|
|
stdout: printJson(detail, runtime.outputOptions),
|
|
stderr: "",
|
|
exitCode: 0,
|
|
};
|
|
}
|
|
|
|
const powersStr = (detail.powers || [])
|
|
.map((p) => `${p.powerName || p.powerCode || ""} (${p.powerCode || ""})`)
|
|
.join(", ");
|
|
|
|
const out = printKeyValue({
|
|
"应用 ID": detail.id,
|
|
"应用名称": detail.name,
|
|
"应用编码": detail.code || "-",
|
|
"开通状态": formatStatus(detail.status),
|
|
"有效期限": formatDate(detail.expireTime),
|
|
"支持配置": detail.hasConfig ? `是 (${detail.configStyle || "默认"})` : "否",
|
|
"绑定能力": powersStr || "-",
|
|
"应用摘要": detail.summary || "-",
|
|
});
|
|
|
|
return {
|
|
stdout: `====== 应用详情: ${detail.name} ======\n${out}\n`,
|
|
stderr: "",
|
|
exitCode: 0,
|
|
};
|
|
} catch (err: any) {
|
|
return {
|
|
stdout: "",
|
|
stderr: `获取应用详情失败: ${err?.message || err}\n`,
|
|
exitCode: 1,
|
|
};
|
|
}
|
|
}
|