refactor(v0.11+): dict 4 子命令改用 webapi 镜像

Phase 1 审计 (cmd-review/dict.md) 标记 P0 必做:
- webapi/dict.ts 已镜像 15 函数, 使用率 0%
- 4 子命令 (typeList/typeCreate/dataList/dataCreate) 100% 绕 webapi

修复:
- typeList → loadDict.getDictionaryType (客户端 keyword 过滤)
- typeCreate → loadDict.createDictionaryType + createDictionaryData (循环)
- dataList → loadDict.getDictionaryDataList / getDictionaryAll
- dataCreate → loadDict.createDictionaryData (循环)

收益:
- 消除 ~80 行 c.get/postJson 硬编码
- 自动同步 web 端字段变更 (如 enabledMark 默认值)
- 与 v0.11+ 镜像架构对齐

Co-Authored-By: Claude <[email protected]>
This commit is contained in:
sswandClaude committed 2026-06-25 15:07:47 +08:00
1 parent 7a34ee6b5d
commit 20ddbfbe9c
4 files changed
+105 -50

No files matched your search

+39 -17
View File
@@ -1,18 +1,14 @@
/**
* commands/dict/datacreate.ts — 往已存在字典类型追加字典项
*
* API: POST /api/system/DictionaryData
*
* 用法:
* dacli dict data-create --typeId=443409825091648261 --enCode=new_item --fullName="新项"
* dacli dict data-create --typeId=X --items='[{"enCode":"x1","fullName":"X1"}]'
*
* 补充 type-create 用法, 单条/批量追加字典项.
* v0.11+ Phase 2 (2026-06-25): 改用 webapi 镜像 (loadDict.createDictionaryData)
* 替代 v0.10 直接 c.postJson('/system/DictionaryData')
*/
import { Command } from 'commander'
import { JnpfClient } from '../../core/client.ts'
import { installAiHelpHook } from '../../core/help.ts'
import { log, printJson } from '../../core/log.ts'
import { initWebapi, loadDict } from '../../webapi/dict.ts'
interface Opts {
typeId: string
@@ -46,18 +42,34 @@ export function buildDictDataCreateCommand(): Command {
}
const c = await JnpfClient.fromToken()
await initWebapi(c)
let items: Array<{ enCode: string; fullName: string; sortCode?: number; parentId?: string }>
if (opts.items) {
try { items = JSON.parse(opts.items) }
catch (e) { log.error(`--items JSON 解析失败: ${(e as Error).message}`); process.exit(2) }
if (!Array.isArray(items) || items.length === 0) { log.error('--items 必是非空数组'); process.exit(2) }
try {
items = JSON.parse(opts.items)
} catch (e) {
log.error(`--items JSON 解析失败: ${(e as Error).message}`)
process.exit(2)
}
if (!Array.isArray(items) || items.length === 0) {
log.error('--items 必是非空数组')
process.exit(2)
}
} else {
items = [{ enCode: opts.enCode!, fullName: opts.fullName!, sortCode: Number(opts.sortCode || '0'), parentId: opts.parentId || '0' }]
items = [
{
enCode: opts.enCode!,
fullName: opts.fullName!,
sortCode: Number(opts.sortCode || '0'),
parentId: opts.parentId || '0',
},
]
}
log.info(`POST /system/DictionaryData typeId=${opts.typeId} count=${items.length}`)
let ok = 0, fail = 0
log.info(`loadDict.createDictionaryData typeId=${opts.typeId} count=${items.length}`)
let ok = 0,
fail = 0
const ids: string[] = []
for (const it of items) {
const body = {
@@ -67,9 +79,19 @@ export function buildDictDataCreateCommand(): Command {
parentId: it.parentId || '0',
sortCode: it.sortCode ?? 0,
}
const r = await c.postJson<{ id: string }>('/system/DictionaryData', body)
if (r.code === 200) { ok++; ids.push(r.data?.id || ''); log.ok(` ✓ ${it.enCode} → ${it.fullName} id=${r.data?.id || ''}`) }
else { fail++; log.warn(` ✗ ${it.enCode} (code=${r.code}): ${r.msg}`) }
const r = (await loadDict.createDictionaryData(body)) as {
id?: string
code?: number
msg?: string
}
if (r.code === 200 || r.code === undefined) {
ok++
ids.push(r.id || '')
log.ok(` ✓ ${it.enCode} → ${it.fullName} id=${r.id || ''}`)
} else {
fail++
log.warn(` ✗ ${it.enCode} (code=${r.code}): ${r.msg}`)
}
}
log.info(`完成: 成功 ${ok}, 失败 ${fail}`)
if (opts.json) printJson({ typeId: opts.typeId, success: ok, failed: fail, ids })
@@ -87,7 +109,7 @@ export function buildDictDataCreateCommand(): Command {
},
example: 'dacli dict dataCreate --typeId=443409825091648261 --enCode=new --fullName="新项"',
source: { api: 'POST /api/system/DictionaryData' },
notes: ['解决盲点 41: 往已有字典类型追加字典项'],
notes: ['v0.11+ 改用 webapi 镜像 (loadDict.createDictionaryData)', '解决盲点 41: 往已有字典类型追加字典项'],
})
return cmd
}
+15 -6
View File
@@ -1,12 +1,15 @@
/**
* commands/dict/datalist.ts — 字典数据列表
*
* API: GET /api/system/DictionaryData/{typeId}/Data 或 /All
* v0.11+ Phase 2 (2026-06-25): 改用 webapi 镜像
* - loadDict.getDictionaryDataList({typeId}) 替代 c.get('/system/DictionaryData/{typeId}/Data')
* - loadDict.getDictionaryAll() 替代 c.get('/system/DictionaryData/All')
*/
import { Command } from 'commander'
import { JnpfClient } from '../../core/client.ts'
import { installAiHelpHook } from '../../core/help.ts'
import { log, printJson } from '../../core/log.ts'
import { initWebapi, loadDict } from '../../webapi/dict.ts'
interface Opts {
typeId?: string
@@ -23,18 +26,23 @@ export function buildDictDataListCommand(): Command {
.option('--json', '原始 JSON 输出', false)
.action(async (opts: Opts) => {
const c = await JnpfClient.fromToken()
await initWebapi(c)
let path: string
let data: unknown
if (opts.all || !opts.typeId) {
path = '/system/DictionaryData/All'
data = await loadDict.getDictionaryAll()
} else {
path = `/system/DictionaryData/${opts.typeId}/Data`
data = await loadDict.getDictionaryDataList({ typeId: opts.typeId })
}
const r = await c.get<unknown>(path)
if (r.code !== 200) { log.error(`dict datalist 失败: ${r.msg}`); process.exit(1) }
if (opts.json) { printJson(r.data); return }
const list = (Array.isArray(r.data) ? r.data : (r.data as { list?: unknown[] })?.list) || []
if (opts.json) {
printJson(data)
return
}
const list = (Array.isArray(data) ? data : (data as { list?: unknown[] })?.list) || []
log.info(`${path} 共 ${list.length} 条`)
console.log('ENCODE FULL_NAME SORT')
console.log('ENCODE'.padEnd(21) + 'FULL_NAME'.padEnd(22) + 'SORT')
console.log('-'.repeat(60))
for (const d of list as Record<string, unknown>[]) {
console.log(`${String(d.enCode || d.id || '').padEnd(21)}${String(d.fullName || '').padEnd(22)}${d.sortCode || ''}`)
@@ -47,6 +55,7 @@ export function buildDictDataListCommand(): Command {
optional: { '--typeId': '字典类型 id', '--all': '拿全部字典' },
example: 'dacli dict datalist --typeId=<id>',
source: { api: 'GET /api/system/DictionaryData/{typeId}/Data 或 /All' },
notes: ['v0.11+ 改用 webapi 镜像 (getDictionaryDataList + getDictionaryAll)'],
})
return cmd
}
+26 -18
View File
@@ -1,18 +1,15 @@
/**
* commands/dict/typecreate.ts — 创建字典类型 (含 items)
*
* API: POST /api/system/DictionaryType
* POST /api/system/DictionaryData (per item)
*
* 用法:
* dacli dict type-create --enCode=tds_space_status --fullName="空间状态" --items='[{"enCode":"draft","fullName":"草稿"},{"enCode":"running","fullName":"运行中"}]'
*
* 解决盲点 41 (TS 版 dict 只读). 端到端: 1 条命令建类型 + N 条字典项.
* v0.11+ Phase 2 (2026-06-25): 改用 webapi 镜像
* - loadDict.createDictionaryType (替代 c.postJson DictionaryType)
* - loadDict.createDictionaryData (替代 c.postJson DictionaryData)
*/
import { Command } from 'commander'
import { JnpfClient } from '../../core/client.ts'
import { installAiHelpHook } from '../../core/help.ts'
import { log, printJson } from '../../core/log.ts'
import { initWebapi, loadDict } from '../../webapi/dict.ts'
interface Opts {
enCode: string
@@ -43,8 +40,9 @@ export function buildDictTypeCreateCommand(): Command {
process.exit(2)
}
const c = await JnpfClient.fromToken()
await initWebapi(c)
// 1. 建字典类型
// 1. 建字典类型 (走 webapi)
const typeBody: Record<string, unknown> = {
fullName: opts.fullName,
enCode: opts.enCode,
@@ -53,16 +51,16 @@ export function buildDictTypeCreateCommand(): Command {
isTree: Number(opts.isTree || '0'),
sortCode: Number(opts.sortCode || '0'),
}
log.info(`POST /system/DictionaryType enCode=${opts.enCode}`)
const tr = await c.postJson<{ id: string }>('/system/DictionaryType', typeBody)
if (tr.code !== 200) {
log.info(`loadDict.createDictionaryType enCode=${opts.enCode}`)
const tr = (await loadDict.createDictionaryType(typeBody)) as { id: string; code?: number; msg?: string }
if (tr.code !== undefined && tr.code !== 200) {
log.error(`建字典类型失败 (code=${tr.code}): ${tr.msg}`)
process.exit(1)
}
const typeId = tr.data?.id || '(unknown)'
const typeId = tr.id || '(unknown)'
log.ok(`✓ 字典类型创建: id=${typeId} enCode=${opts.enCode}`)
// 2. 批量建字典项 (--items)
// 2. 批量建字典项 (--items, 走 webapi)
if (opts.items) {
let parsed: Array<{ enCode: string; fullName: string; sortCode?: number; parentId?: string }>
try {
@@ -86,9 +84,18 @@ export function buildDictTypeCreateCommand(): Command {
// v0.11 修复: web Form.vue:50-54 enabledMark Switch 默认 1
enabledMark: (item as any).enabledMark ?? 1,
}
const r = await c.postJson<{ id: string }>('/system/DictionaryData', body)
if (r.code === 200) { ok++; log.ok(` ✓ ${item.enCode} → ${item.fullName}`) }
else { fail++; log.warn(` ✗ ${item.enCode} (code=${r.code}): ${r.msg}`) }
const r = (await loadDict.createDictionaryData(body)) as {
id?: string
code?: number
msg?: string
}
if (r.code === 200 || r.code === undefined) {
ok++
log.ok(` ✓ ${item.enCode} → ${item.fullName}`)
} else {
fail++
log.warn(` ✗ ${item.enCode} (code=${r.code}): ${r.msg}`)
}
}
log.info(`字典项: 成功 ${ok}, 失败 ${fail}`)
}
@@ -110,9 +117,10 @@ export function buildDictTypeCreateCommand(): Command {
'--isTree': '是否树形 (默认 0)',
'--sortCode': '排序 (默认 0)',
},
example: 'dacli dict typeCreate --enCode=tds_space_status --fullName="空间状态" --items=\'[{"enCode":"draft","fullName":"草稿"},{"enCode":"running","fullName":"运行中"}]\'',
example:
'dacli dict typeCreate --enCode=tds_space_status --fullName="空间状态" --items=\'[{"enCode":"draft","fullName":"草稿"},{"enCode":"running","fullName":"运行中"}]\'',
source: { api: 'POST /api/system/DictionaryType + POST /api/system/DictionaryData' },
notes: ['解决盲点 41 (TS 版 dict 只读): 端到端建类型+字典项 1 条命令'],
notes: ['v0.11+ 改用 webapi 镜像 (loadDict.createDictionaryType + createDictionaryData)'],
})
return cmd
}
+25 -9
View File
@@ -1,12 +1,16 @@
/**
* commands/dict/typelist.ts — 字典类型列表
*
* API: GET /api/system/DictionaryType
* v0.11+ Phase 2 (2026-06-25): 改用 webapi 镜像 (loadDict.getDictionaryType)
* 替代 v0.10 直接 c.get('/system/DictionaryType')
*
* 注: web getDictionaryType() 不支持服务端 keyword 过滤, 改在客户端 filter
*/
import { Command } from 'commander'
import { JnpfClient } from '../../core/client.ts'
import { installAiHelpHook } from '../../core/help.ts'
import { log, printJson } from '../../core/log.ts'
import { initWebapi, loadDict } from '../../webapi/dict.ts'
interface Opts { json?: boolean; keyword?: string }
@@ -14,16 +18,27 @@ export function buildDictTypeListCommand(): Command {
const cmd = new Command('typeList')
.alias('type-list')
.description('字典类型列表')
.option('--keyword <k>', '按编码/名称搜索')
.option('--keyword <k>', '按编码/名称搜索 (客户端过滤)')
.option('--json', '原始 JSON 输出', false)
.action(async (opts: Opts) => {
const c = await JnpfClient.fromToken()
const r = await c.get<unknown>('/system/DictionaryType', { keyword: opts.keyword })
if (r.code !== 200) { log.error(`dict typelist 失败: ${r.msg}`); process.exit(1) }
if (opts.json) { printJson(r.data); return }
const list = (r.data as { list?: Record<string, unknown>[] })?.list || []
await initWebapi(c)
const data = (await loadDict.getDictionaryType()) as { list?: Record<string, unknown>[] }
if (opts.json) {
printJson(data)
return
}
let list = data?.list || []
if (opts.keyword) {
const kw = opts.keyword.toLowerCase()
list = list.filter(
(t) =>
String(t.enCode || '').toLowerCase().includes(kw) ||
String(t.fullName || '').toLowerCase().includes(kw),
)
}
log.info(`共 ${list.length} 个字典类型`)
console.log('ID ENCODE FULL_NAME')
console.log('ID'.padEnd(36) + 'ENCODE'.padEnd(21) + 'FULL_NAME')
console.log('-'.repeat(80))
for (const t of list) {
console.log(`${String(t.id || '').padEnd(36)}${String(t.enCode || '').padEnd(21)}${t.fullName || ''}`)
@@ -33,9 +48,10 @@ export function buildDictTypeListCommand(): Command {
installAiHelpHook(cmd, {
purpose: '列出所有字典类型 (e.g. 性别/状态/教育程度 等下拉字典).',
required: {},
optional: { '--keyword': '搜索关键字' },
optional: { '--keyword': '搜索关键字 (客户端过滤, 因 web 端不支持服务端)' },
example: 'dacli dict typelist',
source: { api: 'GET /api/system/DictionaryType' },
source: { api: 'GET /api/system/DictionaryType', ref: 'web/.../api/systemData/dictionary.ts:9 getDictionaryType()' },
notes: ['v0.11+ 改用 webapi 镜像', 'BS-47 dict 全平台唯一, 创前必跑此命令查重'],
})
return cmd
}