From 01953022842fc855a7006c2d79ae29efa5571de3 Mon Sep 17 00:00:00 2001 From: Rosbit Xu Date: Wed, 11 Mar 2020 13:48:33 +0800 Subject: [PATCH] simplify redirect-url --- README.md | 47 ++++++++------------------------ conf/conf.go | 10 +++---- go.mod | 2 +- go.sum | 10 ++----- handlers/wx-menu-redirector.go | 49 ---------------------------------- router.go | 12 ++------- sample.conf.json | 14 +++++----- 7 files changed, 28 insertions(+), 116 deletions(-) delete mode 100644 handlers/wx-menu-redirector.go diff --git a/README.md b/README.md index d8be363..30eea55 100644 --- a/README.md +++ b/README.md @@ -55,8 +55,8 @@ "redirect-path": "/redirect --这个是微信网页授权用到的,设置菜单时都用这个路径" }, "msg-proxy-pass": "http://yourhost.or.ip.here --这个地址指向消息/事件处理的服务,如果不处理可以为空", - "menu-redirect-url": "http://yourhost.or.ip/path/to/redirect --完全转发http请求,响应将返回微信服务号,如果不为空,将忽略下面的menu-handler配置", - "menu-handler": "http://yourhost.or.ip/handler/path --deprecated,只有在menu-redirect-url为空时有效,这个地址指向菜单处理的服务,如果不处理可以为空" + "redirect-url": "http://yourhost.or.ip/path/to/redirect --完全转发http请求,响应将返回微信服务号,如果不为空,将忽略下面的menu-handler配置", + "redirect-userinfo-flag": "如果通过 snsapi_userinfo 获取参数,在redrect-url中加上特殊字符串参数,用于区分,比如 login。如果为空,使用 snsapi_base 方式获取用户参数" }, { "name": "如果有其它的公众号服务,可以参考上面的信息配置", @@ -161,45 +161,20 @@ } ``` - "type"可以是"voice"、"video"、"image"等,"msg"则是它们对应的"mediaId" - 3. ~~与`menu-handler`的通讯~~(deprecated, 请参考第4部分) - - `menu-handler`配置的是一个URL,比如 - - `menu-handler`配置的是`http://wx.myhost.com/menu/redirect` - - 当`wx-gateway`接收到菜单请求时,则会把消息转发给上面的URL - - HTTP请求的方法都是`POST`,请求/响应结果都是`JSON` - - 为了让`wx-gateway`收到菜单点击事件,要按下面的格式设置菜单触发的URL: - - `https://open.weixin.qq.com/connect/oauth2/authorize?appid=在这里填公众号的AppId&redirect_uri=http%3A//wx.myhost.com/这是redirect-path配置的值&response_type=code&scope=snsapi_base&state=这个值用于区分菜单项#wechat_redirect` - - 只有配置正确`wx-gateway`才能收到菜单事件,并通过code获取到点击菜单的用户的openId,并转发给`menu-handler` - - 请求`menu-handler`的请求消息格式 - ```json - { - "appId": "公众号的AppId,如果同时处理多个公众号,可以用来区分来源", - "openId": "点击菜单的用户的openId", - "state": "在菜单配置中的state的值,用于区分菜单项" - } - ``` - - 响应结果消息格式 - ```json - { - "h": { - "如果不为空": "是需要给微信浏览器设置的header信息", - "Set-Cookie": "可以是cookie格式的header信息" - }, - "r": "如果不为空,这里指定需要wx-gateway通过302跳转的URL", - "c": "在r值不为空的情况下,这里的内容会显示在微信浏览器" - } - ``` - 4. 与`menu-redirect-url`的通讯(推荐使用,替换第3部分) - - `menu-redirect-url`配置的是一个URL,比如 - - `menu-redirect-url`配置的是`http://wx.myhost.com/menu/path/to/redirect` + 3. 与`redirect-url`的通讯 + - 处理网页授权请求。公众号的相关配置请参考微信文档。回调URL只需`wx-gateway`所在的域名 + - `redirect-url`配置的是一个URL,比如 + - `redirect-url`配置的是`http://wx.myhost.com/menu/path/to/redirect` - 当`wx-gateway`接收到菜单请求时,则会把消息转发给上面的URL - HTTP请求的方法是`POST`,响应结果完全由转发处理服务决定,它的HTTP响应结果将反映到公众号浏览器 - 为了让`wx-gateway`收到菜单点击事件,要按下面的格式设置菜单触发的URL: - `https://open.weixin.qq.com/connect/oauth2/authorize?appid=在这里填公众号的AppId&redirect_uri=http%3A//wx.myhost.com/这是redirect-path配置的值&response_type=code&scope=snsapi_base&state=这个值用于区分菜单项#wechat_redirect` - - 只有配置正确`wx-gateway`才能收到菜单事件,并通过code获取到点击菜单的用户的openId,并转发给`menu-redirect-url` - - 请求`menu-redirect-url`的请求消息格式 + - 只有配置正确`wx-gateway`才能收到菜单事件,并通过code获取到点击菜单的用户的openId,并转发给`redirect-url` + - 请求`redirect-url`的请求消息格式 ```json { + "requestURI": "转发请求的URI,这是微信服务器访问gateway的URI,可以根据实际情况做进一步判断", "appId": "公众号的AppId,如果同时处理多个公众号,可以用来区分来源", "openId": "点击菜单的用户的openId", "state": "在菜单配置中的state的值,用于区分菜单项", @@ -219,10 +194,10 @@ } ``` - 所有的HTTP请求头、Cookie都会转发给`menu-redirect-url`,它可以根据需求进行处理 + 所有的HTTP请求头、Cookie都会转发给`redirect-url`,它可以根据需求进行处理 - 响应结果消息格式 - - 响应结果完全有`menu-redirect-url`自主决定,包括 + - 响应结果完全有`redirect-url`自主决定,包括 - 设置响应头、设置Cookie - 或者跳转到另外的URL - 响应内容会直接输出到公众号浏览器 diff --git a/conf/conf.go b/conf/conf.go index 2a93221..cee5dfe 100644 --- a/conf/conf.go +++ b/conf/conf.go @@ -24,8 +24,8 @@ "redirect-path": "/redirect" }, "msg-proxy-pass": "http://yourhost.or.ip.here", - "menu-handler": "http://yourhost.or.ip/handler/path", - "menu-redirect-url": "http://yourhost.or.ip/path/to/redirect" + "redirect-url": "http://yourhost.or.ip/path/to/redirect", + "redirect-userinfo-flag": "login, register or any-strings else if you want use snsapi_userinfo", } ], "token-cache-dir": "/root/dir/to/save/token", @@ -67,9 +67,9 @@ type WxServiceConf struct { ServicePath string `json:"service-path"` RedirectPath string `json:"redirect-path"` } `json:"listen-endpoints"` - MsgProxyPass string `json:"msg-proxy-pass"` - MenuHandler string `json:"menu-handler"` - MenuRedirectURL string `json:"menu-redirect-url"` + MsgProxyPass string `json:"msg-proxy-pass"` + RedirectURL string `json:"redirect-url"` + RedirectUserInfoFlag string `json:"redirect-userinfo-flag"` } `json:"services"` TokenCacheDir string `json:"token-cache-dir"` CommonEndpoints struct { diff --git a/go.mod b/go.mod index 443df0d..cdc9f5e 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,6 @@ go 1.14 require ( github.com/gernest/alien v0.1.0 github.com/rosbit/go-wget v1.2.5 - github.com/rosbit/go-wx-api v0.4.1 + github.com/rosbit/go-wx-api v0.4.4 github.com/urfave/negroni v1.0.0 ) diff --git a/go.sum b/go.sum index bce00df..5334b74 100644 --- a/go.sum +++ b/go.sum @@ -4,13 +4,7 @@ github.com/gernest/alien v0.1.0 h1:yjCZyo2a46tXmqfgHLUSwlr23GSn2PqWwAP3jPXfICQ= github.com/gernest/alien v0.1.0/go.mod h1:qtjw+zYKFIL3T2RPYS8TGpMx+ZPwUYFm+BPSd7F+AH0= github.com/rosbit/go-wget v1.2.5 h1:XQFUwTJR8HDwYvxYH8X0cq3BkN1BOMoADc92KKp9Pcs= github.com/rosbit/go-wget v1.2.5/go.mod h1:8Tt92GKM/K9WI65yYkX0lwJDnRp64Vd082ojViWT6T4= -github.com/rosbit/go-wx-api v0.3.1 h1:4lb1bIPyg6eh51IhPN++8ltGrLRB8LSTUStZQDuVFHU= -github.com/rosbit/go-wx-api v0.3.1/go.mod h1:+HyYx6Avz1sPXTN+JBGyW1mf/jToDkXoNZXHX7S2F8I= -github.com/rosbit/go-wx-api v0.3.2 h1:cXvsuxk+sfNItnHZggDNOy/7RbgiwSkbHEdQsUr0XA4= -github.com/rosbit/go-wx-api v0.3.2/go.mod h1:+HyYx6Avz1sPXTN+JBGyW1mf/jToDkXoNZXHX7S2F8I= -github.com/rosbit/go-wx-api v0.4.0 h1:aehGihnMu9QRkOIje+/Leke2ob9qWftdGzj9FrIH6gg= -github.com/rosbit/go-wx-api v0.4.0/go.mod h1:+HyYx6Avz1sPXTN+JBGyW1mf/jToDkXoNZXHX7S2F8I= -github.com/rosbit/go-wx-api v0.4.1 h1:LoEzpk0rlIuOV602kRqyWMMNwyvv8VN8J2ebOAnJGnk= -github.com/rosbit/go-wx-api v0.4.1/go.mod h1:+HyYx6Avz1sPXTN+JBGyW1mf/jToDkXoNZXHX7S2F8I= +github.com/rosbit/go-wx-api v0.4.4 h1:e7W3D+HaVWJjJQRtOCmuHpFq3Syh07UI7G+gY9FCHEo= +github.com/rosbit/go-wx-api v0.4.4/go.mod h1:+HyYx6Avz1sPXTN+JBGyW1mf/jToDkXoNZXHX7S2F8I= github.com/urfave/negroni v1.0.0 h1:kIimOitoypq34K7TG7DUaJ9kq/N4Ofuwi1sjz0KipXc= github.com/urfave/negroni v1.0.0/go.mod h1:Meg73S6kFm/4PpbYdq35yYWoCZ9mS/YSx+lKnmiohz4= diff --git a/handlers/wx-menu-redirector.go b/handlers/wx-menu-redirector.go deleted file mode 100644 index d05a673..0000000 --- a/handlers/wx-menu-redirector.go +++ /dev/null @@ -1,49 +0,0 @@ -/** - * 微信网页授权处理,用于处理服务号菜单点击事件 - * Rosbit Xu - */ -package gwhandlers - -import ( - "github.com/rosbit/go-wx-api/auth" -) - -/** - * @deprecated - * 为了更充分发挥菜单处理的能力,请在配置文件中加上"menu-redirect-url",该配置将完全忽略下面的实现。 - * - * 根据服务号菜单state做跳转 - * @param appId 公众号的appId - * @param openId 订阅用户的openId - * @param state 微信网页授权中的参数,用来标识某个菜单 - * @return - * c 需要显示服务号对话框中的内容 - * h 需要在微信内嵌浏览器中设置的header信息,包括Cookie - * r 需要通过302跳转的URL。如果r不是空串,c的内容被忽略 - * err 如果没有错误返回nil,非nil表示错误 - */ -func CreateMenuRedirector(menuHandler string) wxauth.RedirectHandler { - return func(appId, openId, state string) (c string, h map[string]string, r string, err error) { - res, e := JsonCall(menuHandler, "POST", map[string]string{"appId": appId, "openId": openId, "state": state}) - if e != nil { - err = e - return - } - - if cc, ok := res["c"]; ok { - c = cc.(string) - } - if hh, ok := res["h"]; ok { - h1 := hh.(map[string]interface{}) - h = make(map[string]string, len(h1)) - for k, v := range h1 { - h[k] = v.(string) - } - } - if rr, ok := res["r"]; ok { - r = rr.(string) - } - - return - } -} diff --git a/router.go b/router.go index 4e6d502..8f16d23 100644 --- a/router.go +++ b/router.go @@ -57,16 +57,8 @@ func StartWxGateway() error { wxService.RegisterWxMsghandler(wxmsg.MsgHandler) } - if len(service.MenuRedirectURL) > 0 { - wxService.RegisterRedirectUrl(service.MenuRedirectURL) - } else { - var menuRedirect wxauth.RedirectHandler - if service.MenuHandler != "" { - menuRedirect = gwhandlers.CreateMenuRedirector(service.MenuHandler) - } else { - menuRedirect = wxauth.ToAppIdRedirectHandler(wxauth.HandleRedirect) - } - wxService.RegisterRedictHandler(menuRedirect) + if len(service.RedirectURL) > 0 { + wxService.RegisterRedirectUrl(service.RedirectURL, service.RedirectUserInfoFlag) } } diff --git a/sample.conf.json b/sample.conf.json index ebbd090..2798862 100644 --- a/sample.conf.json +++ b/sample.conf.json @@ -17,15 +17,15 @@ "redirect-path": "/redirect" }, "msg-proxy-pass": "http://yourhost.or.ip.here/empty_is_ok", - "menu-redirect-url": "http://yourhost.io.ip.here/path/to/redirect", - "menu-handler": "http://yourhost.or.ip/handler/path/or_empty_string, deprecated" + "redirect-url": "http://yourhost.io.ip.here/path/to/redirect", + "redirect-userinfo-flag": "login, register or any-strings else if you want use snsapi_userinfo" } ], - "token-cache-dir": "/path/to/cache_dir", - "common-endpoints": { + "token-cache-dir": "/path/to/cache_dir", + "common-endpoints": { "health-check": "/health", "wx-qr": "/qr", - "wx-user": "/user" - }, - "dont-append-userinfo": false + "wx-user": "/user" + }, + "dont-append-userinfo": false }