add: 接口调整

This commit is contained in:
hzm
2026-01-31 21:29:46 +08:00
parent fe2c21527b
commit 6eb70c93b7
2 changed files with 204 additions and 16 deletions

View File

@@ -1,5 +1,16 @@
import { NextResponse } from 'next/server';
const CORS_HEADERS = {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET,OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type,Authorization,Accept',
'Access-Control-Max-Age': '86400'
};
export async function OPTIONS() {
return new NextResponse(null, { status: 204, headers: CORS_HEADERS });
}
async function fetchGZ(code) {
const url = `https://fundgz.1234567.com.cn/js/${code}.js`;
const res = await fetch(url, { cache: 'no-store' });
@@ -75,18 +86,18 @@ export async function GET(req) {
const { searchParams } = new URL(req.url);
const code = (searchParams.get('code') || '').trim();
if (!code) {
return NextResponse.json({ error: '缺少基金编号' }, { status: 400 });
return NextResponse.json({ error: '缺少基金编号' }, { status: 400, headers: CORS_HEADERS });
}
const [gz, holdings] = await Promise.allSettled([fetchGZ(code), fetchHoldings(code)]);
if (gz.status !== 'fulfilled') {
return NextResponse.json({ error: gz.reason?.message || '基金估值获取失败' }, { status: 404 });
return NextResponse.json({ error: gz.reason?.message || '基金估值获取失败' }, { status: 404, headers: CORS_HEADERS });
}
const data = {
...gz.value,
holdings: holdings.status === 'fulfilled' ? holdings.value : []
};
return NextResponse.json(data, { status: 200 });
return NextResponse.json(data, { status: 200, headers: CORS_HEADERS });
} catch (e) {
return NextResponse.json({ error: e.message || '服务异常' }, { status: 500 });
return NextResponse.json({ error: e.message || '服务异常' }, { status: 500, headers: CORS_HEADERS });
}
}