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.

192 lines
5.1 KiB

/**
* 处理项目数据
*
*/
import _ from 'lodash'
import {assistComponents, imageComponents, inputComponents, selectComponents} from '@/components/generator/config'
/**
* 表单json转换为后台需要的对象
* @param item
*/
export function formItemConvertData(item, projectKey) {
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,
'showLabel': item.__config__.showLabel,
'span': item.__config__.span,
'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
}
// 类型关系map
5 years ago
let typeMap = new Map()
/**
* 后台存储的数据转换为elementui表单需要的Json
* @param data
*/
export function dbDataConvertForItemJson(data) {
let {required, placeholder} = data
if (required && !placeholder) { // 必填项目验证未填默认提示语
data.placeholder = '此题为必填项目'
}
5 years ago
if (!typeMap.size > 0) {
// 根据类型获取默认数据
_.concat(inputComponents, selectComponents, imageComponents, assistComponents).forEach(item => {
5 years ago
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.dId = data.id
jsonItem.sort = data.sort
5 years ago
jsonItem.typeId = data.type
jsonItem.__config__.span = data.span
5 years ago
jsonItem.__config__.formId = data.formItemId
jsonItem.__config__.label = data.label
jsonItem.__config__.required = data.required
jsonItem.__config__.regList = data.regList
jsonItem.__config__.showLabel = data.showLabel
5 years ago
if (data.defaultValue) {
if (data.defaultValue.json) {
jsonItem.__config__.defaultValue = JSON.parse(data.defaultValue.value)
} else {
jsonItem.__config__.defaultValue = data.defaultValue.value
}
}
// 不同项目地址区分 动态修改上传地址
4 years ago
if (jsonItem.action) {
jsonItem.action = jsonItem.action + data.projectKey
}
5 years ago
jsonItem.regList = data.regList
jsonItem.placeholder = data.placeholder
jsonItem.formItemId = data.formItemId
5 years ago
jsonItem.__vModel__ = 'field' + data.formItemId
return jsonItem
}
/**
*
* @type {{'1': {maxlength: string, prepend: string, append: string}}}
*
*/
const dataParams = {
// 单行文本
4 years ago
'INPUT': {
'prepend': '__slot__.prepend',
'maxlength': 'maxlength',
'append': '__slot__.append'
},
// 多行文本
4 years ago
'TEXTAREA': {
'minRows': 'autosize.minRows',
'maxRows': 'autosize.maxRows',
'maxlength': 'maxlength'
},
// 计数器
4 years ago
'NUMBER_INPUT': {
'min': 'min',
5 years ago
'max': 'max',
'maxlength': 'maxlength',
'step': 'step',
'step-strictly': 'step-strictly',
'precision': 'precision',
'controls-position': 'controls-position'
},
// 下拉选择
4 years ago
'SELECT': {
'options': '__slot__.options',
'filterable': 'filterable',
'multiple': 'multiple'
},
// 级联选择
4 years ago
'CASCADER': {
'options': 'options',
'filterable': 'filterable',
'multiple': 'props.props.multiple'
},
// 单选框组
4 years ago
'RADIO': {
'options': '__slot__.options',
'filterable': 'filterable',
'multiple': 'props.props.multiple'
},
// 多选框组
4 years ago
'CHECKBOX': {
'optionType': '__config__.optionType',
'size': 'size',
'options': '__slot__.options',
'max': 'max',
'min': 'min'
}, // 开关
4 years ago
'SWITCH': {},
// 滑块
4 years ago
'SLIDER': {
'min': 'min',
'max': 'max',
5 years ago
'step': 'step'
}, // 时间选择
4 years ago
'TIME': {},
'RATE': {
'max': 'max'
5 years ago
},
// 文件上传
4 years ago
'UPLOAD': {
5 years ago
'buttonText': '__config__.buttonText',
'showTip': '__config__.showTip',
'fileSize': '__config__.fileSize',
'sizeUnit': '__config__.sizeUnit',
'listType': 'list-type',
'limit': 'limit',
'multiple': 'multiple'
},
// 图片
4 years ago
'IMAGE': {
'src': 'src',
'alt': 'alt'
},
// 图片选择
'IMAGE_SELECT': {
'options': 'options',
'multiple': 'multiple'
},
// 图片轮播
'IMAGE_CAROUSEL': {
'options': '__slot__.options'
4 years ago
},
// 文字描述
'DESC_TEXT': {
'color': 'color',
'textAlign': 'textAlign'
},
// 手写签名
'SIGN_PAD': {
'color': 'color'
5 years ago
}
}