99 lines
2.0 KiB
Go
99 lines
2.0 KiB
Go
package syseventhub
|
|
|
|
import (
|
|
"time"
|
|
|
|
"git.rosy.net.cn/baseapi/utils"
|
|
"git.rosy.net.cn/jx-callback/globals"
|
|
|
|
"git.rosy.net.cn/jx-callback/business/jxutils/eventhub"
|
|
"git.rosy.net.cn/jx-callback/globals/api"
|
|
)
|
|
|
|
const (
|
|
EventCategory = "sys"
|
|
|
|
EventTypeWXToken = "wxtoken"
|
|
EventTypeYLYToken = "ylytoken"
|
|
)
|
|
|
|
type Hub struct {
|
|
eventHub *eventhub.EventHub
|
|
}
|
|
|
|
type Criteria struct {
|
|
}
|
|
|
|
type TokenInfo struct {
|
|
IsNew bool `json:"isNew"`
|
|
Token string `json:"token"`
|
|
}
|
|
|
|
var (
|
|
SysEventHub *Hub
|
|
)
|
|
|
|
func init() {
|
|
SysEventHub = New()
|
|
}
|
|
|
|
func New() (hub *Hub) {
|
|
hub = &Hub{
|
|
eventHub: eventhub.New(),
|
|
}
|
|
hub.eventHub.RegisterProducer(EventCategory, hub)
|
|
return hub
|
|
}
|
|
|
|
func (h *Hub) IsCriteriaMatch(eventInfo *eventhub.EventInfo, criteria interface{}) bool {
|
|
return true
|
|
}
|
|
|
|
func (h *Hub) OnNewWXToken(token string) {
|
|
h.eventHub.PostNewEvent(EventCategory, &eventhub.EventInfo{
|
|
Type: EventTypeWXToken,
|
|
Data: token,
|
|
})
|
|
}
|
|
|
|
func (h *Hub) OnNewYLYToken(token string) {
|
|
h.eventHub.PostNewEvent(EventCategory, &eventhub.EventInfo{
|
|
Type: EventTypeYLYToken,
|
|
Data: token,
|
|
})
|
|
}
|
|
|
|
func (h *Hub) GetToken(tokenType, oldToken string, waitTime time.Duration) (tokenInfo *TokenInfo) {
|
|
var token string
|
|
switch tokenType {
|
|
case EventTypeWXToken:
|
|
token = api.WeixinAPI.CBGetToken()
|
|
case EventTypeYLYToken:
|
|
token = api.YilianyunAPI.GetToken()
|
|
}
|
|
if token != oldToken {
|
|
tokenInfo = &TokenInfo{
|
|
IsNew: false,
|
|
Token: token,
|
|
}
|
|
} else {
|
|
eventInfo, err := h.eventHub.GetEvent(EventCategory, []string{tokenType}, nil, waitTime)
|
|
if err == nil && eventInfo != nil {
|
|
tokenInfo = &TokenInfo{
|
|
IsNew: true,
|
|
Token: eventInfo.Data.(string),
|
|
}
|
|
}
|
|
}
|
|
globals.SugarLogger.Debugf("GetToken tokenInfo:%s", utils.Format4Output(tokenInfo, true))
|
|
return tokenInfo
|
|
}
|
|
|
|
func (h *Hub) GetWXToken(oldToken string, waitTime time.Duration) (tokenInfo *TokenInfo) {
|
|
return h.GetToken(EventTypeWXToken, oldToken, waitTime)
|
|
}
|
|
|
|
func (h *Hub) GetYLYToken(oldToken string, waitTime time.Duration) (tokenInfo *TokenInfo) {
|
|
return h.GetToken(EventTypeYLYToken, oldToken, waitTime)
|
|
}
|