wangzhibo
2026-08-31 17ac2ce0ed52874616c79c910667e41e34604826
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
<template>
  <el-dialog
    :title="$t('cache.dialogTitle')"
    :visible.sync="dialogVisible"
    width="70%"
    :before-close="handleClose"
  >
    <div v-if="isLoading" class="loading-container">
      <p>{{ $t('cache.loading') }}</p>
    </div>
    
    <div v-else>
      <div v-if="!cacheAvailable" class="no-cache-message">
        <i class="el-icon-warning-outline"></i>
        <p>{{ $t('cache.notSupported') }}</p>
        <el-button type="primary" @click="refreshPage">{{ $t('cache.refreshPage') }}</el-button>
      </div>
      
      <div v-else>
        <el-alert
            v-if="cacheData.totalCached === 0"
            :title="$t('cache.noCachedResources')"
            type="warning"
            :closable="false"
            show-icon
          >
            <p>{{ $t('cache.noCachedResourcesDesc') }}</p>
          </el-alert>
        
        <div v-else>
          <el-alert
            :title="$t('cache.cdnCacheStatus')"
            type="success"
            :closable="false"
            show-icon
          >
            {{ $t('cache.totalCachedResources').replace('{count}', cacheData.totalCached) }}
          </el-alert>
          
          <h3>{{ $t('cache.jsResources').replace('{count}', cacheData.js.length) }}</h3>
          <el-table :data="cacheData.js" stripe style="width: 100%">
            <el-table-column prop="url" label="URL" width="auto" show-overflow-tooltip />
            <el-table-column prop="cached" :label="$t('cache.status')" width="100">
              <template slot-scope="scope">
                <el-tag type="success" v-if="scope.row.cached">{{ $t('cache.cached') }}</el-tag>
                <el-tag type="danger" v-else>{{ $t('cache.notCached') }}</el-tag>
              </template>
            </el-table-column>
          </el-table>
          
          <h3>{{ $t('cache.cssResources').replace('{count}', cacheData.css.length) }}</h3>
          <el-table :data="cacheData.css" stripe style="width: 100%">
            <el-table-column prop="url" label="URL" width="auto" show-overflow-tooltip />
            <el-table-column prop="cached" label="状态" width="100">
              <template slot-scope="scope">
                <el-tag type="success" v-if="scope.row.cached">已缓存</el-tag>
                <el-tag type="danger" v-else>未缓存</el-tag>
              </template>
            </el-table-column>
          </el-table>
        </div>
      </div>
    </div>
    
    <span slot="footer" class="dialog-footer">
      <el-button @click="handleClose">{{ $t('button.close') }}</el-button>
      <el-button type="primary" @click="refreshCache">{{ $t('cache.refreshStatus') }}</el-button>
      <el-button type="danger" @click="clearCache">{{ $t('cache.clearCache') }}</el-button>
    </span>
  </el-dialog>
</template>
 
<script>
import {
  getCacheNames,
  checkCdnCacheStatus,
  clearAllCaches,
  logCacheStatus
} from '../utils/cacheViewer';
 
export default {
  name: 'CacheViewer',
  props: {
    visible: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      dialogVisible: this.visible,
      isLoading: true,
      cacheAvailable: false,
      cacheData: {
        css: [],
        js: [],
        totalCached: 0,
        totalNotCached: 0
      }
    };
  },
  watch: {
    visible(newVal) {
      this.dialogVisible = newVal;
      if (newVal) {
        this.loadCacheData();
      }
    },
    dialogVisible(val) {
      this.$emit('update:visible', val);
    }
  },
  methods: {
    async loadCacheData() {
      this.isLoading = true;
      
      try {
        // 先检查是否支持缓存API
        if (!('caches' in window)) {
          this.cacheAvailable = false;
          this.isLoading = false;
          return;
        }
        
        // 检查是否有Service Worker缓存
        const cacheNames = await getCacheNames();
        this.cacheAvailable = cacheNames.length > 0;
        
        if (this.cacheAvailable) {
          // 获取CDN缓存状态
          this.cacheData = await checkCdnCacheStatus();
          
          // 在控制台输出完整缓存状态
          await logCacheStatus();
        }
      } catch (error) {
        console.error('加载缓存数据失败:', error);
        this.$message.error('加载缓存数据失败');
      } finally {
        this.isLoading = false;
      }
    },
    
    async refreshCache() {
      this.loadCacheData();
      this.$message.success(this.$t('cache.refreshingStatus'));
    },
    
    async clearCache() {
      this.$confirm(this.$t('cache.confirmClear'), this.$t('message.warning'), {
        confirmButtonText: this.$t('button.ok'),
        cancelButtonText: this.$t('button.cancel'),
        type: 'warning'
      }).then(async () => {
        try {
          const success = await clearAllCaches();
          if (success) {
            this.$message.success(this.$t('cache.clearedSuccess'));
            await this.loadCacheData();
          } else {
            this.$message.error(this.$t('cache.clearFailed'));
          }
        } catch (error) {
          console.error('清除缓存失败:', error);
          this.$message.error(this.$t('cache.clearFailed'));
        }
      }).catch(() => {
        this.$message.info(this.$t('cache.clearCanceled'));
      });
    },
    
    refreshPage() {
      window.location.reload();
    },
    
    handleClose(done) {
      this.dialogVisible = false;
      if (typeof done === 'function') {
        done();
      }
    }
  }
};
</script>
 
<style scoped>
.loading-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 20px;
}
 
.loading-spinner {
  margin-bottom: 10px;
}
 
.no-cache-message {
  text-align: center;
  padding: 20px;
}
 
.no-cache-message i {
  font-size: 48px;
  color: #E6A23C;
  margin-bottom: 10px;
}
 
h3 {
  margin-top: 20px;
  margin-bottom: 10px;
  font-weight: 500;
}
</style>