- compatible to current db format(elm token).
This commit is contained in:
@@ -15,12 +15,26 @@ import (
|
|||||||
const (
|
const (
|
||||||
weixinTokenExpires = 7200 * time.Second
|
weixinTokenExpires = 7200 * time.Second
|
||||||
elmTokenExpires = 20 * 24 * 3600 * 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() {
|
go func() {
|
||||||
sleepGap := expiresTime / 10
|
sleepGap := expiresTime / 10
|
||||||
needRefreshGap := expiresTime * 8 / 10
|
needRefreshGap := expiresTime * 8 / 10
|
||||||
|
if sleepGap > maxRefreshGap {
|
||||||
|
sleepGap = maxRefreshGap
|
||||||
|
}
|
||||||
|
|
||||||
for {
|
for {
|
||||||
curConfig := &models.Config{
|
curConfig := &models.Config{
|
||||||
@@ -42,6 +56,8 @@ func RefreshConfig(configKey string, expiresTime time.Duration, configGetter fun
|
|||||||
if curConfig.Date <= latestTimeStr {
|
if curConfig.Date <= latestTimeStr {
|
||||||
curConfig.Token = configGetter()
|
curConfig.Token = configGetter()
|
||||||
handleType = 1
|
handleType = 1
|
||||||
|
} else {
|
||||||
|
configSetter(curConfig.Token)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -77,14 +93,33 @@ func RefreshWeixinToken() {
|
|||||||
return tokenInfo.AccessToken
|
return tokenInfo.AccessToken
|
||||||
}
|
}
|
||||||
return ""
|
return ""
|
||||||
|
}, func(value string) bool {
|
||||||
|
return globals2.WeixinAPI.SetToken(value)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func RefreshElmToken() {
|
func RefreshElmToken() {
|
||||||
RefreshConfig("eleme", elmTokenExpires, func() string {
|
RefreshConfig("eleme", elmTokenExpires, func() string {
|
||||||
if tokenInfo, err := globals2.ElmAPI.RefreshToken(); err == nil {
|
if tokenInfo, err := globals2.ElmAPI.RefreshTokenIndividual(); err == nil {
|
||||||
return string(utils.MustMarshal(tokenInfo))
|
tokenInfo2 := &ElmTokenForCompatible{
|
||||||
|
Error: "",
|
||||||
|
ErrorDescription: "",
|
||||||
|
AccessToken: tokenInfo.AccessToken,
|
||||||
|
TokenType: tokenInfo.TokenType,
|
||||||
|
Expires: tokenInfo.ExpiresIn,
|
||||||
|
RefreshToken: "",
|
||||||
|
Success: true,
|
||||||
|
}
|
||||||
|
return string(utils.MustMarshal(tokenInfo2))
|
||||||
}
|
}
|
||||||
return ""
|
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