2025-05-07 23:57:41 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 针对 https://github.com/xaboy/form-create-designer 封装的工具类
|
|
|
|
|
|
*/
|
2025-05-12 12:58:57 +08:00
|
|
|
|
// TODO @芋艿:后续这些 form-create 的优化;另外需要使用 form-create-helper 会好点
|
2025-05-07 23:57:41 +08:00
|
|
|
|
import { isRef } from 'vue';
|
|
|
|
|
|
|
2025-08-25 09:42:18 +08:00
|
|
|
|
import formCreate from '@form-create/ant-design-vue';
|
2025-05-07 23:57:41 +08:00
|
|
|
|
// 编码表单 Conf
|
2025-06-28 00:22:16 +08:00
|
|
|
|
export const encodeConf = (designerRef: any) => {
|
2025-05-07 23:57:41 +08:00
|
|
|
|
return JSON.stringify(designerRef.value.getOption());
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// 编码表单 Fields
|
2025-06-28 00:22:16 +08:00
|
|
|
|
export const encodeFields = (designerRef: any) => {
|
2025-05-07 23:57:41 +08:00
|
|
|
|
const rule = JSON.parse(designerRef.value.getJson());
|
|
|
|
|
|
const fields: string[] = [];
|
|
|
|
|
|
rule.forEach((item: unknown) => {
|
|
|
|
|
|
fields.push(JSON.stringify(item));
|
|
|
|
|
|
});
|
|
|
|
|
|
return fields;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// 解码表单 Fields
|
|
|
|
|
|
export const decodeFields = (fields: string[]) => {
|
|
|
|
|
|
const rule: object[] = [];
|
|
|
|
|
|
fields.forEach((item) => {
|
2025-08-25 09:42:18 +08:00
|
|
|
|
rule.push(formCreate.parseJson(item));
|
2025-05-07 23:57:41 +08:00
|
|
|
|
});
|
|
|
|
|
|
return rule;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// 设置表单的 Conf 和 Fields,适用 FcDesigner 场景
|
|
|
|
|
|
export const setConfAndFields = (
|
2025-06-28 00:22:16 +08:00
|
|
|
|
designerRef: any,
|
2025-05-07 23:57:41 +08:00
|
|
|
|
conf: string,
|
2025-05-21 01:29:03 +08:00
|
|
|
|
fields: string | string[],
|
2025-05-07 23:57:41 +08:00
|
|
|
|
) => {
|
2025-08-25 09:42:18 +08:00
|
|
|
|
designerRef.value.setOption(formCreate.parseJson(conf));
|
2025-06-28 00:22:16 +08:00
|
|
|
|
// 处理 fields 参数类型,确保传入 decodeFields 的是 string[] 类型
|
|
|
|
|
|
const fieldsArray = Array.isArray(fields) ? fields : [fields];
|
|
|
|
|
|
designerRef.value.setRule(decodeFields(fieldsArray));
|
2025-05-07 23:57:41 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// 设置表单的 Conf 和 Fields,适用 form-create 场景
|
|
|
|
|
|
export const setConfAndFields2 = (
|
2025-06-28 00:22:16 +08:00
|
|
|
|
detailPreview: any,
|
2025-05-07 23:57:41 +08:00
|
|
|
|
conf: string,
|
|
|
|
|
|
fields: string[],
|
2025-06-28 00:22:16 +08:00
|
|
|
|
value?: any,
|
2025-05-07 23:57:41 +08:00
|
|
|
|
) => {
|
|
|
|
|
|
if (isRef(detailPreview)) {
|
|
|
|
|
|
detailPreview = detailPreview.value;
|
|
|
|
|
|
}
|
2025-08-25 09:42:18 +08:00
|
|
|
|
detailPreview.option = formCreate.parseJson(conf);
|
2025-05-07 23:57:41 +08:00
|
|
|
|
detailPreview.rule = decodeFields(fields);
|
|
|
|
|
|
if (value) {
|
|
|
|
|
|
detailPreview.value = value;
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|