common.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. import lodash from 'lodash'
  2. export const addDateRange = (params: any, dateRange: any[], propName?: string) => {
  3. const search = params
  4. search.params = typeof search.params === 'object' && search.params !== null && !Array.isArray(search.params) ? search.params : {}
  5. dateRange = Array.isArray(dateRange) ? dateRange : []
  6. if (typeof propName === 'undefined') {
  7. search.params['beginTime'] = dateRange[0]
  8. search.params['endTime'] = dateRange[1]
  9. } else {
  10. search.params['begin' + propName] = dateRange[0]
  11. search.params['end' + propName] = dateRange[1]
  12. }
  13. return search
  14. }
  15. /**
  16. * 通用防抖
  17. *
  18. * @param callback 回调函数
  19. * @param wait 等待毫秒数
  20. * @param type 回调函数额外参数
  21. * @param options 防抖额外参数
  22. * @returns
  23. */
  24. export const lodashFunc = (callback: Function, wait: number, type?: any, options?: any) => {
  25. return lodash.debounce(
  26. function () {
  27. type ? callback(type) : callback()
  28. },
  29. wait,
  30. options
  31. )
  32. }
  33. /**
  34. * 构造树型结构数据
  35. * @param {*} data 数据源
  36. * @param {*} id id字段 默认 'id'
  37. * @param {*} parentId 父节点字段 默认 'parentId'
  38. * @param {*} children 孩子节点字段 默认 'children'
  39. */
  40. export const handleTree = <T>(data: any, id: any, parentId?: any, children?: any) => {
  41. const config: {
  42. id: string
  43. parentId: string
  44. childrenList: string
  45. } = {
  46. id: id || 'id',
  47. parentId: parentId || 'parentId',
  48. childrenList: children || 'children'
  49. }
  50. const childrenListMap: any = {}
  51. const nodeIds: any = {}
  52. const tree: T[] = []
  53. for (const d of data) {
  54. const parentId = d[config.parentId]
  55. if (childrenListMap[parentId] == null) {
  56. childrenListMap[parentId] = []
  57. }
  58. nodeIds[d[config.id]] = d
  59. childrenListMap[parentId].push(d)
  60. }
  61. for (const d of data) {
  62. const parentId = d[config.parentId]
  63. if (nodeIds[parentId] == null) {
  64. tree.push(d)
  65. }
  66. }
  67. const adaptToChildrenList = (o: any) => {
  68. if (childrenListMap[o[config.id]] !== null) {
  69. o[config.childrenList] = childrenListMap[o[config.id]]
  70. }
  71. if (o[config.childrenList]) {
  72. for (const c of o[config.childrenList]) {
  73. adaptToChildrenList(c)
  74. }
  75. }
  76. }
  77. for (const t of tree) {
  78. adaptToChildrenList(t)
  79. }
  80. return tree
  81. }
  82. /**
  83. * 参数处理
  84. *
  85. * @param {*} params 参数
  86. */
  87. export const tansParams = (params: { [x: string]: any }) => {
  88. let result = ''
  89. for (const propName of Object.keys(params)) {
  90. const value = params[propName]
  91. const part = encodeURIComponent(propName) + '='
  92. if (value !== null && value !== '' && typeof value !== 'undefined') {
  93. if (typeof value === 'object') {
  94. for (const key of Object.keys(value)) {
  95. if (value[key] !== null && value !== '' && typeof value[key] !== 'undefined') {
  96. let params = propName + '[' + key + ']'
  97. const subPart = encodeURIComponent(params) + '='
  98. result += subPart + encodeURIComponent(value[key]) + '&'
  99. }
  100. }
  101. } else {
  102. result += part + encodeURIComponent(value) + '&'
  103. }
  104. }
  105. }
  106. return result
  107. }
  108. // 转换字符串,undefined,null等转化为""
  109. export function parseStrEmpty(str: string | number | undefined) {
  110. if (!str || str === 'undefined' || str === 'null') {
  111. return ''
  112. }
  113. return str
  114. }
  115. // 验证是否为blob格式
  116. export function blobValidate(data: any) {
  117. return data.type !== 'application/json'
  118. }
  119. export const setEnumMap = async (enumMap, { prop, enum: enumValue }: any) => {
  120. if (!enumValue) return
  121. // 如果当前 enumMap 存在相同的值 return
  122. if (enumMap.value.has(prop!) && (typeof enumValue === 'function' || enumMap.value.get(prop!) === enumValue)) return
  123. // 当前 enum 为静态数据,则直接存储到 enumMap
  124. if (typeof enumValue !== 'function') return enumMap.value.set(prop!, unref(enumValue!))
  125. // 为了防止接口执行慢,而存储慢,导致重复请求,所以预先存储为[],接口返回后再二次存储
  126. enumMap.value.set(prop!, [])
  127. // 当前 enum 为后台数据需要请求数据,则调用该请求接口,并存储到 enumMap
  128. let { data } = await enumValue()
  129. enumMap.value.set(prop!, data)
  130. }
  131. export const handelEnum = (enumMap, item: any) => {
  132. let enumData = enumMap.value.get(item.formItem.prop)
  133. if (!enumData) return []
  134. // 返回对象类型
  135. if (item.type === 'select-tree' && item.attrs?.labelKey && item.attrs?.valueKey) {
  136. enumData = enumData.map((el: { [key: string]: any }) => {
  137. return {
  138. ...el,
  139. label: el[item.attrs?.labelKey || 'label'],
  140. children: el[item.attrs?.children || 'children'] || []
  141. }
  142. })
  143. }
  144. return enumData
  145. }
  146. // 对象转成指定字符串分隔
  147. export const listToString = (list: any[], separator?: string) => {
  148. let strings = ''
  149. separator = separator || ','
  150. for (let i in list) {
  151. if (undefined !== list[i].ossId && list[i].url.indexOf('blob:') !== 0) {
  152. strings += list[i].ossId + separator
  153. }
  154. }
  155. return strings != '' ? strings.substring(0, strings.length - 1) : ''
  156. }