wujianwei
2025-08-15 cf3ded70bae93c841633db920fe851db541dad72
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
 
import {defineStore} from "pinia";
 
export const useTagsViewStore = defineStore('tagsView', {
    state: () => ({
        visitedViews: [] as any[], // 访问过的视图
        cachedViews: [] as string[], // 缓存的视图
    }),
 
    actions: {
        // 添加访问过的视图
        addVisitedView(view: any) {
            if (this.visitedViews.some(v => v.path === view.path)) return;
            this.visitedViews.push({
                ...view,
                title: view.meta.title || 'no-name',
            });
        },
        delView( view:any) {
            return new Promise(resolve => {
                this.delVisitedView(view);
                this.delCachedView(view);
                resolve({
                    visitedViews: [...this.visitedViews],
                    cachedViews: [...this.cachedViews]
                })
            })
        },
        // 添加缓存视图
        addCachedView(view: any) {
            if (this.cachedViews.includes(view.name)) return;
            if (!view.meta.noCache) {
                this.cachedViews.push(view.name);
            }
        },
 
        // 删除访问过的视图
        delVisitedView(view: any) {
            this.visitedViews = this.visitedViews.filter(v => v.path !== view.path);
        },
 
        // 删除缓存视图
        delCachedView(view: any) {
            return new Promise((resolve, reject) => {
                const index = this.cachedViews.indexOf(view.name);
                if (index > -1) {
                    this.cachedViews.splice(index, 1);
                }
                return resolve(index);
            })
        },
        delAllViews( ) {
            return new Promise(resolve => {
                this.delAllVisitedViews()
                this.delAllCachedViews()
                resolve({
                    visitedViews: [...this.visitedViews],
                    cachedViews: [...this.cachedViews]
                })
            })
        },
        delOthersViews( view:any) {
            return new Promise(resolve => {
                this.delOthersVisitedViews( view)
                this.delOthersCachedViews( view)
                resolve({
                    visitedViews: [...this.visitedViews],
                    cachedViews: [...this.cachedViews]
                })
            })
        },
        // 删除其他访问过的视图
        delOthersVisitedViews(view: any) {
            this.visitedViews = this.visitedViews.filter(v => v.meta.affix || v.path === view.path);
        },
 
        // 删除其他缓存视图
        delOthersCachedViews(view: any) {
            const index = this.cachedViews.indexOf(view.name);
            if (index > -1) {
                this.cachedViews = this.cachedViews.slice(index, index + 1);
            } else {
                this.cachedViews = [];
            }
        },
 
        // 删除所有访问过的视图
        delAllVisitedViews() {
            this.visitedViews = this.visitedViews.filter(tag => tag.meta.affix);
        },
 
        // 删除所有缓存视图
        delAllCachedViews() {
            this.cachedViews = [];
        },
 
        // 更新访问过的视图
        updateVisitedView(view: any) {
            for (const v of this.visitedViews) {
                if (v.path === view.path) {
                    Object.assign(v, view);
                    break;
                }
            }
        },
 
        // 删除右侧标签
        delRightViews(view: any) {
            const index = this.visitedViews.findIndex(v => v.path === view.path);
            if (index === -1) return;
 
            this.visitedViews = this.visitedViews.filter((item, idx) => {
                if (idx <= index || (item.meta && item.meta.affix)) {
                    return true;
                }
                const i = this.cachedViews.indexOf(item.name);
                if (i > -1) {
                    this.cachedViews.splice(i, 1);
                }
                return false;
            });
        },
 
        // 删除左侧标签
        delLeftViews(view: any) {
            const index = this.visitedViews.findIndex(v => v.path === view.path);
            if (index === -1) return;
 
            this.visitedViews = this.visitedViews.filter((item, idx) => {
                if (idx >= index || (item.meta && item.meta.affix)) {
                    return true;
                }
                const i = this.cachedViews.indexOf(item.name);
                if (i > -1) {
                    this.cachedViews.splice(i, 1);
                }
                return false;
            });
        },
    },
});