This commit is contained in:
2021-12-21 18:03:55 +08:00
parent 0df3603ef2
commit b8d1391bde
12 changed files with 458 additions and 169 deletions

View File

@@ -14,9 +14,14 @@ package wxgzh
import (
"fmt"
"github.com/gin-gonic/gin"
"github.com/jageros/hawox/errcode"
"github.com/jageros/hawox/httpc"
"stock/stock"
"github.com/jageros/hawox/logx"
"net/http"
"stock/module"
"stock/user"
"strings"
"time"
)
@@ -43,21 +48,8 @@ type AccessToken struct {
Errmsg string `json:"errmsg"`
}
type RData struct {
ToUserName string `xml:"ToUserName"`
FromUserName string `xml:"FromUserName"`
CreateTime int `xml:"CreateTime"`
MsgType string `xml:"MsgType"`
Content string `xml:"Content"`
MsgID int64 `xml:"MsgId"`
}
type xml struct {
ToUserName string `xml:"ToUserName"`
FromUserName string `xml:"FromUserName"`
CreateTime int `xml:"CreateTime"`
MsgType string `xml:"MsgType"`
Content string `xml:"Content"`
type IArg interface {
Arg(openid string) map[string]interface{}
}
func getAccessToken(update bool) (string, error) {
@@ -77,16 +69,16 @@ func getAccessToken(update bool) (string, error) {
return resp.AccessToken, nil
}
func send(openID string, stk stock.IArg, recall bool) error {
func send(openID string, arg IArg, recall bool) error {
token, err := getAccessToken(false)
if err != nil {
return err
}
url := fmt.Sprintf(sendUrl, token)
arg := stk.Arg(openID)
msg := arg.Arg(openID)
resp := &Resp{}
err = httpc.RequestWithInterface(httpc.POST, url, httpc.JSON, arg, nil, resp)
err = httpc.RequestWithInterface(httpc.POST, url, httpc.JSON, msg, nil, resp)
if err != nil {
return err
}
@@ -97,7 +89,7 @@ func send(openID string, stk stock.IArg, recall bool) error {
return err
}
if recall {
return send(openID, stk, false)
return send(openID, arg, false)
}
}
return errcode.New(int32(resp.Errcode), resp.Errmsg)
@@ -105,6 +97,76 @@ func send(openID string, stk stock.IArg, recall bool) error {
return nil
}
func Send(openId string, stk stock.IArg) error {
func Send(openId string, stk IArg) error {
return send(openId, stk, true)
}
func SendAll(stk IArg) error {
user.ForEachUser(func(u module.IUser) bool {
if u.HasSubscribed(false, "") {
err := Send(u.OpenID(), stk)
if err != nil {
logx.Error(err)
return false
}
}
return true
})
return nil
}
type rData struct {
ToUserName string `xml:"ToUserName"`
FromUserName string `xml:"FromUserName"`
CreateTime int64 `xml:"CreateTime"`
MsgType string `xml:"MsgType"`
Content string `xml:"Content"`
MsgID int64 `xml:"MsgId"`
}
type xml struct {
ToUserName string `xml:"ToUserName"`
FromUserName string `xml:"FromUserName"`
CreateTime int64 `xml:"CreateTime"`
MsgType string `xml:"MsgType"`
Content string `xml:"Content"`
}
func Handle(c *gin.Context) {
rMsg := &rData{}
err := c.BindXML(rMsg)
if err != nil {
c.String(http.StatusOK, err.Error())
c.Abort()
return
}
wMsg := &xml{
ToUserName: rMsg.FromUserName,
FromUserName: rMsg.ToUserName,
MsgType: "text",
CreateTime: time.Now().Unix(),
Content: "订阅股票:+st股票代码例如+st600905\n订阅基金+fd基金代码例如+fd161725\n" +
"取消订阅股票:-st股票代码例如-st600905\n取消订阅基金-fd基金代码例如-fd161725",
}
if rMsg.MsgType == "text" {
u, err := user.GetUser(rMsg.FromUserName)
if err != nil {
c.String(http.StatusOK, err.Error())
c.Abort()
return
}
switch {
case strings.HasPrefix(rMsg.Content, "+"):
u.Subscribe(rMsg.Content[1:3] == "fd", rMsg.Content[3:])
wMsg.Content = "订阅成功!"
case strings.HasPrefix(rMsg.Content, "-"):
u.UnSubscribe(rMsg.Content[1:3] == "fd", rMsg.Content[3:])
wMsg.Content = "成功取消订阅!"
}
}
c.XML(http.StatusOK, wMsg)
}