|
|
|
@ -9,10 +9,13 @@ import (
|
|
|
|
|
"stock/cfg"
|
|
|
|
|
"strconv"
|
|
|
|
|
"strings"
|
|
|
|
|
"sync"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var (
|
|
|
|
|
baseUrl = "https://hq.sinajs.cn/"
|
|
|
|
|
fds *stocks
|
|
|
|
|
mx sync.RWMutex
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type IStock interface {
|
|
|
|
@ -28,6 +31,14 @@ type stock struct {
|
|
|
|
|
nowRise float64
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *stock) Code() string {
|
|
|
|
|
return s.code
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *stock) Name() string {
|
|
|
|
|
return s.values[0]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *stock) update(values []string) {
|
|
|
|
|
s.values = values
|
|
|
|
|
}
|
|
|
|
@ -130,41 +141,84 @@ func (s *stock) Msg() string {
|
|
|
|
|
return msg
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ==========================
|
|
|
|
|
|
|
|
|
|
func Init(codes ...string) error {
|
|
|
|
|
fds = &stocks{}
|
|
|
|
|
return fds.AddCodes(codes...)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type stocks struct {
|
|
|
|
|
codes []string
|
|
|
|
|
ss []*stock
|
|
|
|
|
//codes []string
|
|
|
|
|
//ss []*stock
|
|
|
|
|
stkMap map[string]*stock
|
|
|
|
|
mx sync.RWMutex
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (sk *stocks) getStocks(codes ...string) *stocks {
|
|
|
|
|
var stks = &stocks{}
|
|
|
|
|
for _, code := range codes {
|
|
|
|
|
stks.stkMap[code] = sk.stkMap[code]
|
|
|
|
|
}
|
|
|
|
|
return stks
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (sk *stocks) Codes() []string {
|
|
|
|
|
var codes []string
|
|
|
|
|
for code := range sk.stkMap {
|
|
|
|
|
codes = append(codes, code)
|
|
|
|
|
}
|
|
|
|
|
return codes
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (sk *stocks) AddCodes(codes ...string) error {
|
|
|
|
|
sk.mx.Lock()
|
|
|
|
|
defer sk.mx.Unlock()
|
|
|
|
|
var cds []string
|
|
|
|
|
for _, code := range codes {
|
|
|
|
|
if _, ok := sk.stkMap[code]; ok {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
cds = append(cds, code)
|
|
|
|
|
}
|
|
|
|
|
stks, err := newStocks(cds...)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
for _, stk := range stks {
|
|
|
|
|
sk.stkMap[stk.Code()] = stk
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (sk *stocks) Clear() {
|
|
|
|
|
if len(sk.ss) <= 0 {
|
|
|
|
|
return
|
|
|
|
|
sk.mx.Lock()
|
|
|
|
|
defer sk.mx.Unlock()
|
|
|
|
|
for _, fd := range fds.stkMap {
|
|
|
|
|
fd.lastRise = 0
|
|
|
|
|
}
|
|
|
|
|
sk.ss = []*stock{}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (sk *stocks) Update() error {
|
|
|
|
|
str, err := getStockStr(sk.codes)
|
|
|
|
|
sk.mx.Lock()
|
|
|
|
|
sk.mx.Unlock()
|
|
|
|
|
str, err := getStockStr(sk.Codes())
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
strs := strings.Split(str, ";\n")
|
|
|
|
|
if len(sk.ss) == 0 {
|
|
|
|
|
sk.ss = []*stock{}
|
|
|
|
|
for _, s := range strs {
|
|
|
|
|
ss := strings.Split(s, "\"")
|
|
|
|
|
if len(ss) >= 2 {
|
|
|
|
|
v := &stock{
|
|
|
|
|
code: ss[0][13:19],
|
|
|
|
|
values: strings.Split(ss[1], ","),
|
|
|
|
|
}
|
|
|
|
|
sk.ss = append(sk.ss, v)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if skk, ok := sk.stkMap[v.code]; ok {
|
|
|
|
|
skk.update(v.values)
|
|
|
|
|
} else {
|
|
|
|
|
l := len(sk.ss)
|
|
|
|
|
for i, s := range strs {
|
|
|
|
|
ss := strings.Split(s, "\"")
|
|
|
|
|
if len(ss) >= 2 && i < l {
|
|
|
|
|
sk.ss[i].update(strings.Split(ss[1], ","))
|
|
|
|
|
sk.stkMap[v.code] = v
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -173,8 +227,10 @@ func (sk *stocks) Update() error {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (sk *stocks) Msg() string {
|
|
|
|
|
sk.mx.RLock()
|
|
|
|
|
defer sk.mx.RUnlock()
|
|
|
|
|
var resp string
|
|
|
|
|
for _, s := range sk.ss {
|
|
|
|
|
for _, s := range sk.stkMap {
|
|
|
|
|
if s.notify() {
|
|
|
|
|
msg := s.Msg()
|
|
|
|
|
resp = resp + msg + "\n"
|
|
|
|
@ -183,33 +239,65 @@ func (sk *stocks) Msg() string {
|
|
|
|
|
return resp
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type IArg interface {
|
|
|
|
|
Arg(openId string) map[string]interface{}
|
|
|
|
|
func (sk *stocks) Arg(openid string) map[string]interface{} {
|
|
|
|
|
arg := map[string]interface{}{
|
|
|
|
|
"touser": openid,
|
|
|
|
|
"template_id": "yWuLbhAy7TTuqdeB9-VS6CR_t2rZQ8MHkJ62MF3VlS8",
|
|
|
|
|
"data": map[string]interface{}{
|
|
|
|
|
"keyword": map[string]interface{}{
|
|
|
|
|
"value": sk.Msg(),
|
|
|
|
|
"color": "#173177",
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
return arg
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (sk *stocks) ForEachStock(f func(stk IArg) error) error {
|
|
|
|
|
for _, k := range sk.ss {
|
|
|
|
|
err := f(k)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
func GetStocks(codes ...string) (*stocks, error) {
|
|
|
|
|
if len(codes) <= 0 {
|
|
|
|
|
return nil, errcode.New(1, "股票代码为空")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mx.Lock()
|
|
|
|
|
defer mx.Unlock()
|
|
|
|
|
if fds == nil {
|
|
|
|
|
fds = &stocks{}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
err := fds.AddCodes(codes...)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
return fds.getStocks(codes...), nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewStocks(codes ...string) (*stocks, error) {
|
|
|
|
|
func Update() error {
|
|
|
|
|
return fds.Update()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func Clear() {
|
|
|
|
|
fds.Clear()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func newStocks(codes ...string) ([]*stock, error) {
|
|
|
|
|
if len(codes) <= 0 {
|
|
|
|
|
return nil, errcode.New(1, "股票代码为空")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
s := &stocks{
|
|
|
|
|
codes: codes,
|
|
|
|
|
}
|
|
|
|
|
err := s.Update()
|
|
|
|
|
str, err := getStockStr(codes)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
return s, nil
|
|
|
|
|
strs := strings.Split(str, ";\n")
|
|
|
|
|
var stks []*stock
|
|
|
|
|
for _, s := range strs {
|
|
|
|
|
ss := strings.Split(s, "\"")
|
|
|
|
|
if len(ss) >= 2 {
|
|
|
|
|
v := &stock{
|
|
|
|
|
values: strings.Split(ss[1], ","),
|
|
|
|
|
}
|
|
|
|
|
stks = append(stks, v)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return stks, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func addPrefix(code string) string {
|
|
|
|
@ -251,66 +339,67 @@ const msgTemplate = `%s
|
|
|
|
|
前五总买单:%s 前五总卖单:%s
|
|
|
|
|
`
|
|
|
|
|
|
|
|
|
|
func (s *stock) Arg(openId string) map[string]interface{} {
|
|
|
|
|
|
|
|
|
|
arg := map[string]interface{}{
|
|
|
|
|
"touser": openId,
|
|
|
|
|
"template_id": "L7fOGJURj-1HF4cIpFizCOOiAMqER3PG-pfgn37Dalw",
|
|
|
|
|
|
|
|
|
|
"data": map[string]interface{}{
|
|
|
|
|
"first": map[string]interface{}{
|
|
|
|
|
"value": s.values[0],
|
|
|
|
|
"color": "#173177",
|
|
|
|
|
},
|
|
|
|
|
"keyword1": map[string]interface{}{
|
|
|
|
|
"value": fmt.Sprintf("%s %s", s.values[30], s.values[31]),
|
|
|
|
|
"color": "#173177",
|
|
|
|
|
},
|
|
|
|
|
"keyword2": map[string]interface{}{
|
|
|
|
|
"value": s.values[2],
|
|
|
|
|
"color": "#173177",
|
|
|
|
|
},
|
|
|
|
|
"keyword3": map[string]interface{}{
|
|
|
|
|
"value": s.values[1],
|
|
|
|
|
"color": "#173177",
|
|
|
|
|
},
|
|
|
|
|
"keyword4": map[string]interface{}{
|
|
|
|
|
"value": s.values[3],
|
|
|
|
|
"color": "#173177",
|
|
|
|
|
},
|
|
|
|
|
"keyword5": map[string]interface{}{
|
|
|
|
|
"value": s.rise(),
|
|
|
|
|
"color": "#173177",
|
|
|
|
|
},
|
|
|
|
|
"keyword6": map[string]interface{}{
|
|
|
|
|
"value": s.values[4],
|
|
|
|
|
"color": "#173177",
|
|
|
|
|
},
|
|
|
|
|
"keyword7": map[string]interface{}{
|
|
|
|
|
"value": s.values[5],
|
|
|
|
|
"color": "#173177",
|
|
|
|
|
},
|
|
|
|
|
"keyword8": map[string]interface{}{
|
|
|
|
|
"value": s.tradingVolume(),
|
|
|
|
|
"color": "#173177",
|
|
|
|
|
},
|
|
|
|
|
"keyword9": map[string]interface{}{
|
|
|
|
|
"value": numFormat(s.values[9]),
|
|
|
|
|
"color": "#173177",
|
|
|
|
|
},
|
|
|
|
|
"keyword10": map[string]interface{}{
|
|
|
|
|
"value": s.buyCount(),
|
|
|
|
|
"color": "#173177",
|
|
|
|
|
},
|
|
|
|
|
"keyword11": map[string]interface{}{
|
|
|
|
|
"value": s.sellCount(),
|
|
|
|
|
"color": "#173177",
|
|
|
|
|
},
|
|
|
|
|
"remark": map[string]interface{}{
|
|
|
|
|
"value": "欢迎再次购买!",
|
|
|
|
|
"color": "#173177",
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
return arg
|
|
|
|
|
}
|
|
|
|
|
//
|
|
|
|
|
//func (s *stock) Arg(openId string) map[string]interface{} {
|
|
|
|
|
//
|
|
|
|
|
// arg := map[string]interface{}{
|
|
|
|
|
// "touser": openId,
|
|
|
|
|
// "template_id": "L7fOGJURj-1HF4cIpFizCOOiAMqER3PG-pfgn37Dalw",
|
|
|
|
|
//
|
|
|
|
|
// "data": map[string]interface{}{
|
|
|
|
|
// "first": map[string]interface{}{
|
|
|
|
|
// "value": s.values[0],
|
|
|
|
|
// "color": "#173177",
|
|
|
|
|
// },
|
|
|
|
|
// "keyword1": map[string]interface{}{
|
|
|
|
|
// "value": fmt.Sprintf("%s %s", s.values[30], s.values[31]),
|
|
|
|
|
// "color": "#173177",
|
|
|
|
|
// },
|
|
|
|
|
// "keyword2": map[string]interface{}{
|
|
|
|
|
// "value": s.values[2],
|
|
|
|
|
// "color": "#173177",
|
|
|
|
|
// },
|
|
|
|
|
// "keyword3": map[string]interface{}{
|
|
|
|
|
// "value": s.values[1],
|
|
|
|
|
// "color": "#173177",
|
|
|
|
|
// },
|
|
|
|
|
// "keyword4": map[string]interface{}{
|
|
|
|
|
// "value": s.values[3],
|
|
|
|
|
// "color": "#173177",
|
|
|
|
|
// },
|
|
|
|
|
// "keyword5": map[string]interface{}{
|
|
|
|
|
// "value": s.rise(),
|
|
|
|
|
// "color": "#173177",
|
|
|
|
|
// },
|
|
|
|
|
// "keyword6": map[string]interface{}{
|
|
|
|
|
// "value": s.values[4],
|
|
|
|
|
// "color": "#173177",
|
|
|
|
|
// },
|
|
|
|
|
// "keyword7": map[string]interface{}{
|
|
|
|
|
// "value": s.values[5],
|
|
|
|
|
// "color": "#173177",
|
|
|
|
|
// },
|
|
|
|
|
// "keyword8": map[string]interface{}{
|
|
|
|
|
// "value": s.tradingVolume(),
|
|
|
|
|
// "color": "#173177",
|
|
|
|
|
// },
|
|
|
|
|
// "keyword9": map[string]interface{}{
|
|
|
|
|
// "value": numFormat(s.values[9]),
|
|
|
|
|
// "color": "#173177",
|
|
|
|
|
// },
|
|
|
|
|
// "keyword10": map[string]interface{}{
|
|
|
|
|
// "value": s.buyCount(),
|
|
|
|
|
// "color": "#173177",
|
|
|
|
|
// },
|
|
|
|
|
// "keyword11": map[string]interface{}{
|
|
|
|
|
// "value": s.sellCount(),
|
|
|
|
|
// "color": "#173177",
|
|
|
|
|
// },
|
|
|
|
|
// "remark": map[string]interface{}{
|
|
|
|
|
// "value": "欢迎再次购买!",
|
|
|
|
|
// "color": "#173177",
|
|
|
|
|
// },
|
|
|
|
|
// },
|
|
|
|
|
// }
|
|
|
|
|
// return arg
|
|
|
|
|
//}
|
|
|
|
|