| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- import { getUrlType } from './meta';
- // 加载第三方库
- export const loadLib = (() => {
- const cache: Record<string, Promise<void>> = {};
- const load = (
- lib: string,
- success: () => void,
- err: () => void,
- maxReq = 0
- ) => {
- const el = document.createElement("script");
- el.src = lib;
- document.body.appendChild(el);
- el.onload = success;
- el.onerror = () => {
- if (maxReq > 0) {
- load(lib, success, err, --maxReq);
- } else {
- err();
- }
- };
- };
- return (lib: string) => {
- if (!cache[lib]) {
- cache[lib] = new Promise((resolve, reject) => {
- load(lib, resolve, reject, 3);
- });
- }
- return cache[lib];
- };
- })();
- export const getSizeStr = (size: number) => {
- let mb = size;
- if (mb < 1024) {
- return `${mb.toFixed(2)}BYTE`;
- }
- mb /= 1024;
- if (mb < 1024) {
- return `${mb.toFixed(2)}KB`;
- }
- mb /= 1024;
- if (mb < 1024) {
- return `${mb.toFixed(2)}MB`;
- }
- mb /= 1024;
- if (mb < 1024) {
- return `${mb.toFixed(2)}GB`;
- }
- mb /= 1024;
- return `${mb.toFixed(2)}TB`;
- };
- export const togetherCallback = (cbs: (() => void)[]) => () => together(cbs)
- export const together = (cbs: (() => void)[]) => {
- cbs.forEach(cb => cb())
- }
- export const getFileUrl = (file: LocalFile | string) =>
- typeof file === 'string'
- ? file
- : file.url
- export const getFileName = (file: any): string => {
- return typeof file === 'string'
- ? file.substring(file.indexOf('/') + 1)
- : file.blob instanceof File ? file.blob.name : getUrlType(file.url)
- }
- export const asyncTimeout = (mis: number = 0) => new Promise(resolve => setTimeout(resolve, mis))
- export const jsonToForm = (data: { [key in string]: any }) => {
- const formData = new FormData()
- for (const [key, val] of Object.entries(data)) {
- if (Array.isArray(val)) {
- for (let i = 0; i < val.length; i++) {
- formData.append(`${key}`, val[i])
- }
- } else {
- formData.append(key, val)
- }
- }
- return formData
- }
- // 防抖
- export const debounce = <T extends (...args: any) => any>(
- fn: T,
- delay: number = 160
- ) => {
- let timeout: any;
- return function (...args: Parameters<T>) {
- clearTimeout(timeout);
- timeout = setTimeout(() => {
- fn.apply(null, args);
- }, delay);
- };
- };
- // 防抖
- export const debounceStack = <T extends (...args: any) => any>(
- fn: T,
- success: (data: ReturnType<T> extends Promise<infer P> ? P : ReturnType<T>) => void,
- delay: number = 160
- ) => {
- let dsToken = 0
- return debounce(async (...args: Parameters<T>) => {
- ++dsToken
- const currentToken = dsToken
- const result = await fn.apply(null, args);
- if (currentToken === dsToken) {
- success(result)
- }
- }, delay)
- };
- // 四舍五入保留指定位数
- export const round = (num: number, index: number = 2) => {
- const s = Math.pow(10, index)
- return Math.round(num * s) / s
- }
- export * from './store-help'
- export * from "./stack";
- export * from "./loading";
- export * from "./route";
- export * from "./asyncBus";
- export * from './mount'
- export * from './watch'
- export * from './diff'
- export * from './params'
- export * from './file-serve'
- export * from './video-cover'
- export * from './meta'
|