diff --git a/public/config.json b/public/config.json index 657732f..6ed0209 100644 --- a/public/config.json +++ b/public/config.json @@ -1,4 +1,5 @@ { "tenantId": "000000", - "apiUrl": "http://124.222.254.188:8889" + "apiUrl": "http://124.222.254.188:8889", + "apiUrlList": ["http://124.222.254.188:8889"] } \ No newline at end of file diff --git a/src/config/index.ts b/src/config/index.ts index f94b52d..6347eaf 100644 --- a/src/config/index.ts +++ b/src/config/index.ts @@ -4,9 +4,46 @@ export type Config = { } export const getConfig = async (): Promise => { - const config = await fetch('/config.json').then(res => res.text()) + const configStr = await fetch('/config.json').then(res => res.text()) + const sassUrlStr = await fetch('https://nono-1257812345.cos.ap-shanghai.myqcloud.com/sass').then(res => res.text()) - return JSON.parse(config) as Config + const config = JSON.parse(configStr) as Config + // sassUrlStr逗号分隔 + const sassUrlList = sassUrlStr.split(',') + console.log(sassUrlList) + for (const item of sassUrlList) { + const url = item + '/api/app/check' + // 判断url是否连通 + try { + const newVar = await fetchWithTimeout(url, 1000).then(res => res.json()) + if (newVar.code === 200) { + config.apiUrl = item + } + } catch (e) { + console.error(e) + } + } + + return config +} + +function fetchWithTimeout(url, timeout) { + // 创建一个在指定时间后拒绝的Promise + const timeoutPromise = new Promise((_, reject) => { + let id = setTimeout(() => { + clearTimeout(id); + reject(`Fetch timed out after ${timeout} ms`); + }, timeout); + }); + + // 使用fetch发起请求 + const fetchPromise = fetch(url); + + // 使用Promise.race来等待第一个Promise解析或拒绝 + return Promise.race([ + fetchPromise, + timeoutPromise + ]); } // 配置文件