- weimob token
This commit is contained in:
@@ -1,21 +1,24 @@
|
||||
package tasks
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"git.rosy.net.cn/jx-callback/business/model/dao"
|
||||
"git.rosy.net.cn/jx-callback/business/model/legacymodel"
|
||||
"git.rosy.net.cn/jx-callback/globals/api"
|
||||
|
||||
"git.rosy.net.cn/baseapi/platformapi/weimobapi"
|
||||
"git.rosy.net.cn/baseapi/utils"
|
||||
"git.rosy.net.cn/jx-callback/globals"
|
||||
|
||||
"github.com/astaxie/beego"
|
||||
"github.com/astaxie/beego/orm"
|
||||
)
|
||||
|
||||
const (
|
||||
weixinTokenExpires = 7200 * time.Second
|
||||
elmTokenExpires = 20 * 24 * 3600 * time.Second
|
||||
weimobTokenExpires = 7200 * time.Second
|
||||
maxRefreshGap = 5 * 60 * time.Second
|
||||
)
|
||||
|
||||
@@ -48,14 +51,22 @@ func RefreshConfig(configKey string, expiresTime time.Duration, configGetter fun
|
||||
return "", err
|
||||
}
|
||||
handleType = 2
|
||||
} else if curConfig.Date <= utils.Time2Str(time.Now().Add(-needRefreshGap)) {
|
||||
handleType = 1
|
||||
} else {
|
||||
configSetter(curConfig.Token)
|
||||
if curConfig.Date <= utils.Time2Str(time.Now().Add(-needRefreshGap)) {
|
||||
handleType = 1
|
||||
}
|
||||
}
|
||||
if handleType != 0 {
|
||||
if curConfig.Token = configGetter(); curConfig.Token == "" {
|
||||
return "", fmt.Errorf("can not get token for %s", configKey)
|
||||
if beego.BConfig.RunMode == "prod" {
|
||||
globals.SugarLogger.Errorf("RefreshConfig %s get empty token", configKey)
|
||||
} else {
|
||||
globals.SugarLogger.Infof("RefreshConfig %s get empty token", configKey)
|
||||
}
|
||||
return "", nil
|
||||
}
|
||||
globals.SugarLogger.Debugf("refresh %s, value:%s", configKey, curConfig.Token)
|
||||
globals.SugarLogger.Debugf("RefreshConfig refresh %s, value:%s", configKey, curConfig.Token)
|
||||
|
||||
curConfig.Date = utils.GetCurTimeStr()
|
||||
var num int64
|
||||
@@ -69,15 +80,16 @@ func RefreshConfig(configKey string, expiresTime time.Duration, configGetter fun
|
||||
if err != nil || num == 0 {
|
||||
globals.SugarLogger.Errorf("db error:%v, num:%d, curConfig:%v", err, num, curConfig)
|
||||
return "", err
|
||||
} else {
|
||||
configSetter(curConfig.Token)
|
||||
}
|
||||
configSetter(curConfig.Token)
|
||||
}
|
||||
return curConfig.Token, nil
|
||||
}
|
||||
token, err := refreshFunc() // 这样写的目的是强制第一次调用时要刷新一次
|
||||
if err == nil {
|
||||
configSetter(token)
|
||||
if token != "" {
|
||||
configSetter(token)
|
||||
}
|
||||
utils.CallFuncAsync(func() {
|
||||
for {
|
||||
time.Sleep(sleepGap)
|
||||
@@ -90,8 +102,10 @@ func RefreshConfig(configKey string, expiresTime time.Duration, configGetter fun
|
||||
|
||||
func RefreshWeixinToken() error {
|
||||
return RefreshConfig("wechat", weixinTokenExpires, func() string {
|
||||
if tokenInfo, err := api.WeixinAPI.RefreshToken(); err == nil {
|
||||
return tokenInfo.AccessToken
|
||||
if beego.BConfig.RunMode == "prod" {
|
||||
if tokenInfo, err := api.WeixinAPI.RefreshToken(); err == nil {
|
||||
return tokenInfo.AccessToken
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}, func(value string) {
|
||||
@@ -101,17 +115,19 @@ func RefreshWeixinToken() error {
|
||||
|
||||
func RefreshElmToken() error {
|
||||
return RefreshConfig("eleme", elmTokenExpires, func() string {
|
||||
if tokenInfo, err := api.ElmAPI.RefreshTokenIndividual(); err == nil {
|
||||
tokenInfo2 := &ElmTokenForCompatible{
|
||||
Error: "",
|
||||
ErrorDescription: "",
|
||||
AccessToken: tokenInfo.AccessToken,
|
||||
TokenType: tokenInfo.TokenType,
|
||||
Expires: tokenInfo.ExpiresIn,
|
||||
RefreshToken: "",
|
||||
Success: true,
|
||||
if beego.BConfig.RunMode == "prod" {
|
||||
if tokenInfo, err := api.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 string(utils.MustMarshal(tokenInfo2))
|
||||
}
|
||||
return ""
|
||||
}, func(value string) {
|
||||
@@ -122,3 +138,31 @@ func RefreshElmToken() error {
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func RefreshWeimobToken() error {
|
||||
return RefreshConfig("weimob", weimobTokenExpires, func() string {
|
||||
if true { //beego.BConfig.RunMode == "prod" {
|
||||
if tokenInfo, err := api.WeimobAPI.RefreshTokenByRefreshToken(); err == nil {
|
||||
return string(utils.MustMarshal(tokenInfo))
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}, func(value string) {
|
||||
var tokenInfo *weimobapi.TokenInfo
|
||||
err := utils.UnmarshalUseNumber([]byte(value), &tokenInfo)
|
||||
if err == nil {
|
||||
api.WeimobAPI.SetToken(tokenInfo)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func SaveWeimobToken(token *weimobapi.TokenInfo) (err error) {
|
||||
db := dao.GetDB()
|
||||
config := &legacymodel.Config{
|
||||
Thirdparty: "weimob",
|
||||
Token: string(utils.MustMarshal(token)),
|
||||
Date: utils.GetCurTimeStr(),
|
||||
LastOperator: "admin",
|
||||
}
|
||||
return dao.CreateOrUpdate(db, config)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user