tick
This commit is contained in:
parent
521caaa702
commit
e4a02b4bb5
|
|
@ -87,6 +87,8 @@ type AppConfig struct {
|
||||||
type App struct {
|
type App struct {
|
||||||
tokenExpire int64
|
tokenExpire int64
|
||||||
token string
|
token string
|
||||||
|
tick string
|
||||||
|
tickExpire int64
|
||||||
config *AppConfig
|
config *AppConfig
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -109,6 +111,14 @@ func (q *App) GetToken() string {
|
||||||
return q.token
|
return q.token
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (q *App) GetTick() string {
|
||||||
|
if time.Now().Unix() <= q.tickExpire-600 {
|
||||||
|
return q.tick
|
||||||
|
}
|
||||||
|
q.refreshTick()
|
||||||
|
return q.tick
|
||||||
|
}
|
||||||
|
|
||||||
func (q *App) GetResult(rspBody []byte) (map[string]interface{}, error) {
|
func (q *App) GetResult(rspBody []byte) (map[string]interface{}, error) {
|
||||||
result := make(map[string]interface{})
|
result := make(map[string]interface{})
|
||||||
if err := json.Unmarshal(rspBody, &result); err != nil {
|
if err := json.Unmarshal(rspBody, &result); err != nil {
|
||||||
|
|
@ -458,6 +468,27 @@ func (q *App) refreshToken() error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (q *App) refreshTick() error {
|
||||||
|
if time.Now().Unix() <= q.tickExpire-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 {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
body, err := io.ReadAll(resp.Body)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
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"])
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (q *App) UpdateMenu(menu string) error {
|
func (q *App) UpdateMenu(menu string) error {
|
||||||
reqUrl := fmt.Sprintf("https://qyapi.weixin.qq.com/cgi-bin/menu/create?access_token=%s&agentid=%s", q.GetToken(), q.config.Agent)
|
reqUrl := fmt.Sprintf("https://qyapi.weixin.qq.com/cgi-bin/menu/create?access_token=%s&agentid=%s", q.GetToken(), q.config.Agent)
|
||||||
rspBody, err := util.HttpPostJson(reqUrl, nil, []byte(menu))
|
rspBody, err := util.HttpPostJson(reqUrl, nil, []byte(menu))
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue