import { uniq } from 'lodash' ; import NeteaseStockTransform from "@stocks/netease/transforms/stock" ; import NeteaseCommonCodeTransform from "@stocks/netease/transforms/common-code" ; import fetch from "@utils/fetch" ; import { DEFAULT_STOCK } from "@utils/constant" ; import { COMMON_SZ , COMMON_SH } from "@stocks/base/utils/constant" ; import Stock from "types/utils/stock" ; import StockApi from "types/stocks/index" ; import Dictionary from "types/utils/dictionary" ; /**
 * 网易股票代码接口
 */ const Netease = { param code async getStock ( code ) { const transform = ( new NeteaseCommonCodeTransform ) . transform ( code ) ; const url = `https://api.money.126.net/data/feed/${ transform },money.api?callback=topstock` ; const res = await fetch . get ( url ) ; const items = JSON . parse ( res . body . toString ( ) . replace ( /\(|\)|;|(topstock)|\s/g , '' ) . replace ( '{{' , '{' ) . replace ( '}}}' , "}}" ) ) ; const params = items [ transform ] ; if ( ! params ) { return { ... DEFAULT_STOCK , code } ; } const data = ( new NeteaseStockTransform ( code , params ) ) ; return data . getStock ( ) ; } , param codes async getStocks ( codes ) { codes = uniq ( codes . filter ( i => i !== '' ) ) ; if ( codes . length === 0 ) { return [ ] ; } const transforms = ( new NeteaseCommonCodeTransform ) . transforms ( codes ) ; const url = `https://api.money.126.net/data/feed/${ transforms . join ( ',' ) },money.api?callback=topstock` ; const res = await fetch . get ( url ) ; const items = JSON . parse ( res . body . toString ( ) . replace ( /\(|\)|;|(topstock)|\s/g , '' ) . replace ( '{{' , '{' ) . replace ( '}}}' , "}}" ) ) ; return codes . map ( code => { const transform = ( new NeteaseCommonCodeTransform ) . transform ( code ) ; const params = items [ transform ] ; if ( ! params ) { return { ... DEFAULT_STOCK , code } ; } const data = ( new NeteaseStockTransform ( code , params ) ) ; return data . getStock ( ) ; } ) ; } , param key async searchStocks ( key ) { const url = `https://quotes.money.163.com/stocksearch/json.do?type=&count=5&word=${ encodeURIComponent ( key ) }&callback=topstock` ; const res = await fetch . get ( url ) ; const row = res . body . toString ( ) . replace ( /\(|\)|;|(topstock)|\s/g , '' ) . replace ( '{{' , '{' ) . replace ( '}}}' , "}}" ) ; const items = JSON . parse ( row ) ; const codes = items . map ( i => { if ( i . tag . includes ( COMMON_SZ ) || i . type . includes ( COMMON_SZ ) ) { return COMMON_SZ + i . symbol ; } if ( i . tag . includes ( COMMON_SH ) || i . type . includes ( COMMON_SH ) ) { return COMMON_SH + i . symbol ; } return '' ; } ) ; return await Netease . getStocks ( uniq ( codes . filter ( i => i !== '' ) ) ) ; } } ; export default Netease ;