iboard/server/apizero/oil-price-forecast.js
2026-08-07 14:36:57 +08:00

42 lines
1.5 KiB
JavaScript

// apizero: 今日油价 (oil-price-forecast)
// 4 个 action:
// forecast - 油价 + 国际原油 + 下次调价预测 (每次都查实时, 不缓存)
// price - 单省油价
// price-all - 32 省全部油价
// schedule - 某年调价日历 (按 year 缓存到文件, 同年份不重复调)
//
// 文档: https://apizero.cn/aidocs/oil-price-forecast/raw.md
import * as client from './client.js'
import * as cache from './cache.js'
// 调价日历: 按年份缓存, 文件存在即视为有效, 不重复调用
// cache key 形如 "oil-schedule-2026"
export async function getSchedule(year) {
const y = Number(year)
if (!Number.isInteger(y) || y < 2025 || y > 2026) {
throw new client.ApizeroError(4000, `year 不在 2025-2026 范围: ${year}`, 400)
}
const cacheKey = `oil-schedule-${y}`
const hit = cache.read(cacheKey)
if (hit) return { ...hit, _cached: true }
const data = await client.get('oil-price-forecast', { action: 'schedule', year: y })
cache.write(cacheKey, data)
return { ...data, _cached: false }
}
// 实时: 国际原油 + 下次调价预测
export async function getForecast() {
return client.get('oil-price-forecast', { action: 'forecast' })
}
// 单省油价
export async function getPrice(province) {
if (!province) throw new client.ApizeroError(4000, 'province 必填', 400)
return client.get('oil-price-forecast', { action: 'price', province })
}
// 32 省全部油价
export async function getPriceAll() {
return client.get('oil-price-forecast', { action: 'price-all' })
}