This commit is contained in:
2021-12-22 19:09:04 +08:00
parent 0ac81a4405
commit 4e0cb3c02c
9 changed files with 126 additions and 72 deletions

View File

@@ -20,7 +20,6 @@ import (
"github.com/jageros/hawox/logx"
"net/http"
"stock/fund"
"stock/module"
"stock/stock"
"stock/user"
"strings"
@@ -116,19 +115,19 @@ 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
}
//func SendAll(stk IArg) error {
// user.ForEachUser(func(u *user.User) 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"`
@@ -137,6 +136,7 @@ type rData struct {
MsgType string `xml:"MsgType"`
Content string `xml:"Content"`
MsgID int64 `xml:"MsgId"`
Event string `xml:"Event"`
}
type xml struct {
@@ -163,18 +163,52 @@ func Handle(c *gin.Context) {
CreateTime: time.Now().Unix(),
Content: "查询股票:=st股票代码例如=st600905\n查询基金=fd基金代码例如=fd161725\n\n" +
"订阅股票:+st股票代码例如+st600905\n订阅基金+fd基金代码例如+fd161725\n\n" +
"取消订阅股票:-st股票代码例如-st600905\n取消订阅基金-fd基金代码例如-fd161725",
"取消订阅股票:-st股票代码例如-st600905\n取消订阅基金-fd基金代码例如-fd161725\n\n" +
"查询已订阅股票lst\n" +
"查询已订阅基金lfd\n" +
"停止通知stop\n" +
"启动通知start\n",
}
u, err := user.GetUser(rMsg.FromUserName)
if err != nil {
wMsg.Content = err.Error()
rMsg.MsgType = ""
}
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.ToLower(rMsg.Content) == "stop":
u.Stop()
wMsg.Content = "已停止通知!"
case strings.ToLower(rMsg.Content) == "start":
u.Start()
wMsg.Content = "已启动通知!"
case strings.ToLower(rMsg.Content) == "lst":
codes := u.Codes(false)
stks, err := stock.GetStocks(codes...)
if err != nil {
wMsg.Content = "查询错误:\n" + err.Error()
} else {
msg := stks.Msg()
if msg == "" {
wMsg.Content = "您未订阅任何股票!"
} else {
wMsg.Content = "所有订阅股票信息:\n" + msg
}
}
case strings.ToLower(rMsg.Content) == "lfd":
codes := u.Codes(true)
msg := fund.FundsMsg(codes...)
if msg == "" {
wMsg.Content = "您未订阅任何基金!"
} else {
wMsg.Content = "所有订阅基金信息:\n" + msg
}
case len(rMsg.Content) < 9:
break
@@ -227,5 +261,19 @@ func Handle(c *gin.Context) {
}
}
if rMsg.MsgType == "event" {
switch rMsg.Event {
case "subscribe":
logx.Infof("user=%s subscribe!", u.OpenID())
u.Start()
case "unsubscribe":
logx.Infof("user=%s unsubscribe!", u.OpenID())
u.Stop()
c.String(http.StatusOK, "success")
c.Abort()
return
}
}
c.XML(http.StatusOK, wMsg)
}