- compatible to current db format(elm token).
This commit is contained in:
@@ -15,12 +15,26 @@ import (
|
||||
const (
|
||||
weixinTokenExpires = 7200 * time.Second
|
||||
elmTokenExpires = 20 * 24 * 3600 * time.Second
|
||||
maxRefreshGap = 5 * 60 * time.Second
|
||||
)
|
||||
|
||||
func RefreshConfig(configKey string, expiresTime time.Duration, configGetter func() string) {
|
||||
type ElmTokenForCompatible struct {
|
||||
Error string `json:"error"`
|
||||
ErrorDescription string `json:"error_description"`
|
||||
AccessToken string `json:"accessToken"`
|
||||
TokenType string `json:"tokenType"`
|
||||
Expires int `json:"expires"`
|
||||
RefreshToken string `json:"refreshToken"`
|
||||
Success bool `json:"success"`
|
||||
}
|
||||
|
||||
func RefreshConfig(configKey string, expiresTime time.Duration, configGetter func() string, configSetter func(value string) bool) {
|
||||
go func() {
|
||||
sleepGap := expiresTime / 10
|
||||
needRefreshGap := expiresTime * 8 / 10
|
||||
if sleepGap > maxRefreshGap {
|
||||
sleepGap = maxRefreshGap
|
||||
}
|
||||
|
||||
for {
|
||||
curConfig := &models.Config{
|
||||
@@ -42,6 +56,8 @@ func RefreshConfig(configKey string, expiresTime time.Duration, configGetter fun
|
||||
if curConfig.Date <= latestTimeStr {
|
||||
curConfig.Token = configGetter()
|
||||
handleType = 1
|
||||
} else {
|
||||
configSetter(curConfig.Token)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -77,14 +93,33 @@ func RefreshWeixinToken() {
|
||||
return tokenInfo.AccessToken
|
||||
}
|
||||
return ""
|
||||
}, func(value string) bool {
|
||||
return globals2.WeixinAPI.SetToken(value)
|
||||
})
|
||||
}
|
||||
|
||||
func RefreshElmToken() {
|
||||
RefreshConfig("eleme", elmTokenExpires, func() string {
|
||||
if tokenInfo, err := globals2.ElmAPI.RefreshToken(); err == nil {
|
||||
return string(utils.MustMarshal(tokenInfo))
|
||||
if tokenInfo, err := globals2.ElmAPI.RefreshTokenIndividual(); err == nil {
|
||||
tokenInfo2 := &ElmTokenForCompatible{
|
||||
Error: "",
|
||||
ErrorDescription: "",
|
||||
AccessToken: tokenInfo.AccessToken,
|
||||
TokenType: tokenInfo.TokenType,
|
||||
Expires: tokenInfo.ExpiresIn,
|
||||
RefreshToken: "",
|
||||
Success: true,
|
||||
}
|
||||
return string(utils.MustMarshal(tokenInfo2))
|
||||
}
|
||||
return ""
|
||||
}, func(value string) bool {
|
||||
var tokenInfo ElmTokenForCompatible
|
||||
err := utils.UnmarshalUseNumber([]byte(value), &tokenInfo)
|
||||
if err == nil {
|
||||
var tokenInfo ElmTokenForCompatible
|
||||
return globals2.ElmAPI.SetToken(tokenInfo.AccessToken)
|
||||
}
|
||||
return false
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user