Skip to content

Commit

Permalink
simplify redirect-url
Browse files Browse the repository at this point in the history
  • Loading branch information
rosbit committed Mar 11, 2020
1 parent 25cfdf1 commit 0195302
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 116 deletions.
47 changes: 11 additions & 36 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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": "如果有其它的公众号服务,可以参考上面的信息配置",
Expand Down Expand Up @@ -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的值,用于区分菜单项",
Expand All @@ -219,10 +194,10 @@
}
```

所有的HTTP请求头、Cookie都会转发给`menu-redirect-url`,它可以根据需求进行处理
所有的HTTP请求头、Cookie都会转发给`redirect-url`,它可以根据需求进行处理

- 响应结果消息格式
- 响应结果完全有`menu-redirect-url`自主决定,包括
- 响应结果完全有`redirect-url`自主决定,包括
- 设置响应头、设置Cookie
- 或者跳转到另外的URL
- 响应内容会直接输出到公众号浏览器
10 changes: 5 additions & 5 deletions conf/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
10 changes: 2 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
49 changes: 0 additions & 49 deletions handlers/wx-menu-redirector.go

This file was deleted.

12 changes: 2 additions & 10 deletions router.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down
14 changes: 7 additions & 7 deletions sample.conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 0195302

Please sign in to comment.