Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

officeaccount 和 miniprogram 添加UseStableAK 配置项 #788

Open
wants to merge 1 commit into
base: v2
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

用同一个key 有覆盖的可能吗?比如某处用了 UseStable 其他地方用了非 UseStable,这个key 是否区分一下呢?

Copy link
Author

@cyjaysong cyjaysong Aug 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个跟key没关系。你至于你说的不同地方用的不一样我想应该不常见吧,单机应用的话,一般会全局一个单例,分布式部署的话即便是非StableAK,开发者也得考虑多机共享ak,对于微信来说,无论你有多少个模块,它只允许你只有一个ak。所以跟这个无关

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
Loading