formMixins.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import { isArray, isObject } from '@/utils/types.js'
  2. export default {
  3. methods: {
  4. /**
  5. * 格式化默认获取到的字段值
  6. * @param {Object} field
  7. */
  8. mixinsFormatFieldValue(field) {
  9. let value = field.value || field.defaultValue || ''
  10. if (['select', 'checkbox'].includes(field.formType)) {
  11. if (this.$isEmpty(value)) return []
  12. if (isArray(value)) {
  13. return value.map(o => {
  14. return isObject(o) ? o : { label: o, value: o }
  15. })
  16. } else {
  17. return value.split(',').map(o => {
  18. return { label: o, value: o }
  19. })
  20. }
  21. }
  22. switch (field.formType) {
  23. case 'single_user':
  24. if (this.$isEmpty(value)) return []
  25. if (!isArray(value)) return [value]
  26. return value
  27. case 'user':
  28. if (this.$isEmpty(value)) return []
  29. if (!isArray(value)) return [value]
  30. return value
  31. case 'contract':
  32. if (this.$isEmpty(value)) return []
  33. return value.map(o => {
  34. if (!o.hasOwnProperty('num')) {
  35. return {
  36. contractId: o.contractId,
  37. num: o.contractNum
  38. }
  39. } else {
  40. return o
  41. }
  42. })
  43. case 'receivables_plan':
  44. if (this.$isEmpty(value)) return []
  45. if (!isArray(value)) return [value]
  46. return value
  47. case 'date_interval':
  48. if (this.$isEmpty(value)) return []
  49. if (!isArray(value)) return []
  50. return value
  51. case 'detail_table':
  52. if (this.$isEmpty(field.value)) return []
  53. return value
  54. }
  55. return value
  56. },
  57. /**
  58. * 获取所有与逻辑表单相关字段的 formAssistId
  59. */
  60. getAllFormAssistId(fieldArr = []) {
  61. let ids = []
  62. for (let i = 0; i < fieldArr.length; i++) {
  63. const field = fieldArr[i]
  64. if (![
  65. 'select',
  66. 'checkbox'
  67. ].includes(field.formType)) continue
  68. if (field.remark !== 'options_type') continue
  69. ids.push(field.formAssistId)
  70. Object.keys(field.optionsData).forEach(key => {
  71. ids = ids.concat(field.optionsData[key])
  72. })
  73. }
  74. ids = ids.filter(o => Boolean(o))
  75. return Array.from(new Set(ids))
  76. }
  77. }
  78. }