Files
AstrBot/dashboard/src/stores/routerLoading.ts

61 lines
1.6 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { defineStore } from 'pinia';
import { ref } from 'vue';
export const useRouterLoadingStore = defineStore('routerLoading', () => {
const isLoading = ref(false);
const progress = ref(0);
let progressInterval: ReturnType<typeof setInterval> | null = null;
function start() {
isLoading.value = true;
progress.value = 0;
if (progressInterval) {
clearInterval(progressInterval);
}
let currentProgress = 0;
progressInterval = setInterval(() => {
if (currentProgress < 80) {
// 快速阶段0-80%
currentProgress += Math.random() * 20 + 10;
if (currentProgress > 80) {
currentProgress = 80;
}
} else if (currentProgress < 90) {
// 缓慢阶段80-90%
currentProgress += Math.random() * 3 + 1;
if (currentProgress > 90) {
currentProgress = 90;
}
}
progress.value = Math.min(currentProgress, 90);
}, 50);
}
function finish() {
// 清理interval
if (progressInterval) {
clearInterval(progressInterval);
progressInterval = null;
}
// 快速完成到100%
progress.value = 100;
// 延迟隐藏让用户看到100%
setTimeout(() => {
isLoading.value = false;
progress.value = 0;
}, 300);
}
return {
isLoading,
progress,
start,
finish
};
});