feat: add deploy script
This commit is contained in:
78
src/lib/ipLocation.ts
Normal file
78
src/lib/ipLocation.ts
Normal file
@@ -0,0 +1,78 @@
|
||||
/**
|
||||
* IP 地理位置检测工具
|
||||
* 用于根据用户IP地址判断语言偏好
|
||||
*/
|
||||
|
||||
interface IPLocationResponse {
|
||||
country_code?: string;
|
||||
country?: string;
|
||||
error?: boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据IP地址检测用户所在国家/地区
|
||||
* 使用免费的 ipapi.co 服务(无需API key)
|
||||
*
|
||||
* @returns Promise<string | null> 返回国家代码(如 'CN', 'US'),失败返回 null
|
||||
*/
|
||||
export async function detectCountryByIP(): Promise<string | null> {
|
||||
try {
|
||||
// 使用 ipapi.co 免费服务(无需API key,有速率限制但足够使用)
|
||||
const response = await fetch('https://ipapi.co/json/', {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
'Accept': 'application/json',
|
||||
},
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
console.warn('IP location detection failed:', response.status);
|
||||
return null;
|
||||
}
|
||||
|
||||
const data: IPLocationResponse = await response.json();
|
||||
|
||||
if (data.error || !data.country_code) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return data.country_code.toUpperCase();
|
||||
} catch (error) {
|
||||
// 静默失败,不影响用户体验
|
||||
console.warn('IP location detection error:', error);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据国家代码判断应该使用的语言
|
||||
*
|
||||
* @param countryCode 国家代码(如 'CN', 'TW', 'HK', 'SG' 等)
|
||||
* @returns 'zh' | 'en' 推荐的语言
|
||||
*/
|
||||
export function getLanguageByCountry(countryCode: string | null): 'zh' | 'en' {
|
||||
if (!countryCode) {
|
||||
return 'en';
|
||||
}
|
||||
|
||||
// 中文地区列表
|
||||
const chineseRegions = [
|
||||
'CN', // 中国大陆
|
||||
'TW', // 台湾
|
||||
'HK', // 香港
|
||||
'MO', // 澳门
|
||||
'SG', // 新加坡(主要使用中文)
|
||||
];
|
||||
|
||||
return chineseRegions.includes(countryCode) ? 'zh' : 'en';
|
||||
}
|
||||
|
||||
/**
|
||||
* 检测用户IP并返回推荐语言
|
||||
*
|
||||
* @returns Promise<'zh' | 'en'> 推荐的语言
|
||||
*/
|
||||
export async function detectLanguageByIP(): Promise<'zh' | 'en'> {
|
||||
const countryCode = await detectCountryByIP();
|
||||
return getLanguageByCountry(countryCode);
|
||||
}
|
||||
Reference in New Issue
Block a user