1
0
mirror of https://github.com/silenceper/wechat.git synced 2026-02-04 12:52:27 +08:00

use context when getting access token (#815)

* use context

Signed-off-by: mqf20 <mingqingfoo@gmail.com>

* added docs

Signed-off-by: mqf20 <mingqingfoo@gmail.com>

* improved docs

Signed-off-by: mqf20 <mingqingfoo@gmail.com>

* added SetAccessTokenContextHandle

Signed-off-by: mqf20 <mingqingfoo@gmail.com>

---------

Signed-off-by: mqf20 <mingqingfoo@gmail.com>
This commit is contained in:
mqf20
2025-01-07 22:13:03 +08:00
committed by GitHub
parent 6b9d4f82da
commit 92bf6c7699
5 changed files with 40 additions and 6 deletions

View File

@@ -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
}

View File

@@ -8,5 +8,5 @@ import (
// Context struct
type Context struct {
*config.Config
credential.AccessTokenHandle
credential.AccessTokenContextHandle
}

View File

@@ -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