Skip to content

Commit f8e9d24

Browse files
committed
默认cache没生效fix
1 parent bda3a81 commit f8e9d24

File tree

4 files changed

+44
-26
lines changed

4 files changed

+44
-26
lines changed

mp/bridge/page_auth_handler.go

+19-9
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ type PageOAuthHandler struct {
2020

2121
openID string
2222
openIDExisting bool
23-
checkOpenIDExistingFunc func(openID string) bool
23+
checkOpenIDExistingFunc func(openID string) (existing bool, stopNow bool)
2424

2525
user.Info
26-
afterGetUserInfoFunc func(user user.Info) bool
26+
afterGetUserInfoFunc func(user user.Info) (stopNow bool)
2727
}
2828

2929
//NewPageOAuthHandler PageOAuthHandler初始化
@@ -50,7 +50,7 @@ handler:
5050
}
5151
5252
*/
53-
func (c *PageOAuthHandler) SetFuncCheckOpenIDExisting(handler func(string) bool) {
53+
func (c *PageOAuthHandler) SetFuncCheckOpenIDExisting(handler func(string) (existing bool, stopNow bool)) {
5454
c.checkOpenIDExistingFunc = handler
5555
}
5656

@@ -82,18 +82,28 @@ func (c *PageOAuthHandler) Handle() (err error) {
8282
return
8383
}
8484
openID := acsTkn.OpenID
85-
if c.checkOpenIDExistingFunc(openID) { //系统中已经存在openID
85+
existing, stopNow := c.checkOpenIDExistingFunc(openID)
86+
if stopNow {
87+
return
88+
}
89+
if existing {
8690
http.Redirect(c.Writer, c.Request, c.urlNeedOAuth, 302)
8791
return
8892
}
8993
//用 user模块的,没用oauth模板,可以获得更多信息
9094
u, err := user.NewUser(c.Oauth.Context).GetUserInfo(openID)
91-
if err == nil {
92-
if !c.afterGetUserInfoFunc(*u) {
93-
http.Redirect(c.Writer, c.Request, c.urlNeedOAuth, 302)
94-
}
95+
if err != nil {
96+
return err
97+
}
98+
stopNow = c.afterGetUserInfoFunc(*u)
99+
if stopNow {
100+
return nil
95101
}
102+
http.Redirect(c.Writer, c.Request, c.urlNeedOAuth, 302)
103+
return nil
104+
} else {
105+
//code为空时
106+
c.Redirect(c.getCallbackURL(), "snsapi_base", "base")
96107
}
97-
c.Redirect(c.getCallbackURL(), "snsapi_base", "base")
98108
return
99109
}

mp_test.go

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package gowechat
2+
3+
import (
4+
"testing"
5+
6+
"github.com/astaxie/beego"
7+
"github.com/yaotian/gowechat/wxcontext"
8+
)
9+
10+
func TestGetQrcode(t *testing.T) {
11+
config := wxcontext.Config{
12+
AppID: "wx88c493c9a9f67ea6",
13+
AppSecret: "0c1fe2db856c60d9c52b65383feadae1",
14+
Token: "zyt2864",
15+
}
16+
wc := NewWechat(config)
17+
beego.Debug("wechat's cache:", wc.Context.Cache)
18+
mp, _ := wc.MpMgr()
19+
mp.GetQrcode().CreatePermanentQRCodeWithSceneString("test")
20+
}

wechat.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ type Wechat struct {
2121
// NewWechat init
2222
func NewWechat(cfg wxcontext.Config) *Wechat {
2323
context := new(wxcontext.Context)
24-
initContext(cfg, context)
24+
initContext(&cfg, context)
2525
return &Wechat{context}
2626
}
2727

28-
func initContext(cfg wxcontext.Config, context *wxcontext.Context) {
28+
func initContext(cfg *wxcontext.Config, context *wxcontext.Context) {
2929
if cfg.Cache == nil {
3030
if MemCache == nil {
3131
MemCache, _ = cache.NewCache("memory", `{"interval":60}`)
@@ -98,10 +98,10 @@ func (wc *Wechat) checkCfgMch() (err error) {
9898
if wc.Context.MchAPIKey == "" {
9999
return fmt.Errorf("%s", "配置中没有MchAPIKey")
100100
}
101-
if wc.Context.SslCertFilePath == "" || wc.Context.SslCertContent == "" {
101+
if wc.Context.SslCertFilePath == "" && wc.Context.SslCertContent == "" {
102102
return fmt.Errorf("%s", "配置中没有SslCert")
103103
}
104-
if wc.Context.SslKeyFilePath == "" || wc.Context.SslKeyContent == "" {
104+
if wc.Context.SslKeyFilePath == "" && wc.Context.SslKeyContent == "" {
105105
return fmt.Errorf("%s", "配置中没有SslKey")
106106
}
107107
return

wxcontext/context.go

+1-13
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,11 @@ package wxcontext
33
import (
44
"net/http"
55
"sync"
6-
7-
"github.com/astaxie/beego/cache"
86
)
97

108
// Context struct
119
type Context struct {
12-
Config
13-
// AppID string
14-
// AppSecret string
15-
// Token string
16-
// EncodingAESKey string
17-
18-
Cache cache.Cache
10+
*Config
1911

2012
Writer http.ResponseWriter
2113
Request *http.Request
@@ -28,10 +20,6 @@ type Context struct {
2820

2921
HTTPClient *http.Client
3022
SHTTPClient *http.Client //SSL client
31-
32-
// //商户平台APIKey
33-
// MchAPIKey string
34-
// MchID string
3523
}
3624

3725
// Query returns the keyed url query value if it exists

0 commit comments

Comments
 (0)