--
:
--
:
--
hugo-teek is loading...
ip 天气 欢迎语模块
最后更新于:
ip-天气-欢迎语模块
需求背景



豆包输出
以下是 3个适合博客自建的开源项目,覆盖「IP地理位置查询+天气显示+动态欢迎语」核心需求,支持私有化部署,部署难度从低到高,适配不同技术基础的用户:
一、核心推荐:一站式轻量方案(零代码集成首选)
项目名称:「Meteo-API」(天气+IP定位整合服务)
- 项目地址:https://github.com/metowolf/meteo-api(国内维护活跃,适配中文环境)
- 核心功能:
- IP自动定位:基于 GeoLite2/GeoIP2-CN 数据库(国内城市定位准确率>95%);
- 天气数据:支持高德/和风/OpenWeatherMap 等数据源(国内优先选高德,免费额度足够);
- 动态欢迎语:返回 JSON 数据包含「城市+天气+气温+风向」,可自定义欢迎语模板;
- 额外福利:支持星座、节日、生肖等拓展字段,可丰富欢迎语内容。
- 技术栈:Node.js + Docker(无复杂依赖,部署即用)
- 部署难度:⭐️⭐️(Docker 一键启动)
- 部署步骤:
- 准备工作:
- 申请 高德天气API密钥(免费,https://lbs.amap.com/ 注册后创建「Web服务」应用);
- 下载 GeoIP2-CN 数据库(国内IP优化,https://github.com/Hackl0us/GeoIP2-CN 下载
Country.mmdb)。
- 克隆项目:
1git clone https://github.com/metowolf/meteo-api.git && cd meteo-api - 配置文件:修改
docker-compose.yml,填入关键参数:1version: '3' 2services: 3 meteo-api: 4 image: metowolf/meteo-api 5 ports: 6 - "3000:3000" # 暴露3000端口,可自定义 7 environment: 8 - GEOIP_PATH=/app/GeoIP2-CN.mmdb # IP库路径 9 - AMAP_KEY=你的高德API密钥 # 国内首选 10 - DEFAULT_LANG=zh-CN # 中文返回 11 volumes: 12 - ./GeoIP2-CN.mmdb:/app/GeoIP2-CN.mmdb # 映射本地IP库文件 13 restart: always - 启动服务:
1docker-compose up -d - 测试接口:访问
http://你的服务器IP:3000/api/weather?ip=auto,返回示例:1{ 2 "city": "北京市", 3 "weather": "晴", 4 "temp": 22, 5 "wind": "东北风3级", 6 "date": "2025-11-13", 7 "welcome": "欢迎来自北京市的朋友,今天晴,气温22℃,祝你心情愉快~" 8}
- 准备工作:
- 博客集成(以 Hexo/WordPress 为例):
在博客模板(如
footer.ejs或sidebar.php)添加 JS 代码,自动渲染欢迎语:1<div id="blog-welcome" style="padding: 10px; background: #f5f5f5; border-radius: 8px;"></div> 2<script> 3 // 调用自建API 4 fetch('http://你的服务器IP:3000/api/weather?ip=auto') 5 .then(res => res.json()) 6 .then(data => { 7 document.getElementById('blog-welcome').innerText = data.welcome; 8 // 可自定义样式,比如根据天气显示不同图标 9 }) 10 .catch(err => console.log('加载失败:', err)); 11</script> - 优点:整合度高、部署简单、国内数据源优化、支持自定义;
- 缺点:需申请天气API密钥(5分钟搞定)。
二、模块化方案(灵活替换数据源)
适合想「自主控制每个模块」的用户,拆分「IP定位服务+天气代理+欢迎语组件」,按需组合。
1. IP定位服务:GeoIP-API-Server
- 项目地址:https://github.com/tomasen/geoip-api-server(Go 语言,高性能)
- 核心功能:纯IP定位服务,支持 GeoLite2/GeoIP2-CN 数据库,返回城市/经纬度;
- 部署:Docker 一键启动:
1docker run -d -p 8080:8080 -v ./GeoIP2-CN.mmdb:/data/GeoIP2-CN.mmdb tomasen/geoip-api-server -db /data/GeoIP2-CN.mmdb - 接口示例:
http://IP:8080/json/223.5.5.5(返回「杭州市」等信息)。
2. 天气代理服务(国内数据源优化)
- 无需复杂项目,用 Python 写10行代码即可实现(转发高德天气API,避免密钥泄露):
1# weather-proxy.py 2from flask import Flask, request 3import requests 4 5app = Flask(__name__) 6AMAP_KEY = "你的高德API密钥" 7 8@app.route('/api/weather') 9def get_weather(): 10 city = request.args.get('city') # 接收前端传的城市名 11 url = f"https://restapi.amap.com/v3/weather/weatherInfo?city={city}&key={AMAP_KEY}&extensions=base" 12 res = requests.get(url).json() 13 return { 14 "weather": res['lives'][0]['weather'], 15 "temp": res['lives'][0]['temperature'], 16 "wind": res['lives'][0]['winddirection'] + res['lives'][0]['windpower'] + '级' 17 } 18 19if __name__ == '__main__': 20 app.run(host='0.0.0.0', port=8081) - 部署:用 Docker 包装(或直接
nohup python3 weather-proxy.py &后台运行)。
3. 博客集成(组合两个服务)
1<div id="blog-welcome"></div>
2<script>
3 // 步骤1:获取IP定位城市
4 fetch('http://你的服务器IP:8080/json/auto')
5 .then(res => res.json())
6 .then(ipData => {
7 const city = ipData.city;
8 // 步骤2:根据城市获取天气
9 return fetch(`http://你的服务器IP:8081/api/weather?city=${encodeURIComponent(city)}`);
10 })
11 .then(res => res.json())
12 .then(weatherData => {
13 // 步骤3:渲染欢迎语
14 const welcome = `Hi~ 来自${city}的访客,今天${weatherData.weather},气温${weatherData.temp}℃,${weatherData.wind}~`;
15 document.getElementById('blog-welcome').innerText = welcome;
16 });
17</script>
- 优点:灵活替换数据源(比如IP库换淘宝IP库,天气换百度)、无依赖捆绑;
- 缺点:需要手动组合服务,适合有基础的用户。
三、极简方案(无服务器部署,适合小白)
如果没有云服务器,可用 Cloudflare Workers 免费部署(全球CDN,国内访问尚可),无需服务器成本。
项目名称:Cloudflare Workers IP+天气API
- 核心逻辑:利用 Cloudflare 内置的 GeoIP 功能(免费)+ 天气API转发,无需自建数据库;
- 步骤:
- 打开 Cloudflare 控制台 → Workers → 创建服务;
- 粘贴以下代码(替换
AMAP_KEY):1addEventListener('fetch', event => { 2 event.respondWith(handleRequest(event.request)); 3}); 4 5async function handleRequest(request) { 6 // 1. Cloudflare 内置IP定位 7 const ip = request.headers.get('CF-Connecting-IP'); 8 const city = request.cf?.city || '未知城市'; 9 10 // 2. 调用高德天气API 11 const AMAP_KEY = '你的高德API密钥'; 12 const weatherRes = await fetch(`https://restapi.amap.com/v3/weather/weatherInfo?city=${encodeURIComponent(city)}&key=${AMAP_KEY}&extensions=base`); 13 const weatherData = await weatherRes.json(); 14 const weather = weatherData.lives?.[0]?.weather || '未知'; 15 const temp = weatherData.lives?.[0]?.temperature || '未知'; 16 17 // 3. 返回欢迎语数据 18 return new Response(JSON.stringify({ 19 welcome: `欢迎来自${city}的朋友,今天${weather},气温${temp}℃~` 20 }), { 21 headers: { 'Content-Type': 'application/json', 'Access-Control-Allow-Origin': '*' } 22 }); 23} - 部署后,获得 Workers 域名(如
xxx.workers.dev); - 博客集成:直接调用该域名接口,无需服务器。
- 优点:零成本、免部署、全球快速访问;
- 缺点:国内部分地区访问 Cloudflare 不稳定,IP定位精度略低于自建库。
四、关键注意事项
- IP库更新:自建IP定位需定期更新 GeoIP2-CN 数据库(可写定时脚本:
0 0 * * 0 wget https://github.com/Hackl0us/GeoIP2-CN/releases/latest/download/GeoIP2-CN.mmdb -O /path/GeoIP2-CN.mmdb); - 天气API额度:高德/和风天气免费额度足够个人博客(日调用量≤10000),避免高频调用;
- 隐私合规:自建服务不会泄露用户IP给第三方,建议在博客添加「隐私说明」(如“本网站仅用于IP定位显示,不存储IP数据”);
- 服务器要求:轻量项目(如 Meteo-API)仅需 1核1G 云服务器(阿里云轻量应用服务器¥24/月起),本地服务器也可部署(需端口映射)。
总结
- 小白/追求简单:选「Meteo-API」(Docker 一键部署,国内优化);
- 想灵活定制:选「模块化方案」(IP定位+天气代理组合);
- 无服务器/零成本:选「Cloudflare Workers 方案」。
这三个方案均为开源自建,完全可控,可根据自己的技术基础和服务器资源选择~
📡
👤
作者:
余温Gueen
🌐
版权:
本站文章除特别声明外,均采用
CC BY-NC-SA 4.0
协议,转载请注明来自
余温Gueen Blog!
推荐使用微信支付

推荐使用支付宝
