common.ts 4.9 KB

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