//在WxMenuController类中,生成菜单的方法,原本自带的
ServletRequestAttributes servletRequestAttributes =
(ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
if (servletRequestAttributes != null) {
HttpServletRequest request = servletRequestAttributes.getRequest();
URL requestURL = new URL(request.getRequestURL().toString());
String url = this.wxService.switchoverTo(appid).getOAuth2Service().buildAuthorizationUrl(
String.format("%s://%s/wx/redirect/%s/greet", requestURL.getProtocol(), requestURL.getHost(), appid),
WxConsts.OAuth2Scope.SNSAPI_USERINFO, null);
button34.setUrl(url);
}
//修改完成后的
WxMenu menu = new WxMenu();
WxMenuButton button1 = new WxMenuButton();
button1.setType(MenuButtonType.VIEW);
button1.setName("登录");
ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
if (servletRequestAttributes != null) {
String url = this.wxService.switchoverTo(appid).getOAuth2Service().buildAuthorizationUrl(
String.format(RuoYiConfig.getWxWebUrl() , RuoYiConfig.getPathMapping() , "/wx/portal/" , properties.getConfigs().get(0).getAppId() , "/wx_login"),
WxConsts.OAuth2Scope.SNSAPI_USERINFO, null);
button1.setUrl(url);
}
menu.getButtons().add(button1);
url路径中的String.format里面写的是需要跳转的三方链接,这个链接还需要在公众号中配置,否则应该是打不开的
这个是微信官方文档中的,Oauth2.0授权登录流程
1.用户点击三方链接,后台拿到链接后调用微信的OAuth2.0授权登录接口
2.用户同意授权,微信重定向页面到三方链接上,并带上授权临时票据code
3.通过code和appid可以换取access_token,从而获取用户的信息