feat: analysis gen qrcode
This commit is contained in:
@@ -1,8 +1,13 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/md5"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"lone-services/pkg/redis"
|
||||
"lone-services/pkg/utils"
|
||||
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
@@ -17,10 +22,21 @@ type AccessTokenResult struct {
|
||||
ErrMsg string `json:"errmsg"`
|
||||
}
|
||||
|
||||
func (c *Client) AccessToken() (*AccessTokenResult, error) {
|
||||
func (c *Client) AccessToken(ctx context.Context) (*AccessTokenResult, error) {
|
||||
if c == nil || c.AppID == utils.StringEmpty || c.Secret == utils.StringEmpty {
|
||||
return nil, fmt.Errorf("wechat app config missing")
|
||||
}
|
||||
if ctx == nil {
|
||||
ctx = context.Background()
|
||||
}
|
||||
|
||||
secretMd5 := md5.Sum([]byte(c.Secret))
|
||||
key := fmt.Sprintf("wechat:access_token:%s:%s", c.AppID, hex.EncodeToString(secretMd5[:])[:8])
|
||||
|
||||
token, err := redis.Client.Get(ctx, key).Result()
|
||||
if err == nil && token != utils.StringEmpty {
|
||||
return &AccessTokenResult{AccessToken: token}, nil
|
||||
}
|
||||
|
||||
body, err := utils.Curl(accessTokenURL).SetData(map[string]string{
|
||||
"grant_type": "client_credential",
|
||||
@@ -41,5 +57,10 @@ func (c *Client) AccessToken() (*AccessTokenResult, error) {
|
||||
if result.AccessToken == utils.StringEmpty {
|
||||
return nil, fmt.Errorf("wechat access_token empty")
|
||||
}
|
||||
|
||||
expire := time.Duration(result.ExpiresIn-300) * time.Second
|
||||
if expire > 0 {
|
||||
_ = redis.Client.Set(ctx, key, result.AccessToken, expire).Err()
|
||||
}
|
||||
return &result, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user