import get from 'lodash/get' import set from 'lodash/set' import customForm from '@/common/customform.js' /** * 用于自定义应用的表单加载 * 注意:自定义应用既不是代码生成器页面,也不是工作流表单 * 自首页 /function/list 接口拉取「我的应用」列表,列表中值的 F_Scheme 字段,即为自定义表单列表页的 scheme 定义 * * 提供以下工具方法: * * 生成自定义表单相关数据结构 * async getCustomAppForm(prop) * * 获取最终需要POST的数据 * async getPostData(originFormValue, scheme) * */ export default { mixins: [customForm], methods: { // 生成自定义表单相关数据结构 // 参数: schemeData (必填), formData, useDefault // 返回: scheme, formValue async getCustomAppForm(prop) { const { schemeData, formData, keyValue, useDefault } = prop const schemeInfoId = schemeData.schemeInfoId const schemeRef = {} const refList = [] const scheme = [] const rel = {} const formValue = { schemeInfoId, formData: {} } if (keyValue) { formValue.keyValue = keyValue } for (let dataIndex = 0; dataIndex < schemeData.F_Scheme.data.length; ++dataIndex) { const { componts } = schemeData.F_Scheme.data[dataIndex] for (const t of componts) { // 之后的 t 即表示每个 scheme 项 t.__valuePath__ = `formData.${t.id}` if (t.type === 'girdtable' && t.table) { // 数据项是表格的情况 // 先设置源数据,不然无法获取默认值 for (const fieldItem of t.fieldsData) { fieldItem.__sourceData__ = await this.getSourceData(fieldItem) } t.__defaultItem__ = await this.getDefaultData(t, prop) if (formData) { // 有表单值的情况,从表单值中获取数据 const val = [] for (const valueItem of get(formData, `${schemeInfoId}.${t.table}`, [])) { const tableItemValue = {} for (const fieldItem of t.fieldsData.filter(t => t.field)) { const formDataValue = get(valueItem, fieldItem.field.toLowerCase()) tableItemValue[fieldItem.field] = await this.convertToFormValue(fieldItem, formDataValue) } val.push(tableItemValue) } // useDefault 表示在从 formData 取不到值的时候使用默认值 if ((!val || val.length <= 0) && useDefault) { set(formValue, t.__valuePath__, [this.COPY(t.__defaultItem__)]) } else { set(formValue, t.__valuePath__, val) } } else { // 无表单值的情况,默认值 set(formValue, t.__valuePath__, [this.COPY(t.__defaultItem__)]) } } else if (t.field) { // 数据项不是表格的情况 // 先设置源数据,不然无法获取默认值 t.__sourceData__ = await this.getSourceData(t) if (formData) { // 有表单值的情况,从表单值中获取数据 const path = `${schemeInfoId}.${t.table}.${dataIndex}.${t.field.toLowerCase()}` const formDataValue = get(formData, path) // useDefault 表示在从 formData 取不到值的时候使用默认值 if (!formDataValue && useDefault) { set(formValue, t.__valuePath__, await this.getDefaultData(t, prop)) } else { set(formValue, t.__valuePath__, await this.convertToFormValue(t, formDataValue)) } } else { // 无表单值的情况,默认值 set(formValue, t.__valuePath__, await this.getDefaultData(t, prop)) } } // organize、datetime 可能作为其他 organize 或 datetimerange 的依赖项,引用它们 if (['organize', 'datetime'].includes(t.type)) { schemeRef[t.id] = t } // datetimerange、带有 relation 级联字段的 organize,依赖其他项 if ((t.type === 'datetimerange' && t.startTime && t.endTime) || (t.type === 'organize' && t.relation)) { refList.push(t) } // 依次处理表单关联 refList.forEach(t => { if (t.type === 'organize') { // 处理组件结构级联 // 给当前组件赋上级级联的值路径 __relationPath__ const parent = schemeRef[t.relation] t.__relationPath__ = parent.__valuePath__ // 给上级级联的组件注册自动重置当前组件的 change 事件 const relItem = { type: 'organize', id: t.id, path: t.__valuePath__ } rel[parent.id] = rel[parent.id] ? rel[parent.id].concat(relItem) : [relItem] } else if (t.type === 'datetimerange') { // 处理日期区间 const start = schemeRef[t.startTime] const end = schemeRef[t.endTime] const relItem = { type: 'datetimerange', path: t.__valuePath__, id: t.id, startPath: start.__valuePath__, endPath: end.__valuePath__, } rel[start.id] = rel[start.id] ? rel[start.id].concat(relItem) : [relItem] rel[end.id] = rel[end.id] ? rel[end.id].concat(relItem) : [relItem] } }) scheme.push(t) } } return { formValue, scheme, rel } }, // 获取最终需要POST的数据 // 参数: formValue , scheme async getPostData(originFormValue, scheme) { const formValue = this.COPY(originFormValue) // 依次按照 scheme 项目遍历 for (const item of scheme) { if (item.field) { // 不是表格的情况 const path = item.__valuePath__ const val = get(formValue, path) const result = await this.convertToPostData(item, val, originFormValue, scheme) set(formValue, path, result) } else if (item.table && item.fieldsData) { // 是表格的情况 const tableValue = get(formValue, item.__valuePath__, []) for (let valueIndex = 0; valueIndex < tableValue.length; ++valueIndex) { for (const schemeItem of item.fieldsData) { const path = `${item.__valuePath__}.${valueIndex}.${schemeItem.field}` const val = get(formValue, path) const result = await this.convertToPostData(schemeItem, val, originFormValue, scheme) set(formValue, path, result) } } } } formValue.formData = JSON.stringify(formValue.formData) return [formValue] } } }