Selaa lähdekoodia

PageOAuthHandler

yaotian 8 vuotta sitten
vanhempi
commit
e9178e79cb
5 muutettua tiedostoa jossa 52 lisäystä ja 21 poistoa
  1. 23 0
      examples/beego/beego.go
  2. 8 0
      mp.go
  3. 1 0
      mp/bridge/msg_handler.go
  4. 18 19
      mp/bridge/page_auth_handler.go
  5. 2 2
      mp/oauth/oauth.go

+ 23 - 0
examples/beego/beego.go

@@ -10,6 +10,8 @@ import (
 	"github.com/yaotian/gowechat/wxcontext"
 )
 
+var appURL = "http://localhost:8001"
+
 func hello(ctx *context.Context) {
 	//配置微信参数
 	config := wxcontext.Config{
@@ -27,6 +29,7 @@ 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 {
 
@@ -43,7 +46,27 @@ func hello(ctx *context.Context) {
 	}
 }
 
+//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.Handle()
+}
+
 func main() {
 	beego.Any("/", hello)
+	beego.Any("/oauth", wxOAuth)
 	beego.Run(":8001")
 }

+ 8 - 0
mp.go

@@ -62,6 +62,14 @@ func (c *MpMgr) GetMsgHandler(req *http.Request, writer http.ResponseWriter) *br
 	return bridge.NewMsgHandler(c.Context)
 }
 
+//GetPageOAuthHandler 网页授权
+func (c *MpMgr) GetPageOAuthHandler(req *http.Request, writer http.ResponseWriter, oAuthCallbackURL string) *bridge.PageOAuthHandler {
+	c.Context.Request = req
+	c.Context.Writer = writer
+	handler := bridge.NewPageOAuthHandler(c.Context, oAuthCallbackURL)
+	return handler
+}
+
 // GetQrcode 带参数的二维码
 func (c *MpMgr) GetQrcode() *account.Qrcode {
 	return account.NewQrcode(c.Context)

+ 1 - 0
mp/bridge/msg_handler.go

@@ -35,6 +35,7 @@ type MsgHandler struct {
 //NewMsgHandler init
 func NewMsgHandler(context *wxcontext.Context) *MsgHandler {
 	srv := new(MsgHandler)
+	fmt.Println("NewMsgHandler:", srv)
 	srv.Context = context
 	return srv
 }

+ 18 - 19
mp/bridge/page_auth_handler.go

@@ -11,11 +11,11 @@ import (
 
 //PageOAuthHandler 微信网页授权
 type PageOAuthHandler struct {
-	*wxcontext.Context
-	oauth *oauth.Oauth
+	// *wxcontext.Context
+	*oauth.Oauth
 
-	oAuthURL     string
-	urlNeedOAuth string
+	oAuthCallbackURL string
+	urlNeedOAuth     string
 
 	openIDExisting          bool
 	checkOpenIDExistingFunc func(openID string) bool
@@ -25,45 +25,44 @@ type PageOAuthHandler struct {
 }
 
 //NewPageOAuthHandler PageOAuthHandler初始化
-func NewPageOAuthHandler(context *wxcontext.Context, oAuthURL string) *PageOAuthHandler {
-	srv := new(PageOAuthHandler)
-	srv.Context = context
-	srv.oauth = new(oauth.Oauth)
-	srv.oAuthURL = oAuthURL
-	return srv
+func NewPageOAuthHandler(context *wxcontext.Context, oAuthCallbackURL string) *PageOAuthHandler {
+	pa := new(PageOAuthHandler)
+	pa.Oauth = oauth.NewOauth(context)
+	pa.oAuthCallbackURL = oAuthCallbackURL
+	return pa
 }
 
 func (c *PageOAuthHandler) getCallbackURL() (u string) {
-	return fmt.Sprintf("%s?target=%s", c.oAuthURL, url.QueryEscape(c.urlNeedOAuth))
+	return fmt.Sprintf("%s?target=%s", c.oAuthCallbackURL, url.QueryEscape(c.urlNeedOAuth))
 }
 
-//Handler handler
-func (c *PageOAuthHandler) Handler() (err error) {
+//Handle handler
+func (c *PageOAuthHandler) Handle() (err error) {
 	code := c.Query("code")
 	state := c.Query("state")
 	c.urlNeedOAuth = c.Query("target")
 	if code != "" {
 		var acsTkn oauth.ResAccessToken
-		acsTkn, err = c.oauth.GetUserAccessToken(code)
+		acsTkn, err = c.GetUserAccessToken(code)
 		if err != nil {
 			return
 		}
 		openID := acsTkn.OpenID
 		if c.checkOpenIDExistingFunc(openID) { //系统中已经存在openID
-			http.Redirect(c.Writer, nil, c.urlNeedOAuth, 302)
+			http.Redirect(c.Writer, c.Request, c.urlNeedOAuth, 302)
 			return
 		}
 		if state == "base" {
-			c.oauth.Redirect(c.Writer, c.getCallbackURL(), "snsapi_userinfo", "userinfo")
+			c.Redirect(c.getCallbackURL(), "snsapi_userinfo", "userinfo")
 			return
 		}
-		c.UserInfo, err = c.oauth.GetUserInfo(acsTkn.AccessToken, openID)
+		c.UserInfo, err = c.GetUserInfo(acsTkn.AccessToken, openID)
 		if err == nil {
 			c.afterGetUserInfoFunc(c.UserInfo)
-			http.Redirect(c.Writer, nil, c.urlNeedOAuth, 302)
+			http.Redirect(c.Writer, c.Request, c.urlNeedOAuth, 302)
 			return
 		}
 	}
-	c.oauth.Redirect(c.Writer, c.getCallbackURL(), "snsapi_base", "base")
+	c.Redirect(c.getCallbackURL(), "snsapi_base", "base")
 	return
 }

+ 2 - 2
mp/oauth/oauth.go

@@ -38,10 +38,10 @@ func (oauth *Oauth) GetRedirectURL(redirectURI, scope, state string) string {
 }
 
 //Redirect 跳转到网页授权
-func (oauth *Oauth) Redirect(writer http.ResponseWriter, redirectURI, scope, state string) error {
+func (oauth *Oauth) Redirect(redirectURI, scope, state string) error {
 	location := oauth.GetRedirectURL(redirectURI, scope, state)
 	//location 为完整地址,所以不需要request
-	http.Redirect(writer, nil, location, 302)
+	http.Redirect(oauth.Writer, oauth.Request, location, 302)
 	return nil
 }