Skip to content

Commit 3fbe863

Browse files
authored
officeaccount 和 miniprogram 添加UseStableAK 配置项 (#788)
1 parent 990ba6e commit 3fbe863

File tree

4 files changed

+16
-2
lines changed

4 files changed

+16
-2
lines changed

miniprogram/config/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ type Config struct {
1414
Token string `json:"token"` // token
1515
EncodingAESKey string `json:"encoding_aes_key"` // EncodingAESKey
1616
Cache cache.Cache
17+
UseStableAK bool // use the stable access_token
1718
}

miniprogram/miniprogram.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,13 @@ type MiniProgram struct {
3434

3535
// NewMiniProgram 实例化小程序 API
3636
func NewMiniProgram(cfg *config.Config) *MiniProgram {
37-
defaultAkHandle := credential.NewDefaultAccessToken(cfg.AppID, cfg.AppSecret, credential.CacheKeyMiniProgramPrefix, cfg.Cache)
37+
var defaultAkHandle credential.AccessTokenContextHandle
38+
const cacheKeyPrefix = credential.CacheKeyMiniProgramPrefix
39+
if cfg.UseStableAK {
40+
defaultAkHandle = credential.NewStableAccessToken(cfg.AppID, cfg.AppSecret, cacheKeyPrefix, cfg.Cache)
41+
} else {
42+
defaultAkHandle = credential.NewDefaultAccessToken(cfg.AppID, cfg.AppSecret, cacheKeyPrefix, cfg.Cache)
43+
}
3844
ctx := &context.Context{
3945
Config: cfg,
4046
AccessTokenHandle: defaultAkHandle,

officialaccount/config/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ type Config struct {
1111
Token string `json:"token"` // token
1212
EncodingAESKey string `json:"encoding_aes_key"` // EncodingAESKey
1313
Cache cache.Cache
14+
UseStableAK bool // use the stable access_token
1415
}

officialaccount/officialaccount.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,13 @@ type OfficialAccount struct {
4949

5050
// NewOfficialAccount 实例化公众号API
5151
func NewOfficialAccount(cfg *config.Config) *OfficialAccount {
52-
defaultAkHandle := credential.NewDefaultAccessToken(cfg.AppID, cfg.AppSecret, credential.CacheKeyOfficialAccountPrefix, cfg.Cache)
52+
var defaultAkHandle credential.AccessTokenContextHandle
53+
const cacheKeyPrefix = credential.CacheKeyOfficialAccountPrefix
54+
if cfg.UseStableAK {
55+
defaultAkHandle = credential.NewStableAccessToken(cfg.AppID, cfg.AppSecret, cacheKeyPrefix, cfg.Cache)
56+
} else {
57+
defaultAkHandle = credential.NewDefaultAccessToken(cfg.AppID, cfg.AppSecret, cacheKeyPrefix, cfg.Cache)
58+
}
5359
ctx := &context.Context{
5460
Config: cfg,
5561
AccessTokenHandle: defaultAkHandle,

0 commit comments

Comments
 (0)