diff --git a/mini/access_token.go b/mini/access_token.go index 98867cc..68dc1da 100644 --- a/mini/access_token.go +++ b/mini/access_token.go @@ -10,52 +10,52 @@ import ( // 获取小程序全局唯一后台接口调用凭据(access_token) // 微信小程序文档:https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/mp-access-token/getAccessToken.html -func (s *SDK) getAccessToken() (err error) { - defer func() { - if err != nil { - // reset default refresh internal - s.RefreshInternal = time.Second * 20 - if s.callback != nil { - go s.callback("", "", 0, err) - } - } - }() - - path := "/cgi-bin/token?grant_type=client_credential&appid=" + s.Appid + "&secret=" + s.Secret - at := &AccessToken{} - if _, err = s.DoRequestGet(s.ctx, path, at); err != nil { - return - } - if at.Errcode != Success { - err = fmt.Errorf("errcode(%d), errmsg(%s)", at.Errcode, at.Errmsg) - return - } - s.accessToken = at.AccessToken - s.RefreshInternal = time.Second * time.Duration(at.ExpiresIn) - if s.callback != nil { - go s.callback(s.Appid, at.AccessToken, at.ExpiresIn, nil) - } - return nil -} +//func (s *SDK) getAccessToken() (err error) { +// defer func() { +// if err != nil { +// // reset default refresh internal +// s.RefreshInternal = time.Second * 20 +// if s.callback != nil { +// go s.callback("", "", 0, err) +// } +// } +// }() +// +// path := "/cgi-bin/token?grant_type=client_credential&appid=" + s.Appid + "&secret=" + s.Secret +// at := &AccessToken{} +// if _, err = s.DoRequestGet(s.ctx, path, at); err != nil { +// return +// } +// if at.Errcode != Success { +// err = fmt.Errorf("errcode(%d), errmsg(%s)", at.Errcode, at.Errmsg) +// return +// } +// s.accessToken = at.AccessToken +// s.RefreshInternal = time.Second * time.Duration(at.ExpiresIn) +// if s.callback != nil { +// go s.callback(s.Appid, at.AccessToken, at.ExpiresIn, nil) +// } +// return nil +//} -func (s *SDK) goAutoRefreshAccessToken() { - defer func() { - if r := recover(); r != nil { - buf := make([]byte, 64<<10) - buf = buf[:runtime.Stack(buf, false)] - s.logger.Errorf("mini_goAutoRefreshAccessToken: panic recovered: %s\n%s", r, buf) - } - }() - for { - // every one hour, request new access token, default 10s - time.Sleep(s.RefreshInternal / 2) - err := s.getAccessToken() - if err != nil { - s.logger.Errorf("get access token error, after 10s retry: %+v", err) - continue - } - } -} +//func (s *SDK) goAutoRefreshAccessToken() { +// defer func() { +// if r := recover(); r != nil { +// buf := make([]byte, 64<<10) +// buf = buf[:runtime.Stack(buf, false)] +// s.logger.Errorf("mini_goAutoRefreshAccessToken: panic recovered: %s\n%s", r, buf) +// } +// }() +// for { +// // every one hour, request new access token, default 10s +// time.Sleep(s.RefreshInternal / 2) +// err := s.getAccessToken() +// if err != nil { +// s.logger.Errorf("get access token error, after 10s retry: %+v", err) +// continue +// } +// } +//} // 获取稳定版接口调用凭据 // 微信小程序文档:https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/mp-access-token/getStableAccessToken.html @@ -93,6 +93,11 @@ func (s *SDK) goAutoRefreshStableAccessToken() { buf := make([]byte, 64<<10) buf = buf[:runtime.Stack(buf, false)] s.logger.Errorf("mini_goAutoRefreshAccessToken: panic recovered: %s\n%s", r, buf) + time.Sleep(time.Second * 3) + if err := s.getStableAccessToken(); err != nil { + // 失败就不再自动刷新了 + return + } s.goAutoRefreshStableAccessToken() } }() diff --git a/open/access_token.go b/open/access_token.go index 4565b2b..9c1b66e 100644 --- a/open/access_token.go +++ b/open/access_token.go @@ -46,6 +46,7 @@ func (s *SDK) goAutoRefreshAccessTokenJob() { buf := make([]byte, 64<<10) buf = buf[:runtime.Stack(buf, false)] s.logger.Errorf("open_goAutoRefreshAccessToken: panic recovered: %s\n%s", r, buf) + time.Sleep(time.Second * 3) s.goAutoRefreshAccessTokenJob() } }() diff --git a/public/access_token.go b/public/access_token.go index 8d5e5cb..0711a92 100644 --- a/public/access_token.go +++ b/public/access_token.go @@ -36,13 +36,18 @@ func (s *SDK) getAccessToken() (err error) { return nil } -func (s *SDK) goAutoRefreshAccessToken() { +func (s *SDK) goAutoRefreshAccessTokenJob() { defer func() { if r := recover(); r != nil { buf := make([]byte, 64<<10) buf = buf[:runtime.Stack(buf, false)] - s.logger.Errorf("public_goAutoRefreshAccessToken: panic recovered: %s\n%s", r, buf) - s.goAutoRefreshAccessToken() + s.logger.Errorf("public_goAutoRefreshAccessTokenJob: panic recovered: %s\n%s", r, buf) + time.Sleep(time.Second * 3) + if err := s.getAccessToken(); err != nil { + // 失败就不再自动刷新了 + return + } + s.goAutoRefreshAccessTokenJob() } }() for { diff --git a/public/public.go b/public/public.go index 7f9d8ab..d7f7720 100644 --- a/public/public.go +++ b/public/public.go @@ -47,7 +47,7 @@ func New(appid, secret string, autoManageToken bool) (p *SDK, err error) { if err = p.getAccessToken(); err != nil { return nil, err } - go p.goAutoRefreshAccessToken() + go p.goAutoRefreshAccessTokenJob() } return }