index.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. import { getUrlType } from './meta';
  2. // 加载第三方库
  3. export const loadLib = (() => {
  4. const cache: Record<string, Promise<void>> = {};
  5. const load = (
  6. lib: string,
  7. success: () => void,
  8. err: () => void,
  9. maxReq = 0
  10. ) => {
  11. const el = document.createElement("script");
  12. el.src = lib;
  13. document.body.appendChild(el);
  14. el.onload = success;
  15. el.onerror = () => {
  16. if (maxReq > 0) {
  17. load(lib, success, err, --maxReq);
  18. } else {
  19. err();
  20. }
  21. };
  22. };
  23. return (lib: string) => {
  24. if (!cache[lib]) {
  25. cache[lib] = new Promise((resolve, reject) => {
  26. load(lib, resolve, reject, 3);
  27. });
  28. }
  29. return cache[lib];
  30. };
  31. })();
  32. export const getSizeStr = (size: number) => {
  33. let mb = size;
  34. if (mb < 1024) {
  35. return `${mb.toFixed(2)}BYTE`;
  36. }
  37. mb /= 1024;
  38. if (mb < 1024) {
  39. return `${mb.toFixed(2)}KB`;
  40. }
  41. mb /= 1024;
  42. if (mb < 1024) {
  43. return `${mb.toFixed(2)}MB`;
  44. }
  45. mb /= 1024;
  46. if (mb < 1024) {
  47. return `${mb.toFixed(2)}GB`;
  48. }
  49. mb /= 1024;
  50. return `${mb.toFixed(2)}TB`;
  51. };
  52. export const togetherCallback = (cbs: (() => void)[]) => () => together(cbs)
  53. export const together = (cbs: (() => void)[]) => {
  54. cbs.forEach(cb => cb())
  55. }
  56. export const getFileUrl = (file: LocalFile | string) =>
  57. typeof file === 'string'
  58. ? file
  59. : file.url
  60. export const getFileName = (file: any): string => {
  61. return typeof file === 'string'
  62. ? file.substring(file.indexOf('/') + 1)
  63. : file.blob instanceof File ? file.blob.name : getUrlType(file.url)
  64. }
  65. export const asyncTimeout = (mis: number = 0) => new Promise(resolve => setTimeout(resolve, mis))
  66. export const jsonToForm = (data: { [key in string]: any }) => {
  67. const formData = new FormData()
  68. for (const [key, val] of Object.entries(data)) {
  69. if (Array.isArray(val)) {
  70. for (let i = 0; i < val.length; i++) {
  71. formData.append(`${key}`, val[i])
  72. }
  73. } else {
  74. formData.append(key, val)
  75. }
  76. }
  77. return formData
  78. }
  79. // 防抖
  80. export const debounce = <T extends (...args: any) => any>(
  81. fn: T,
  82. delay: number = 160
  83. ) => {
  84. let timeout: any;
  85. return function (...args: Parameters<T>) {
  86. clearTimeout(timeout);
  87. timeout = setTimeout(() => {
  88. fn.apply(null, args);
  89. }, delay);
  90. };
  91. };
  92. // 防抖
  93. export const debounceStack = <T extends (...args: any) => any>(
  94. fn: T,
  95. success: (data: ReturnType<T> extends Promise<infer P> ? P : ReturnType<T>) => void,
  96. delay: number = 160
  97. ) => {
  98. let dsToken = 0
  99. return debounce(async (...args: Parameters<T>) => {
  100. ++dsToken
  101. const currentToken = dsToken
  102. const result = await fn.apply(null, args);
  103. if (currentToken === dsToken) {
  104. success(result)
  105. }
  106. }, delay)
  107. };
  108. // 四舍五入保留指定位数
  109. export const round = (num: number, index: number = 2) => {
  110. const s = Math.pow(10, index)
  111. return Math.round(num * s) / s
  112. }
  113. export * from './store-help'
  114. export * from "./stack";
  115. export * from "./loading";
  116. export * from "./route";
  117. export * from "./asyncBus";
  118. export * from './mount'
  119. export * from './watch'
  120. export * from './diff'
  121. export * from './params'
  122. export * from './file-serve'
  123. export * from './video-cover'
  124. export * from './meta'