ls 3 mesiacov pred
rodič
commit
f05e4a6ade
1 zmenil súbory, kde vykonal 24 pridanie a 9 odobranie
  1. 24 9
      wechat/mini.go

+ 24 - 9
wechat/mini.go

@@ -29,6 +29,11 @@ func NewMiniClient(appID, appSecret string) *MiniClient {
 	return c
 }
 
+// TokenKey token key in cache
+func (mc *MiniClient) TokenKey() string {
+	return keyMiniToken(mc.AppID)
+}
+
 // GetMiniOpenID get openid by code
 func (mc *MiniClient) GetMiniOpenID(frm FormCode) (s MiniSession, err error) {
 	uri := BaseURL + "/sns/jscode2session?"
@@ -79,6 +84,12 @@ func (mc *MiniClient) getToken() (token string, err error) {
 		}
 	}
 
+	token, err = mc.UpdateToken()
+	return
+}
+
+func (mc *MiniClient) UpdateToken() (token string, err error) {
+	now := time.Now().Unix()
 	uri := BaseURL + "/cgi-bin/token?"
 
 	args := url.Values{}
@@ -88,16 +99,20 @@ func (mc *MiniClient) getToken() (token string, err error) {
 
 	uri += args.Encode()
 	var jq *myth.JSONQuery
-	if jq, err = getJSON(uri); err == nil {
-		mc.LastTokenTime = now
-		mc.AccessToken, err = jq.String(`access_token`)
-		token = mc.AccessToken
-		k := keyMini(mc.AppID)
-		memcache.Store(k, mc)
-		ct := ClientToken{AppID: mc.AppID, AccessToken: mc.AccessToken, LastTokenTime: now}
-		bs, _ := json.Marshal(ct)
-		cache.Set(key, string(bs), 0)
+	jq, err = getJSON(uri)
+	if err != nil {
+		return
 	}
+
+	key := keyMiniToken(mc.AppID)
+	mc.LastTokenTime = now
+	mc.AccessToken, err = jq.String(`access_token`)
+	token = mc.AccessToken
+	k := keyMini(mc.AppID)
+	memcache.Store(k, mc)
+	ct := ClientToken{AppID: mc.AppID, AccessToken: mc.AccessToken, LastTokenTime: now}
+	bs, _ := json.Marshal(ct)
+	cache.Set(key, string(bs), 0)
 	return
 }