This commit is contained in:
539943419 committed 2024-08-10 18:57:12 +08:00
1 parent d9363d3b93
commit a668b3b840
13 files changed
+554 -145

No files matched your search

+20
View File
@@ -0,0 +1,20 @@
export default function dateFormatter(date: Date, fmt: string) {
var o:any = {
"M+": date.getMonth() + 1, //月份
"d+": date.getDate(), //日
"h+": date.getHours(), //小时
"m+": date.getMinutes(), //分
"s+": date.getSeconds(), //秒
"q+": Math.floor((date.getMonth() + 3) / 3), //季度
"S": date.getMilliseconds() //毫秒
};
if (/(y+)/.test(fmt)) {
fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
}
for (var k in o) {
if (new RegExp("(" + k + ")").test(fmt)) {
fmt = fmt.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
}
}
return fmt;
}
+22 -7
View File
@@ -2,14 +2,30 @@ import config from "@/config";
const { uploadSizeLimit } = config;
type dropHandlerHooks = {
onDrop?: (files: File[]) => void;
onEnter?: () => void;
export type dropHandlerHooks = {
/**
* 仅返回小于config.ts中大小限制的文件,不会返回文件夹
*/
onDropFiles?: (files: File[]) => void;
/**
* 当拖动时
*/
onOver?: () => void;
/**
* 当进入时
*/
onEnter?: () => void;
/**
* 当离开时
*/
onLeave?: () => void;
/**
* 离开或是放下时都会触发
* @returns {any}
*/
onEnd?: () => void;
};
const isFile = (file: File) =>
@@ -26,7 +42,6 @@ const isFile = (file: File) =>
reader.readAsText(file);
});
/**
* 只保留文件,移去文件夹
* @param {File[]} files
@@ -36,7 +51,6 @@ const filesFilter = async (files: File[]) => {
while (index < files.length) {
const currentFile = files[index];
const _isFile = await isFile(currentFile);
console.log(_isFile);
if (_isFile) {
index += 1;
continue;
@@ -48,7 +62,6 @@ const filesFilter = async (files: File[]) => {
};
export default function dropHandler(dom: HTMLDivElement, hooks?: dropHandlerHooks) {
console.log(dom);
dom.addEventListener(
"dragenter",
function (e) {
@@ -75,6 +88,7 @@ export default function dropHandler(dom: HTMLDivElement, hooks?: dropHandlerHook
e.preventDefault();
e.stopPropagation();
hooks?.onLeave && hooks?.onLeave();
hooks?.onEnd && hooks.onEnd();
},
false
);
@@ -87,7 +101,8 @@ export default function dropHandler(dom: HTMLDivElement, hooks?: dropHandlerHook
e.stopPropagation();
const files = [...(e.dataTransfer?.files ?? [])].filter(file => file.size < uploadSizeLimit);
await filesFilter(files);
hooks?.onDrop && hooks.onDrop(files);
hooks?.onDropFiles && hooks.onDropFiles(files);
hooks?.onEnd && hooks.onEnd();
},
false
);
+16
View File
@@ -0,0 +1,16 @@
const idList: number[] = [];
/**
* 生成一个唯一的id
* @param {any} length 生成id的长度
* @returns {number}
*/
export default function generateId(length?: number) {
length || (length = 16);
let id;
while (!id || idList.includes(id)) {
id = Math.ceil(Math.random() * 10 ** 16);
}
idList.push(id)
return id
}
+15
View File
@@ -0,0 +1,15 @@
const dataStorage: any = {};
export default function useDataBus() {
return {
set(key: string | number, value: any) {
dataStorage[key] = value;
return value;
},
get(key: string | number) {
const value = dataStorage[key];
dataStorage[key] = undefined;
return value;
}
};
}
+22
View File
@@ -0,0 +1,22 @@
type miaoFetchConfig = {
retry: number;
};
export default function useMiaoFetch(config: miaoFetchConfig) {
const { retry } = config;
return async (input: RequestInfo | URL, init?: RequestInit) => {
let retryTimes = 0;
let err: any;
while (retryTimes <= retry) {
try {
const response = await fetch(input, init);
return response;
} catch (e) {
err = e;
retryTimes += 1;
}
}
throw err;
};
}