/** * commands/print/delete.ts — 删除打印模板 * * v0.15+ (2026-07-02 v0.15-1): 镜像 web 端 delPrintDev * 替代 v0.10 直接 c.delJson * * 镜像源: web/apps/jnpf-web-apps-main/src/api/system/printDev.ts:28 delPrintDev(id) * web 端: defHttp.delete({ url: Api.Prefix + '/' + id }) * 端点: DELETE /api/system/printDev/{id} (走 30000 jnpf-admin) * * 后端: PrintDevController @DeleteMapping("/{id}") * * v0.15+ 跟 report delete 一致: * - --yes 跳过 TTY 确认 * - id 数字校验 (跟 portal setDefault P2-4 / role assign P0-9 一致) */ import { Command } from 'commander' import { JnpfClient } from '../../core/client.ts' import { installHelpHook } from '../../core/help.ts' import { log, printJson } from '../../core/log.ts' import { enhanceFromBackend, formatErrorLine } from '../_error_codes.ts' import { initWebapi as initPrintDevWebapi, loadPrintDev } from '../../webapi/printDev.ts' import { setupOutputFilter, addOutputOptions, wrapCommand } from '../_output_filter.ts' // v2.1+ 输出过滤; v2.2+ wrapCommand 收口 5c interface DeleteOpts { id: string yes?: boolean json?: boolean /** v2.1+ (2026-07-21): --verbose, addOutputOptions 注入 */ verbose?: boolean } export function buildPrintDeleteCommand(): Command { const cmd = new Command('delete') .description('删除打印模板 (v0.15+ 镜像 web 端 delPrintDev, 走 /api/system/printDev/{id})') .requiredOption('--id ', '打印模板 id') .option('--yes', '跳过 TTY 确认 (强制删除)', false) addOutputOptions(cmd) .action(async (opts: DeleteOpts) => { setupOutputFilter(opts) const c = await JnpfClient.fromToken() await wrapCommand({ command: 'print delete', appCode: c.appCode, startTime: Date.now(), verbose: opts.verbose, json: opts.json, fixes: [], }, async (ctx) => { try { // v0.15+ (P0-9): id 数字校验 (跟 portal/role 一致) const idRegex = /^\d{10,30}$/ if (!idRegex.test(opts.id)) { log.error('提示: 用 `dacli print list` 查 id, 别传 fullName/enCode') return ctx.fail(`--id "${opts.id}" 不是合法 JNPF id (期望纯数字 10-30 位)`, { id: opts.id }) } // v0.15+ TTY 确认 (跟 report delete 一致) if (!opts.yes && process.stdin.isTTY) { log.warn(' 提示: 加 --yes 跳过确认 (AI/脚本场景)') return ctx.fail(`即将删除打印模板 id=${opts.id}, 此操作不可恢复 (加 --yes 确认)`, { id: opts.id }) } await initPrintDevWebapi(c) // 镜像 web 端 delPrintDev(id) → defHttp.delete const r = (await loadPrintDev.delPrintDev(opts.id)) as { code?: number; msg?: string } if (r && r.code !== undefined && r.code !== 200) { const enhanced = enhanceFromBackend(r.msg || '', 'print', { entity: 'print' }) log.error(` ${formatErrorLine(enhanced, { step: 0, stepName: '后端API' })}`) return ctx.fail(`print delete 失败 (code=${r.code}): ${r.msg}`, { id: opts.id, code: r.code }) } log.ok(`✓ 打印模板已删除 id=${opts.id} (v0.15+ webapi 镜像)`) if (opts.json) printJson({ id: opts.id, code: 200, msg: '删除成功' }) ctx.success({ id: opts.id }) } catch (e) { ctx.fail(`print delete 失败: ${(e as Error).message?.slice(0, 200)}`) } }) }) installHelpHook(cmd, { purpose: '删除打印模板 (v0.15+ 镜像 web delPrintDev)', required: { '--id': '打印模板 id' }, optional: { '--yes': '跳过 TTY 确认 (强制删除)' }, example: [ 'dacli print delete --id=585817367276160133 --yes', ], source: { web: 'web/apps/jnpf-web-apps-main/src/api/system/printDev.ts:28 delPrintDev(id)', api: 'DELETE /api/system/printDev/{id} (走 webapi Proxy, 端口 30000 jnpf-admin)', ref: 'jnpf/后端/jnpf-system/jnpf-system-controller/.../PrintDevController.java @DeleteMapping', }, notes: [ 'v0.15+ 走 webapi 镜像 (loadPrintDev.delPrintDev), 替代旧版 c.delete', '**v0.15+ (P0-9)**: --yes 跳过 TTY 确认 (跟 report/form delete 一致)', '**v0.15+ (P0-9)**: id 数字校验, 输错字符串提前 exit 1', ], }) return cmd }