Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 
 
 

187 rindas
6.8 KiB

  1. import get from 'lodash/get'
  2. import set from 'lodash/set'
  3. import customForm from '@/common/customform.js'
  4. /**
  5. * 用于自定义应用的表单加载
  6. * 注意:自定义应用既不是代码生成器页面,也不是工作流表单
  7. * 自首页 /function/list 接口拉取「我的应用」列表,列表中值的 F_Scheme 字段,即为自定义表单列表页的 scheme 定义
  8. *
  9. * 提供以下工具方法:
  10. *
  11. * 生成自定义表单相关数据结构
  12. * async getCustomAppForm(prop)
  13. *
  14. * 获取最终需要POST的数据
  15. * async getPostData(originFormValue, scheme)
  16. *
  17. */
  18. export default {
  19. mixins: [customForm],
  20. methods: {
  21. // 生成自定义表单相关数据结构
  22. // 参数: schemeData (必填), formData, useDefault
  23. // 返回: scheme, formValue
  24. async getCustomAppForm(prop) {
  25. const { schemeData, formData, keyValue, useDefault } = prop
  26. // const schemeInfoId = schemeData.schemeInfoId
  27. // dyy 2022-03-18 16:10
  28. const schemeInfoId = schemeData.F_SchemeInfoId
  29. const schemeRef = {}
  30. const refList = []
  31. const scheme = []
  32. const rel = {}
  33. const formValue = { schemeInfoId, formData: {} }
  34. if (keyValue) {
  35. formValue.keyValue = keyValue
  36. }
  37. for (let dataIndex = 0; dataIndex < schemeData.F_Scheme.data.length; ++dataIndex) {
  38. const { componts } = schemeData.F_Scheme.data[dataIndex]
  39. for (const t of componts) {
  40. // 之后的 t 即表示每个 scheme 项
  41. t.__valuePath__ = `formData.${t.id}`
  42. if (t.type === 'girdtable' && t.table) {
  43. // 数据项是表格的情况
  44. // 先设置源数据,不然无法获取默认值
  45. for (const fieldItem of t.fieldsData) {
  46. fieldItem.__sourceData__ = await this.getSourceData(fieldItem)
  47. }
  48. t.__defaultItem__ = await this.getDefaultData(t, prop)
  49. if (formData) {
  50. // 有表单值的情况,从表单值中获取数据
  51. const val = []
  52. for (const valueItem of get(formData, `${schemeInfoId}.${t.table}`, [])) {
  53. const tableItemValue = {}
  54. for (const fieldItem of t.fieldsData.filter(t => t.field)) {
  55. const formDataValue = get(valueItem, fieldItem.field.toLowerCase())
  56. tableItemValue[fieldItem.field] = await this.convertToFormValue(fieldItem, formDataValue)
  57. }
  58. val.push(tableItemValue)
  59. }
  60. // useDefault 表示在从 formData 取不到值的时候使用默认值
  61. if ((!val || val.length <= 0) && useDefault) {
  62. set(formValue, t.__valuePath__, [this.COPY(t.__defaultItem__)])
  63. } else {
  64. set(formValue, t.__valuePath__, val)
  65. }
  66. } else {
  67. // 无表单值的情况,默认值
  68. set(formValue, t.__valuePath__, [this.COPY(t.__defaultItem__)])
  69. }
  70. } else if (t.field) {
  71. // 数据项不是表格的情况
  72. // 先设置源数据,不然无法获取默认值
  73. t.__sourceData__ = await this.getSourceData(t)
  74. if (formData) {
  75. // 有表单值的情况,从表单值中获取数据
  76. const path = `${schemeInfoId}.${t.table}.${dataIndex}.${t.field.toLowerCase()}`
  77. const formDataValue = get(formData, path)
  78. // useDefault 表示在从 formData 取不到值的时候使用默认值
  79. if (!formDataValue && useDefault) {
  80. set(formValue, t.__valuePath__, await this.getDefaultData(t, prop))
  81. } else {
  82. set(formValue, t.__valuePath__, await this.convertToFormValue(t, formDataValue))
  83. }
  84. } else {
  85. // 无表单值的情况,默认值
  86. set(formValue, t.__valuePath__, await this.getDefaultData(t, prop))
  87. }
  88. }
  89. // organize、datetime 可能作为其他 organize 或 datetimerange 的依赖项,引用它们
  90. if (['organize', 'datetime'].includes(t.type)) {
  91. schemeRef[t.id] = t
  92. }
  93. // datetimerange、带有 relation 级联字段的 organize,依赖其他项
  94. if ((t.type === 'datetimerange' && t.startTime && t.endTime) || (t.type === 'organize' && t.relation)) {
  95. refList.push(t)
  96. }
  97. // 依次处理表单关联
  98. refList.forEach(t => {
  99. if (t.type === 'organize') {
  100. // 处理组件结构级联
  101. // 给当前组件赋上级级联的值路径 __relationPath__
  102. const parent = schemeRef[t.relation]
  103. t.__relationPath__ = parent.__valuePath__
  104. // 给上级级联的组件注册自动重置当前组件的 change 事件
  105. const relItem = { type: 'organize', id: t.id, path: t.__valuePath__ }
  106. rel[parent.id] = rel[parent.id] ? rel[parent.id].concat(relItem) : [relItem]
  107. } else if (t.type === 'datetimerange') {
  108. // 处理日期区间
  109. const start = schemeRef[t.startTime]
  110. const end = schemeRef[t.endTime]
  111. const relItem = {
  112. type: 'datetimerange',
  113. path: t.__valuePath__,
  114. id: t.id,
  115. startPath: start.__valuePath__,
  116. endPath: end.__valuePath__,
  117. }
  118. rel[start.id] = rel[start.id] ? rel[start.id].concat(relItem) : [relItem]
  119. rel[end.id] = rel[end.id] ? rel[end.id].concat(relItem) : [relItem]
  120. }
  121. })
  122. scheme.push(t)
  123. }
  124. }
  125. return { formValue, scheme, rel }
  126. },
  127. // 获取最终需要POST的数据
  128. // 参数: formValue , scheme
  129. async getPostData(originFormValue, scheme) {
  130. const formValue = this.COPY(originFormValue)
  131. // 依次按照 scheme 项目遍历
  132. for (const item of scheme) {
  133. if (item.field) {
  134. // 不是表格的情况
  135. const path = item.__valuePath__
  136. const val = get(formValue, path)
  137. const result = await this.convertToPostData(item, val, originFormValue, scheme)
  138. set(formValue, path, result)
  139. } else if (item.table && item.fieldsData) {
  140. // 是表格的情况
  141. const tableValue = get(formValue, item.__valuePath__, [])
  142. for (let valueIndex = 0; valueIndex < tableValue.length; ++valueIndex) {
  143. for (const schemeItem of item.fieldsData) {
  144. const path = `${item.__valuePath__}.${valueIndex}.${schemeItem.field}`
  145. const val = get(formValue, path)
  146. const result = await this.convertToPostData(schemeItem, val, originFormValue, scheme)
  147. set(formValue, path, result)
  148. }
  149. }
  150. }
  151. }
  152. formValue.formData = JSON.stringify(formValue.formData)
  153. return [formValue]
  154. }
  155. }
  156. }