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:
@@ -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
|
||||
}
|
||||
|
||||
@@ -8,5 +8,5 @@ import (
|
||||
// Context struct
|
||||
type Context struct {
|
||||
*config.Config
|
||||
credential.AccessTokenHandle
|
||||
credential.AccessTokenContextHandle
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user