|
|
@@ -8,40 +8,54 @@ type Options = Parameters<typeof ElLoading.service>[0];
|
|
|
export const genLoading = <T, K extends PFN<T>>(fn: K, options?: Options): K =>
|
|
|
((...args) => loading(() => fn(...args), options)) as K;
|
|
|
|
|
|
-const loadingStack = ref<Array<Options | undefined>>([])
|
|
|
-const tokens: string[] = []
|
|
|
+const loadingStack = ref<Array<Options | undefined>>([]);
|
|
|
+const tokens: string[] = [];
|
|
|
let instance: ReturnType<typeof ElLoading.service> | null = null;
|
|
|
watchEffect(() => {
|
|
|
if (!loadingStack.value.length && instance) {
|
|
|
- instance.close()
|
|
|
- instance = null
|
|
|
+ instance.close();
|
|
|
+ instance = null;
|
|
|
}
|
|
|
if (loadingStack.value.length && !instance && !import.meta.env.DEV) {
|
|
|
- instance = ElLoading.service(loadingStack.value[0])
|
|
|
+ instance = ElLoading.service(loadingStack.value[0]);
|
|
|
}
|
|
|
-})
|
|
|
+});
|
|
|
|
|
|
+let showErrIng = false;
|
|
|
+const error = (e: string) => {
|
|
|
+ if (!showErrIng) {
|
|
|
+ showErrIng = true;
|
|
|
+ ElMessage({
|
|
|
+ type: "error",
|
|
|
+ message: e,
|
|
|
+ duration: 3000,
|
|
|
+ onClose() {
|
|
|
+ showErrIng = false;
|
|
|
+ },
|
|
|
+ });
|
|
|
+ }
|
|
|
+};
|
|
|
export const loading = <T, K extends PFN<T>>(
|
|
|
fn: K | Promise<T>,
|
|
|
options?: Options
|
|
|
): Promise<T> => {
|
|
|
- loadingStack.value.push(options as any)
|
|
|
- const token = onlyId()
|
|
|
- tokens.push(token)
|
|
|
+ loadingStack.value.push(options as any);
|
|
|
+ const token = onlyId();
|
|
|
+ tokens.push(token);
|
|
|
|
|
|
- console.log('开始请求')
|
|
|
const ret = typeof fn === "function" ? fn() : fn;
|
|
|
ret
|
|
|
.catch((e) => {
|
|
|
- ElMessage({ type: "error", message: e });
|
|
|
+ error(e);
|
|
|
+
|
|
|
throw e;
|
|
|
})
|
|
|
.finally(() => {
|
|
|
setTimeout(() => {
|
|
|
- const ndx = tokens.indexOf(token)
|
|
|
- loadingStack.value.splice(ndx, 1)
|
|
|
- tokens.splice(ndx, 1)
|
|
|
- }, 50)
|
|
|
+ const ndx = tokens.indexOf(token);
|
|
|
+ loadingStack.value.splice(ndx, 1);
|
|
|
+ tokens.splice(ndx, 1);
|
|
|
+ }, 50);
|
|
|
});
|
|
|
return ret;
|
|
|
};
|