whiteUrlManager.js
1.66 KB
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
/*
* @Descripttion:
* @version: 1.0.0
* @Author: limin
* @Date: 2025-09-09 09:49:02
* @LastEditors: limin
* @LastEditTime: 2025-09-09 09:49:33
* @FilePath: \glsx-common\src\whiteUrlManager.js
*/
// 使用IIFE和闭包来管理白名单状态
const WhiteUrlManager = (() => {
let cachedWhiteUrl = null
let fetchPromise = null
// 返回获取白名单的函数
return {
getWhiteUrls: () => {
// 如果已有缓存,直接返回Promise
if (cachedWhiteUrl) {
return Promise.resolve(cachedWhiteUrl)
}
// 如果已经有一个请求在进行中,返回该Promise
if (fetchPromise) {
return fetchPromise
}
// 创建新的Promise并保存引用
fetchPromise = fetch('https://config-center-backend.glsx.com.cn/glsxAdmin/config?moduleUrl=glsx-common')
.then(response => response.json())
.then(result => {
cachedWhiteUrl = result.whiteUrl || []
if (Array.isArray(cachedWhiteUrl)) {
// 过滤掉http和https开头的url
cachedWhiteUrl = cachedWhiteUrl.map(url => url.replace(/^https?:/, ''))
}
return cachedWhiteUrl
})
.catch(error => {
console.error('获取配置失败:', error)
cachedWhiteUrl = []
return []
})
.finally(() => {
fetchPromise = null
})
return fetchPromise
}
}
// 添加清除缓存的方法
clearCache: () => {
cachedWhiteUrl = null
fetchPromise = null
return true
}
})()
// 模块加载时立即开始预加载白名单,但不阻塞后续代码执行
WhiteUrlManager.getWhiteUrls()
export default WhiteUrlManager