diff --git a/weixin/base.go b/weixin/base.go index 2b561cc..17f4092 100644 --- a/weixin/base.go +++ b/weixin/base.go @@ -2,11 +2,15 @@ package weixin import ( "fmt" - "github.com/tidwall/gjson" "io" "net/http" "sync" "time" + + cache2 "git.u8t.cn/open/gosdk/cache" + "github.com/go-redis/redis" + uuid2 "github.com/google/uuid" + "github.com/tidwall/gjson" ) const ( @@ -87,6 +91,65 @@ func (o *BaseSdk) getAccessToken() (string, error) { return o.accessToken, nil } +// redis share version +func (o *BaseSdk) getAccessToken2() (string, error) { + redix := o.getRedis() + tokenKey := fmt.Sprintf("weixin_oa_access_token_v2_%s", o.appid) + lockKey := fmt.Sprintf("get_access_token_unique_%s", o.appid) + uuid := uuid2.New().String() + + // get token + token := redix.Get(tokenKey).Val() + if token != "" { + return token, nil + } + + // setEX and expire + // if not set key. wait 200ms and get again and retry 5 times + if ok := redix.SetNX(lockKey, uuid, 10*time.Second).Val(); !ok { + for i := range 5 { + time.Sleep(time.Duration(200*(i+2)) * time.Millisecond) + if token = redix.Get(tokenKey).Val(); token != "" { + return token, nil + } + } + return "", fmt.Errorf(" wait 5 times,but still not get token") + } + + defer func() { + if redix.Get(lockKey).Val() == uuid { + redix.Del(lockKey) + } + }() + + // get new token + url := fmt.Sprintf("%s?grant_type=client_credential&appid=%s&secret=%s", accessTokenUrl, o.appid, o.secret) + res, err := http.Get(url) + if err != nil { + return "", err + } + defer res.Body.Close() + body, err := io.ReadAll(res.Body) + if err != nil { + return "", err + } + g := gjson.ParseBytes(body) + + token = g.Get("access_token").String() + if token == "" { + return "", fmt.Errorf("%d:%s", g.Get("errcode").Int(), g.Get("errmsg").String()) + } + + expire := g.Get("expires_in").Int() - 10 + redix.Set(tokenKey, token, time.Second*time.Duration(expire)) + + return token, nil +} + +func (o *BaseSdk) getRedis() *redis.Client { + return cache2.GetRedis() +} + func (o *BaseSdk) getUserInfoByCode(code string, auth bool) (*UserInfo, error) { url := fmt.Sprintf("%s?appid=%s&secret=%s&code=%s&grant_type=authorization_code", code2AccessTokenUrl, o.appid, o.secret, code)