测试号
框架:WxJava
Demo:WxJava 的 SpringBoot , http://gitee.com/binary/weixin-java-mp-demo-springboot
测试代码如下:
@AllArgsConstructor
@Controller
@RequestMapping("/wx/redirect/{appid}")
@Slf4j
public class WxRedirectController {
@Autowired
private final WxMpService wxService;
@RequestMapping("/greet")
public String greetUser(@PathVariable String appid, @RequestParam String code, ModelMap map) {
if (!this.wxService.switchover(appid)) {
throw new IllegalArgumentException(String.format("未找到对应appid=[%s]的配置,请核实!", appid));
}
try {
log.info("code = " + code);
WxOAuth2AccessToken accessToken = wxService.getOAuth2Service().getAccessToken(code);
log.info("accessToken = " + accessToken);
WxOAuth2UserInfo user = wxService.getOAuth2Service().getUserInfo(accessToken, null);
map.put("user", user);
} catch (WxErrorException e) {
e.printStackTrace();
}
return "greet_user"
菜单创建代码:
@AllArgsConstructor
@RestController
@RequestMapping("/wx/menu/{appid}")
public class WxMenuController {
private final WxMpService wxService;
/**
* * 自定义菜单创建接口
* 详情请见:https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421141013&token=&lang=zh_CN
* 如果要创建个性化菜单,请设置matchrule属性
* 详情请见:https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1455782296&token=&lang=zh_CN
*
*
* @return 如果是个性化菜单,则返回menuid,否则返回null
*/
@PostMapping("/create")
public String menuCreate(@PathVariable String appid, @RequestBody WxMenu menu) throws WxErrorException {
return this.wxService.switchoverTo(appid).getMenuService().menuCreate(menu);
}
@GetMapping("/create")
public String menuCreateSample(@PathVariable String appid) throws WxErrorException, MalformedURLException {
WxMenu menu = new WxMenu();
WxMenuButton button1 = new WxMenuButton();
button1.setType(MenuButtonType.CLICK);
button1.setName("今日歌曲");
button1.setKey("V1001_TODAY_MUSIC");
// WxMenuButton button2 = new WxMenuButton();
// button2.setType(WxConsts.BUTTON_MINIPROGRAM);
// button2.setName("小程序");
// button2.setAppId("wx286b93c14bbf93aa");
// button2.setPagePath("pages/lunar/index.html");
// button2.setUrl("http://mp.weixin.qq.com");
WxMenuButton button3 = new WxMenuButton();
button3.setName("菜单");
menu.getButtons().add(button1);
// menu.getButtons().add(button2);
menu.getButtons().add(button3);
WxMenuButton button31 = new WxMenuButton();
button31.setType(MenuButtonType.VIEW);
button31.setName("搜索");
button31.setUrl("http://www.soso.com/");
WxMenuButton button32 = new WxMenuButton();
button32.setType(MenuButtonType.VIEW);
button32.setName("视频");
button32.setUrl("http://v.qq.com/");
WxMenuButton button33 = new WxMenuButton();
button33.setType(MenuButtonType.CLICK);
button33.setName("赞一下我们");
button33.setKey("V1001_GOOD");
WxMenuButton button34 = new WxMenuButton();
button34.setType(MenuButtonType.VIEW);
button34.setName("获取用户信息");
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);
}
button3.getSubButtons().add(button31);
button3.getSubButtons().add(button32);
button3.getSubButtons().add(button33);
button3.getSubButtons().add(button34);
this.wxService.switchover(appid);
return this.wxService.getMenuService().menuCreate(menu);
}
手机点击菜单按钮 “ 获取用户信息” ,获取的URL如下:
https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxeb029ffb13db45dc&redirect_uri=http%3A%2F%2Fnerddinner.vaiwan.com%2Fwx%2Fredirect%2Fwxeb029ffb13db45dc%2Fgreet&response_type=code&scope=snsapi_userinfo&state=&connect_redirect=1#wechat_redirect
手机显示页面如下图:
请问这该如何解决?