util.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import { unref } from 'vue'
  2. import { getFieldValue, isNotEmpty, setFieldValue } from '@cip/utils/util'
  3. export const getValuesByKeys = (data = {}, keys = []) => {
  4. const result = {}
  5. keys.forEach((key) => {
  6. if (typeof key === 'object') {
  7. const object = key
  8. setFieldValue(result, object.key, getFieldValue(data, object.key))
  9. } else {
  10. setFieldValue(result, key, getFieldValue(data, key))
  11. }
  12. })
  13. return result
  14. }
  15. export const setValuesByKeys = (target = {}, keys = [], values = {}) => {
  16. keys.forEach((key) => {
  17. if (typeof key === 'object') {
  18. const object = key
  19. setFieldValue(target, object.key, getFieldValue(values, object.key))
  20. } else {
  21. setFieldValue(target, key, getFieldValue(values, key))
  22. }
  23. })
  24. }
  25. export const isHideLabel = (config) => {
  26. return (
  27. config.hideLabel ||
  28. (isNotEmpty(config.labelWidth) && !config.labelWidth) ||
  29. !config.label
  30. )
  31. }
  32. export const getLabelWidth = (config) => {
  33. if (config.hideLabel) return '0px'
  34. if (config.labelWidth) return config.labelWidth + 'px'
  35. if (!config.label) return '0px' // 兼容老的设计
  36. return undefined
  37. }
  38. export const getChangeIndex = (values, oldValues) => {
  39. const result = []
  40. values.forEach((v, i) => {
  41. if (typeof v === 'object') {
  42. // 数组 对象相等判断
  43. // 无法通过值来判断对象级数组是否变化 (地址引用导致2个值一直都是相等的)
  44. result.push(i)
  45. } else {
  46. if (v !== oldValues[i]) {
  47. result.push(i)
  48. }
  49. }
  50. })
  51. return result
  52. }
  53. const secureNewFn = (...params) => {
  54. const funcBody = params.pop()
  55. try {
  56. // eslint-disable-next-line no-new-func
  57. return new Function(...params, funcBody)
  58. } catch (error) {
  59. console.error(error)
  60. return () => {}
  61. }
  62. }
  63. export const judgeUseFn = (key, config, effect) => {
  64. // console.log(key, config, effect);
  65. // eslint-disable-next-line no-new-func
  66. if (key === 'changeValue' && config.changeValueStr)
  67. return secureNewFn(
  68. 'dependOnValues',
  69. 'outDependOnValues',
  70. config.changeValueStr
  71. )
  72. if (key === 'changeConfig' && config.changeConfigStr)
  73. return secureNewFn(
  74. 'config',
  75. 'dependOnValues',
  76. 'outDependOnValues',
  77. config.changeConfigStr
  78. )
  79. if (key === 'asyncOptions' && typeof config.asyncOptions === 'string')
  80. return secureNewFn('dependOnValues', 'outDependOnValues', config.asyncOptions)
  81. if (!effect) return config[key] // 没有effect 参数则直接使用config[key]
  82. if (effect && key in effect) {
  83. // 有effect 且 effect对象明确存在key(不管其值为什么)
  84. if (typeof effect[key] === 'function') return effect[key] // effect中的值为函数 则认为此次响应使用局部方法
  85. return config[key]
  86. }
  87. }
  88. export class UpdateModelQueue {
  89. constructor(getModel, updateModel) {
  90. this.getModel = getModel
  91. this.updateModel = updateModel
  92. }
  93. init() {
  94. this.data = new Map()
  95. }
  96. append(key, value) {
  97. if (!this.isCollect) {
  98. this.init()
  99. this.isCollect = true
  100. // setTimeout(() => {
  101. // }, 20)
  102. }
  103. this.data.set(key, value) // = value
  104. this.update() // 直接触发
  105. // setFieldValue(this.data, key, value)
  106. }
  107. update() {
  108. const model = unref(this.getModel()) // 维持model是最新的
  109. // eslint-disable-next-line no-unused-vars
  110. for (const key of this.data.keys()) {
  111. const value = this.data.get(key)
  112. setFieldValue(model, key, value)
  113. }
  114. this.isCollect = false
  115. this.updateModel(model)
  116. }
  117. }