auto(284e8d004cbb): session end @ 2026-07-01T17:07:27 (+1f)

This commit is contained in:
ssw committed 2026-07-01 17:07:28 +08:00
1 parent b43b03f83a
commit a2bf3712cb
1 file changed
+265 -2
+265 -2
View File
@@ -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<string, unknown>): Promise<unknown>
export async function delObj(id: string): Promise<unknown> {
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<string, unknown> | null = null
export function getBaseConfig(): Record<string, unknown> {
if (cachedBaseConfig) return cachedBaseConfig
// 简化兜底: web 端 option/config.js 第 35-72 行的默认 config 对象 (跟 shim 保持一致)
const fallback: Record<string, unknown> = {
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<string, unknown>
} else {
cachedBaseConfig = fallback
}
} catch (err) {
console.warn('[datascreen] getBaseConfig 解析失败, 用 fallback:', err)
cachedBaseConfig = fallback
}
return cachedBaseConfig
}
/**
* Phase 2: 镜像 jnpf-web-datascreen-vue3/src/option/components/<prop>.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. 提取 `<script>` 段的 `data() { return { ... } }` 内容
* 3. 简化元数据: hasData, name, prop, filePath
* 详细的 data() 解析留给 Phase 3 (constructConfigDetail) + Phase 5 (dacli bigscreen construct 命令)
* 一起做, 避免 Phase 2 提前膨胀.
*
* 返回值 schema (Phase 2 简化版):
* {
* prop: 'bar',
* name: 'bar' | 'echart-bar' | 'avue-echart-bar', // vue component name
* filePath: '/.../option/components/bar.vue',
* hasData: boolean, // 是否有 data() { return { ... } } 段
* dataKeys?: string[], // hasData=true 时, 列出 data 对象第一层 key (name/code/api/...)
* isOptionConfig: true, // 这是 option/config 侧的配置 UI 组件, 不是 chart 组件本身
* }
* 找不到时返回 null.
*
* 注: web 端 option/components/ 下是 36 个配置文件 UI, 实际 chart 组件在 echart/packages/<name>/index.vue
* (33 个). nav 数组里放的 component.name 是 'echart-bar'/'echart-line'/'avue-echart-bar' 等.
* Phase 2 镜像的是 option/config 侧, 它定义了 nav entry 的 default shape (option 对象, 拖到 canvas 后
* 会渲染 echart/packages 里的实际 chart).
*/
let cachedComponentFiles: string[] | null = null
function listComponentFiles(): string[] {
if (cachedComponentFiles) return cachedComponentFiles
if (!existsSync(DATASCREEN_OPTION_COMPONENTS_DIR)) {
cachedComponentFiles = []
return cachedComponentFiles
}
cachedComponentFiles = readdirSync(DATASCREEN_OPTION_COMPONENTS_DIR)
.filter(f => f.endsWith('.vue'))
.map(f => f.replace(/\.vue$/, ''))
.sort()
return cachedComponentFiles
}
// 解析 .vue 文件的 <script> 段, 提取 default export 的 data() 段 (简化版: 只取 key 列表)
interface ParsedVueScript {
name: string
dataKeys: string[]
hasData: boolean
}
function parseVueScript(script: string): ParsedVueScript {
// 提取 name: 'xxx' 或 name: "xxx"
const nameMatch = script.match(/name\s*:\s*['"]([^'"]+)['"]/)
const name = nameMatch ? nameMatch[1] : ''
// 提取 data() { return { ... } } 段 (用括号配对避免嵌套误判)
const dataIdx = script.search(/data\s*\(\s*\)\s*\{/)
if (dataIdx === -1) {
return { name, dataKeys: [], hasData: false }
}
// 找到 data() 之后的第一个 {, 然后配对找 }
const openBrace = script.indexOf('{', dataIdx)
if (openBrace === -1) return { name, dataKeys: [], hasData: false }
let depth = 0
let dataBodyEnd = -1
for (let i = openBrace; i < script.length; i++) {
if (script[i] === '{') depth++
else if (script[i] === '}') {
depth--
if (depth === 0) { dataBodyEnd = i; break }
}
}
if (dataBodyEnd === -1) return { name, dataKeys: [], hasData: false }
// 在 data() body 内找 `return { ... }`, 然后列出 top-level key
const dataBody = script.slice(openBrace + 1, dataBodyEnd)
const returnIdx = dataBody.indexOf('return')
if (returnIdx === -1) return { name, dataKeys: [], hasData: false }
const returnOpen = dataBody.indexOf('{', returnIdx)
if (returnOpen === -1) return { name, dataKeys: [], hasData: false }
// 找 return { ... } 的对应 }
let rd = 0
let returnEnd = -1
for (let i = returnOpen; i < dataBody.length; i++) {
if (dataBody[i] === '{') rd++
else if (dataBody[i] === '}') {
rd--
if (rd === 0) { returnEnd = i; break }
}
}
if (returnEnd === -1) return { name, dataKeys: [], hasData: false }
const returnBody = dataBody.slice(returnOpen + 1, returnEnd)
// 列 top-level key
const keyRegex = /^\s*([a-zA-Z_$][\w$]*)\s*:/gm
const keys: string[] = []
let m: RegExpExecArray | null
while ((m = keyRegex.exec(returnBody)) !== null) {
if (!keys.includes(m[1])) keys.push(m[1])
}
return { name, dataKeys: keys, hasData: keys.length > 0 }
}
const componentFileCache = new Map<string, ParsedVueScript & { filePath: string }>()
export interface ComponentDefaults {
/** 组件 prop 名 (e.g. 'bar'/'line'/'text'/'pie'/'map'/'video'/'swiper'/'iframe'/'table') */
prop: string
/** .vue 文件里 `name: 'xxx'` 字段, web 端 `components.js:10` 用 `cmp.name + 'Option'` 做 component name */
name: string
/** 完整 .vue 文件路径 */
filePath: string
/** 是否有 `data() { return { ... } }` 段 */
hasData: boolean
/** hasData=true 时, 列出 data 对象 top-level key 列表 (e.g. ['api', 'code', 'dicOption']) */
dataKeys: string[]
/** 标记这是 option/config 侧的配置 UI 组件, 跟 echart/packages 侧的 chart 组件区分 */
isOptionConfig: true
}
export function getComponentDefaults(prop: string): ComponentDefaults | null {
if (!prop || typeof prop !== 'string') return null
if (componentFileCache.has(prop)) {
const cached = componentFileCache.get(prop)!
return { ...cached, prop }
}
const filePath = join(DATASCREEN_OPTION_COMPONENTS_DIR, prop + '.vue')
if (!existsSync(filePath)) return null
try {
const code = readFileSync(filePath, 'utf-8')
// 提取 <script>...</script> 段
const scriptMatch = code.match(/<script[^>]*>([\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()
}