73 lines
2.5 KiB
TypeScript
73 lines
2.5 KiB
TypeScript
|
|
|
||
|
|
import { FormSchema } from '/@/components/Form';
|
||
|
|
import { BasicColumn } from "/@/components/Table";
|
||
|
|
import { getFormTemplateUsingCache } from '/@/api/form/design';
|
||
|
|
import { deepMerge } from '/@/utils';
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
export function useFormConfig() {
|
||
|
|
async function mergeFormSchemas(formSchema: FormSchema[],formId:String){
|
||
|
|
const formProps=await queryConfig(formId,'formProps');
|
||
|
|
let fschemas=formProps?.schemas;
|
||
|
|
if(fschemas&&fschemas.length>0){
|
||
|
|
return deepMerge(formSchema,fschemas);
|
||
|
|
}else{
|
||
|
|
return formSchema;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
async function mergeColumns(columns: BasicColumn[],formId:String){
|
||
|
|
const cols=await queryConfig(formId,'columns');
|
||
|
|
if(cols&&cols.length>0){
|
||
|
|
const filteredCol = cols.filter(colItem =>
|
||
|
|
columns.some(column => column.dataIndex === colItem.dataIndex)
|
||
|
|
);
|
||
|
|
return filteredCol;
|
||
|
|
}else{
|
||
|
|
return columns;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
async function mergeSearchFormSchema(searchFormSchema: FormSchema[],formId:String){
|
||
|
|
const sFormSchema=await queryConfig(formId,'searchFormSchema');
|
||
|
|
if(sFormSchema&&sFormSchema.length>0){
|
||
|
|
const filteredsFormSchema = sFormSchema.filter(sItem =>
|
||
|
|
searchFormSchema.some( searchItem => searchItem.field === sItem.field)
|
||
|
|
);
|
||
|
|
return filteredsFormSchema;
|
||
|
|
}else{
|
||
|
|
return searchFormSchema;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
async function queryConfig(formId:String,configName:String){
|
||
|
|
const formTemplate = await getFormTemplateUsingCache(formId);
|
||
|
|
const renderConfig=formTemplate.renderConfig;
|
||
|
|
const exportMatches = renderConfig.matchAll(/export\s+const\s+(\w+)\s*(?::\s*\w+(?:\[\])?)?\s*=\s*([\s\S]*?)(?=\n\s*export\s+const|\n\s*const|\n\s*export\s+function|\n\s*function|$)/g);
|
||
|
|
for (const match of exportMatches) {
|
||
|
|
const varName = match[1];
|
||
|
|
const valueCode = match[2].trim();
|
||
|
|
const cleanCode = valueCode.endsWith(';') ? valueCode.slice(0, -1) : valueCode;
|
||
|
|
if(varName==configName) {
|
||
|
|
try {
|
||
|
|
const value = new Function(`return ${cleanCode}`)();
|
||
|
|
// console.log(value);
|
||
|
|
return value;
|
||
|
|
} catch (e) {
|
||
|
|
console.error(`Failed to parse ${varName}:`, e);
|
||
|
|
throw new Error('解析表单渲染覆盖配置出错,请联系管理员处理');
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
|
||
|
|
return {
|
||
|
|
mergeFormSchemas,
|
||
|
|
mergeColumns,
|
||
|
|
mergeSearchFormSchema
|
||
|
|
};
|
||
|
|
}
|