Skip to content

Commit d01c858

Browse files
committed
use context
Signed-off-by: mqf20 <[email protected]>
1 parent 6b9d4f8 commit d01c858

File tree

5 files changed

+33
-6
lines changed

5 files changed

+33
-6
lines changed

credential/access_token.go

+8
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@ type AccessTokenHandle interface {
77
GetAccessToken() (accessToken string, err error)
88
}
99

10+
type AccessTokenCompatibleHandle struct {
11+
AccessTokenHandle
12+
}
13+
14+
func (c AccessTokenCompatibleHandle) GetAccessTokenContext(_ context.Context) (accessToken string, err error) {
15+
return c.GetAccessToken()
16+
}
17+
1018
// AccessTokenContextHandle AccessToken 接口
1119
type AccessTokenContextHandle interface {
1220
AccessTokenHandle

miniprogram/business/phone_number.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func (business *Business) GetPhoneNumber(in *GetPhoneNumberRequest) (info PhoneI
3434

3535
// GetPhoneNumberWithContext 利用context将code换取用户手机号。 每个code只能使用一次,code的有效期为5min
3636
func (business *Business) GetPhoneNumberWithContext(ctx context.Context, in *GetPhoneNumberRequest) (info PhoneInfo, err error) {
37-
accessToken, err := business.GetAccessToken()
37+
accessToken, err := business.GetAccessTokenContext(ctx)
3838
if err != nil {
3939
return
4040
}

miniprogram/context/context.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ import (
88
// Context struct
99
type Context struct {
1010
*config.Config
11-
credential.AccessTokenHandle
11+
credential.AccessTokenContextHandle
1212
}

miniprogram/miniprogram.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,17 @@ func NewMiniProgram(cfg *config.Config) *MiniProgram {
4242
defaultAkHandle = credential.NewDefaultAccessToken(cfg.AppID, cfg.AppSecret, cacheKeyPrefix, cfg.Cache)
4343
}
4444
ctx := &context.Context{
45-
Config: cfg,
46-
AccessTokenHandle: defaultAkHandle,
45+
Config: cfg,
46+
AccessTokenContextHandle: defaultAkHandle,
4747
}
4848
return &MiniProgram{ctx}
4949
}
5050

5151
// SetAccessTokenHandle 自定义 access_token 获取方式
5252
func (miniProgram *MiniProgram) SetAccessTokenHandle(accessTokenHandle credential.AccessTokenHandle) {
53-
miniProgram.ctx.AccessTokenHandle = accessTokenHandle
53+
miniProgram.ctx.AccessTokenContextHandle = credential.AccessTokenCompatibleHandle{
54+
AccessTokenHandle: accessTokenHandle,
55+
}
5456
}
5557

5658
// GetContext get Context

openplatform/miniprogram/miniprogram.go

+18-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package miniprogram
22

33
import (
4+
originalContext "context"
45
"fmt"
56

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

41+
// GetAccessTokenContext 利用ctx获取ak
42+
func (miniProgram *MiniProgram) GetAccessTokenContext(ctx originalContext.Context) (string, error) {
43+
ak, akErr := miniProgram.openContext.GetAuthrAccessTokenContext(ctx, miniProgram.AppID)
44+
if akErr == nil {
45+
return ak, nil
46+
}
47+
if miniProgram.authorizerRefreshToken == "" {
48+
return "", fmt.Errorf("please set the authorizer_refresh_token first")
49+
}
50+
akRes, akResErr := miniProgram.GetComponent().RefreshAuthrTokenContext(ctx, miniProgram.AppID, miniProgram.authorizerRefreshToken)
51+
if akResErr != nil {
52+
return "", akResErr
53+
}
54+
return akRes.AccessToken, nil
55+
}
56+
4057
// SetAuthorizerRefreshToken 设置代执操作业务授权账号authorizer_refresh_token
4158
func (miniProgram *MiniProgram) SetAuthorizerRefreshToken(authorizerRefreshToken string) *MiniProgram {
4259
miniProgram.authorizerRefreshToken = authorizerRefreshToken
@@ -68,7 +85,7 @@ func (miniProgram *MiniProgram) GetBasic() *basic.Basic {
6885
// GetURLLink 小程序URL Link接口 调用前需确认已调用 SetAuthorizerRefreshToken 避免由于缓存中 authorizer_access_token 过期执行中断
6986
func (miniProgram *MiniProgram) GetURLLink() *urllink.URLLink {
7087
return urllink.NewURLLink(&miniContext.Context{
71-
AccessTokenHandle: miniProgram,
88+
AccessTokenContextHandle: miniProgram,
7289
})
7390
}
7491

0 commit comments

Comments
 (0)