echoes/frontend/interface/fields.ts
lsy e54c6b67c0 前端:实现开发模式动态主题
后端:自定义字段restful路由
2024-12-20 00:34:54 +08:00

40 lines
954 B
TypeScript

export enum FieldType {
meta = "meta",
data = "data",
}
export interface Field {
field_key: string;
field_type: FieldType;
field_value: any;
}
export function FindField(fields: Array<Field>, field_key: string, field_type: FieldType) {
return fields.find(field => field.field_key === field_key && field.field_type === field_type);
}
export function deserializeFields(rawFields: any[]): Field[] {
return rawFields.map(field => {
let parsedValue = field.field_value;
// 如果是字符串,尝试解析
if (typeof field.field_value === 'string') {
try {
// 先尝试解析为 JSON
parsedValue = JSON.parse(field.field_value);
} catch {
// 如果解析失败,保持原始字符串
parsedValue = field.field_value;
}
}
return {
field_key: field.field_key,
field_type: field.field_type as FieldType,
field_value: parsedValue
};
});
}