File size: 1,613 Bytes
8ede856
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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
    };
});