feat(dacli): msg/* 模块 wrapCommand 迁移 (notice+template CRUD 7 文件, 14 exit 收口)

notice/templateCreate 从旧 printCommandEnd 升级到 wrapCommand (保留 auto-fix fixes).
noticeUpdate 复用 DirtyCheckError catch. noticeSend 非 site 通道 → ctx.success (note).
notice/template Delete/Update process.exit → ctx.fail. msg/ 模块 process.exit 归零.

Co-Authored-By: Claude <[email protected]>
This commit is contained in:
sswandClaude committed 2026-07-21 21:00:25 +08:00
1 parent 6d190a947c
commit 183d07bcc6
7 files changed
+316 -258

No files matched your search

+57 -76
View File
@@ -19,7 +19,7 @@ import { log, printJson } from '../../core/log.ts'
import { validateMsgCreate } from './_validate.ts'
import { enhanceFromBackend, formatErrorLine } from '../_error_codes.ts'
import { runCommandAutoFix, fetchExistingLists } from '../_auto_fix.ts' // v2.1+ (2026-07-21): 跨命令 auto-fix
import { setupOutputFilter, printCommandEnd } from '../_output_filter.ts' // v2.1+ (2026-07-21): stdout 三模式 + CommandLog 落盘
import { setupOutputFilter, addOutputOptions, wrapCommand } from '../_output_filter.ts' // v2.1+ stdout 三模式 + CommandLog; v2.2+ wrapCommand 收口 5c
interface CreateOpts {
fullName: string
@@ -36,8 +36,7 @@ export function buildMsgNoticeCreateCommand(): Command {
.requiredOption('--fullName <name>', '通知标题')
.requiredOption('--enCode <code>', '通知编码')
.option('--body <text>', '通知正文 (可选)')
.option('--verbose', '完整输出 (含 step machine、API 调用等过程日志)', false)
.option('--json', '原始 JSON 输出', false)
addOutputOptions(cmd)
.action(async (opts: CreateOpts) => {
// v2.1+ L1: stdout 三模式
setupOutputFilter(opts)
@@ -51,83 +50,65 @@ export function buildMsgNoticeCreateCommand(): Command {
Object.assign(opts, cmdFix.opts)
}
// v0.11e+ Phase D1: step machine 校验 (MSERR114 系统编码规则)
const validation = validateMsgCreate({
fullName: opts.fullName,
enCode: opts.enCode,
})
if (!validation.ok) {
for (const s of validation.steps) {
for (const e of s.errors) {
log.error(` ${formatErrorLine(e, { step: s.step, stepName: s.stepName })}`)
}
}
// v2.1+ L3: 失败落 CommandLog (MSERR114 系统编码规则)
printCommandEnd({
command: 'msg noticeCreate',
appCode: c.appCode,
result: 'failure',
summary: { enCode: opts.enCode, fullName: opts.fullName },
fixes: [...cmdFix.silent, ...cmdFix.assertive, ...cmdFix.suggestions],
error: validation.steps.flatMap(s => s.errors).map(e => e.message).join('; '),
duration_ms: Date.now() - startTime,
verbose: opts.verbose,
json: opts.json,
})
process.exit(1)
}
log.info(`✓ step machine 校验通过 (${validation.steps.length}/${validation.steps.length})`)
const body = {
fullName: opts.fullName,
enCode: opts.enCode,
title: opts.fullName,
body: opts.body || '通知内容',
type: 1, // 1=系统公告 2=待办
enabledMark: 1,
sortCode: 0,
}
const r = await c.postJson<{ id?: string }>('/message/Notice', body)
if (r.code !== 200) {
const enhanced = enhanceFromBackend(r.msg || '', 'msg', { entity: 'msg' })
log.error(` ${formatErrorLine(enhanced, { step: 1, stepName: '后端API' })}`)
// v2.1+ L3: API 失败落 CommandLog
printCommandEnd({
command: 'msg noticeCreate',
appCode: c.appCode,
result: 'failure',
summary: { enCode: opts.enCode, fullName: opts.fullName },
fixes: [...cmdFix.silent, ...cmdFix.assertive, ...cmdFix.suggestions],
error: r.msg || `code=${r.code}`,
duration_ms: Date.now() - startTime,
verbose: opts.verbose,
json: opts.json,
})
process.exit(1)
}
let id = r.data?.id
if (!id) {
const listR = await c.get<{ list: { id: string; enCode: string; fullName: string }[] }>(
'/message/Notice',
{ keyword: opts.enCode, pageSize: '50' },
)
const found = (listR.data?.list || []).find(
(d) => d.enCode === opts.enCode || d.fullName === opts.fullName,
)
id = found?.id
}
log.ok(`✓ 通知公告创建成功 id=${id}`)
if (opts.json) printJson({ ...(r.data as object), id })
// v2.1+ L3: 落 CommandLog + 摘要 + "详情: dacli log last"
printCommandEnd({
await wrapCommand({
command: 'msg noticeCreate',
appCode: c.appCode,
summary: { id, enCode: opts.enCode, fullName: opts.fullName },
fixes: [...cmdFix.silent, ...cmdFix.assertive, ...cmdFix.suggestions],
duration_ms: Date.now() - startTime,
startTime,
verbose: opts.verbose,
json: opts.json,
fixes: [...cmdFix.silent, ...cmdFix.assertive, ...cmdFix.suggestions],
}, async (ctx) => {
try {
// v0.11e+ Phase D1: step machine 校验 (MSERR114 系统编码规则)
const validation = validateMsgCreate({
fullName: opts.fullName,
enCode: opts.enCode,
})
if (!validation.ok) {
for (const s of validation.steps) {
for (const e of s.errors) {
log.error(` ${formatErrorLine(e, { step: s.step, stepName: s.stepName })}`)
}
}
return ctx.fail(
validation.steps.flatMap(s => s.errors).map(e => e.message).join('; '),
{ enCode: opts.enCode, fullName: opts.fullName },
)
}
log.info(`✓ step machine 校验通过 (${validation.steps.length}/${validation.steps.length})`)
const body = {
fullName: opts.fullName,
enCode: opts.enCode,
title: opts.fullName,
body: opts.body || '通知内容',
type: 1, // 1=系统公告 2=待办
enabledMark: 1,
sortCode: 0,
}
const r = await c.postJson<{ id?: string }>('/message/Notice', body)
if (r.code !== 200) {
const enhanced = enhanceFromBackend(r.msg || '', 'msg', { entity: 'msg' })
log.error(` ${formatErrorLine(enhanced, { step: 1, stepName: '后端API' })}`)
return ctx.fail(r.msg || `code=${r.code}`, { enCode: opts.enCode, fullName: opts.fullName })
}
let id = r.data?.id
if (!id) {
const listR = await c.get<{ list: { id: string; enCode: string; fullName: string }[] }>(
'/message/Notice',
{ keyword: opts.enCode, pageSize: '50' },
)
const found = (listR.data?.list || []).find(
(d) => d.enCode === opts.enCode || d.fullName === opts.fullName,
)
id = found?.id
}
log.ok(`✓ 通知公告创建成功 id=${id}`)
if (opts.json) printJson({ ...(r.data as object), id })
ctx.success({ id, enCode: opts.enCode, fullName: opts.fullName })
} catch (e) {
ctx.fail(`msg noticeCreate 失败: ${(e as Error).message?.slice(0, 200)}`)
}
})
})
installHelpHook(cmd, {
+29 -13
View File
@@ -9,8 +9,9 @@ import { JnpfClient } from '../../core/client.ts'
import { installHelpHook } from '../../core/help.ts'
import { log } from '../../core/log.ts'
import { initWebapi, loadNotice } from '../../webapi/message.ts'
import { setupOutputFilter, addOutputOptions, wrapCommand } from '../_output_filter.ts' // v2.1+ 输出过滤; v2.2+ wrapCommand 收口 5c
interface Opts { id: string; yes?: boolean }
interface Opts { id: string; yes?: boolean; json?: boolean; verbose?: boolean }
export function buildMsgNoticeDeleteCommand(): Command {
const cmd = new Command('noticeDelete')
@@ -18,20 +19,35 @@ export function buildMsgNoticeDeleteCommand(): Command {
.description('通知公告删除 (硬删, v0.11+ webapi)')
.requiredOption('--id <id>', '公告 id')
.option('--yes', '跳过确认', false)
addOutputOptions(cmd)
.action(async (opts: Opts) => {
if (!opts.yes) {
log.warn(`即将删除公告 id=${opts.id}, 加 --yes 跳过确认`)
process.exit(2)
}
setupOutputFilter(opts)
const c = await JnpfClient.fromToken()
await initWebapi(c)
log.info(`loadNotice.delNotice (id=${opts.id})`)
const r = (await loadNotice.delNotice(opts.id)) as { code?: number; msg?: string }
if (r.code !== 200 && r.code !== undefined) {
log.error(`msg noticeDelete 失败: ${r.msg}`)
process.exit(1)
}
log.ok(`✓ 公告已删除 (id=${opts.id})`)
await wrapCommand({
command: 'msg noticeDelete',
appCode: c.appCode,
startTime: Date.now(),
verbose: opts.verbose,
json: opts.json,
fixes: [],
}, async (ctx) => {
try {
if (!opts.yes) {
return ctx.fail(`即将删除公告 id=${opts.id}, 加 --yes 跳过确认`, { id: opts.id })
}
await initWebapi(c)
log.info(`loadNotice.delNotice (id=${opts.id})`)
const r = (await loadNotice.delNotice(opts.id)) as { code?: number; msg?: string }
if (r.code !== 200 && r.code !== undefined) {
return ctx.fail(`msg noticeDelete 失败: ${r.msg}`, { id: opts.id, code: r.code })
}
log.ok(`✓ 公告已删除 (id=${opts.id})`)
ctx.success({ id: opts.id })
} catch (e) {
ctx.fail(`msg noticeDelete 失败: ${(e as Error).message?.slice(0, 200)}`)
}
})
})
installHelpHook(cmd, {
purpose: '通知公告删除',
+37 -17
View File
@@ -8,10 +8,14 @@ import { Command } from 'commander'
import { JnpfClient } from '../../core/client.ts'
import { installHelpHook } from '../../core/help.ts'
import { log } from '../../core/log.ts'
import { setupOutputFilter, addOutputOptions, wrapCommand } from '../_output_filter.ts' // v2.1+ 输出过滤; v2.2+ wrapCommand 收口 5c
interface SendOpts {
id: string
type?: string
json?: boolean
/** v2.1+ (2026-07-21): --verbose, addOutputOptions 注入 */
verbose?: boolean
}
export function buildMsgNoticeSendCommand(): Command {
@@ -20,26 +24,42 @@ export function buildMsgNoticeSendCommand(): Command {
.description('发布/发送通知 (走 release 端点, 决策 8: type=site 站内信)')
.requiredOption('--id <id>', '通知 id')
.option('--type <t>', '通道: site=站内信 (默认) | email=邮件 | sms=短信 | webhook=Webhook | dingTalk=钉钉 | weCom=企微', 'site')
addOutputOptions(cmd)
.action(async (opts: SendOpts) => {
const validTypes = ['site', 'email', 'sms', 'webhook', 'dingTalk', 'weCom']
if (!validTypes.includes(opts.type || '')) {
log.error(`--type 必须是 ${validTypes.join('|')}`)
process.exit(2)
}
setupOutputFilter(opts)
const c = await JnpfClient.fromToken()
// 站内信走 notice release
if (opts.type === 'site') {
const r = await c.putJson(`/message/Notice/${opts.id}/Actions/Release`, {})
if (r.code !== 200) {
log.error(`msg noticesend (站内信) 失败: ${r.msg}`)
process.exit(1)
await wrapCommand({
command: 'msg noticeSend',
appCode: c.appCode,
startTime: Date.now(),
verbose: opts.verbose,
json: opts.json,
fixes: [],
}, async (ctx) => {
try {
const validTypes = ['site', 'email', 'sms', 'webhook', 'dingTalk', 'weCom']
if (!validTypes.includes(opts.type || '')) {
return ctx.fail(`--type 必须是 ${validTypes.join('|')}`, { id: opts.id, type: opts.type })
}
// 站内信走 notice release
if (opts.type === 'site') {
const r = await c.putJson(`/message/Notice/${opts.id}/Actions/Release`, {})
if (r.code !== 200) {
return ctx.fail(`msg noticesend (站内信) 失败: ${r.msg}`, { id: opts.id, code: r.code })
}
log.info(`✓ 通知 id=${opts.id} 已发布 (站内信推送)`)
ctx.success({ id: opts.id, type: 'site' })
} else {
// 邮件/短信/Webhook 走 integration 端点, 留 v0.9+ 实做
log.warn(`--type=${opts.type} 通道的发送端点 v0.9+ 实做, 当前只支持 site (站内信)`)
log.info(` 提示: 走 dacli integration ${opts.type} send (W12+)`)
ctx.success({ id: opts.id, type: opts.type, note: 'channel not implemented (v0.9+)' })
}
} catch (e) {
ctx.fail(`msg noticeSend 失败: ${(e as Error).message?.slice(0, 200)}`)
}
log.info(`✓ 通知 id=${opts.id} 已发布 (站内信推送)`)
} else {
// 邮件/短信/Webhook 走 integration 端点, 留 v0.9+ 实做
log.warn(`--type=${opts.type} 通道的发送端点 v0.9+ 实做, 当前只支持 site (站内信)`)
log.info(` 提示: 走 dacli integration ${opts.type} send (W12+)`)
}
})
})
installHelpHook(cmd, {
purpose: '发送/发布通知 (决策 8: 通道 site/email/sms/webhook/dingTalk/weCom)',
+68 -41
View File
@@ -11,8 +11,9 @@ import { JnpfClient } from '../../core/client.ts'
import { installHelpHook } from '../../core/help.ts'
import { log, printJson } from '../../core/log.ts'
import { initWebapi, loadNotice } from '../../webapi/message.ts'
import { dirtyCheck, dirtyCheckFailAndExit } from '../_dirty_check.ts'
import { dirtyCheck, dirtyCheckFailAndExit, DirtyCheckError } from '../_dirty_check.ts'
import { enhanceFromBackend, formatErrorLine } from '../_error_codes.ts'
import { setupOutputFilter, addOutputOptions, wrapCommand } from '../_output_filter.ts' // v2.1+ 输出过滤; v2.2+ wrapCommand 收口 5c
interface Opts {
id: string
@@ -23,6 +24,8 @@ interface Opts {
expectedEnCode?: string
force?: boolean
json?: boolean
/** v2.1+ (2026-07-21): --verbose, addOutputOptions 注入 */
verbose?: boolean
}
export function buildMsgNoticeUpdateCommand(): Command {
@@ -36,50 +39,74 @@ export function buildMsgNoticeUpdateCommand(): Command {
.option('--category <c>', '分类 (1=系统 2=业务)')
.option('--expected-enCode <e>', 'v0.11e+ Phase A2: 校验 enCode 未变')
.option('--force', 'v0.11e+ Phase A2: 跳过 dirty check', false)
.option('--json', '原始 JSON 输出', false)
addOutputOptions(cmd)
.action(async (opts: Opts) => {
setupOutputFilter(opts)
const c = await JnpfClient.fromToken()
await initWebapi(c)
const orig = (await loadNotice.getInfo(opts.id)) as Record<string, unknown>
if (!orig) {
const enhanced = enhanceFromBackend('拉原公告失败, id 不存在', 'id', { entity: 'msg' })
log.error(` ${formatErrorLine(enhanced, { step: 0, stepName: '配置拉取' })}`)
process.exit(1)
}
// v0.11e+ Phase A2: dirty check
const dirtyResult = dirtyCheck({
expectedEnCode: opts.expectedEnCode,
force: opts.force,
current: {
enCode: orig.enCode as string | undefined,
state: orig.enabledMark as string | number | undefined,
fullName: orig.fullName as string | undefined,
type: 'di',
},
id: opts.id,
await wrapCommand({
command: 'msg noticeUpdate',
appCode: c.appCode,
startTime: Date.now(),
verbose: opts.verbose,
json: opts.json,
fixes: [],
}, async (ctx) => {
try {
await initWebapi(c)
const orig = (await loadNotice.getInfo(opts.id)) as Record<string, unknown>
if (!orig) {
const enhanced = enhanceFromBackend('拉原公告失败, id 不存在', 'id', { entity: 'msg' })
log.error(` ${formatErrorLine(enhanced, { step: 0, stepName: '配置拉取' })}`)
return ctx.fail(`msg noticeUpdate 失败: 拉原公告 id=${opts.id} 失败`, { id: opts.id })
}
// v0.11e+ Phase A2: dirty check
const dirtyResult = dirtyCheck({
expectedEnCode: opts.expectedEnCode,
force: opts.force,
current: {
enCode: orig.enCode as string | undefined,
state: orig.enabledMark as string | number | undefined,
fullName: orig.fullName as string | undefined,
type: 'di',
},
id: opts.id,
})
if (!dirtyResult.ok) {
// v2.2+ (2026-07-21): dirtyCheckFailAndExit 改 throw (DirtyCheckError), 下方 catch → ctx.fail
try {
dirtyCheckFailAndExit(dirtyResult, { type: 'di', id: opts.id, current: { enCode: orig.enCode as string | undefined, fullName: orig.fullName as string | undefined } }, log.error)
} catch (e) {
if (e instanceof DirtyCheckError) {
return ctx.fail(e.message, { id: opts.id, type: 'msg', errorCount: e.errorCount })
}
throw e
}
} else if (dirtyResult.skipped) {
log.info(`⚠ dirty check 已 --force 跳过`)
}
const body: Record<string, unknown> = { ...orig, id: opts.id }
if (opts.fullName) body.fullName = opts.fullName
if (opts.enCode) body.enCode = opts.enCode
if (opts.body) body.body = opts.body
if (opts.category) body.category = opts.category
log.info(`loadNotice.update (id=${opts.id})`)
const r = (await loadNotice.update(body)) as { code?: number; msg?: string; data?: unknown }
if (r.code !== 200 && r.code !== undefined) {
const enhanced = enhanceFromBackend(r.msg || '', 'msg', { entity: 'msg' })
log.error(` ${formatErrorLine(enhanced, { step: 1, stepName: '后端API' })}`)
if (opts.json) printJson(r)
return ctx.fail(`msg noticeUpdate 失败 (code=${r.code}): ${r.msg}`, { id: opts.id, code: r.code })
}
log.ok(`✓ 公告更新成功 (id=${opts.id})`)
if (opts.json) printJson(r.data)
ctx.success({ id: opts.id })
} catch (e) {
ctx.fail(`msg noticeUpdate 失败: ${(e as Error).message?.slice(0, 200)}`)
}
})
if (!dirtyResult.ok) {
dirtyCheckFailAndExit(dirtyResult, { type: 'di', id: opts.id, current: { enCode: orig.enCode as string | undefined, fullName: orig.fullName as string | undefined } }, log.error)
} else if (dirtyResult.skipped) {
log.info(`⚠ dirty check 已 --force 跳过`)
}
const body: Record<string, unknown> = { ...orig, id: opts.id }
if (opts.fullName) body.fullName = opts.fullName
if (opts.enCode) body.enCode = opts.enCode
if (opts.body) body.body = opts.body
if (opts.category) body.category = opts.category
log.info(`loadNotice.update (id=${opts.id})`)
const r = (await loadNotice.update(body)) as { code?: number; msg?: string; data?: unknown }
if (r.code !== 200 && r.code !== undefined) {
const enhanced = enhanceFromBackend(r.msg || '', 'msg', { entity: 'msg' })
log.error(` ${formatErrorLine(enhanced, { step: 1, stepName: '后端API' })}`)
if (opts.json) printJson(r)
process.exit(1)
}
log.ok(`✓ 公告更新成功 (id=${opts.id})`)
if (opts.json) printJson(r.data)
})
installHelpHook(cmd, {
purpose: '通知公告修改. 改 fullName/enCode/body/category, PUT 语义 + v0.11e+ dirty check.',
+57 -76
View File
@@ -20,7 +20,7 @@ import { log, printJson } from '../../core/log.ts'
import { validateMsgCreate } from './_validate.ts'
import { enhanceFromBackend, formatErrorLine } from '../_error_codes.ts'
import { runCommandAutoFix, fetchExistingLists } from '../_auto_fix.ts' // v2.1+ (2026-07-21): 跨命令 auto-fix
import { setupOutputFilter, printCommandEnd } from '../_output_filter.ts' // v2.1+ (2026-07-21): stdout 三模式 + CommandLog 落盘
import { setupOutputFilter, addOutputOptions, wrapCommand } from '../_output_filter.ts' // v2.1+ stdout 三模式 + CommandLog; v2.2+ wrapCommand 收口 5c
interface CreateOpts {
fullName: string
@@ -39,8 +39,7 @@ export function buildMsgTemplateCreateCommand(): Command {
.requiredOption('--enCode <code>', '模板编码 (全平台唯一)')
.option('--category <c>', '分类 (1=系统 2=业务)', '1')
.option('--content <text>', '模板内容 (可选)')
.option('--verbose', '完整输出 (含 step machine、API 调用等过程日志)', false)
.option('--json', '原始 JSON 输出', false)
addOutputOptions(cmd)
.action(async (opts: CreateOpts) => {
// v2.1+ L1: stdout 三模式
setupOutputFilter(opts)
@@ -54,83 +53,65 @@ export function buildMsgTemplateCreateCommand(): Command {
Object.assign(opts, cmdFix.opts)
}
// v0.11e+ Phase D1: step machine 校验 (fullName/enCode 必填 + MSERR114 系统编码规则)
const validation = validateMsgCreate({
fullName: opts.fullName,
enCode: opts.enCode,
})
if (!validation.ok) {
for (const s of validation.steps) {
for (const e of s.errors) {
log.error(` ${formatErrorLine(e, { step: s.step, stepName: s.stepName })}`)
}
}
// v2.1+ L3: 失败落 CommandLog
printCommandEnd({
command: 'msg templateCreate',
appCode: c.appCode,
result: 'failure',
summary: { enCode: opts.enCode, fullName: opts.fullName },
fixes: [...cmdFix.silent, ...cmdFix.assertive, ...cmdFix.suggestions],
error: validation.steps.flatMap(s => s.errors).map(e => e.message).join('; '),
duration_ms: Date.now() - startTime,
verbose: opts.verbose,
json: opts.json,
})
process.exit(1)
}
log.info(`✓ step machine 校验通过 (${validation.steps.length}/${validation.steps.length})`)
const body = {
fullName: opts.fullName,
enCode: opts.enCode,
category: opts.category || '1',
enabledMark: 1,
sortCode: 0,
...(opts.content ? { content: opts.content } : {}),
}
// web 端真实端点: /api/message/MessageTemplateConfig
const r = await c.postJson<{ id?: string }>('/message/MessageTemplateConfig', body)
if (r.code !== 200) {
const enhanced = enhanceFromBackend(r.msg || '', 'msgTemplate', { entity: 'msg' })
log.error(` ${formatErrorLine(enhanced, { step: 1, stepName: '后端API' })}`)
// v2.1+ L3: API 失败落 CommandLog
printCommandEnd({
command: 'msg templateCreate',
appCode: c.appCode,
result: 'failure',
summary: { enCode: opts.enCode, fullName: opts.fullName },
fixes: [...cmdFix.silent, ...cmdFix.assertive, ...cmdFix.suggestions],
error: r.msg || `code=${r.code}`,
duration_ms: Date.now() - startTime,
verbose: opts.verbose,
json: opts.json,
})
process.exit(1)
}
let id = r.data?.id
if (!id) {
const listR = await c.get<{ list: { id: string; enCode: string; fullName: string }[] }>(
'/message/MessageTemplateConfig',
{ keyword: opts.enCode, pageSize: '50' },
)
const found = (listR.data?.list || []).find(
(d) => d.enCode === opts.enCode || d.fullName === opts.fullName,
)
id = found?.id
}
log.ok(`✓ 消息模板创建成功 id=${id}`)
if (opts.json) printJson({ ...(r.data as object), id })
// v2.1+ L3: 落 CommandLog
printCommandEnd({
await wrapCommand({
command: 'msg templateCreate',
appCode: c.appCode,
summary: { id, enCode: opts.enCode, fullName: opts.fullName, category: opts.category },
fixes: [...cmdFix.silent, ...cmdFix.assertive, ...cmdFix.suggestions],
duration_ms: Date.now() - startTime,
startTime,
verbose: opts.verbose,
json: opts.json,
fixes: [...cmdFix.silent, ...cmdFix.assertive, ...cmdFix.suggestions],
}, async (ctx) => {
try {
// v0.11e+ Phase D1: step machine 校验 (fullName/enCode 必填 + MSERR114 系统编码规则)
const validation = validateMsgCreate({
fullName: opts.fullName,
enCode: opts.enCode,
})
if (!validation.ok) {
for (const s of validation.steps) {
for (const e of s.errors) {
log.error(` ${formatErrorLine(e, { step: s.step, stepName: s.stepName })}`)
}
}
return ctx.fail(
validation.steps.flatMap(s => s.errors).map(e => e.message).join('; '),
{ enCode: opts.enCode, fullName: opts.fullName },
)
}
log.info(`✓ step machine 校验通过 (${validation.steps.length}/${validation.steps.length})`)
const body = {
fullName: opts.fullName,
enCode: opts.enCode,
category: opts.category || '1',
enabledMark: 1,
sortCode: 0,
...(opts.content ? { content: opts.content } : {}),
}
// web 端真实端点: /api/message/MessageTemplateConfig
const r = await c.postJson<{ id?: string }>('/message/MessageTemplateConfig', body)
if (r.code !== 200) {
const enhanced = enhanceFromBackend(r.msg || '', 'msgTemplate', { entity: 'msg' })
log.error(` ${formatErrorLine(enhanced, { step: 1, stepName: '后端API' })}`)
return ctx.fail(r.msg || `code=${r.code}`, { enCode: opts.enCode, fullName: opts.fullName })
}
let id = r.data?.id
if (!id) {
const listR = await c.get<{ list: { id: string; enCode: string; fullName: string }[] }>(
'/message/MessageTemplateConfig',
{ keyword: opts.enCode, pageSize: '50' },
)
const found = (listR.data?.list || []).find(
(d) => d.enCode === opts.enCode || d.fullName === opts.fullName,
)
id = found?.id
}
log.ok(`✓ 消息模板创建成功 id=${id}`)
if (opts.json) printJson({ ...(r.data as object), id })
ctx.success({ id, enCode: opts.enCode, fullName: opts.fullName, category: opts.category })
} catch (e) {
ctx.fail(`msg templateCreate 失败: ${(e as Error).message?.slice(0, 200)}`)
}
})
})
installHelpHook(cmd, {
+29 -13
View File
@@ -9,8 +9,9 @@ import { JnpfClient } from '../../core/client.ts'
import { installHelpHook } from '../../core/help.ts'
import { log } from '../../core/log.ts'
import { initWebapi, loadMsgTemplate } from '../../webapi/message.ts'
import { setupOutputFilter, addOutputOptions, wrapCommand } from '../_output_filter.ts' // v2.1+ 输出过滤; v2.2+ wrapCommand 收口 5c
interface Opts { id: string; yes?: boolean }
interface Opts { id: string; yes?: boolean; json?: boolean; verbose?: boolean }
export function buildMsgTemplateDeleteCommand(): Command {
const cmd = new Command('templateDelete')
@@ -18,20 +19,35 @@ export function buildMsgTemplateDeleteCommand(): Command {
.description('消息模板删除 (硬删, v0.11+ webapi)')
.requiredOption('--id <id>', '模板 id')
.option('--yes', '跳过确认', false)
addOutputOptions(cmd)
.action(async (opts: Opts) => {
if (!opts.yes) {
log.warn(`即将删除模板 id=${opts.id}, 加 --yes 跳过确认`)
process.exit(2)
}
setupOutputFilter(opts)
const c = await JnpfClient.fromToken()
await initWebapi(c)
log.info(`loadMsgTemplate.delMsgTemplate (id=${opts.id})`)
const r = (await loadMsgTemplate.delMsgTemplate(opts.id)) as { code?: number; msg?: string }
if (r.code !== 200 && r.code !== undefined) {
log.error(`msg templateDelete 失败: ${r.msg}`)
process.exit(1)
}
log.ok(`✓ 模板已删除 (id=${opts.id})`)
await wrapCommand({
command: 'msg templateDelete',
appCode: c.appCode,
startTime: Date.now(),
verbose: opts.verbose,
json: opts.json,
fixes: [],
}, async (ctx) => {
try {
if (!opts.yes) {
return ctx.fail(`即将删除模板 id=${opts.id}, 加 --yes 跳过确认`, { id: opts.id })
}
await initWebapi(c)
log.info(`loadMsgTemplate.delMsgTemplate (id=${opts.id})`)
const r = (await loadMsgTemplate.delMsgTemplate(opts.id)) as { code?: number; msg?: string }
if (r.code !== 200 && r.code !== undefined) {
return ctx.fail(`msg templateDelete 失败: ${r.msg}`, { id: opts.id, code: r.code })
}
log.ok(`✓ 模板已删除 (id=${opts.id})`)
ctx.success({ id: opts.id })
} catch (e) {
ctx.fail(`msg templateDelete 失败: ${(e as Error).message?.slice(0, 200)}`)
}
})
})
installHelpHook(cmd, {
purpose: '消息模板删除',
+39 -22
View File
@@ -9,6 +9,7 @@ import { JnpfClient } from '../../core/client.ts'
import { installHelpHook } from '../../core/help.ts'
import { log, printJson } from '../../core/log.ts'
import { initWebapi, loadMsgTemplate } from '../../webapi/message.ts'
import { setupOutputFilter, addOutputOptions, wrapCommand } from '../_output_filter.ts' // v2.1+ 输出过滤; v2.2+ wrapCommand 收口 5c
interface Opts {
id: string
@@ -18,6 +19,8 @@ interface Opts {
content?: string
enabledMark?: string
json?: boolean
/** v2.1+ (2026-07-21): --verbose, addOutputOptions 注入 */
verbose?: boolean
}
export function buildMsgTemplateUpdateCommand(): Command {
@@ -30,30 +33,44 @@ export function buildMsgTemplateUpdateCommand(): Command {
.option('--category <c>', '分类 (1=系统 2=业务)')
.option('--content <text>', '模板内容')
.option('--enabledMark <0|1>', '启用/禁用')
.option('--json', '原始 JSON 输出', false)
addOutputOptions(cmd)
.action(async (opts: Opts) => {
setupOutputFilter(opts)
const c = await JnpfClient.fromToken()
await initWebapi(c)
const orig = (await loadMsgTemplate.getInfo(opts.id)) as Record<string, unknown>
if (!orig) {
log.error(`msg templateUpdate 失败: 拉原模板 id=${opts.id} 失败`)
process.exit(1)
}
const body: Record<string, unknown> = { ...orig, id: opts.id }
if (opts.fullName) body.fullName = opts.fullName
if (opts.enCode) body.enCode = opts.enCode
if (opts.category) body.category = opts.category
if (opts.content) body.content = opts.content
if (opts.enabledMark !== undefined) body.enabledMark = parseInt(opts.enabledMark, 10)
log.info(`loadMsgTemplate.update (id=${opts.id})`)
const r = (await loadMsgTemplate.update(body)) as { code?: number; msg?: string; data?: unknown }
if (r.code !== 200 && r.code !== undefined) {
log.error(`msg templateUpdate 失败 (code=${r.code}): ${r.msg}`)
if (opts.json) printJson(r)
process.exit(1)
}
log.ok(`✓ 模板更新成功 (id=${opts.id})`)
if (opts.json) printJson(r.data)
await wrapCommand({
command: 'msg templateUpdate',
appCode: c.appCode,
startTime: Date.now(),
verbose: opts.verbose,
json: opts.json,
fixes: [],
}, async (ctx) => {
try {
await initWebapi(c)
const orig = (await loadMsgTemplate.getInfo(opts.id)) as Record<string, unknown>
if (!orig) {
return ctx.fail(`msg templateUpdate 失败: 拉原模板 id=${opts.id} 失败`, { id: opts.id })
}
const body: Record<string, unknown> = { ...orig, id: opts.id }
if (opts.fullName) body.fullName = opts.fullName
if (opts.enCode) body.enCode = opts.enCode
if (opts.category) body.category = opts.category
if (opts.content) body.content = opts.content
if (opts.enabledMark !== undefined) body.enabledMark = parseInt(opts.enabledMark, 10)
log.info(`loadMsgTemplate.update (id=${opts.id})`)
const r = (await loadMsgTemplate.update(body)) as { code?: number; msg?: string; data?: unknown }
if (r.code !== 200 && r.code !== undefined) {
if (opts.json) printJson(r)
return ctx.fail(`msg templateUpdate 失败 (code=${r.code}): ${r.msg}`, { id: opts.id, code: r.code })
}
log.ok(`✓ 模板更新成功 (id=${opts.id})`)
if (opts.json) printJson(r.data)
ctx.success({ id: opts.id })
} catch (e) {
ctx.fail(`msg templateUpdate 失败: ${(e as Error).message?.slice(0, 200)}`)
}
})
})
installHelpHook(cmd, {
purpose: '消息模板修改',