From 923b7df3a8ca5e32ae06a1956cefd0b3131906ba Mon Sep 17 00:00:00 2001 From: CyJay Date: Tue, 6 Aug 2024 17:30:31 +0800 Subject: [PATCH] =?UTF-8?q?officeaccount=20=E5=92=8C=20miniprogram=20?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0UseStableAK=20=E9=85=8D=E7=BD=AE=E9=A1=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- miniprogram/config/config.go | 1 + miniprogram/miniprogram.go | 8 +++++++- officialaccount/config/config.go | 1 + officialaccount/officialaccount.go | 8 +++++++- 4 files changed, 16 insertions(+), 2 deletions(-) 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,