From a2bf3712cbc02385d9b32fc3badc5f08cc3f008a Mon Sep 17 00:00:00 2001 From: ssw Date: Wed, 1 Jul 2026 17:07:28 +0800 Subject: [PATCH] auto(284e8d004cbb): session end @ 2026-07-01T17:07:27 (+1f) --- src/webapi/datascreen.ts | 267 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 265 insertions(+), 2 deletions(-) diff --git a/src/webapi/datascreen.ts b/src/webapi/datascreen.ts index f6db718..42757a0 100644 --- a/src/webapi/datascreen.ts +++ b/src/webapi/datascreen.ts @@ -17,14 +17,16 @@ * 特殊处理: visual.js 用 `import request from '../axios'` (相对路径, 不是 defHttp), * loadWebModule 默认规则不替换. 不用 loader, 直接读 + 手动 replace + dynamic import. */ -import { readFileSync, writeFileSync, mkdirSync } from 'node:fs' +import { readFileSync, writeFileSync, mkdirSync, readdirSync, existsSync } from 'node:fs' import { tmpdir } from 'node:os' -import { join, dirname } from 'node:path' +import { join, dirname, basename } from 'node:path' import { pathToFileURL } from 'node:url' import { JnpfClient } from '../core/client.ts' import { initDatascreenShim } from './_datascreen_shim.ts' const DATASCREEN_SRC = '/home/ubuntu/ccWorkDir/jnpf/前端/jnpf-web-datascreen-vue3/src/api/visual.js' +const DATASCREEN_OPTION_CONFIG_SRC = '/home/ubuntu/ccWorkDir/jnpf/前端/jnpf-web-datascreen-vue3/src/option/config.js' +const DATASCREEN_OPTION_COMPONENTS_DIR = '/home/ubuntu/ccWorkDir/jnpf/前端/jnpf-web-datascreen-vue3/src/option/components' const SHIM_DIR = dirname(new URL(import.meta.url).pathname) const DATASCREEN_SHIM_PATH = join(SHIM_DIR, '_datascreen_shim.ts') @@ -144,3 +146,264 @@ export async function updateObj(data: Record): Promise export async function delObj(id: string): Promise { return loadDatascreen.delObj(id) } + +// ===================================================================== +// Phase 1+2: 镜像 option/config.js (基础配置) + option/components/*.vue (组件元数据) +// 详见 dacli/docs/cmd-review/v0.13-webapi-form-workflow-refactor.md §8.2 +// ===================================================================== + +/** + * Phase 1: 镜像 jnpf-web-datascreen-vue3/src/option/config.js 第 35-72 行的 `config` 对象. + * + * 用途: dacli bigscreen construct 命令构造 save payload 时, config.detail = JSON.stringify(this.config), + * 这个 this.config 必须跟 web 设计器初始值一致 (web 端 `result.vue:50` 也 import { config as defaultConfig }) + * 跟 web 端 `result.vue:163` 的 `Object.assign(defaultConfig, json.detail)` 行为一致 — + * 即 defaultConfig 是基础模板, json.detail 是覆盖. + * + * 注: 38 组件 metadata (themeList/colorOption/tableOption/dicOption) 暂时省略, 那些是设计器面板的 dic data, + * 跟 save payload 无关 (save 只 save config + nav, 不 save 元数据). + * Phase 5+ 如果要镜像 `header.vue:200-211` 完整 formdata 构造, 还需要 themeList 等. + * + * 实现策略: 直接读 web 端 option/config.js, 提取 `export const config = { ... }` 字面量对象, 用 Function 解析. + * 跟 web 端 `result.vue:50` `import { config } from '@/option/config'` 行为一致 — 都是同一个对象. + */ +let cachedBaseConfig: Record | null = null +export function getBaseConfig(): Record { + if (cachedBaseConfig) return cachedBaseConfig + + // 简化兜底: web 端 option/config.js 第 35-72 行的默认 config 对象 (跟 shim 保持一致) + const fallback: Record = { + width: 1920, + height: 1080, + query: "function(){\n return window.$glob.params || {}\n}", + header: "function(){\n return window.$glob.params || {}\n}", + screen: 'x', + mark: { + show: false, + text: '', + fontSize: 20, + color: 'rgba(100,100,100,0.2)', + degree: -20 + }, + scale: 1, + backgroundImage: `/img/bg/bg.png`, + url: '', + group: [], + glob: [], + theme: {}, + themeId: '', + filters: {}, + style: '', + before: '', + overflow: false, + styles: { + show: false, + contrast: 100, + saturate: 100, + brightness: 100, + opacity: 100, + grayscale: 0, + hueRotate: 0, + invert: 0, + blur: 0 + }, + gradeShow: false, + gradeLen: 30, + } + + if (!existsSync(DATASCREEN_OPTION_CONFIG_SRC)) { + cachedBaseConfig = fallback + return cachedBaseConfig + } + + try { + const code = readFileSync(DATASCREEN_OPTION_CONFIG_SRC, 'utf-8') + // 提取 `export const config = { ... }` 字面量 (顶层 config 对象) + // 用括号配对法, 避免非贪婪 regex 在嵌套对象 (mark, styles) 处误终止 + const startIdx = code.indexOf('export const config = {') + if (startIdx === -1) { + cachedBaseConfig = fallback + return cachedBaseConfig + } + let depth = 0 + let bodyStart = -1 + let bodyEnd = -1 + for (let i = startIdx; i < code.length; i++) { + const ch = code[i] + if (ch === '{') { + if (depth === 0) bodyStart = i + 1 + depth++ + } else if (ch === '}') { + depth-- + if (depth === 0) { + bodyEnd = i + break + } + } + } + if (bodyStart === -1 || bodyEnd === -1) { + cachedBaseConfig = fallback + return cachedBaseConfig + } + const body = code.slice(bodyStart, bodyEnd) + // 解析: `width: 1920, ...` 转成 JS 对象 + // eslint-disable-next-line no-new-func + const parsed = Function(`return ({${body}})`)() + if (parsed && typeof parsed === 'object') { + cachedBaseConfig = parsed as Record + } else { + cachedBaseConfig = fallback + } + } catch (err) { + console.warn('[datascreen] getBaseConfig 解析失败, 用 fallback:', err) + cachedBaseConfig = fallback + } + return cachedBaseConfig +} + +/** + * Phase 2: 镜像 jnpf-web-datascreen-vue3/src/option/components/.vue 的 default data 段. + * + * 用途: dacli bigscreen construct 命令构造组件 entry (nav 数组元素) 时, 知道每个 prop 类型 + * (bar/line/pie/text/map/...) 的 default data 形状 (data/option/component/group/index 等). + * web 端 `build.vue` 通过 `import.meta.globEager` 加载 38 个 .vue, 然后拖拽时调 `initData()` + * 派生一个 nav entry. dacli 端不能跑 vue compiler, 只能: + * 1. 列目录 (fs.readdirSync) 找 .vue 文件 + * 2. 提取 ` 段 + const scriptMatch = code.match(/]*>([\s\S]*?)<\/script>/) + if (!scriptMatch) return null + const { name, dataKeys, hasData } = parseVueScript(scriptMatch[1]) + const result: ComponentDefaults = { prop, name, filePath, hasData, dataKeys, isOptionConfig: true } + componentFileCache.set(prop, result) + return result + } catch (err) { + console.warn(`[datascreen] getComponentDefaults(${prop}) 解析失败:`, err) + return null + } +} + +/** 列出 option/components/ 下所有 prop 名 (e.g. ['audio', 'bar', 'borderBox', ...]) */ +export function listComponentProps(): string[] { + return listComponentFiles() +}