配置文件

This commit is contained in:
John
2024-05-22 00:06:13 +08:00
parent bfbec3a56e
commit e38a67e782
2 changed files with 41 additions and 3 deletions

View File

@@ -1,4 +1,5 @@
{ {
"tenantId": "000000", "tenantId": "000000",
"apiUrl": "http://124.222.254.188:8889" "apiUrl": "http://124.222.254.188:8889",
"apiUrlList": ["http://124.222.254.188:8889"]
} }

View File

@@ -4,9 +4,46 @@ export type Config = {
} }
export const getConfig = async (): Promise<Config> => { export const getConfig = async (): Promise<Config> => {
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
]);
} }
// 配置文件 // 配置文件