import { DynamicFormTableColumnVo } from '../../../../entity/dynamic/dynamic-form-table' import dynamicTableRequestMethod from '../../../../api/dynamic/table' import { useDynamicTableStore } from '../../../../store/dynamic-table' /** * 字段类型 */ export interface FieldType { /** * 字段key */ key: string /** * 字段key类型 */ keyType: 1 | 2 | 3 | 4 /** * 字段类型 */ type: string /** * 名称 */ label: string /** * 默认长度 */ size: string /** * 默认值 */ defaultValue: string } /** * 基础信息 */ export const BASIC_TYPE_ARRAY = [ { key: 'varchar', type: 'varchar', label: '字符串', size: '50', defaultValue: '', keyType: 1 }, { key: 'datetime', type: 'datetime', label: '日期时间', size: '', defaultValue: '', keyType: 1 }, { key: 'date', type: 'date', label: '日期', size: '', defaultValue: '', keyType: 1 }, { key: 'time', type: 'time', label: '时间', size: '', defaultValue: '', keyType: 1 }, { key: 'text', type: 'text', label: '文本', size: '1000', defaultValue: '', keyType: 1 }, { key: 'int', type: 'int', label: '整型', size: '11', defaultValue: '', keyType: 1 }, { key: 'tinyint', type: 'tinyint', label: '短整型', size: '1', defaultValue: '', keyType: 1 }, { key: 'numeric', type: 'decimal', label: '浮点数', size: '10,2', defaultValue: '', keyType: 1 } ] /** * 高级字段 */ export const SENIOR_TYPE_ARRAY = [ { key: 'image', type: 'varchar', label: '图片', size: '32', defaultValue: '', keyType: 2 }, { key: 'imageList', type: '', label: '多图片', keyType: 2 }, { key: 'file', type: 'varchar', label: '文件', size: '32', defaultValue: '', keyType: 2 }, { key: 'fileList', type: '', label: '多文件', keyType: 2 } ] /** * 源数据 */ const META_DATA_MAP: { [key: string]: FieldType[] } = {} /** * 向数组中插入信息 * @param label * @param array */ export const putTypeArray = (label: string, array: FieldType[]) => { META_DATA_MAP[label] = array } /** * 向数组中移除信息 * @param label */ export const removeTypeArray = (label: string) => { delete META_DATA_MAP[label] } /** * 构建options * @param label 分组名称 * @param array 组内信息 */ const buildGroup = (label: string, array: FieldType[]) => ({ label, options: array.map(item => ({ value: item.key, label: item.label })) }) /** * 所有的字段信息 */ export const getTypeOption = () => Object.keys(META_DATA_MAP).map(key => buildGroup(key, META_DATA_MAP[key])) /** * 字段的key item */ export const getKeyTypeMap = (): { [key: string]: FieldType } => { const map: { [key: string]: FieldType } = {} Object.values(META_DATA_MAP) .flat(1) .forEach(value => { map[value.key] = value }) return map } /** * 初始化信息 */ const init = () => { putTypeArray('基础信息', BASIC_TYPE_ARRAY) putTypeArray('高级类型', SENIOR_TYPE_ARRAY) } // 执行初始化 void init() const dynamicTableStore = useDynamicTableStore() // 创建获取信息的方法 export const refreshData = (record: DynamicFormTableColumnVo, isRender = false) => { if (record.columnKeyType == 3) { if (!dynamicTableStore.hasColumns(record.columnKeyId)) { dynamicTableRequestMethod.getById(record.columnKeyId).then(data => { dynamicTableStore.setColumns(data.data) if (isRender && data.data.columnList.length > 0) { record.showTextField = data.data.columnList[0].columnName } }) } else { if (isRender) { const options = dynamicTableStore.listColumns(record.columnKeyId) if (options.length > 0) { record.showTextField = options[0].value } } } } }