Skip to content

Commit

Permalink
officeaccount 和 miniprogram 添加UseStableAK 配置项 (#788)
Browse files Browse the repository at this point in the history
  • Loading branch information
cyjaysong authored Nov 23, 2024
1 parent 990ba6e commit 3fbe863
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 2 deletions.
1 change: 1 addition & 0 deletions miniprogram/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ type Config struct {
Token string `json:"token"` // token
EncodingAESKey string `json:"encoding_aes_key"` // EncodingAESKey
Cache cache.Cache
UseStableAK bool // use the stable access_token
}
8 changes: 7 additions & 1 deletion miniprogram/miniprogram.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,13 @@ type MiniProgram struct {

// NewMiniProgram 实例化小程序 API
func NewMiniProgram(cfg *config.Config) *MiniProgram {
defaultAkHandle := credential.NewDefaultAccessToken(cfg.AppID, cfg.AppSecret, credential.CacheKeyMiniProgramPrefix, cfg.Cache)
var defaultAkHandle credential.AccessTokenContextHandle
const cacheKeyPrefix = credential.CacheKeyMiniProgramPrefix
if cfg.UseStableAK {
defaultAkHandle = credential.NewStableAccessToken(cfg.AppID, cfg.AppSecret, cacheKeyPrefix, cfg.Cache)
} else {
defaultAkHandle = credential.NewDefaultAccessToken(cfg.AppID, cfg.AppSecret, cacheKeyPrefix, cfg.Cache)
}
ctx := &context.Context{
Config: cfg,
AccessTokenHandle: defaultAkHandle,
Expand Down
1 change: 1 addition & 0 deletions officialaccount/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ type Config struct {
Token string `json:"token"` // token
EncodingAESKey string `json:"encoding_aes_key"` // EncodingAESKey
Cache cache.Cache
UseStableAK bool // use the stable access_token
}
8 changes: 7 additions & 1 deletion officialaccount/officialaccount.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,13 @@ type OfficialAccount struct {

// NewOfficialAccount 实例化公众号API
func NewOfficialAccount(cfg *config.Config) *OfficialAccount {
defaultAkHandle := credential.NewDefaultAccessToken(cfg.AppID, cfg.AppSecret, credential.CacheKeyOfficialAccountPrefix, cfg.Cache)
var defaultAkHandle credential.AccessTokenContextHandle
const cacheKeyPrefix = credential.CacheKeyOfficialAccountPrefix
if cfg.UseStableAK {
defaultAkHandle = credential.NewStableAccessToken(cfg.AppID, cfg.AppSecret, cacheKeyPrefix, cfg.Cache)
} else {
defaultAkHandle = credential.NewDefaultAccessToken(cfg.AppID, cfg.AppSecret, cacheKeyPrefix, cfg.Cache)
}
ctx := &context.Context{
Config: cfg,
AccessTokenHandle: defaultAkHandle,
Expand Down

0 comments on commit 3fbe863

Please sign in to comment.