117 lines
4.0 KiB
TypeScript
117 lines
4.0 KiB
TypeScript
|
|
import { FormSchema } from '/@/components/Form';
|
|
import { BasicColumn } from "/@/components/Table";
|
|
import { getFormTemplateUsingCache } from '/@/api/form/design';
|
|
import { deepMerge } from '/@/utils';
|
|
import { useI18n } from '/@/hooks/web/useI18n';
|
|
import { useMessage } from '/@/hooks/web/useMessage';
|
|
|
|
const { t } = useI18n();
|
|
const { notification } = useMessage();
|
|
|
|
export function useFormConfig() {
|
|
async function mergeFormSchemas(formSchema: FormSchema[],formId:String){
|
|
try{
|
|
const formProps=await queryConfig(formId,'formProps');
|
|
let fschemas=formProps?.schemas;
|
|
if(fschemas&&fschemas.length>0){
|
|
return deepMerge(formSchema,fschemas);
|
|
}else{
|
|
return formSchema;
|
|
}
|
|
}catch(e){
|
|
notification.error({
|
|
message: t('提示'),
|
|
description: '解析表单渲染覆盖配置出错[解析formProps发生异常]',
|
|
});
|
|
return [];
|
|
}
|
|
}
|
|
|
|
async function mergeColumns(columns: BasicColumn[],formId:String){
|
|
try{
|
|
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;
|
|
}
|
|
}catch(e){
|
|
notification.error({
|
|
message: t('提示'),
|
|
description: '解析表单渲染覆盖配置出错[解析columns发生异常]',
|
|
});
|
|
return [];
|
|
}
|
|
}
|
|
|
|
async function mergeSearchFormSchema(searchFormSchema: FormSchema[],formId:String){
|
|
try{
|
|
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;
|
|
}
|
|
}catch(e){
|
|
notification.error({
|
|
message: t('提示'),
|
|
description: '解析表单渲染覆盖配置出错[解析searchFormSchema发生异常]',
|
|
});
|
|
return [];
|
|
}
|
|
}
|
|
|
|
async function mergeButtons(buttons,formId:String){
|
|
try{
|
|
const btns=await queryConfig(formId,'buttons');
|
|
if(btns&&btns.length>0){
|
|
return btns;
|
|
}else{
|
|
return buttons;
|
|
}
|
|
}catch(e){
|
|
notification.error({
|
|
message: t('提示'),
|
|
description: '解析表单渲染覆盖配置出错[解析buttons发生异常]',
|
|
});
|
|
return[];
|
|
}
|
|
}
|
|
|
|
|
|
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}`)();
|
|
return value;
|
|
} catch (e) {
|
|
console.error(`Failed to parse ${varName}:`, e);
|
|
throw new Error('解析表单渲染覆盖配置出错,请联系管理员处理');
|
|
}
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
return {
|
|
mergeFormSchemas,
|
|
mergeColumns,
|
|
mergeSearchFormSchema,
|
|
mergeButtons
|
|
};
|
|
}
|