feat: about页面添加公司地址跳转功能
All checks were successful
deploy to server / build-and-deploy (push) Successful in 3m21s
All checks were successful
deploy to server / build-and-deploy (push) Successful in 3m21s
- 外部链接跳转:点击公司地址时,跳转带外部地图服务商链接 - 智能跳转: 根据用户的连接情况,自动选择google地图或者高德地图
This commit is contained in:
43
app/utils/autoMap.ts
Normal file
43
app/utils/autoMap.ts
Normal file
@ -0,0 +1,43 @@
|
||||
async function testSpeed(url: string, timeout = 1500) {
|
||||
const start = performance.now();
|
||||
|
||||
try {
|
||||
await Promise.race([
|
||||
fetch(url, { method: 'HEAD', mode: 'no-cors' }),
|
||||
new Promise((_, reject) => setTimeout(() => reject('timeout'), timeout)),
|
||||
]);
|
||||
|
||||
return performance.now() - start;
|
||||
} catch {
|
||||
return Infinity; // unreachable or timed out
|
||||
}
|
||||
}
|
||||
|
||||
async function selectBestMap() {
|
||||
const testTargets = {
|
||||
amap: 'https://www.amap.com/favicon.ico',
|
||||
google: 'https://maps.google.com/favicon.ico',
|
||||
};
|
||||
|
||||
const results: Record<string, number> = {};
|
||||
|
||||
for (const key in testTargets) {
|
||||
results[key] = await testSpeed(testTargets[key]);
|
||||
}
|
||||
|
||||
logger.debug(results);
|
||||
|
||||
// 根据延迟排序,选择最稳最快的平台
|
||||
return Object.entries(results).sort((a, b) => a[1] - b[1])[0][0];
|
||||
}
|
||||
|
||||
export async function getAutoMappedService(): Promise<string> {
|
||||
const target = {
|
||||
amap: 'https://surl.amap.com/2dYNorIJ1dgoN',
|
||||
google: 'https://maps.app.goo.gl/9LqvMwEq7VaRkqnM6',
|
||||
};
|
||||
|
||||
const fastestMap = await selectBestMap();
|
||||
|
||||
return target[fastestMap];
|
||||
}
|
||||
Reference in New Issue
Block a user