修改标识

This commit is contained in:
CN-JS-HuiBai
2026-04-05 01:36:54 +08:00
parent e66905e57f
commit 6b82cfb561

View File

@@ -9,6 +9,24 @@ const db = require('./db');
const ipInfoToken = process.env.IPINFO_TOKEN; const ipInfoToken = process.env.IPINFO_TOKEN;
/**
* Normalizes geo data for consistent display
*/
function normalizeGeo(geo) {
if (!geo) return geo;
// Custom normalization for TW, HK, MO to "China, {CODE}"
const specialRegions = ['TW'];
if (specialRegions.includes(geo.country?.toUpperCase())) {
return {
...geo,
city: `China, ${geo.country.toUpperCase()}`,
country_name: 'China'
};
}
return geo;
}
async function getLocation(ip) { async function getLocation(ip) {
// Normalize IP (strip port if present) // Normalize IP (strip port if present)
const cleanIp = ip.split(':')[0]; const cleanIp = ip.split(':')[0];
@@ -26,7 +44,7 @@ async function getLocation(ip) {
const data = rows[0]; const data = rows[0];
const age = Date.now() - new Date(data.last_updated).getTime(); const age = Date.now() - new Date(data.last_updated).getTime();
if (age < 30 * 24 * 60 * 60 * 1000) { if (age < 30 * 24 * 60 * 60 * 1000) {
return data; return normalizeGeo(data);
} }
} }
@@ -34,7 +52,7 @@ async function getLocation(ip) {
console.log(`[Geo Service] Resolving location for IP: ${cleanIp}`); console.log(`[Geo Service] Resolving location for IP: ${cleanIp}`);
const url = `https://ipinfo.io/${cleanIp}/json${ipInfoToken ? `?token=${ipInfoToken}` : ''}`; const url = `https://ipinfo.io/${cleanIp}/json${ipInfoToken ? `?token=${ipInfoToken}` : ''}`;
const response = await axios.get(url, { timeout: 5000 }); const response = await axios.get(url, { timeout: 5000 });
const geo = response.data; const geo = normalizeGeo(response.data);
if (geo && geo.loc) { if (geo && geo.loc) {
const [lat, lon] = geo.loc.split(',').map(Number); const [lat, lon] = geo.loc.split(',').map(Number);