Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use context when getting access token #815

Merged
merged 4 commits into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions credential/access_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@ type AccessTokenHandle interface {
GetAccessToken() (accessToken string, err error)
}

// AccessTokenCompatibleHandle 同时实现 AccessTokenHandle 和 AccessTokenContextHandle
type AccessTokenCompatibleHandle struct {
AccessTokenHandle
}

// GetAccessTokenContext 获取access_token,先从cache中获取,没有则从服务端获取
func (c AccessTokenCompatibleHandle) GetAccessTokenContext(_ context.Context) (accessToken string, err error) {
return c.GetAccessToken()
}

// AccessTokenContextHandle AccessToken 接口
type AccessTokenContextHandle interface {
AccessTokenHandle
Expand Down
2 changes: 1 addition & 1 deletion miniprogram/business/phone_number.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (business *Business) GetPhoneNumber(in *GetPhoneNumberRequest) (info PhoneI

// GetPhoneNumberWithContext 利用context将code换取用户手机号。 每个code只能使用一次,code的有效期为5min
func (business *Business) GetPhoneNumberWithContext(ctx context.Context, in *GetPhoneNumberRequest) (info PhoneInfo, err error) {
accessToken, err := business.GetAccessToken()
accessToken, err := business.GetAccessTokenContext(ctx)
if err != nil {
return
}
Expand Down
2 changes: 1 addition & 1 deletion miniprogram/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ import (
// Context struct
type Context struct {
*config.Config
credential.AccessTokenHandle
credential.AccessTokenContextHandle
}
13 changes: 10 additions & 3 deletions miniprogram/miniprogram.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,22 @@ func NewMiniProgram(cfg *config.Config) *MiniProgram {
defaultAkHandle = credential.NewDefaultAccessToken(cfg.AppID, cfg.AppSecret, cacheKeyPrefix, cfg.Cache)
}
ctx := &context.Context{
Config: cfg,
AccessTokenHandle: defaultAkHandle,
Config: cfg,
AccessTokenContextHandle: defaultAkHandle,
}
return &MiniProgram{ctx}
}

// SetAccessTokenHandle 自定义 access_token 获取方式
func (miniProgram *MiniProgram) SetAccessTokenHandle(accessTokenHandle credential.AccessTokenHandle) {
miniProgram.ctx.AccessTokenHandle = accessTokenHandle
miniProgram.ctx.AccessTokenContextHandle = credential.AccessTokenCompatibleHandle{
AccessTokenHandle: accessTokenHandle,
}
}

// SetAccessTokenContextHandle 自定义 access_token 获取方式
func (miniProgram *MiniProgram) SetAccessTokenContextHandle(accessTokenContextHandle credential.AccessTokenContextHandle) {
miniProgram.ctx.AccessTokenContextHandle = accessTokenContextHandle
}

// GetContext get Context
Expand Down
19 changes: 18 additions & 1 deletion openplatform/miniprogram/miniprogram.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package miniprogram

import (
originalContext "context"
"fmt"

"github.com/silenceper/wechat/v2/credential"
Expand Down Expand Up @@ -37,6 +38,22 @@ func (miniProgram *MiniProgram) GetAccessToken() (string, error) {
return akRes.AccessToken, nil
}

// GetAccessTokenContext 利用ctx获取ak
func (miniProgram *MiniProgram) GetAccessTokenContext(ctx originalContext.Context) (string, error) {
ak, akErr := miniProgram.openContext.GetAuthrAccessTokenContext(ctx, miniProgram.AppID)
if akErr == nil {
return ak, nil
}
if miniProgram.authorizerRefreshToken == "" {
return "", fmt.Errorf("please set the authorizer_refresh_token first")
}
akRes, akResErr := miniProgram.GetComponent().RefreshAuthrTokenContext(ctx, miniProgram.AppID, miniProgram.authorizerRefreshToken)
if akResErr != nil {
return "", akResErr
}
return akRes.AccessToken, nil
}

// SetAuthorizerRefreshToken 设置代执操作业务授权账号authorizer_refresh_token
func (miniProgram *MiniProgram) SetAuthorizerRefreshToken(authorizerRefreshToken string) *MiniProgram {
miniProgram.authorizerRefreshToken = authorizerRefreshToken
Expand Down Expand Up @@ -68,7 +85,7 @@ func (miniProgram *MiniProgram) GetBasic() *basic.Basic {
// GetURLLink 小程序URL Link接口 调用前需确认已调用 SetAuthorizerRefreshToken 避免由于缓存中 authorizer_access_token 过期执行中断
func (miniProgram *MiniProgram) GetURLLink() *urllink.URLLink {
return urllink.NewURLLink(&miniContext.Context{
AccessTokenHandle: miniProgram,
AccessTokenContextHandle: miniProgram,
})
}

Expand Down
Loading