feat(api): 接入币安公共接口,升级为股票+加密货币的双轨制全市场行情引擎

This commit is contained in:
2026-04-28 13:21:28 +08:00
parent 80029817a9
commit 48e834e338
3 changed files with 49 additions and 24 deletions
+37 -20
View File
@@ -17,33 +17,50 @@ function getTencentSymbol(asset: { symbol: string; exchange: string | null }): s
}
}
export async function syncAllStockPrices() {
const stockAssets = await db
export async function syncAllMarketPrices() {
const allAssets = await db
.select()
.from(assets)
.where(eq(assets.type, 'STOCK'));
.from(assets);
let successCount = 0;
for (const asset of stockAssets) {
for (const asset of allAssets) {
try {
const tCode = getTencentSymbol(asset);
const response = await fetch(`https://qt.gtimg.cn/q=${tCode}`, { cache: 'no-store' });
const arrayBuffer = await response.arrayBuffer();
const decoder = new TextDecoder('gbk');
const text = decoder.decode(arrayBuffer);
if (asset.type === 'STOCK') {
const tCode = getTencentSymbol(asset);
const response = await fetch(`https://qt.gtimg.cn/q=${tCode}`, { cache: 'no-store' });
const arrayBuffer = await response.arrayBuffer();
const decoder = new TextDecoder('gbk');
const text = decoder.decode(arrayBuffer);
const match = text.match(/="([^"]+)"/);
if (match && match[1]) {
const dataArr = match[1].split('~');
const latestPrice = dataArr[3];
const match = text.match(/="([^"]+)"/);
if (match && match[1]) {
const dataArr = match[1].split('~');
const latestPrice = dataArr[3];
if (latestPrice && !isNaN(Number(latestPrice)) && Number(latestPrice) > 0) {
const stockName = dataArr[1] || null;
await db.update(assets)
.set({ latestPrice: latestPrice, name: stockName })
.where(eq(assets.id, asset.id));
successCount++;
if (latestPrice && !isNaN(Number(latestPrice)) && Number(latestPrice) > 0) {
const stockName = dataArr[1] || null;
await db.update(assets)
.set({ latestPrice: latestPrice, name: stockName })
.where(eq(assets.id, asset.id));
successCount++;
}
}
} else if (asset.type === 'CRYPTO') {
const cryptoSymbol = asset.symbol.trim().toUpperCase() + 'USDT';
const response = await fetch(`https://api.binance.com/api/v3/ticker/price?symbol=${cryptoSymbol}`, { cache: 'no-store' });
if (response.ok) {
const data = await response.json();
if (data.price) {
await db.update(assets)
.set({
latestPrice: data.price,
name: asset.symbol.toUpperCase(),
})
.where(eq(assets.id, asset.id));
successCount++;
}
}
}
} catch (error) {