Pārlūkot izejas kodu

update wechat mp & mini use cache token default

ls 2 mēneši atpakaļ
vecāks
revīzija
9712a19ec9
3 mainītis faili ar 19 papildinājumiem un 22 dzēšanām
  1. 1 1
      wechat/client.go
  2. 17 21
      wechat/mini.go
  3. 1 0
      wechat/structure.go

+ 1 - 1
wechat/client.go

@@ -30,7 +30,7 @@ func NewClient(appID, appSecret, token, encodingAESKey string) *Client {
 	if v, ok := memcache.Load(key); ok {
 		return v.(*Client)
 	}
-	c := &Client{AppID: appID, AppSecret: appSecret, Token: token, EncodingAESKey: encodingAESKey}
+	c := &Client{AppID: appID, AppSecret: appSecret, Token: token, EncodingAESKey: encodingAESKey, UseCacheToken: true}
 	memcache.Store(key, c)
 	return c
 }

+ 17 - 21
wechat/mini.go

@@ -24,7 +24,7 @@ func NewMiniClient(appID, appSecret string) *MiniClient {
 	if v, ok := memcache.Load(key); ok {
 		return v.(*MiniClient)
 	}
-	c := &MiniClient{AppID: appID, AppSecret: appSecret}
+	c := &MiniClient{AppID: appID, AppSecret: appSecret, UseCacheToken: true}
 	memcache.Store(key, c)
 	return c
 }
@@ -56,34 +56,30 @@ func (mc *MiniClient) GetMiniOpenID(frm FormCode) (s MiniSession, err error) {
 
 // getToken get token
 func (mc *MiniClient) getToken() (token string, err error) {
-	/*
-		now := time.Now().Unix()
-		if mc.LastTokenTime > 0 {
-			if now-mc.LastTokenTime < TokenExpires {
-				token = mc.AccessToken
-				return
-			}
+	if mc.UseCacheToken {
+		key := keyMiniToken(mc.AppID)
+		var ct ClientToken
+		s := ``
+		//now := time.Now().Unix()
+		s, err = cache.Get(key)
+		if err != nil {
+			return
 		}
-		// */
-	key := keyMiniToken(mc.AppID)
-	var ct ClientToken
-	s := ``
-	now := time.Now().Unix()
-	s, err = cache.Get(key)
-	if err == nil {
 		err = json.Unmarshal([]byte(s), &ct)
 		if err != nil {
 			return
 		}
+		token = mc.AccessToken
+		return
+	}
 
-		if mc.LastTokenTime > 0 {
-			if now-mc.LastTokenTime < TokenExpires {
-				token = mc.AccessToken
-				return
-			}
+	now := time.Now().Unix()
+	if mc.LastTokenTime > 0 {
+		if now-mc.LastTokenTime < TokenExpires {
+			token = mc.AccessToken
+			return
 		}
 	}
-
 	token, err = mc.UpdateToken()
 	return
 }

+ 1 - 0
wechat/structure.go

@@ -41,6 +41,7 @@ type MiniClient struct {
 	AppID     string `json:"appid"`
 	AppSecret string `json:"appsecret"`
 
+	UseCacheToken bool
 	AccessToken   string
 	LastTokenTime int64
 }