2026-07-21 15:32:26 +08:00

106 lines
2.4 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { baiduHttp } from '../http'
import type {
BaiduKlineResponse,
BaiduKlineResult,
BaiduKlineType,
BaiduQuotationResponse,
BaiduQuotationResult,
} from './types'
export interface GetStockQuotationParams {
code: string
}
export interface GetStockKlineParams {
code: string
/** 1=日K,2=周K,3=月K */
ktype: BaiduKlineType
}
/**
* 百度财经 — 期货盘口 / 分时 / 行情快照
* 仅页面打开时拉取一次;实时更新后续对接 WebSocket。
*/
export async function getStockQuotation(
params: GetStockQuotationParams,
): Promise<BaiduQuotationResult> {
const { data } = await baiduHttp.get<BaiduQuotationResponse>(
'/selfselect/getstockquotation',
{
params: {
all: 1,
isIndex: false,
isBk: false,
isBlock: false,
isStock: false,
isEtf: false,
isFutures: true,
isForeign: false,
code: params.code,
stockType: 'ab',
newFormat: 1,
market_type: 'ab',
group: 'quotation_futures_minute',
finClientType: 'pc',
},
},
)
if (data.ResultCode !== '0') {
throw new Error(`行情接口失败: ResultCode=${data.ResultCode}`)
}
const result = data.Result
if (Array.isArray(result)) {
if (!result.length) {
throw new Error('行情接口返回空 Result')
}
return result[0]
}
if (!result || typeof result !== 'object') {
throw new Error('行情接口返回空 Result')
}
return result
}
/**
* 百度财经 — 期货 K 线(日/周/月)
* marketData 为 `;` 分隔的 K 线,每根内字段用 `,` 分隔。
*/
export async function getStockKline(
params: GetStockKlineParams,
): Promise<BaiduKlineResult> {
const { data } = await baiduHttp.get<BaiduKlineResponse>(
'/selfselect/getstockquotation',
{
params: {
all: 1,
code: params.code,
isIndex: false,
isBk: false,
isBlock: false,
isFutures: true,
isStock: false,
newFormat: 1,
ktype: params.ktype,
market_type: 'ab',
group: 'quotation_futures_kline',
finClientType: 'pc',
},
},
)
if (data.ResultCode !== '0') {
throw new Error(`K线接口失败: ResultCode=${data.ResultCode}`)
}
const result = data.Result
if (!result?.newMarketData?.marketData) {
throw new Error('K线接口返回空 marketData')
}
return result
}