Skip to content

Commit 6308ab7

Browse files
committed
MemCache in wechat
1 parent 724c497 commit 6308ab7

File tree

4 files changed

+22
-5
lines changed

4 files changed

+22
-5
lines changed

mch.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77

88
//MchMgr mch mgt
99
type MchMgr struct {
10-
Wechat
10+
*Wechat
1111
}
1212

1313
// GetPay 基本支付api

mp.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717

1818
//MpMgr mp mgr
1919
type MpMgr struct {
20-
Wechat
20+
*Wechat
2121
}
2222

2323
//GetAccessToken 获取access_token

mp/user/user.go

+11
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,14 @@ func (user *User) GetUserInfo(openID string) (userInfo *Info, err error) {
5757
err = json.Unmarshal(response, userInfo)
5858
return
5959
}
60+
61+
//IsSubscribed 是否已经关注公众号
62+
func (user *User) IsSubscribed(openID string) (subscribed bool, err error) {
63+
var userInfo *Info
64+
userInfo, err = user.GetUserInfo(openID)
65+
if err != nil {
66+
return
67+
}
68+
subscribed = userInfo.Subscribe == 1
69+
return
70+
}

wechat.go

+9-3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ import (
1010
"github.com/yaotian/gowechat/wxcontext"
1111
)
1212

13+
//MemCache if wxcontext.Config no cache, this will give a default memory cache.
14+
var MemCache cache.Cache
15+
1316
// Wechat struct
1417
type Wechat struct {
1518
Context *wxcontext.Context
@@ -24,7 +27,10 @@ func NewWechat(cfg wxcontext.Config) *Wechat {
2427

2528
func initContext(cfg wxcontext.Config, context *wxcontext.Context) {
2629
if cfg.Cache == nil {
27-
cfg.Cache, _ = cache.NewCache("memory", `{"interval":60}`)
30+
if MemCache == nil {
31+
MemCache, _ = cache.NewCache("memory", `{"interval":60}`)
32+
}
33+
cfg.Cache = MemCache
2834
}
2935
context.Config = cfg
3036

@@ -52,7 +58,7 @@ func (wc *Wechat) MchMgr() (mch *MchMgr, err error) {
5258
return
5359
}
5460
mch = new(MchMgr)
55-
mch.Wechat = *wc
61+
mch.Wechat = wc
5662
return
5763
}
5864

@@ -63,7 +69,7 @@ func (wc *Wechat) MpMgr() (mp *MpMgr, err error) {
6369
return
6470
}
6571
mp = new(MpMgr)
66-
mp.Wechat = *wc
72+
mp.Wechat = wc
6773
return
6874
}
6975

0 commit comments

Comments
 (0)