From 110eaddc4d6fa115be99c8032cc79dc654f4dbfa Mon Sep 17 00:00:00 2001 From: injoyai <1113655791@qq.com> Date: Tue, 30 Sep 2025 11:43:37 +0800 Subject: [PATCH] =?UTF-8?q?=E5=85=BC=E5=AE=B9=E5=8C=97=E4=BA=A4=E6=89=80?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=E5=88=B0GetCodeAll,=E6=96=B9=E4=BE=BF?= =?UTF-8?q?=E5=8E=9F=E5=85=88=E7=9A=84=E9=80=BB=E8=BE=91=E4=B8=8D=E7=94=A8?= =?UTF-8?q?=E6=94=B9=E5=8A=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- client.go | 22 ++++++++++++- codes.go | 93 ++++++++++++++++++++++++++++--------------------------- 2 files changed, 68 insertions(+), 47 deletions(-) diff --git a/client.go b/client.go index 3c57041..f2d14d9 100644 --- a/client.go +++ b/client.go @@ -220,6 +220,26 @@ func (this *Client) GetCode(exchange protocol.Exchange, start uint16) (*protocol // GetCodeAll 通过多次请求的方式获取全部证券代码 func (this *Client) GetCodeAll(exchange protocol.Exchange) (*protocol.CodeResp, error) { resp := &protocol.CodeResp{} + + //通达信没有北交所代码列表,通过爬虫的方式从北交所官网获取,放在这里是为了方便业务逻辑 + //不放在extend包时防止循环引用 + //todo 这是临时方案,等通达信有北交所代码列表时再改 + if exchange == protocol.ExchangeBJ { + codes, err := GetBjCodes() + if err != nil { + return nil, err + } + resp.Count = uint16(len(codes)) + for _, v := range codes { + resp.List = append(resp.List, &protocol.Code{ + Code: v.Code, + Name: v.Name, + LastPrice: v.Last, + }) + } + return resp, nil + } + size := uint16(1000) for start := uint16(0); ; start += size { r, err := this.GetCode(exchange, start) @@ -238,7 +258,7 @@ func (this *Client) GetCodeAll(exchange protocol.Exchange) (*protocol.CodeResp, // GetStockAll 获取所有股票代码 func (this *Client) GetStockAll() ([]string, error) { ls := []string(nil) - for _, ex := range []protocol.Exchange{protocol.ExchangeSH, protocol.ExchangeSZ} { + for _, ex := range []protocol.Exchange{protocol.ExchangeSH, protocol.ExchangeSZ, protocol.ExchangeBJ} { resp, err := this.GetCodeAll(ex) if err != nil { return nil, err diff --git a/codes.go b/codes.go index 88b0c56..990d27f 100644 --- a/codes.go +++ b/codes.go @@ -155,52 +155,53 @@ func (this *Codes) Get(code string) *CodeModel { return this.Map[code] } -// GetExchange 获取股票交易所,这里的参数不需要带前缀 -func (this *Codes) GetExchange(code string) protocol.Exchange { - if len(code) == 6 { - switch { - case code[:1] == "6": - return protocol.ExchangeSH - case code[:1] == "0": - return protocol.ExchangeSZ - case code[:2] == "30": - return protocol.ExchangeSZ - } - } - var exchange string - exchanges := this.exchanges[code] - if len(exchanges) >= 1 { - exchange = exchanges[0] - } - if len(code) == 8 { - exchange = code[0:2] - } - switch exchange { - case protocol.ExchangeSH.String(): - return protocol.ExchangeSH - case protocol.ExchangeSZ.String(): - return protocol.ExchangeSZ - default: - return protocol.ExchangeSH - } -} +//// GetExchange 获取股票交易所,这里的参数不需要带前缀 +//func (this *Codes) GetExchange(code string) protocol.Exchange { +// if len(code) == 6 { +// switch { +// case code[:1] == "6": +// return protocol.ExchangeSH +// case code[:1] == "0": +// return protocol.ExchangeSZ +// case code[:2] == "30": +// return protocol.ExchangeSZ +// } +// } +// var exchange string +// exchanges := this.exchanges[code] +// if len(exchanges) >= 1 { +// exchange = exchanges[0] +// } +// if len(code) == 8 { +// exchange = code[0:2] +// } +// switch exchange { +// case protocol.ExchangeSH.String(): +// return protocol.ExchangeSH +// case protocol.ExchangeSZ.String(): +// return protocol.ExchangeSZ +// default: +// return protocol.ExchangeSH +// } +//} func (this *Codes) AddExchange(code string) string { - if exchanges := this.exchanges[code]; len(exchanges) == 1 { - return exchanges[0] + code - } - if len(code) == 6 { - switch { - case code[:1] == "6": - return protocol.ExchangeSH.String() + code - case code[:1] == "0": - return protocol.ExchangeSZ.String() + code - case code[:2] == "30": - return protocol.ExchangeSZ.String() + code - } - return this.GetExchange(code).String() + code - } - return code + return protocol.AddPrefix(code) + //if exchanges := this.exchanges[code]; len(exchanges) == 1 { + // return exchanges[0] + code + //} + //if len(code) == 6 { + // switch { + // case code[:1] == "6": + // return protocol.ExchangeSH.String() + code + // case code[:1] == "0": + // return protocol.ExchangeSZ.String() + code + // case code[:2] == "30": + // return protocol.ExchangeSZ.String() + code + // } + // return this.GetExchange(code).String() + code + //} + //return code } // Update 更新数据,从服务器或者数据库 @@ -249,7 +250,7 @@ func (this *Codes) GetCodes(byDatabase bool) ([]*CodeModel, error) { //3. 从服务器获取所有股票代码 insert := []*CodeModel(nil) update := []*CodeModel(nil) - for _, exchange := range []protocol.Exchange{protocol.ExchangeSH, protocol.ExchangeSZ} { + for _, exchange := range []protocol.Exchange{protocol.ExchangeSH, protocol.ExchangeSZ, protocol.ExchangeBJ} { resp, err := this.Client.GetCodeAll(exchange) if err != nil { return nil, err @@ -334,7 +335,7 @@ func (this *CodeModel) FullCode() string { func (this *CodeModel) Price(p protocol.Price) protocol.Price { return protocol.Price(float64(p) * math.Pow10(int(2-this.Decimal))) - return p * protocol.Price(math.Pow10(int(2-this.Decimal))) + //return p * protocol.Price(math.Pow10(int(2-this.Decimal))) } func NewSessionFunc(db *xorm.Engine, fn func(session *xorm.Session) error) error {