tick
This commit is contained in:
parent
e4a02b4bb5
commit
a5d9980087
|
|
@ -84,11 +84,14 @@ type AppConfig struct {
|
||||||
Replay func(message.MixMessage) *message.Reply `json:"-"`
|
Replay func(message.MixMessage) *message.Reply `json:"-"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type Tick struct {
|
||||||
|
Tick string `json:"tick"`
|
||||||
|
Expire int64 `json:"expire"`
|
||||||
|
}
|
||||||
type App struct {
|
type App struct {
|
||||||
tokenExpire int64
|
tokenExpire int64
|
||||||
token string
|
token string
|
||||||
tick string
|
Tick map[string]*Tick
|
||||||
tickExpire int64
|
|
||||||
config *AppConfig
|
config *AppConfig
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -100,6 +103,7 @@ type UserInfo struct {
|
||||||
func NewApp(cfg *AppConfig) *App {
|
func NewApp(cfg *AppConfig) *App {
|
||||||
return &App{
|
return &App{
|
||||||
config: cfg,
|
config: cfg,
|
||||||
|
Tick: make(map[string]*Tick),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -111,12 +115,18 @@ func (q *App) GetToken() string {
|
||||||
return q.token
|
return q.token
|
||||||
}
|
}
|
||||||
|
|
||||||
func (q *App) GetTick() string {
|
func (q *App) GetTick(tickType string) string {
|
||||||
if time.Now().Unix() <= q.tickExpire-600 {
|
if v, ok := q.Tick[tickType]; ok {
|
||||||
return q.tick
|
if time.Now().Unix() <= v.Expire-600 {
|
||||||
|
return v.Tick
|
||||||
}
|
}
|
||||||
q.refreshTick()
|
}
|
||||||
return q.tick
|
|
||||||
|
if err := q.refreshTick(tickType); err != nil {
|
||||||
|
log.Errorf("refresh tick err: %v", err)
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
return q.Tick[tickType].Tick
|
||||||
}
|
}
|
||||||
|
|
||||||
func (q *App) GetResult(rspBody []byte) (map[string]interface{}, error) {
|
func (q *App) GetResult(rspBody []byte) (map[string]interface{}, error) {
|
||||||
|
|
@ -468,10 +478,12 @@ func (q *App) refreshToken() error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (q *App) refreshTick() error {
|
func (q *App) refreshTick(tickType string) error {
|
||||||
if time.Now().Unix() <= q.tickExpire-600 {
|
if v, ok := q.Tick[tickType]; ok {
|
||||||
|
if time.Now().Unix() <= v.Expire-600 {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
resp, err := http.Get("https://qyapi.weixin.qq.com/cgi-bin/ticket/get?access_token=" + q.GetToken() + "&type=agent_config")
|
resp, err := http.Get("https://qyapi.weixin.qq.com/cgi-bin/ticket/get?access_token=" + q.GetToken() + "&type=agent_config")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -484,8 +496,17 @@ func (q *App) refreshTick() error {
|
||||||
}
|
}
|
||||||
jsonData := make(map[string]interface{})
|
jsonData := make(map[string]interface{})
|
||||||
json.Unmarshal(body, &jsonData)
|
json.Unmarshal(body, &jsonData)
|
||||||
q.tick = cast.ToString(jsonData["ticket"])
|
|
||||||
q.tickExpire = time.Now().Unix() + cast.ToInt64(jsonData["expires_in"])
|
if v, ok := q.Tick[tickType]; ok {
|
||||||
|
v.Tick = cast.ToString(jsonData["ticket"])
|
||||||
|
v.Expire = time.Now().Unix() + cast.ToInt64(jsonData["expires_in"])
|
||||||
|
} else {
|
||||||
|
v = &Tick{}
|
||||||
|
q.Tick[tickType] = v
|
||||||
|
v.Tick = cast.ToString(jsonData["ticket"])
|
||||||
|
v.Expire = time.Now().Unix() + cast.ToInt64(jsonData["expires_in"])
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue