增加数据-人民币汇率

This commit is contained in:
cheney
2026-04-25 19:48:19 +08:00
parent d232591a38
commit 306af38fdc
4 changed files with 333 additions and 5 deletions
+6 -3
View File
@@ -98,8 +98,11 @@ export default async function handler(req, res) {
error: error.message
});
} finally {
if (pool) {
await pool.end();
}
// 不关闭连接池,保持连接池打开以提高性能
// 如果需要关闭连接池,应该在关闭后将 pool 变量设置为 null
// if (pool) {
// await pool.end();
// pool = null;
// }
}
}
+31 -2
View File
@@ -2,6 +2,7 @@ import { useState, useEffect } from 'react'
import Head from 'next/head'
import Navbar from '../components/Navbar'
import Footer from '../components/Footer'
import TimeRangePicker from '../components/TimeRangePicker'
import { LineChart, Line, XAxis, YAxis, CartesianGrid, Tooltip, Legend, ResponsiveContainer } from 'recharts'
const chinaBondData = [
@@ -162,18 +163,39 @@ export default function Data() {
const [exchangeRateData, setExchangeRateData] = useState([])
const [loading, setLoading] = useState(false)
const [error, setError] = useState(null)
const [timeRange, setTimeRange] = useState('1m') // 1m: 近一个月, 3m: 近三个月, 1y: 近一年, custom: 自定义
const [startDate, setStartDate] = useState('')
const [endDate, setEndDate] = useState('')
useEffect(() => {
if (selectedIndicator === 'central-bank-usd-cny-rate') {
fetchExchangeRateData()
}
}, [selectedIndicator])
}, [selectedIndicator, timeRange, startDate, endDate])
const handleTimeRangeChange = (range, start, end) => {
setTimeRange(range)
setStartDate(start)
setEndDate(end)
}
const fetchExchangeRateData = async () => {
setLoading(true)
setError(null)
try {
const response = await fetch('/api/exchange-rate?limit=60')
let url = '/api/exchange-rate'
if (timeRange === 'custom' && startDate && endDate) {
url += `?startDate=${startDate}&endDate=${endDate}`
} else if (timeRange === '1m') {
url += '?limit=30'
} else if (timeRange === '3m') {
url += '?limit=90'
} else if (timeRange === '1y') {
url += '?limit=365'
}
const response = await fetch(url)
const data = await response.json()
if (data.success) {
setExchangeRateData(data.data)
@@ -566,6 +588,13 @@ export default function Data() {
<div className="error">{error}</div>
) : (
<>
<div className="chart-header">
<TimeRangePicker
value={timeRange}
onChange={handleTimeRangeChange}
/>
</div>
<div className="chart-container">
<ResponsiveContainer width="100%" height={500}>
<LineChart data={exchangeRateData} margin={{ top: 20, right: 30, left: 20, bottom: 5 }}>