diff --git a/miniprogram/config/config.go b/miniprogram/config/config.go index fb3e1518b..38cbd0365 100644 --- a/miniprogram/config/config.go +++ b/miniprogram/config/config.go @@ -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 } diff --git a/miniprogram/miniprogram.go b/miniprogram/miniprogram.go index 8adbb00d7..c50273827 100644 --- a/miniprogram/miniprogram.go +++ b/miniprogram/miniprogram.go @@ -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, diff --git a/officialaccount/config/config.go b/officialaccount/config/config.go index b38fccec9..409b2bd22 100644 --- a/officialaccount/config/config.go +++ b/officialaccount/config/config.go @@ -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 } diff --git a/officialaccount/officialaccount.go b/officialaccount/officialaccount.go index 0c00817ba..fcad7bd1e 100644 --- a/officialaccount/officialaccount.go +++ b/officialaccount/officialaccount.go @@ -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,