You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
go-library/service/wechatopen/authorizer_access_token.db.go

49 lines
2.3 KiB

2 years ago
package wechatopen
import (
2 years ago
"go.dtapp.net/library/utils/gotime"
2 years ago
"gorm.io/gorm"
"time"
)
// GetAuthorizerAccessTokenMonitor 获取获取/刷新接口调用令牌和监控
func (app *App) GetAuthorizerAccessTokenMonitor() string {
// 查询
authorizerAccessToken := app.GetAuthorizerAccessToken()
if authorizerAccessToken != "" {
return authorizerAccessToken
}
// 重新获取
return app.SetAuthorizerAccessToken(app.CgiBinComponentApiAuthorizerToken()).AuthorizerAccessToken
}
// SetAuthorizerAccessToken 设置获取/刷新接口调用令牌和自动获取
func (app *App) SetAuthorizerAccessToken(info *CgiBinComponentApiAuthorizerTokenResult) CgiBinComponentApiAuthorizerTokenResponse {
if app.Db == nil || info.Result.AuthorizerAccessToken == "" || info.Result.AuthorizerRefreshToken == "" || info.authorizerAppid == "" {
return CgiBinComponentApiAuthorizerTokenResponse{}
}
app.Db.Create(&AuthorizerAccessToken{
ComponentAppId: app.ComponentAppId,
AuthorizerAppId: info.authorizerAppid,
AuthorizerAccessToken: info.Result.AuthorizerAccessToken,
AuthorizerRefreshToken: info.Result.AuthorizerRefreshToken,
ExpiresIn: info.Result.ExpiresIn,
ExpireTime: gotime.Current().AfterHour(2).Time,
})
return info.Result
}
type AuthorizerAccessToken struct {
gorm.Model
ComponentAppId string `json:"component_app_id"` // 第三方平台 appid
AuthorizerAppId string `json:"authorizer_app_id"` // 授权方 appid
AuthorizerAccessToken string `json:"authorizer_access_token"` // 接口调用令牌(在授权的公众号/小程序具备 API 权限时,才有此返回值)
AuthorizerRefreshToken string `json:"authorizer_refresh_token"` // 刷新令牌在授权的公众号具备API权限时才有此返回值刷新令牌主要用于第三方平台获取和刷新已授权用户的 authorizer_access_token。一旦丢失只能让用户重新授权才能再次拿到新的刷新令牌。用户重新授权后之前的刷新令牌会失效
ExpiresIn int64 `json:"expires_in"` // 有效期,单位:秒
ExpireTime time.Time `json:"expire_time"` // 过期时间
}
func (m *AuthorizerAccessToken) TableName() string {
return "authorizer_access_token"
}