- 解决wxtoken的刷新问题

This commit is contained in:
gazebo
2019-04-27 15:44:26 +08:00
parent beb6482a1a
commit dbd9e3a3d2
3 changed files with 54 additions and 66 deletions

View File

@@ -20,6 +20,11 @@ type Hub struct {
type Criteria struct {
}
type WXTokenInfo struct {
IsNew bool `json:"isNew"`
Token string `json:"token"`
}
var (
SysEventHub *Hub
)
@@ -36,31 +41,6 @@ func New() (hub *Hub) {
return hub
}
func (h *Hub) GetEvent(eventTypeList []string, criteria interface{}) (event *eventhub.EventInfo, err error) {
for _, eventType := range eventTypeList {
switch eventType {
case EventTypeWXToken:
return h.getWXToken(criteria)
}
}
return event, err
}
func (h *Hub) getWXToken(criteria interface{}) (event *eventhub.EventInfo, err error) {
criteriaToken := ""
if criteria != nil {
criteriaToken = criteria.(string)
}
token := api.WeixinAPI.CBGetToken()
if token != criteriaToken {
return &eventhub.EventInfo{
Type: EventTypeWXToken,
Data: token,
}, nil
}
return nil, nil
}
func (h *Hub) IsCriteriaMatch(eventInfo *eventhub.EventInfo, criteria interface{}) bool {
return true
}
@@ -72,10 +52,21 @@ func (h *Hub) OnNewWXToken(token string) {
})
}
func (h *Hub) GetWXToken(oldToken string, waitTime time.Duration) (token string) {
eventInfo, err := h.eventHub.GetEvent(EventCategory, []string{EventTypeWXToken}, oldToken, waitTime)
if err == nil && eventInfo != nil {
token = eventInfo.Data.(string)
func (h *Hub) GetWXToken(oldToken string, waitTime time.Duration) (tokenInfo *WXTokenInfo) {
token := api.WeixinAPI.CBGetToken()
if token != oldToken {
tokenInfo = &WXTokenInfo{
IsNew: false,
Token: token,
}
} else {
eventInfo, err := h.eventHub.GetEvent(EventCategory, []string{EventTypeWXToken}, nil, waitTime)
if err == nil && eventInfo != nil {
tokenInfo = &WXTokenInfo{
IsNew: true,
Token: eventInfo.Data.(string),
}
}
}
return token
return tokenInfo
}