You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

155 lines
3.9 KiB

import _ from 'lodash'
5 years ago
import {inputComponents, selectComponents} from '@/components/generator/config'
import {jsonClone} from '@/utils/index'
/**
* 表单json转换为后台需要的对象
* @param item
*/
export function formItemConvertData(item, projectKey) {
5 years ago
console.log(item)
let data = {
'type': item.typeId,
5 years ago
'formItemId': item.__config__.formId,
'label': item.__config__.label,
5 years ago
'defaultValue': item.__config__.defaultValue,
'required': item.__config__.required,
'placeholder': item.placeholder,
'regList': item.__config__.regList,
'projectKey': projectKey
}
let expand = {}
let param = dataParams[item.typeId]
5 years ago
if (param) {
Object.keys(param).forEach(key => {
let value = _.get(item, param[key])
_.set(expand, key, value)
5 years ago
})
_.set(data, 'expand', expand)
5 years ago
}
return data
}
5 years ago
//类型关系map
let typeMap = new Map()
/**
* 后台存储的数据转换为elementui表单需要的Json
* @param data
*/
export function dbDataConvertForItemJson(data) {
if (!typeMap.size > 0) {
//根据类型获取默认数据
_.concat(inputComponents, selectComponents).forEach(item => {
typeMap.set(item.typeId, item)
})
}
const defaultJsonItem = typeMap.get(data.type)
let jsonItem = _.cloneDeep(defaultJsonItem)
let param = dataParams[data.type]
if (param) {
Object.keys(param).forEach(key => {
let value = _.get(data.expand, key)
_.set(jsonItem, param[key], value)
})
}
jsonItem.typeId = data.type
jsonItem.__config__.formId = data.formItemId
jsonItem.__config__.label = data.label
jsonItem.__config__.required = data.required
jsonItem.__config__.regList = data.regList
if (data.defaultValue) {
if (data.defaultValue.json) {
jsonItem.__config__.defaultValue = JSON.parse(data.defaultValue.value)
} else {
jsonItem.__config__.defaultValue = data.defaultValue.value
}
}
5 years ago
jsonItem.regList = data.regList
jsonItem.placeholder = data.placeholder
jsonItem.__vModel__ = 'field' + data.formItemId
return jsonItem
}
/**
* 不同属性的存在json的位置不用 使用变量记录key 通过lodash表达式获取 1 2 3 4 为typeId
* @type {{'1': {maxlength: string, prepend: string, append: string}}}
*/
let dataParams = {
//单行文本
1: {
'prepend': '__slot__.prepend',
'maxlength': 'maxlength',
'append': '__slot__.append'
},
// 多行文本
2: {
'minRows': 'autosize.minRows',
'maxRows': 'autosize.maxRows',
'maxlength': 'maxlength'
},
//计数器
4: {
'min': 'min',
5 years ago
'max': 'max',
'maxlength': 'maxlength',
'step': 'step',
'step-strictly': 'step-strictly',
'precision': 'precision',
'controls-position': 'controls-position'
},
//下拉选择
5: {
'options': '__slot__.options',
'filterable': 'filterable',
'multiple': 'multiple'
},
//级联选择
6: {
'options': 'options',
'filterable': 'filterable',
'multiple': 'props.props.multiple'
},
//单选框组
7: {
'options': '__slot__.options',
'filterable': 'filterable',
'multiple': 'props.props.multiple'
},
//单选框组
8: {
'options': '__slot__.options',
'max': 'max',
'min': 'min'
}, //开关
5 years ago
9: {},
//滑块
5 years ago
10: {
'min': 'min',
'max': 'max',
5 years ago
'step': 'step'
},//时间选择
5 years ago
11: {},
//文件上传
17: {
'buttonText': '__config__.buttonText',
'showTip': '__config__.showTip',
'fileSize': '__config__.fileSize',
'sizeUnit': '__config__.sizeUnit',
'listType': 'list-type',
'limit': 'limit',
'multiple': 'multiple'
5 years ago
}
}