import { uniq } from 'lodash' ; import XueqiuStockTransform from "@stocks/xueqiu/transforms/stock" ; import XueqiuCommonCodeTransform from "@stocks/xueqiu/transforms/common-code" ; import fetch from "@utils/fetch" ; import { DEFAULT_STOCK } from "@utils/constant" ; import { COMMON_SZ , COMMON_SH , COMMON_HK , COMMON_US } from '@stocks/base/utils/constant' ; import Stock from "types/utils/stock" ; import StockApi from "types/stocks/index" ; import Dictionary from "types/utils/dictionary" ; let token = '' ; /**
 * 雪球股票代码接口
 */ const Xueqiu = { /**
     * 获取 Token
     */ async getToken ( ) { if ( token !== '' ) return token ; const res = await fetch . get ( 'https://xueqiu.com/' ) ; const cookies = res . headers [ 'set-cookie' ] ; const param = cookies . filter ( key => key . includes ( 'xq_a_token' ) ) [ 0 ] || '' ; token = param . split ( ';' ) [ 0 ] || '' ; return token ; } , param code async getStock ( code ) { const token = await this . getToken ( ) ; const transform = ( new XueqiuCommonCodeTransform ) . transform ( code ) ; const url = `https://stock.xueqiu.com/v5/stock/quote.json?symbol=${ transform }` ; const res = await fetch . get ( url ) . set ( 'Cookie' , token ) ; const body = JSON . parse ( res . text ) ; const row = body . data . quote ; const params = row ; const data = ( new XueqiuStockTransform ( code , params ) ) ; return params ? data . getStock ( ) : { ... DEFAULT_STOCK , code } ; } , param codes async getStocks ( codes ) { codes = uniq ( codes . filter ( i => i !== '' ) ) ; if ( codes . length === 0 ) { return [ ] ; } const token = await this . getToken ( ) ; const transforms = ( new XueqiuCommonCodeTransform ) . transforms ( codes ) ; const url = `https://stock.xueqiu.com/v5/stock/batch/quote.json?symbol=${ transforms . join ( ',' ) }` ; const res = await fetch . get ( url ) . set ( 'Cookie' , token ) ; const body = JSON . parse ( res . text ) ; const rows = body . data . items ; return codes . map ( code => { const transform = ( new XueqiuCommonCodeTransform ) . transform ( code ) ; const params = rows . find ( i => { if ( ! i . quote ) { return false ; } if ( i . market . region === 'US' ) { return i . quote . code === transform ; } if ( i . market . region === 'CN' ) { return i . quote . symbol === transform ; } if ( i . market . region === 'HK' ) { return i . market . region + i . quote . code === transform ; } } ) || null ; if ( ! params ) { return { ... DEFAULT_STOCK , code } ; } const data = ( new XueqiuStockTransform ( code , params . quote ) ) ; return data . getStock ( ) ; } ) ; } , param key async searchStocks ( key ) { const token = await this . getToken ( ) ; const url = `https://xueqiu.com/stock/search.json?code=${ encodeURIComponent ( key ) }` ; const res = await fetch . get ( url ) . set ( 'Cookie' , token ) ; const body = JSON . parse ( res . text ) ; const rows = body . stocks ; let codes = [ ] ; for ( let i = 0 ; i < rows . length ; i ++ ) { let code = rows [ i ] . code ; if ( code . indexOf ( 'SZ' ) === 0 ) { code = code . replace ( 'SZ' , '' ) ; codes = [ ... codes , COMMON_SZ + code ] ; continue ; } if ( code . indexOf ( 'SH' ) === 0 ) { code = code . replace ( 'SH' , '' ) ; codes = [ ... codes , COMMON_SH + code ] ; continue ; } codes = [ ... codes , COMMON_HK + code , COMMON_US + code ] ; } return await Xueqiu . getStocks ( uniq ( codes . filter ( i => i !== '' ) ) ) ; } } ; export default Xueqiu ;