feat: add access token save to redis
This commit is contained in:
parent
6bdfa4b438
commit
7f0d4976eb
|
|
@ -2,11 +2,15 @@ package weixin
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/tidwall/gjson"
|
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
cache2 "git.u8t.cn/open/gosdk/cache"
|
||||||
|
"github.com/go-redis/redis"
|
||||||
|
uuid2 "github.com/google/uuid"
|
||||||
|
"github.com/tidwall/gjson"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
|
@ -87,6 +91,65 @@ func (o *BaseSdk) getAccessToken() (string, error) {
|
||||||
return o.accessToken, nil
|
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("<get access_token v2> 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) {
|
func (o *BaseSdk) getUserInfoByCode(code string, auth bool) (*UserInfo, error) {
|
||||||
url := fmt.Sprintf("%s?appid=%s&secret=%s&code=%s&grant_type=authorization_code",
|
url := fmt.Sprintf("%s?appid=%s&secret=%s&code=%s&grant_type=authorization_code",
|
||||||
code2AccessTokenUrl, o.appid, o.secret, code)
|
code2AccessTokenUrl, o.appid, o.secret, code)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue