根据IP获取用户信息和天气信息的方法
获取ip信息
async function fetchGeolocation(ip) {try {// const response = await fetch(`http://ip-api.com/json/${ip}?fields=status,message,country,regionName,city,isp`);const response = await fetch(`https://ipapi.co/${ip}/json`);if (!response.ok) {throw new Error('Failed to fetch geolocation data');}return await response.json();} catch (error) {console.error('Error fetching geolocation:', error);return null;}
}
"url":"https://ipapi.co/${ip}/json",
"method" : "GET",
"response" :
{"ip": "1.1.1.1","network": "1.1.1.0/24","version": "IPv4","city": "Sydney","region": "New South Wales","region_code": "NSW","country": "AU","country_name": "Australia","country_code": "AU","country_code_iso3": "AUS","country_capital": "Canberra","country_tld": ".au","continent_code": "OC","in_eu": false,"postal": "2000","latitude": -33.859336,"longitude": 151.203624,"timezone": "Australia/Sydney","utc_offset": "+1000","country_calling_code": "+61","currency": "AUD","currency_name": "Dollar","languages": "en-AU","country_area": 7686850.0,"country_population": 24992369,"asn": "AS13335","org": "CLOUDFLARENET"
}
更多信息见:
https://ipapi.co/
获取天气信息
// Function to fetch weather data from api.open-meteo.com
async function getWeather(latitude, longitude, timezone) {const url = "https://api.open-meteo.com/v1/forecast";const params = new URLSearchParams({latitude: latitude,longitude: longitude,current_weather: "true",timezone: timezone});try {const response = await fetch(`${url}?${params}`);if (!response.ok) {throw new Error(`HTTP error! status: ${response.status}`);}const data = await response.json();if (data.current_weather) {return data;} else {return null}} catch (error) {console.error("fetch weather failed", error);return null}
}function getWeatherDescription(code) {const weatherCodeEmojiMap = {0: { emoji: "☀️", en: "Clear sky", zh: "晴天" },1: { emoji: "🌤️", en: "Mainly clear", zh: "少云" },2: { emoji: "⛅", en: "Partly cloudy", zh: "多云" },3: { emoji: "☁️", en: "Overcast", zh: "阴天" },45: { emoji: "🌫️", en: "Fog", zh: "雾" },48: { emoji: "❄️🌫️", en: "Rime fog", zh: "霜雾" },51: { emoji: "🌦️", en: "Light drizzle", zh: "小到中雨(毛毛雨)" },53: { emoji: "🌧️", en: "Moderate drizzle", zh: "中雨(毛毛雨)" },55: { emoji: "🌧️🌧️", en: "Dense drizzle", zh: "大到暴雨(毛毛雨)" },61: { emoji: "🌦️", en: "Light rain", zh: "小雨" },63: { emoji: "🌧️", en: "Moderate rain", zh: "中雨" },65: { emoji: "🌧️🌧️", en: "Heavy rain", zh: "大雨" },71: { emoji: "🌨️", en: "Light snow", zh: "小雪" },73: { emoji: "❄️", en: "Moderate snow", zh: "中雪" },75: { emoji: "❄️❄️", en: "Heavy snow", zh: "大雪" },80: { emoji: "🌦️", en: "Light showers", zh: "小阵雨" },81: { emoji: "🌧️", en: "Moderate showers", zh: "中阵雨" },82: { emoji: "🌧️🌧️", en: "Heavy showers", zh: "大阵雨" },95: { emoji: "⛈️", en: "Thunderstorm", zh: "雷暴" },96: { emoji: "⛈️🌨️", en: "Thunderstorm with light hail", zh: "雷暴伴小冰雹" },99: { emoji: "⛈️❄️", en: "Thunderstorm with heavy hail", zh: "雷暴伴大冰雹" }};return `${weatherCodeEmojiMap[code].emoji} ${weatherCodeEmojiMap[code].en}`;
}
"url":"
https://api.open-meteo.com/v1/forecast?latitude=-33.859336&longitude=151.203624¤t_weather=true&timezone=Australia%2FSydney",
"method" : "GET",
"response" :
{"latitude": -33.75,"longitude": 151.25,"generationtime_ms": 1.6900300979614258,"utc_offset_seconds": 36000,"timezone": "Australia/Sydney","timezone_abbreviation": "GMT+10","elevation": 24.0,"current_weather_units": {"time": "iso8601","interval": "seconds","temperature": "°C","windspeed": "km/h","winddirection": "°","is_day": "","weathercode": "wmo code"},"current_weather": {"time": "2025-09-14T18:00","interval": 900,"temperature": 20.8,"windspeed": 3.6,"winddirection": 6,"is_day": 0,"weathercode": 1}
}
更多信息见:
https://open-meteo.com/
调用
const geoData = await fetchGeolocation("1.1.1.1");
if (geoData) {const locationText = `${geoData.city}, ${geoData.region}, ${geoData.country_name}`;document.getElementById('location').textContent = locationText;document.getElementById('isp').textContent = geoData.org;const weatherData = await getWeather(geoData.latitude, geoData.longitude, geoData.timezone)if (weatherData) {var weatherText = `${getWeatherDescription(weatherData.current_weather.weathercode)},`weatherText += `${weatherData.current_weather.temperature}${weatherData.current_weather_units.temperature},`weatherText += `wind:${weatherData.current_weather.windspeed}${weatherData.current_weather_units.windspeed} `weatherText += `${weatherData.current_weather.winddirection}${weatherData.current_weather_units.winddirection}`;document.getElementById('weather').textContent = weatherText;} else {document.getElementById('weather').textContent = 'weather data unavailable';}
}
效果
https://lezeya.com/now/