From 4a3dd4b9d57bebbb6fa1c2a3ea62391a582fa57b Mon Sep 17 00:00:00 2001 From: ssw Date: Thu, 23 Jul 2026 13:56:29 +0800 Subject: [PATCH] auto(14b5ea1b9bd9): session end @ 2026-07-23T13:56:28 (+4f) --- src/commands/di/list.ts | 59 +++++++++++++++++++++++++++++++++-- src/commands/form/create.ts | 8 ++++- src/commands/form/info.ts | 61 +++++++++++++++++++++++++++++++++++-- src/commands/test/run.ts | 53 +++++++++++++++++++++++++++++--- 4 files changed, 172 insertions(+), 9 deletions(-) diff --git a/src/commands/di/list.ts b/src/commands/di/list.ts index 6148bd9..1bd729c 100644 --- a/src/commands/di/list.ts +++ b/src/commands/di/list.ts @@ -8,7 +8,7 @@ import { Command } from 'commander' import { JnpfClient } from '../../core/client.ts' import { installHelpHook } from '../../core/help.ts' import { log, printJson } from '../../core/log.ts' -import { initWebapi, getDataInterfaceSelector } from '../../webapi/di.ts' +import { initWebapi, getDataInterfaceSelector, getDataInterfaceList } from '../../webapi/di.ts' import { setupOutputFilter, addOutputOptions, wrapCommand } from '../_output_filter.ts' // v2.1+ 输出过滤; v2.2+ wrapCommand 收口 5c /** @@ -44,6 +44,25 @@ export function flattenDataInterfaceSelector(input: unknown): DataInterfaceListI return result } +/** + * Selector leaves typically omit `enCode` to keep the picker tree small. The + * paged list endpoint returns it but is filtered by category on the platform. + * When an id→enCode map is provided (e.g. fetched from the paged list), fill + * missing enCode on each leaf; existing enCode is preserved. + */ +export function enrichDataInterfaceListWithEnCode( + input: unknown, + idToEnCode: Record, +): DataInterfaceListItem[] { + const leaves = flattenDataInterfaceSelector(input) + return leaves.map((leaf) => { + if (leaf.enCode || !leaf.id) return leaf + const enCode = idToEnCode[String(leaf.id)] + if (!enCode) return leaf + return { ...leaf, enCode } + }) +} + interface Opts { json?: boolean; keyword?: string; currentPage?: string; pageSize?: string; verbose?: boolean } export function buildDiListCommand(): Command { @@ -70,7 +89,43 @@ export function buildDiListCommand(): Command { log.info('loadDataInterface.getDataInterfaceSelector (category-independent)') const selector = await getDataInterfaceSelector() - const all = flattenDataInterfaceSelector(selector) + const leaves = flattenDataInterfaceSelector(selector) + + // Collect every category folder id from the selector tree. The paged + // list endpoint requires a category; we issue one paged request per + // category and merge the resulting id→enCode map. This is more + // thorough than a single category pick and matches the selector + // coverage that the picker tree itself provides. + const categoryIds = (() => { + const ids: string[] = [] + const walk = (nodes: unknown): void => { + for (const n of (Array.isArray(nodes) ? nodes : []) as Array>) { + if (typeof n?.id === 'string' && (n.category === '0' || n.category === 0)) { + if (!ids.includes(n.id)) ids.push(n.id) + } + if (Array.isArray(n?.children)) walk(n.children) + } + } + const tree = Array.isArray(selector) ? selector : (selector as { data?: unknown } | null)?.data + walk(tree) + return ids + })() + const idToEnCode: Record = {} + for (const catId of categoryIds) { + try { + const paged = await getDataInterfaceList({ + currentPage: 1, + pageSize: 1000, + category: catId, + }) as { list?: Array<{ id?: string; enCode?: string }> } + for (const item of paged.list || []) { + if (item?.id && item.enCode) idToEnCode[String(item.id)] = String(item.enCode) + } + } catch (e) { + log.warn(`enCode 回查失败 (cat=${catId}, ${(e as Error).message?.slice(0, 80)}), 跳过该分类`) + } + } + const all = enrichDataInterfaceListWithEnCode(selector, idToEnCode) const keyword = opts.keyword?.trim().toLowerCase() const filtered = keyword ? all.filter((item) => `${item.enCode || ''} ${item.fullName || ''}`.toLowerCase().includes(keyword)) diff --git a/src/commands/form/create.ts b/src/commands/form/create.ts index 82c8bbb..ef96e05 100644 --- a/src/commands/form/create.ts +++ b/src/commands/form/create.ts @@ -96,6 +96,12 @@ interface CreateOpts { * smartProps. Keep the post-processing at the final boundary so dictionary, * numeric-range, validation and relation props cannot be discarded. */ +export function shouldRunFinalSmartProps(opts: CreateOpts): boolean { + if (!opts) return false + if (!opts.ai && !opts.table) return false + return Boolean((opts as any).__aiBuildFormData) +} + async function applyFinalSmartProps( c: JnpfClient, buildResult: { formData: string }, @@ -710,7 +716,7 @@ export function buildFormCreateCommand(): Command { // --table is the final form-data producer when combined with --ai. // Re-run smartProps here so the table rebuild cannot erase dictionary, // range, validation or relation metadata inferred by the AI pass. - if (opts.table && (opts as any).__aiBuildFormData) { + if (shouldRunFinalSmartProps(opts)) { await applyFinalSmartProps(c, (opts as any).__aiBuildFormData, ctx) } // 校验必填 diff --git a/src/commands/form/info.ts b/src/commands/form/info.ts index ec87d27..d5c3d28 100644 --- a/src/commands/form/info.ts +++ b/src/commands/form/info.ts @@ -35,6 +35,44 @@ function tryParseJsonString(value: unknown): unknown { } } +/** + * Flatten formData.fields into a list of binding records. Each record is + * `{field, jnpfKey, label, dataType, dictionaryType, required}` so callers + * can assert dictionary bindings without parsing the JNPF nested JSON. + */ +export interface FormBinding { + field: string + jnpfKey?: string + label?: string + dataType?: string | null + dictionaryType?: string | null + required?: boolean + modelId?: string | null +} + +export function projectFormBindings(input: Record | null | undefined): FormBinding[] { + const formData = (input && typeof input === 'object' ? (input as any).formData : null) + if (!formData) return [] + const fields = Array.isArray(formData.fields) ? formData.fields : [] + const out: FormBinding[] = [] + for (const field of fields) { + if (!field || typeof field !== 'object') continue + const vmodel = (field as any).__vModel__ + const cfg = (field as any).__config__ || {} + if (!vmodel || !cfg) continue + out.push({ + field: vmodel, + jnpfKey: cfg.jnpfKey, + label: cfg.label, + dataType: cfg.dataType ?? null, + dictionaryType: cfg.dictionaryType ?? null, + required: !!cfg.required, + modelId: cfg.modelId ?? null, + }) + } + return out +} + export function buildFormInfoCommand(): Command { const cmd = new Command('info') .description('拉表单完整 schema (含 formData/columnData/tables)') @@ -42,8 +80,9 @@ export function buildFormInfoCommand(): Command { .option('--summary', '只打概要 (id/enCode/state/type/webType/dbLinkId/字段数)', false) // --json 含特殊语义 (BS-新1 解码 formData escaped JSON), 跟 addOutputOptions 的通用 --json 不能混 .option('--json', 'BS-新1: 解码 formData/columnData/tables escaped JSON, 直接 jq (无需 Python 二次解码)', false) + .option('--bindings', '打印 formData 字段的拍平绑定 (field/jnpfKey/dataType/dictionaryType/required)', false) .option('--verbose', '完整输出 (含 step machine、API 调用等过程日志)', false) - .action(async (id: string, opts: { summary?: boolean; json?: boolean; verbose?: boolean }) => { + .action(async (id: string, opts: { summary?: boolean; json?: boolean; verbose?: boolean; bindings?: boolean }) => { // 输出过滤: 默认静默 setupOutputFilter(opts) const c = await JnpfClient.fromToken() @@ -65,7 +104,9 @@ export function buildFormInfoCommand(): Command { enCode: data.enCode, fullName: data.fullName, state: data.state, - mode: opts.summary ? 'summary' : (opts.json ? 'json-decoded' : 'json-raw'), + mode: opts.summary ? 'summary' + : opts.bindings ? 'bindings' + : (opts.json ? 'json-decoded' : 'json-raw'), } if (opts.summary) { const tables = (Array.isArray(data.tables) ? data.tables : []) as Array<{ table?: string }> @@ -77,6 +118,22 @@ export function buildFormInfoCommand(): Command { log.dim(`tables : ${tables.length} (${tables.map((t) => t.table).join(', ')})`) return ctx.success(summary) } + if (opts.bindings) { + const decoded = (() => { + const copy = { ...data } as Record + for (const key of ['formData', 'columnData', 'appColumnData', 'tables']) { + if (key in copy) copy[key] = tryParseJsonString(copy[key]) + } + return copy + })() + printJson({ + id: data.id, + enCode: data.enCode, + fullName: data.fullName, + bindings: projectFormBindings(decoded), + }) + return ctx.success(summary) + } // v0.13+ BS-新1: --json 模式解 formData/columnData/tables escaped JSON if (opts.json) { const decoded = { ...data } as Record diff --git a/src/commands/test/run.ts b/src/commands/test/run.ts index 7100bcc..d66b8bb 100644 --- a/src/commands/test/run.ts +++ b/src/commands/test/run.ts @@ -633,6 +633,8 @@ async function buildSampleData( const sysKeys = new Set(['createUser', 'createTime', 'modifyUser', 'modifyTime', 'currOrganize', 'currPosition', 'billRule']) // 子表缓存 (避免同 enCode 多次查询) const relationCache = new Map() + // 字典缓存 (避免同 dictionaryType 多次查询) + const dictionaryCache = new Map>>() for (const f of fields) { const vmodel = f.__vModel__ @@ -683,13 +685,11 @@ async function buildSampleData( continue } if (key === 'select') { - const optsCfg = cfg.options || [] - sample[vmodel] = optsCfg[0]?.value || '1' + sample[vmodel] = await pickChoiceValue(client, dictionaryCache, cfg, vmodel) continue } if (key === 'radio' || key === 'switch' || key === 'checkbox') { - const optsCfg = cfg.options || [] - sample[vmodel] = optsCfg[0]?.value || '1' + sample[vmodel] = await pickChoiceValue(client, dictionaryCache, cfg, vmodel) continue } if (key === 'userSelect' || key === 'organizeSelect' || key === 'posSelect') { @@ -756,6 +756,51 @@ async function lookupRelationFormId( } } +/** + * Pick a sample value for choice-style controls (select / radio / switch / checkbox). + * Priority: + * 1. JNPF dictionary binding (`dataType === 'dictionary'` + `dictionaryType` id) → first item's enCode + * 2. static options list → first option's value + * 3. fallback `'1'` (test fixture safety) + */ +export async function pickChoiceValue( + client: JnpfClient, + cache: Map>>, + cfg: { options?: Array<{ value: string }>; dataType?: string; dictionaryType?: string; propsValue?: string }, + vmodel: string, +): Promise { + const optsCfg = cfg.options || [] + const optValue = optsCfg[0]?.value + if (cfg.dataType === 'dictionary' && cfg.dictionaryType) { + let items = cache.get(cfg.dictionaryType) + if (!items) { + try { + const { initWebapi, loadDict } = await import('../../webapi/dict.ts') + await initWebapi(client) + const r = (await loadDict.getDictionaryDataSelector(cfg.dictionaryType)) as unknown + const raw = Array.isArray(r) ? r : (r as { data?: unknown })?.data + const flat: Array> = [] + const walk = (nodes: unknown): void => { + for (const n of (Array.isArray(nodes) ? nodes : []) as Array>) { + if (typeof n?.enCode === 'string') flat.push(n) + if (Array.isArray(n?.children)) walk(n.children) + } + } + walk(raw) + items = flat + cache.set(cfg.dictionaryType, items) + } catch { + items = [] + cache.set(cfg.dictionaryType, items) + } + } + const first = items.find((item) => item?.enCode) + if (first && typeof first.enCode === 'string') return first.enCode + } + if (typeof optValue === 'string' && optValue) return optValue + return '1' +} + /** * 通过 enCode 查 formId (缓存于本次 run 周期内). */