ソースを参照

MemCache in wechat

yaotian 8 年 前
コミット
6308ab7b92
4 ファイル変更22 行追加5 行削除
  1. 1 1
      mch.go
  2. 1 1
      mp.go
  3. 11 0
      mp/user/user.go
  4. 9 3
      wechat.go

+ 1 - 1
mch.go

@@ -7,7 +7,7 @@ import (
 
 //MchMgr mch mgt
 type MchMgr struct {
-	Wechat
+	*Wechat
 }
 
 // GetPay 基本支付api

+ 1 - 1
mp.go

@@ -17,7 +17,7 @@ import (
 
 //MpMgr mp mgr
 type MpMgr struct {
-	Wechat
+	*Wechat
 }
 
 //GetAccessToken 获取access_token

+ 11 - 0
mp/user/user.go

@@ -57,3 +57,14 @@ func (user *User) GetUserInfo(openID string) (userInfo *Info, err error) {
 	err = json.Unmarshal(response, userInfo)
 	return
 }
+
+//IsSubscribed 是否已经关注公众号
+func (user *User) IsSubscribed(openID string) (subscribed bool, err error) {
+	var userInfo *Info
+	userInfo, err = user.GetUserInfo(openID)
+	if err != nil {
+		return
+	}
+	subscribed = userInfo.Subscribe == 1
+	return
+}

+ 9 - 3
wechat.go

@@ -10,6 +10,9 @@ import (
 	"github.com/yaotian/gowechat/wxcontext"
 )
 
+//MemCache if wxcontext.Config no cache, this will give a default memory cache.
+var MemCache cache.Cache
+
 // Wechat struct
 type Wechat struct {
 	Context *wxcontext.Context
@@ -24,7 +27,10 @@ func NewWechat(cfg wxcontext.Config) *Wechat {
 
 func initContext(cfg wxcontext.Config, context *wxcontext.Context) {
 	if cfg.Cache == nil {
-		cfg.Cache, _ = cache.NewCache("memory", `{"interval":60}`)
+		if MemCache == nil {
+			MemCache, _ = cache.NewCache("memory", `{"interval":60}`)
+		}
+		cfg.Cache = MemCache
 	}
 	context.Config = cfg
 
@@ -52,7 +58,7 @@ func (wc *Wechat) MchMgr() (mch *MchMgr, err error) {
 		return
 	}
 	mch = new(MchMgr)
-	mch.Wechat = *wc
+	mch.Wechat = wc
 	return
 }
 
@@ -63,7 +69,7 @@ func (wc *Wechat) MpMgr() (mp *MpMgr, err error) {
 		return
 	}
 	mp = new(MpMgr)
-	mp.Wechat = *wc
+	mp.Wechat = wc
 	return
 }