yaotian 8 лет назад
Родитель
Сommit
f8e9d248f2
4 измененных файлов с 44 добавлено и 26 удалено
  1. 19 9
      mp/bridge/page_auth_handler.go
  2. 20 0
      mp_test.go
  3. 4 4
      wechat.go
  4. 1 13
      wxcontext/context.go

+ 19 - 9
mp/bridge/page_auth_handler.go

@@ -20,10 +20,10 @@ type PageOAuthHandler struct {
 
 	openID                  string
 	openIDExisting          bool
-	checkOpenIDExistingFunc func(openID string) bool
+	checkOpenIDExistingFunc func(openID string) (existing bool, stopNow bool)
 
 	user.Info
-	afterGetUserInfoFunc func(user user.Info) bool
+	afterGetUserInfoFunc func(user user.Info) (stopNow bool)
 }
 
 //NewPageOAuthHandler PageOAuthHandler初始化
@@ -50,7 +50,7 @@ handler:
 	}
 
 */
-func (c *PageOAuthHandler) SetFuncCheckOpenIDExisting(handler func(string) bool) {
+func (c *PageOAuthHandler) SetFuncCheckOpenIDExisting(handler func(string) (existing bool, stopNow bool)) {
 	c.checkOpenIDExistingFunc = handler
 }
 
@@ -82,18 +82,28 @@ func (c *PageOAuthHandler) Handle() (err error) {
 			return
 		}
 		openID := acsTkn.OpenID
-		if c.checkOpenIDExistingFunc(openID) { //系统中已经存在openID
+		existing, stopNow := c.checkOpenIDExistingFunc(openID)
+		if stopNow {
+			return
+		}
+		if existing {
 			http.Redirect(c.Writer, c.Request, c.urlNeedOAuth, 302)
 			return
 		}
 		//用 user模块的,没用oauth模板,可以获得更多信息
 		u, err := user.NewUser(c.Oauth.Context).GetUserInfo(openID)
-		if err == nil {
-			if !c.afterGetUserInfoFunc(*u) {
-				http.Redirect(c.Writer, c.Request, c.urlNeedOAuth, 302)
-			}
+		if err != nil {
+			return err
+		}
+		stopNow = c.afterGetUserInfoFunc(*u)
+		if stopNow {
+			return nil
 		}
+		http.Redirect(c.Writer, c.Request, c.urlNeedOAuth, 302)
+		return nil
+	} else {
+		//code为空时
+		c.Redirect(c.getCallbackURL(), "snsapi_base", "base")
 	}
-	c.Redirect(c.getCallbackURL(), "snsapi_base", "base")
 	return
 }

+ 20 - 0
mp_test.go

@@ -0,0 +1,20 @@
+package gowechat
+
+import (
+	"testing"
+
+	"github.com/astaxie/beego"
+	"github.com/yaotian/gowechat/wxcontext"
+)
+
+func TestGetQrcode(t *testing.T) {
+	config := wxcontext.Config{
+		AppID:     "wx88c493c9a9f67ea6",
+		AppSecret: "0c1fe2db856c60d9c52b65383feadae1",
+		Token:     "zyt2864",
+	}
+	wc := NewWechat(config)
+	beego.Debug("wechat's cache:", wc.Context.Cache)
+	mp, _ := wc.MpMgr()
+	mp.GetQrcode().CreatePermanentQRCodeWithSceneString("test")
+}

+ 4 - 4
wechat.go

@@ -21,11 +21,11 @@ type Wechat struct {
 // NewWechat init
 func NewWechat(cfg wxcontext.Config) *Wechat {
 	context := new(wxcontext.Context)
-	initContext(cfg, context)
+	initContext(&cfg, context)
 	return &Wechat{context}
 }
 
-func initContext(cfg wxcontext.Config, context *wxcontext.Context) {
+func initContext(cfg *wxcontext.Config, context *wxcontext.Context) {
 	if cfg.Cache == nil {
 		if MemCache == nil {
 			MemCache, _ = cache.NewCache("memory", `{"interval":60}`)
@@ -98,10 +98,10 @@ func (wc *Wechat) checkCfgMch() (err error) {
 	if wc.Context.MchAPIKey == "" {
 		return fmt.Errorf("%s", "配置中没有MchAPIKey")
 	}
-	if wc.Context.SslCertFilePath == "" || wc.Context.SslCertContent == "" {
+	if wc.Context.SslCertFilePath == "" && wc.Context.SslCertContent == "" {
 		return fmt.Errorf("%s", "配置中没有SslCert")
 	}
-	if wc.Context.SslKeyFilePath == "" || wc.Context.SslKeyContent == "" {
+	if wc.Context.SslKeyFilePath == "" && wc.Context.SslKeyContent == "" {
 		return fmt.Errorf("%s", "配置中没有SslKey")
 	}
 	return

+ 1 - 13
wxcontext/context.go

@@ -3,19 +3,11 @@ package wxcontext
 import (
 	"net/http"
 	"sync"
-
-	"github.com/astaxie/beego/cache"
 )
 
 // Context struct
 type Context struct {
-	Config
-	// AppID          string
-	// AppSecret      string
-	// Token          string
-	// EncodingAESKey string
-
-	Cache cache.Cache
+	*Config
 
 	Writer  http.ResponseWriter
 	Request *http.Request
@@ -28,10 +20,6 @@ type Context struct {
 
 	HTTPClient  *http.Client
 	SHTTPClient *http.Client //SSL client
-
-	// //商户平台APIKey
-	// MchAPIKey string
-	// MchID     string
 }
 
 // Query returns the keyed url query value if it exists