@RequestMapping("/openid") public void getOpenId(HttpServletRequest request, HttpServletResponse response) throws IOException { response.setContentType("text/html;charset=utf-8"); response.setStatus(HttpServletResponse.SC_OK); String code = request.getParameter("code"); try { response.getWriter().println("<h1>code</h1>"); response.getWriter().println(code); WxMpOAuth2AccessToken wxMpOAuth2AccessToken = WeChatConnector.getMpService().oauth2getAccessToken(code); WxMpUser wxMpUser = WeChatConnector.getMpService().oauth2getUserInfo(wxMpOAuth2AccessToken, null); response.getWriter().println("<h1>user open id</h1>"); response.getWriter().println(wxMpUser.getOpenId()); } catch (WxErrorException e) { e.printStackTrace(); } response.getWriter().flush(); response.getWriter().close(); }
@Override protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=utf-8"); response.setStatus(HttpServletResponse.SC_OK); String code = request.getParameter("code"); try { response.getWriter().println("<h1>code</h1>"); response.getWriter().println(code); WxMpOAuth2AccessToken wxMpOAuth2AccessToken = this.wxMpService.oauth2getAccessToken(code); response.getWriter().println("<h1>access token</h1>"); response.getWriter().println(wxMpOAuth2AccessToken.toString()); WxMpUser wxMpUser = this.wxMpService.oauth2getUserInfo(wxMpOAuth2AccessToken, null); response.getWriter().println("<h1>user info</h1>"); response.getWriter().println(wxMpUser.toString()); wxMpOAuth2AccessToken = this.wxMpService.oauth2refreshAccessToken(wxMpOAuth2AccessToken.getRefreshToken()); response.getWriter().println("<h1>after refresh</h1>"); response.getWriter().println(wxMpOAuth2AccessToken.toString()); } catch (WxErrorException e) { e.printStackTrace(); } response.getWriter().flush(); response.getWriter().close(); }
@RequestMapping("/today/{id}") public void today( @PathVariable("id") Long id, HttpServletRequest request, HttpServletResponse response) throws IOException, WxErrorException { logger.debug("跳转活动,活动id=" + id); /* 用code能取得accesstoken,然后用accesstoken得到登录用户信息? */ String code = request.getParameter("code"); // AccessToken:用户的访问令牌 WxMpOAuth2AccessToken wxMpOAuth2AccessToken = WeChatConnector.getMpService().oauth2getAccessToken(code); // WxMpUser:微信用户信息 WxMpUser wxMpUser = WeChatConnector.getMpService().oauth2getUserInfo(wxMpOAuth2AccessToken, null); Activity activity = activityService.findOneById(id); if (activity != null) { /* 保存wcUser并且不重复记录 */ WCUser user = wcUserService.findOneByOpenId(wxMpUser.getOpenId()); if (user == null) { WCUser wcUser = new WCUser( wxMpUser.getOpenId(), wxMpUser.getNickname(), wxMpUser.getSex(), wxMpUser.getCity(), wxMpUser.getProvince(), wxMpUser.getCountry(), wxMpUser.getUnionId()); user = wcUserService.save(wcUser); } /* 检测该用户是否参加过该活动 */ boolean j = true; if (activity.getWcUserss().size() > 0) { for (WCUser wcUser1 : activity.getWcUserss()) { if (wcUser1.getId() == user.getId()) { logger.debug("该用户已经浏览过该活动"); j = false; break; } } } if (j == true) { logger.debug("该活动人数加1"); activity.getWcUserss().add(user); activityService.save(activity); } response.sendRedirect("http://bovps1.taoware.com/web/" + activity.getFolderName() + "/send"); } else { logger.debug("该活动不存在"); } }
/** 查询赠卷? (查询赠卷按钮连接的url) */ @RequestMapping("/query") public String query(HttpServletRequest request, Model model) throws Exception { /* 通过code得到登录用户信息 */ String code = request.getParameter("code"); WxMpOAuth2AccessToken wxMpOAuth2AccessToken = WeChatConnector.getMpService().oauth2getAccessToken(code); WxMpUser wxMpUser = WeChatConnector.getMpService().oauth2getUserInfo(wxMpOAuth2AccessToken, null); String openId = wxMpUser.getOpenId(); Coupon coupon = cUserService.queryActivity(openId); if (null == coupon) return "fail"; else { model.addAttribute("coupon", coupon.getCode()); return "succeed"; } }