This commit is contained in:
jiangyong 2026-09-28 11:48:11 +08:00
parent e4a02b4bb5
commit a5d9980087
1 changed files with 33 additions and 12 deletions

View File

@ -84,11 +84,14 @@ type AppConfig struct {
Replay func(message.MixMessage) *message.Reply `json:"-"`
}
type Tick struct {
Tick string `json:"tick"`
Expire int64 `json:"expire"`
}
type App struct {
tokenExpire int64
token string
tick string
tickExpire int64
Tick map[string]*Tick
config *AppConfig
}
@ -100,6 +103,7 @@ type UserInfo struct {
func NewApp(cfg *AppConfig) *App {
return &App{
config: cfg,
Tick: make(map[string]*Tick),
}
}
@ -111,12 +115,18 @@ func (q *App) GetToken() string {
return q.token
}
func (q *App) GetTick() string {
if time.Now().Unix() <= q.tickExpire-600 {
return q.tick
func (q *App) GetTick(tickType string) string {
if v, ok := q.Tick[tickType]; ok {
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) {
@ -468,10 +478,12 @@ func (q *App) refreshToken() error {
return nil
}
func (q *App) refreshTick() error {
if time.Now().Unix() <= q.tickExpire-600 {
func (q *App) refreshTick(tickType string) error {
if v, ok := q.Tick[tickType]; ok {
if time.Now().Unix() <= v.Expire-600 {
return nil
}
}
resp, err := http.Get("https://qyapi.weixin.qq.com/cgi-bin/ticket/get?access_token=" + q.GetToken() + "&type=agent_config")
if err != nil {
@ -484,8 +496,17 @@ func (q *App) refreshTick() error {
}
jsonData := make(map[string]interface{})
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
}