yaotian %!s(int64=8) %!d(string=hai) anos
pai
achega
f6a64f6acc
Modificáronse 2 ficheiros con 36 adicións e 18 borrados
  1. 25 18
      examples/beego/beego.go
  2. 11 0
      mp/bridge/page_auth_handler.go

+ 25 - 18
examples/beego/beego.go

@@ -7,21 +7,22 @@ import (
 	"github.com/astaxie/beego/context"
 	"github.com/yaotian/gowechat"
 	"github.com/yaotian/gowechat/mp/message"
+	"github.com/yaotian/gowechat/mp/oauth"
 	"github.com/yaotian/gowechat/wxcontext"
 )
 
 var appURL = "http://localhost:8001"
+var config = wxcontext.Config{
+	AppID:          "your app id",
+	AppSecret:      "your app secret",
+	Token:          "your token",
+	EncodingAESKey: "your encoding aes key",
+}
 
 func hello(ctx *context.Context) {
 	//配置微信参数
-	config := wxcontext.Config{
-		AppID:          "your app id",
-		AppSecret:      "your app secret",
-		Token:          "your token",
-		EncodingAESKey: "your encoding aes key",
-	}
 	wc := gowechat.NewWechat(config)
-
+	//微信平台mp
 	mp, err := wc.MpMgr()
 	if err != nil {
 		return
@@ -29,10 +30,9 @@ func hello(ctx *context.Context) {
 
 	// 传入request和responseWriter
 	msgHandler := mp.GetMsgHandler(ctx.Request, ctx.ResponseWriter)
-	fmt.Println("msgHandler:", msgHandler)
+
 	//设置接收消息的处理方法
 	msgHandler.SetHandleMessageFunc(func(msg message.MixMessage) *message.Reply {
-
 		//回复消息:演示回复用户发送的消息
 		text := message.NewText(msg.Content)
 		return &message.Reply{message.MsgTypeText, text}
@@ -42,31 +42,38 @@ func hello(ctx *context.Context) {
 	err = msgHandler.Handle()
 	if err != nil {
 		fmt.Println(err)
-		return
 	}
 }
 
 //wxOAuth 微信公众平台,网页授权
 func wxOAuth(ctx *context.Context) {
 	//配置微信参数
-	config := wxcontext.Config{
-		AppID:          "your app id",
-		AppSecret:      "your app secret",
-		Token:          "your token",
-		EncodingAESKey: "your encoding aes key",
-	}
 	wc := gowechat.NewWechat(config)
-
+	//微信公众平台
 	mp, err := wc.MpMgr()
 	if err != nil {
 		return
 	}
+
 	oauthHandler := mp.GetPageOAuthHandler(ctx.Request, ctx.ResponseWriter, appURL+"/oauth")
+
+	oauthHandler.SetFuncCheckOpenIDExisting(func(openID string) bool {
+		//看自己的系统中是否已经存在此openID的用户
+		//如果已经存在, 调用自己的Login 方法,设置cookie等,return true
+		//如果还不存在,return false, handler会自动去取用户信息
+		return true
+	})
+
+	oauthHandler.SetFuncAfterGetUserInfo(func(user oauth.UserInfo) {
+		//已获得用户信息,这里用信息做注册使用
+		//调用自己的Login方法,设置cookie等
+	})
+
 	oauthHandler.Handle()
 }
 
 func main() {
 	beego.Any("/", hello)
-	beego.Any("/oauth", wxOAuth)
+	beego.Any("/oauth", wxOAuth) //需要网页授权的页面url  /oauth?target=url
 	beego.Run(":8001")
 }

+ 11 - 0
mp/bridge/page_auth_handler.go

@@ -17,6 +17,7 @@ type PageOAuthHandler struct {
 	oAuthCallbackURL string
 	urlNeedOAuth     string
 
+	openID                  string
 	openIDExisting          bool
 	checkOpenIDExistingFunc func(openID string) bool
 
@@ -36,6 +37,16 @@ func (c *PageOAuthHandler) getCallbackURL() (u string) {
 	return fmt.Sprintf("%s?target=%s", c.oAuthCallbackURL, url.QueryEscape(c.urlNeedOAuth))
 }
 
+//SetFuncCheckOpenIDExisting 设置检查OpenID在您的系统中是否已经存在
+func (c *PageOAuthHandler) SetFuncCheckOpenIDExisting(handler func(string) bool) {
+	c.checkOpenIDExistingFunc = handler
+}
+
+//SetFuncAfterGetUserInfo 设置获得用户信息后执行
+func (c *PageOAuthHandler) SetFuncAfterGetUserInfo(handler func(oauth.UserInfo)) {
+	c.afterGetUserInfoFunc = handler
+}
+
 //Handle handler
 func (c *PageOAuthHandler) Handle() (err error) {
 	code := c.Query("code")