import { uniq } from 'lodash' ; import SinaStockTransform from "@stocks/sina/transforms/stock" ; import SinaCommonCodeTransform from "@stocks/sina/transforms/common-code" ; import fetch from "@utils/fetch" ; import iconv from "@utils/iconv" ; import { DEFAULT_STOCK } from "@utils/constant" ; import { COMMON_SZ , COMMON_SH , COMMON_US , COMMON_HK } from "@stocks/base/utils/constant" ; import Stock from "types/utils/stock" ; import StockApi from "types/stocks/index" ; /**
 * 新浪股票代码接口
 */ const Sina = { param code async getStock ( code ) { const transform = ( new SinaCommonCodeTransform ) . transform ( code ) ; const url = `https://hq.sinajs.cn/list=${ transform }` ; const res = await fetch . get ( url ) ; const body = iconv . decode ( res . body , "gb18030" ) ; const rows = body . split ( ";\n" ) ; const row = rows [ 0 ] ; const [ _ , paramsUnformat ] = row . split ( '=' ) ; if ( paramsUnformat === '""' ) { return { ... DEFAULT_STOCK , code } ; } const params = paramsUnformat . replace ( '"' , '' ) . split ( "," ) ; const data = ( new SinaStockTransform ( code , params ) ) ; return data . getStock ( ) ; } , param codes async getStocks ( codes ) { codes = uniq ( codes . filter ( i => i !== '' ) ) ; if ( codes . length === 0 ) { return [ ] ; } const transforms = ( new SinaCommonCodeTransform ) . transforms ( codes ) ; const url = `https://hq.sinajs.cn/list=${ transforms . join ( ',' ) }` ; const res = await fetch . get ( url ) ; const body = iconv . decode ( res . body , "gb18030" ) ; const rows = body . split ( ";\n" ) ; return codes . map ( ( code , index ) => { const [ _ , paramsUnformat ] = rows [ index ] . split ( '=' ) ; if ( paramsUnformat === '""' ) { return { ... DEFAULT_STOCK , code } ; } const params = paramsUnformat . replace ( '"' , '' ) . split ( "," ) ; const data = ( new SinaStockTransform ( code , params ) ) ; return data . getStock ( ) ; } ) ; } , param key async searchStocks ( key ) { const url = `http://suggest3.sinajs.cn/suggest/type=2&key=${ encodeURIComponent ( key ) }` ; const res = await fetch . get ( url ) ; const body = iconv . decode ( res . body , "gb18030" ) ; const rows = body . replace ( 'var suggestvalue="' , '' ) . replace ( '";' , '' ) . split ( ';' ) ; let codes = [ ] ; for ( let i = 0 ; i < rows . length ; i ++ ) { let code = rows [ i ] . split ( ',' ) [ 0 ] ; if ( code . indexOf ( 'us' ) === 0 ) { code = code . replace ( 'us' , '' ) ; codes = [ ... codes , COMMON_US + code ] ; } if ( code . indexOf ( 'sz' ) === 0 ) { code = code . replace ( 'sz' , '' ) ; codes = [ ... codes , COMMON_SZ + code ] ; } if ( code . indexOf ( 'sh' ) === 0 ) { code = code . replace ( 'sh' , '' ) ; codes = [ ... codes , COMMON_SH + code ] ; } if ( code . indexOf ( 'hk' ) === 0 ) { code = code . replace ( 'hk' , '' ) ; codes = [ ... codes , COMMON_HK + code ] ; } if ( code . indexOf ( 'of' ) === 0 ) { code = code . replace ( 'of' , '' ) ; codes = [ ... codes , COMMON_SZ + code , COMMON_SH + code ] ; } } return await Sina . getStocks ( uniq ( codes . filter ( i => i !== '' ) ) ) ; } } ; export default Sina ;