You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
115 lines
2.2 KiB
Go
115 lines
2.2 KiB
Go
/**
|
|
* @Author: jager
|
|
* @Email: lhj168os@gmail.com
|
|
* @File: fund
|
|
* @Date: 2021/12/1 3:05 下午
|
|
* @package: fund
|
|
* @Version: v1.0.0
|
|
*
|
|
* @Description:
|
|
*
|
|
*/
|
|
|
|
package fund
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"github.com/jageros/hawox/errcode"
|
|
"github.com/jageros/hawox/httpc"
|
|
"github.com/jageros/hawox/logx"
|
|
"regexp"
|
|
"strconv"
|
|
)
|
|
|
|
const (
|
|
baseUrl = "http://fundgz.1234567.com.cn/js/%s.js"
|
|
)
|
|
|
|
var (
|
|
jsonStr = regexp.MustCompile(`{(.*?)}`)
|
|
)
|
|
|
|
type fund struct {
|
|
Code string `json:"fundcode"`
|
|
Name string `json:"name"`
|
|
UnitVal string `json:"dwjz"`
|
|
EstimateVal string `json:"gsz"`
|
|
RisePer string `json:"gszzl"`
|
|
UpdateTime string `json:"gztime"`
|
|
}
|
|
|
|
func (f *fund) Msg() string {
|
|
var rise string
|
|
last, err1 := strconv.ParseFloat(f.UnitVal, 64)
|
|
cur, err2 := strconv.ParseFloat(f.EstimateVal, 64)
|
|
if err1 == nil && err2 == nil {
|
|
rise = fmt.Sprintf("%.5f", cur-last)
|
|
}
|
|
msg := fmt.Sprintf(msgTemplate, f.Name, f.UpdateTime, f.UnitVal, f.EstimateVal, rise, f.RisePer)
|
|
return msg
|
|
}
|
|
|
|
type funds struct {
|
|
codes []string
|
|
}
|
|
|
|
func NewFunds(codes ...string) *funds {
|
|
return &funds{
|
|
codes: codes,
|
|
}
|
|
}
|
|
|
|
func FundsMsg(codes ...string) string {
|
|
if len(codes) <= 0 {
|
|
return ""
|
|
}
|
|
//var msg = "基金定投估值 >>>\n"
|
|
msg := ""
|
|
for _, code := range codes {
|
|
fd, err := newFund(code)
|
|
if err != nil {
|
|
logx.Error(err)
|
|
continue
|
|
}
|
|
msg += fd.Msg()
|
|
}
|
|
return msg
|
|
}
|
|
|
|
func newFund(code string) (*fund, error) {
|
|
url := fmt.Sprintf(baseUrl, code)
|
|
result, err := httpc.Request(httpc.GET, url, httpc.FORM, nil, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
ss := jsonStr.FindStringSubmatch(string(result))
|
|
if len(ss) <= 0 {
|
|
return nil, errcode.New(404, "找不到基金:"+code)
|
|
}
|
|
ff := &fund{}
|
|
err = json.Unmarshal([]byte(ss[0]), ff)
|
|
return ff, err
|
|
}
|
|
|
|
const msgTemplate = `%s
|
|
更新时间:%s
|
|
单位净值:%s 估算净值:%s
|
|
估算涨幅:(%s) %s%%
|
|
|
|
`
|
|
|
|
func (fs *funds) Arg(openid string) map[string]interface{} {
|
|
arg := map[string]interface{}{
|
|
"touser": openid,
|
|
"template_id": "SQXWp3RiYySb2GYG-vMYDsSIm-KZNM9szVpFOryUQGQ",
|
|
"data": map[string]interface{}{
|
|
"keyword": map[string]interface{}{
|
|
"value": FundsMsg(fs.codes...),
|
|
"color": "#173177",
|
|
},
|
|
},
|
|
}
|
|
return arg
|
|
}
|