@Override public WxMpXmlOutMessage handle( WxMpXmlMessage wxMessage, Map<String, Object> context, WxMpService wxMpService, WxSessionManager sessionManager) throws WxErrorException { if (wxMessage.getContent().equals("1")) { String url = wxMpService.oauth2buildAuthorizationUrl( Configue.getBaseUrl() + "/binf/oauth/login", WxConsts.OAUTH2_SCOPE_BASE, null); System.out.println("url:" + url); String aTag = "<a href=\"" + url + "\">认证链接</a>"; return WxMpXmlOutMessage.TEXT() .content(aTag) .fromUser(wxMessage.getToUserName()) .toUser(wxMessage.getFromUserName()) .build(); } else { return WxMpXmlOutMessage.TEXT() .content(Constant.DEF_SUBSCRIBE_TEXT) .fromUser(wxMessage.getToUserName()) .toUser(wxMessage.getFromUserName()) .build(); } }
@RequestMapping("data") public void data(HttpServletRequest request, HttpServletResponse response) { try { WxMpService wxService = BuildAndGetWxService.buildAndGetWxService(); if (request.getSession().getAttribute("openId") == null) { String authorizationUrl = wxService.oauth2buildAuthorizationUrl( "http://estate.zcdreams.com/weixin/oauth2/oauth", WxConsts.OAUTH2_SCOPE_BASE, "6"); LogBuilder.writeToLog("url:" + authorizationUrl); response.sendRedirect(authorizationUrl); } else { response.sendRedirect("/weixin/oauth2/oauth?state=6"); } } catch (Exception e) { e.printStackTrace(); LogBuilder.writeToLog(e.getMessage()); } }
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=utf-8"); response.setStatus(HttpServletResponse.SC_OK); String signature = request.getParameter("signature"); String nonce = request.getParameter("nonce"); String timestamp = request.getParameter("timestamp"); if (!wxMpService.checkSignature(timestamp, nonce, signature)) { // 消息签名不正确,说明不是公众平台发过来的消息 response.getWriter().println("非法请求"); return; } String echostr = request.getParameter("echostr"); if (StringUtils.isNotBlank(echostr)) { // 说明是一个仅仅用来验证的请求,回显echostr response.getWriter().println(echostr); return; } String encryptType = StringUtils.isBlank(request.getParameter("encrypt_type")) ? "raw" : request.getParameter("encrypt_type"); if ("raw".equals(encryptType)) { // 明文传输的消息 WxMpXmlMessage inMessage = WxMpXmlMessage.fromXml(request.getInputStream()); WxMpXmlOutMessage outMessage = wxMpMessageRouter.route(inMessage); if (outMessage != null) { response.getWriter().write(outMessage.toXml()); } return; } if ("aes".equals(encryptType)) { // 是aes加密的消息 String msgSignature = request.getParameter("msg_signature"); WxMpXmlMessage inMessage = WxMpXmlMessage.fromEncryptedXml( request.getInputStream(), wxMpConfigStorage, timestamp, nonce, msgSignature); WxMpXmlOutMessage outMessage = wxMpMessageRouter.route(inMessage); response.getWriter().write(outMessage.toEncryptedXml(wxMpConfigStorage)); return; } response.getWriter().println("不可识别的加密类型"); return; }
/** * 网页授权回调 * * @param request * @param response */ @RequestMapping("oauth") public void oauth(HttpServletRequest request, HttpServletResponse response) { try { WxMpService wxService = BuildAndGetWxService.buildAndGetWxService(); String state = request.getParameter("state"); String openId = null; Owners owner = null; LogBuilder.writeToLog("error in oauth"); if (request.getSession().getAttribute("openId") == null) { WxMpOAuth2AccessToken wxMpOAuth2AccessToken = wxService.oauth2getAccessToken(request.getParameter("code")); openId = wxMpOAuth2AccessToken.getOpenId(); LogBuilder.writeToLog(openId); request.getSession().setAttribute("openId", openId); } else { openId = (String) request.getSession().getAttribute("openId"); LogBuilder.writeToLog("error"); } if (state.equals("1")) { response.sendRedirect("/owner/ownerBind?openId=" + openId); return; } else if (state.equals("2")) { owner = (Owners) request.getSession().getAttribute("owner"); if (owner == null) { owner = ownerService.getOwnerByOpenId(openId); } if (owner != null) { request.getSession().setAttribute("owner", owner); response.sendRedirect("/owner/ownerProfile?openId=" + openId); return; } else { request.getSession().setAttribute("visitor", "visitor"); } response.sendRedirect("/owner/ownerBind?openId=" + openId); return; } else if (state.equals("3")) { owner = (Owners) request.getSession().getAttribute("owner"); if (owner == null) { owner = ownerService.getOwnerByOpenId(openId); } if (owner != null) { request.getSession().setAttribute("owner", owner); } else { request.getSession().setAttribute("visitor", "visitor"); } response.sendRedirect("/custom/index.jsp"); return; } else if (state.equals("4")) { owner = (Owners) request.getSession().getAttribute("owner"); if (owner == null) { owner = ownerService.getOwnerByOpenId(openId); } if (owner != null) { request.getSession().setAttribute("owner", owner); response.sendRedirect("/custom/comment.jsp"); return; } else { request.getSession().setAttribute("visitor", "visitor"); response.sendRedirect("/owner/ownerBind?openId=" + openId); return; } } else if (state.equals("5")) { owner = (Owners) request.getSession().getAttribute("owner"); if (owner == null) { owner = ownerService.getOwnerByOpenId(openId); } if (owner != null) { request.getSession().setAttribute("owner", owner); response.sendRedirect("/custom/repair.jsp"); return; } else { request.getSession().setAttribute("visitor", "visitor"); response.sendRedirect("/owner/ownerBind?openId=" + openId); return; } } else if (state.equals("6")) { owner = (Owners) request.getSession().getAttribute("owner"); if (owner == null) { owner = ownerService.getOwnerByOpenId(openId); } if (owner != null) { request.getSession().setAttribute("owner", owner); response.sendRedirect("/owner/myChargeAndMessage?openId=" + openId); return; } else { request.getSession().setAttribute("visitor", "visitor"); response.sendRedirect("/owner/ownerBind?openId=" + openId); return; } } } catch (Exception e) { e.printStackTrace(); LogBuilder.writeToLog(e.getMessage()); } }
@Test public void testMenuCrete() throws WxErrorException { WxMenu menu = new WxMenu(); WxMenuButton button1 = new WxMenuButton(); button1.setType("click"); button1.setName("菜单A"); button1.setKey("V1001_ABOUT_US"); WxMenuButton button2 = new WxMenuButton(); button2.setType("click"); button2.setName("菜单B"); button2.setKey("V1001_TODAY_SINGER"); WxMenuButton button3 = new WxMenuButton(); button3.setType("click"); button3.setName("菜单C"); button3.setKey("V1001_BAT_WEBSITE"); menu.getButtons().add(button1); menu.getButtons().add(button2); menu.getButtons().add(button3); WxMenuButton button11 = new WxMenuButton(); button11.setType("click"); button11.setName("点击事件"); button11.setKey("V1001_GOOD"); WxMenuButton button12 = new WxMenuButton(); button12.setType("view"); button12.setName("登陆页"); button12.setKey("V1002_GOOD"); button12.setUrl(ContextUtils.query("WEBSITE_URL_PRE") + "/wechat/index"); WxMenuButton button13 = new WxMenuButton(); button13.setType("scancode_push"); button13.setName("扫码推事件"); button13.setKey("V1003_GOOD"); WxMenuButton button14 = new WxMenuButton(); button14.setType("view"); button14.setName("主页"); button14.setKey("V1002_GOOD"); button14.setUrl(ContextUtils.query("WEBSITE_URL_PRE") + "/wechat/index"); button1.getSubButtons().add(button11); button1.getSubButtons().add(button12); button1.getSubButtons().add(button13); button1.getSubButtons().add(button14); WxMenuButton button21 = new WxMenuButton(); button21.setType("scancode_waitmsg"); button21.setName("扫码推事件提示框"); button21.setKey("V2001_GOOD"); WxMenuButton button22 = new WxMenuButton(); button22.setType("pic_sysphoto"); button22.setName("系统拍照发图"); button22.setKey("V2002_GOOD"); WxMenuButton button23 = new WxMenuButton(); button23.setType("pic_photo_or_album"); button23.setName("拍照或者相册发图"); button23.setKey("V2003_GOOD"); button2.getSubButtons().add(button21); button2.getSubButtons().add(button22); button2.getSubButtons().add(button23); WxMenuButton button31 = new WxMenuButton(); button31.setType("pic_weixin"); button31.setName("微信相册发图器"); button31.setKey("V3001_GOOD"); WxMenuButton button32 = new WxMenuButton(); button32.setType("location_select"); button32.setName("地理位置选择器"); button32.setKey("V3002_GOOD"); // WxMenuButton button33 = new WxMenuButton(); // button33.setType("media_id"); // button33.setName("下发消息"); // button33.setKey("V3003_GOOD"); // // WxMenuButton button34 = new WxMenuButton(); // button34.setType("view_limited"); // button34.setName("图文消息URL"); // button34.setKey("V3004_GOOD"); button3.getSubButtons().add(button31); button3.getSubButtons().add(button32); // button3.getSubButtons().add(button33); // button3.getSubButtons().add(button34); log.debug(menu.toJson().toString()); wxMpService.getMenuService().menuCreate(menu); }
@Test public void menuCreate() throws WxErrorException, UnsupportedEncodingException { String REDIRECT_URI; String url; WxMenu menu = new WxMenu(); WxMenuButton button1 = new WxMenuButton(); button1.setType("click"); button1.setName("长途拼车"); button1.setKey("V1001_ABOUT_US"); // String host = "http://thebo.ngrok.io/"; // String appid = "wx626332f9fc313014"; String host = "http://www.ezcary.com/"; String appid = "wxa52e7c023025f81b"; WxMenuButton button2 = new WxMenuButton(); button2.setType("view"); button2.setName("个人中心"); button2.setKey("V2000_GOOD"); REDIRECT_URI = URLEncoder.encode(host + "user/center", "utf-8"); url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + appid + "&redirect_uri=" + REDIRECT_URI + "&response_type=code&scope=snsapi_userinfo&state=WE#wechat_redirect"; button2.setUrl(url); menu.getButtons().add(button1); menu.getButtons().add(button2); WxMenuButton button11 = new WxMenuButton(); button11.setType("view"); button11.setName("我是乘客"); button11.setKey("V1001_GOOD"); REDIRECT_URI = URLEncoder.encode(host + "wechat/index", "utf-8"); url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + appid + "&redirect_uri=" + REDIRECT_URI + "&response_type=code&scope=snsapi_userinfo&state=WE#wechat_redirect"; button11.setUrl(url); WxMenuButton button12 = new WxMenuButton(); button12.setType("view"); button12.setName("我是车主"); button12.setKey("V1002_GOOD"); REDIRECT_URI = URLEncoder.encode(host + "journey/createPre", "utf-8"); url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + appid + "&redirect_uri=" + REDIRECT_URI + "&response_type=code&scope=snsapi_userinfo&state=WE#wechat_redirect"; log.debug(url); button12.setUrl(url); button1.getSubButtons().add(button11); button1.getSubButtons().add(button12); wxMpService.getMenuService().menuCreate(menu); }