刘光辉
2 小时以前 cf11e63b659afcc9792ab3e395f1b45d844431a8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import { getScriptFunc } from '@jnpf/utils';
 
import { normalizeVirtualFields } from '#/components/FormGenerator/src/helper/displayOnly';
 
export function resolveDetailTitle(title, fallbackTitle) {
  return title || fallbackTitle;
}
 
export function getMappedValue(data, path) {
  if (!data || !path) return undefined;
  let value = data;
  for (const key of path.split('.')) {
    if (value === null || value === undefined) return undefined;
    value = value[key];
  }
  return value;
}
 
export function applyDetailFieldMapping(fields, formData, params, fieldMapping) {
  if (!fieldMapping) return formData;
  const result = { ...formData };
  const loop = (items) => {
    for (const item of items || []) {
      const field = item.__vModel__;
      const mappedPath = fieldMapping[field];
      const mappedValue = mappedPath ? getMappedValue(params, mappedPath) : undefined;
      if (field && mappedValue !== undefined) result[field] = mappedValue;
      loop(item.__config__?.children);
    }
  };
  loop(fields);
  return result;
}
 
export async function processDetailData(formConf, formData, onlineUtils) {
  normalizeVirtualFields(formConf?.fields);
  const funcs = formConf?.funcs;
  if (!funcs?.customDetailData) return formData;
  const func: any = getScriptFunc(funcs.customDetailDataFunc);
  if (!func) throw new Error('自定义详情数据脚本无效');
  const data = await Promise.resolve(func({ formData, onlineUtils }));
  if (!data || typeof data !== 'object' || Array.isArray(data)) throw new Error('自定义详情数据脚本必须返回对象');
  return data;
}