Skip to content

Commit

Permalink
更新微信AccessToken加载
Browse files Browse the repository at this point in the history
  • Loading branch information
shenghui0779 committed Dec 4, 2024
1 parent 7efa96c commit 6e6e847
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 25 deletions.
17 changes: 11 additions & 6 deletions wechat/corp.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,25 +128,30 @@ func (c *Corp) AccessToken(ctx context.Context) (gjson.Result, error) {
return ret, nil
}

// LoadAccessTokenFunc 自定义加载AccessToken
func (c *Corp) LoadAccessTokenFunc(fn func(ctx context.Context) (string, error), interval time.Duration) error {
// AutoLoadAccessToken 自动加载AccessToken
func (c *Corp) AutoLoadAccessToken(fn func(ctx context.Context, c *Corp) (string, error), interval time.Duration) error {
ctx := context.Background()

// 初始化AccessToken
token, err := fn(context.Background())
token, err := fn(ctx, c)
if err != nil {
return err
}
c.token.Store(token)

// 异步定时加载
go func() {
go func(ctx context.Context) {
ticker := time.NewTicker(interval)
defer ticker.Stop()

for range ticker.C {
_token, _ := fn(context.Background())
_token, _ := fn(ctx, c)
if len(token) != 0 {
c.token.Store(_token)
}
}
}()
}(ctx)

return nil
}

Expand Down
28 changes: 19 additions & 9 deletions wechat/miniprogram.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ type MiniProgram struct {
sfMode *SafeMode
token atomic.Value
client *resty.Client

logger func(ctx context.Context, err error, data map[string]string)
}

Expand Down Expand Up @@ -387,45 +388,54 @@ func (mp *MiniProgram) StableAccessToken(ctx context.Context, forceRefresh bool)

// AutoLoadAccessToken 自动加载AccessToken(使用StableAccessToken接口)
func (mp *MiniProgram) AutoLoadAccessToken(interval time.Duration) error {
ctx := context.Background()

// 初始化AccessToken
ret, err := mp.StableAccessToken(context.Background(), false)
if err != nil {
return err
}
mp.token.Store(ret.Get("access_token").String())

// 异步定时加载
go func() {
go func(ctx context.Context) {
ticker := time.NewTicker(interval)
defer ticker.Stop()

for range ticker.C {
_ret, _ := mp.StableAccessToken(context.Background(), false)
_ret, _ := mp.StableAccessToken(ctx, false)
if token := _ret.Get("access_token").String(); len(token) != 0 {
mp.token.Store(token)
}
}
}()
}(ctx)
return nil
}

// LoadAccessTokenFunc 自定义加载AccessToken
func (mp *MiniProgram) LoadAccessTokenFunc(fn func(ctx context.Context) (string, error), interval time.Duration) error {
// CustomAccessTokenLoad 自定义加载AccessToken
func (mp *MiniProgram) CustomAccessTokenLoad(fn func(ctx context.Context, mp *MiniProgram) (string, error), interval time.Duration) error {
ctx := context.Background()

// 初始化AccessToken
token, err := fn(context.Background())
token, err := fn(ctx, mp)
if err != nil {
return err
}
mp.token.Store(token)

// 异步定时加载
go func() {
go func(ctx context.Context) {
ticker := time.NewTicker(interval)
defer ticker.Stop()

for range ticker.C {
_token, _ := fn(context.Background())
_token, _ := fn(ctx, mp)
if len(token) != 0 {
mp.token.Store(_token)
}
}
}()
}(ctx)

return nil
}

Expand Down
28 changes: 18 additions & 10 deletions wechat/official_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,45 +218,53 @@ func (oa *OfficialAccount) StableAccessToken(ctx context.Context, forceRefresh b

// AutoLoadAccessToken 自动加载AccessToken(使用StableAccessToken接口)
func (oa *OfficialAccount) AutoLoadAccessToken(interval time.Duration) error {
ctx := context.Background()

// 初始化AccessToken
ret, err := oa.StableAccessToken(context.Background(), false)
ret, err := oa.StableAccessToken(ctx, false)
if err != nil {
return err
}
oa.token.Store(ret.Get("access_token").String())

// 异步定时加载
go func() {
go func(ctx context.Context) {
ticker := time.NewTicker(interval)
defer ticker.Stop()
for range ticker.C {
_ret, _ := oa.StableAccessToken(context.Background(), false)
_ret, _ := oa.StableAccessToken(ctx, false)
if token := _ret.Get("access_token").String(); len(token) != 0 {
oa.token.Store(token)
}
}
}()
}(ctx)

return nil
}

// LoadAccessTokenFunc 自定义加载AccessToken
func (oa *OfficialAccount) LoadAccessTokenFunc(fn func(ctx context.Context) (string, error), interval time.Duration) error {
// CustomAccessTokenLoad 自定义加载AccessToken
func (oa *OfficialAccount) CustomAccessTokenLoad(fn func(ctx context.Context, oa *OfficialAccount) (string, error), interval time.Duration) error {
ctx := context.Background()

// 初始化AccessToken
token, err := fn(context.Background())
token, err := fn(ctx, oa)
if err != nil {
return err
}
oa.token.Store(token)

// 异步定时加载
go func() {
go func(ctx context.Context) {
ticker := time.NewTicker(interval)
defer ticker.Stop()
for range ticker.C {
_token, _ := fn(context.Background())
_token, _ := fn(ctx, oa)
if len(token) != 0 {
oa.token.Store(_token)
}
}
}()
}(ctx)

return nil
}

Expand Down

0 comments on commit 6e6e847

Please sign in to comment.