Commit 8c9464d0 by liimin

refactor(whiteUrl): 重构白名单管理逻辑为独立模块

将原有的ossConfigService.js中的白名单获取逻辑重构为独立的whiteUrlManager模块
使用IIFE和闭包管理状态,添加缓存和并发请求处理
在http.js中更新引用并添加缓存清理功能
1 parent 943d0e34
let cachedWhiteUrl = null
/**
* 从配置服务获取OSS基础配置并返回 whiteUrl
* @returns {Promise<string>} whiteUrl 字符串
* @throws {Error} 当获取配置失败时抛出错误
*/
async function fetchWhiteUrl() {
// console.log(cachedWhiteUrl, 'cachedWhiteUrl')
// 如果已有缓存,直接返回
if (cachedWhiteUrl) {
return cachedWhiteUrl
}
try {
const response = await fetch('https://config-center-backend.glsx.com.cn/glsxAdmin/config?moduleUrl=glsx-common')
if (!response.ok) {
throw new Error('网络响应不是 OK')
}
const result = await response.json()
cachedWhiteUrl = result.whiteUrl // 缓存结果
cachedWhiteUrl.forEach((url, index) => {
cachedWhiteUrl[index] = url.startsWith('http:') ? url.substring(5) : url
})
console.log('获取配置成功:', cachedWhiteUrl)
return cachedWhiteUrl
} catch (error) {
console.error('获取配置失败:', error)
throw error
}
}
// 直接导出异步获取 whiteUrl 的函数
export default fetchWhiteUrl
...@@ -7,8 +7,7 @@ ...@@ -7,8 +7,7 @@
import util from './util' import util from './util'
import axios from 'axios' import axios from 'axios'
import qs from 'qs' import qs from 'qs'
import OssConfigService from '../api/ossConfigService' // 导入配置服务 import WhiteUrlManager from './whiteUrlManager' // 导入白名单管理器
function Http(options = {}) { function Http(options = {}) {
this.service = axios.create({ this.service = axios.create({
withCredentials: true, withCredentials: true,
...@@ -27,17 +26,15 @@ function Http(options = {}) { ...@@ -27,17 +26,15 @@ function Http(options = {}) {
Object.assign(this.service.defaults, { headers: Object.assign({ 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8' }, headers) }, { baseURL, timeout, withCredentials, responseType }) Object.assign(this.service.defaults, { headers: Object.assign({ 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8' }, headers) }, { baseURL, timeout, withCredentials, responseType })
return this.create() return this.create()
} }
Http.prototype.create = function () { Http.prototype.create = function () {
this.service.interceptors.request.use( this.service.interceptors.request.use(
async config => { async config => {
let { data = {}, sign = true, noSignAttr = [], noSessionId = false } = config let { data = {}, sign = true, noSignAttr = [], noSessionId = false } = config
const whiteUrl = await OssConfigService() // 获取白名单并检查接口
console.log(whiteUrl, 'whiteUrl') const whiteUrl = await WhiteUrlManager.getWhiteUrls();
console.log(data?.method, 'whiteUrl')
console.log(whiteUrl.includes(data?.method))
if (whiteUrl.includes(data?.method)) { if (whiteUrl.includes(data?.method)) {
data.sdctag = true data.sdctag = true;
} }
if (!noSessionId) { if (!noSessionId) {
data.sessionId = this.customer.sessionId || data.sessionId data.sessionId = this.customer.sessionId || data.sessionId
...@@ -107,6 +104,7 @@ Http.prototype.create = function () { ...@@ -107,6 +104,7 @@ Http.prototype.create = function () {
return this.service return this.service
} }
function logout(logout_function) { function logout(logout_function) {
WhiteUrlManager.clearCache();
if (window[logout_function]) { if (window[logout_function]) {
window[logout_function](window.location.href) window[logout_function](window.location.href)
return return
......
/*
* @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)) {
cachedWhiteUrl.forEach((url, index) => {
cachedWhiteUrl[index] = url.startsWith('http:') ? url.substring(5) : url
})
}
console.log('获取配置成功:', cachedWhiteUrl)
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
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!