other.ts 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import type { App } from 'vue';
  2. /**
  3. * 对象深克隆
  4. * @param obj 源对象
  5. * @returns 克隆后的对象
  6. */
  7. export function deepClone(obj: EmptyObjectType) {
  8. let newObj: EmptyObjectType;
  9. try {
  10. newObj = obj.push ? [] : {};
  11. } catch (error) {
  12. newObj = {};
  13. }
  14. for (let attr in obj) {
  15. if (obj[attr] && typeof obj[attr] === 'object') {
  16. newObj[attr] = deepClone(obj[attr]);
  17. } else {
  18. newObj[attr] = obj[attr];
  19. }
  20. }
  21. return newObj;
  22. }
  23. /**
  24. * 判断数组对象中所有属性是否为空,为空则删除当前行对象
  25. * @description @感谢大黄
  26. * @param list 数组对象
  27. * @returns 删除空值后的数组对象
  28. */
  29. export function handleEmpty(list: EmptyArrayType) {
  30. const arr = [];
  31. for (const i in list) {
  32. const d = [];
  33. for (const j in list[i]) {
  34. d.push(list[i][j]);
  35. }
  36. const leng = d.filter((item) => item === '').length;
  37. if (leng !== d.length) {
  38. arr.push(list[i]);
  39. }
  40. }
  41. return arr;
  42. }
  43. export function getDictLabel(arr: RowDicDataType[], val:string) {
  44. if(arr.length) {
  45. const obj = arr.find(item => item.dictValue == val)
  46. if(obj) return obj.dictLabel
  47. return val
  48. }
  49. return val
  50. }
  51. /**
  52. * 防抖函数
  53. * @param func 要执行的函数
  54. * @param delay 延迟时间(毫秒)
  55. * @returns 防抖后的函数
  56. */
  57. export function debounce(func: Function, delay: number) {
  58. let timer: number | null = null
  59. return (...args: any[]) => {
  60. if (timer) {
  61. window.clearTimeout(timer)
  62. }
  63. timer = window.setTimeout(() => {
  64. func(...args)
  65. timer = null
  66. }, delay)
  67. }
  68. }
  69. /**
  70. * 统一批量导出
  71. * @method elSvg 导出全局注册 element plus svg 图标
  72. * @method useTitle 设置浏览器标题国际化
  73. * @method setTagsViewNameI18n 设置 自定义 tagsView 名称、 自定义 tagsView 名称国际化
  74. * @method lazyImg 图片懒加载
  75. * @method globalComponentSize() element plus 全局组件大小
  76. * @method deepClone 对象深克隆
  77. * @method isMobile 判断是否是移动端
  78. * @method handleEmpty 判断数组对象中所有属性是否为空,为空则删除当前行对象
  79. * @method handleOpenLink 打开外部链接
  80. * @method debounce 防抖函数
  81. */
  82. const other = {
  83. deepClone: (obj: EmptyObjectType) => {
  84. return deepClone(obj);
  85. },
  86. handleEmpty: (list: EmptyArrayType) => {
  87. return handleEmpty(list);
  88. },
  89. getDictLabel: (arr: RowDicDataType[], val:string) => {
  90. return getDictLabel(arr, val)
  91. },
  92. debounce: (func: Function, delay: number) => {
  93. return debounce(func, delay)
  94. }
  95. };
  96. // 统一批量导出
  97. export default other;