Skip to content

Commit

Permalink
feature:支持关闭token缓存功能
Browse files Browse the repository at this point in the history
  • Loading branch information
NICEXAI committed Jul 28, 2021
1 parent 1683a73 commit 672ce37
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
3 changes: 3 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type Options struct {
EncodingAESKey string // 回调消息加解密参数是AES密钥的Base64编码,用于解密回调消息内容对应的密文
Cache cache.Cache // 数据缓存
ExpireTime time.Duration // 令牌过期时间
IsCloseCache bool // 是否关闭自动缓存AccessToken, 默认缓存
}

// Client 微信客服实例
Expand All @@ -33,6 +34,7 @@ type Client struct {
eventQueue sync.Map //事件队列
mutex sync.Mutex
accessToken string // 用户访问凭证
isCloseCache bool // 是否自动缓存AccessToken, 默认缓存
}

// New 初始化微信客服实例
Expand All @@ -54,6 +56,7 @@ func New(options Options) (client *Client, err error) {
cache: options.Cache,
eventQueue: sync.Map{},
mutex: sync.Mutex{},
isCloseCache: options.IsCloseCache,
}

if err = client.initAccessToken(); err != nil {
Expand Down
8 changes: 8 additions & 0 deletions token.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ func (r *Client) RefreshAccessToken() error {
}

func (r *Client) initAccessToken() error {
//如果关闭自动缓存则直接刷新AccessToken
if r.isCloseCache {
if err := r.RefreshAccessToken(); err != nil {
return err
}
return nil
}

//判断是否已初始化完成,如果己初始化则直接返回当前实例
token, err := r.getAccessToken()
if err != nil {
Expand Down

0 comments on commit 672ce37

Please sign in to comment.