【搭建你的日记本】在文章正文中添加天气信息

给一个自己的:

<%* 
let ipUrl = 'https://restapi.amap.com/v3/ip';
let weatherUrl = 'https://restapi.amap.com/v3/weather/weatherInfo'
let key = '高德key'

let tencentIpUrl = 'https://apis.map.qq.com/ws/location/v1/ip';
let tencentKey = 'tencent key'

let tencentInfo = eval("(" + 
    await request({url: tencentIpUrl + `?key=${tencentKey}`, method: "GET"})
+ ")").result.ad_info

let adcode = tencentInfo.adcode
let city = tencentInfo.city.slice(0, -1)

let weather = ''
let temperature = ''
await fetch(weatherUrl + `?key=${key}&city=${adcode}&extensions=all`)
.then(res => res.json())
.then((data) => {
	let info = data.forecasts[0]
	weather = (info.casts[0].dayweather === info.casts[0].nightweather) 
              ? info.casts[0].dayweather
              : `${info.casts[0].dayweather}转${info.casts[0].nightweather}`
	temperature = info.casts[0].nighttemp + '~' + info.casts[0].daytemp + '℃'
})
-%>

不同点:

  1. 只显示城市,例如显示:上海、苏州、广州,不显示省,也不显示“市”字
  2. 显示每天的夜间温度与白天温度
  3. 显示每天的夜间天气白天天气,并有条件判断,如果夜间白天天气不一致,则会显示“xx转xx”

效果如下:
image

PixPin_2026-01-08_03-04-37

结合大家的思路,自己也做了一个,请大家指正

<%* 
// 高德地图 API 配置
const amapIpUrl = 'https://restapi.amap.com/v3/ip';
const amapWeatherUrl = 'https://restapi.amap.com/v3/weather/weatherInfo';
const amapKey = "YOURKEY"; // 你的高德 API Key

// 腾讯地图 API 配置(备用,可注释)
const tencentIpUrl = 'https://apis.map.qq.com/ws/location/v1/ip';
const tencentKey = "YOURKEY";

// 初始化变量
let ipResult = {};
let 位置 = '未知位置';
let 天气 = '未知天气';
let 温度 = '未知温度';
let 风向 = '';
let 一言 = '';
let 来源 = '';
let 作者 = '';

try {
    // 第一步:通过高德 IP API 获取属地信息
    console.log('正在获取IP属地...');
    const ipResponse = await fetch(`${amapIpUrl}?key=${amapKey}`);
    ipResult = await ipResponse.json();
    
    // 检查 IP 接口返回是否成功
    if (ipResult.status === '1' && ipResult.adcode) {
        // 拼接位置信息(省-市)
        位置 = `${ipResult.province || '未知省'}-${ipResult.city || '未知市'}`;
        
        // 第二步:根据 adcode 获取天气信息
        console.log('正在获取天气信息...');
        const weatherResponse = await fetch(`${amapWeatherUrl}?key=${amapKey}&city=${ipResult.adcode}&extensions=all`);
        const weatherData = await weatherResponse.json();
        
        if (weatherData.status === '1' && weatherData.forecasts && weatherData.forecasts.length > 0) {
            const info = weatherData.forecasts[0];
            const cast = info.casts[0];
            // 处理天气(白天和夜间一致则只显示一个,不一致则显示“转”)
            天气 = (cast.dayweather === cast.nightweather) 
                ? cast.dayweather
                : `${cast.dayweather}转${cast.nightweather}`;
            // 处理温度(夜间~白天)
            温度 = `${cast.nighttemp || '0'}~${cast.daytemp || '0'}℃`;
            风向 = cast.daywind || ''; // 可选:补充风向信息
        } else {
            天气 = '获取天气失败';
            温度 = '未知温度';
        }
    } else {
        console.error('IP属地获取失败:', ipResult.info || '无错误信息');
    }
} catch (error) {
    console.error('获取位置/天气出错:', error);
    位置 = '位置获取失败';
    天气 = '天气获取失败';
}

try {
    // 第三步:获取一言信息
    console.log('正在获取一言...');
    const hitokotoResponse = await fetch('https://v1.hitokoto.cn/?c=d&c=h&c=i&c=j');
    const hitokotoData = await hitokotoResponse.json();
    一言 = hitokotoData.hitokoto || '暂无一言';
    来源 = hitokotoData.from || '未知来源';
    作者 = hitokotoData.from_who === null ? '佚名' : (hitokotoData.from_who || '佚名');
} catch (error) {
    console.error('获取一言出错:', error);
    一言 = '获取一言失败';
    来源 = '未知';
    作者 = '佚名';
}
// 生成日期文件名并移动到指定文件夹
const fileName = tp.date.now("YYYY-MM-DD");
const targetFolder = "工作笔记";

if (!tp.file.exists(targetFolder)) {
  await this.app.vault.createFolder(targetFolder);
}
await tp.file.move(`${targetFolder}/${fileName}`);
-%>

日期 ::  <% tp.file.creation_date("YYYY MM DD dddd") %> 
时间 ::  <% tp.file.creation_date("HH:mm:ss") %> 
位置 ::  <% 位置 %>
天气 ::  <% 天气 %> ,<% 温度 %>

>[!quote] 一言
 <% 一言 %>  —— 《<% 来源 %>》 · <% 作者 %>

---
## 📝 今日工作记录
### ✅ 今日待办
- [ ] 
### 📌 今日完成
1. 
### 💡 灵感/想法
- 
### 🔖 明日计划
- 
---

<< [[<% tp.date.now("YYYY-MM-DD", -1) %>]] | [[<% tp.date.now("YYYY-MM-DD", 1) %>]] >>