diff --git a/wechat/corp.go b/wechat/corp.go index cc7e1f2..00d6ef0 100644 --- a/wechat/corp.go +++ b/wechat/corp.go @@ -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 } diff --git a/wechat/miniprogram.go b/wechat/miniprogram.go index 426e5a7..9e71fbd 100644 --- a/wechat/miniprogram.go +++ b/wechat/miniprogram.go @@ -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) } @@ -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 } diff --git a/wechat/official_account.go b/wechat/official_account.go index bfacd84..933a806 100644 --- a/wechat/official_account.go +++ b/wechat/official_account.go @@ -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 }