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;
|
}
|