ossConfigService.js 1.03 KB
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