项目中要用到关注微信号后进入获得授权,跳转页面,输入手机号成为会员的功能,要把微信的openid和手机号进行关联存储到服务器 , 实现下次扫码可获取到用户信息的功能
这个是servlet 用来重定向和逻辑判断
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); String open = null; // 用户同意授权后,能获取到code String code = request.getParameter("code"); String state = request.getParameter("state"); if(code == null){//如果没有授权跳转到授权页 只能微信客户端打开 response.sendRedirect("https://open.weixin.qq.com/connect/oauth2/authorize?appid=appid&redirect_uri=http://开头的返回地址要进行url utf-8编码&response_type=code&scope=snsapi_userinfo&state=跟据需要传递参数#wechat_redirect"); return; } String oid = (String)request.getSession().getAttribute("openid");//判断session中是否存在openid if(oid!=null){ System.out.println("------hasopenid------"); Patient p = Patient.getUserByopenid(oid); if(p==null){ request.setAttribute("openid", oid); request.setAttribute("state", state); request.getRequestDispatcher("phone.jsp").forward(request, response); return; } request.setAttribute("openid",oid); }else{ // 用户同意授权 if (!"authdeny".equals(code)) { // 获取网页授权access_token OAuthInfo oa = WeiXinUtil.getOAuthOpenId(WeiXinUtil.appid,WeiXinUtil.appsecret,code); // 用户标识 if(oa==null){ request.getRequestDispatcher("error.jsp").forward(request, response); return; } String openId = oa.getOpenId(); String at = oa.getAccessToken(); String openid = WeiXinUtil.getUserInfo(at, openId); System.out.println(openid); System.out.println(openId); request.getSession().setAttribute("openid",openid);//把openid添加到session 下次进入页面可直接获取 request.setAttribute("openid", openid); request.setAttribute("state", state); Patient p = Patient.getUserByopenid(openid);//判断数据库是否有会员 if(p==null){ request.setAttribute("openid", openid); request.setAttribute("state", state); request.getRequestDispatcher("phone.jsp").forward(request, response);//跳转输入手机号页面 return; } open = p.getMobil(); } } // 跳转到index.jsp request.getRequestDispatcher("success.jsp").forward(request, response); }
获取openid方法
public static OAuthInfo getOAuthOpenId(String appid, String secret, String code ) {
OAuthInfo oAuthInfo = null; String o_auth_openid_url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code"; String requestUrl = o_auth_openid_url.replace("APPID", appid).replace("SECRET", secret).replace("CODE", code); String jsonObject = HttpUtils.get(requestUrl);//可能会请求失败 自行判断把 System.out.println(jsonObject); //oAuthInfo是自己把那几个属性参数写在一个类里面了。 //如果请求成功 JSONObject j = null; if (null != jsonObject && !"".equals(jsonObject)) { try { j = JSONObject.fromObject(jsonObject); oAuthInfo = new OAuthInfo(); oAuthInfo.setAccessToken(j.getString("access_token"));oAuthInfo.setExpiresIn(j.getInt("expires_in"));oAuthInfo.setRefreshToken(j.getString("refresh_token")); oAuthInfo.setOpenId(j.getString("openid"));oAuthInfo.setScope(j.getString("scope")); } catch (JSONException e) { oAuthInfo = null; // 获取token失败 System.out.println(e); } } return oAuthInfo; }
获得用户信息方法
这里我只返回了openid 其他数据也能取到 跟据需要自行取
public static String getUserInfo(String accessToken, String openId) {
OAuthInfo oAuthInfo = null; String requestUrl = "https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID"; requestUrl = requestUrl.replace("ACCESS_TOKEN", accessToken).replace("OPENID", openId); String jsonObject = HttpUtils.get(requestUrl);//可能会请求失败 自行判断把 System.out.println("userinfo ==== "+jsonObject); //oAuthInfo是自己把那几个属性参数写在一个类里面了。 //如果请求成功 JSONObject j = null; if (null != jsonObject && !"".equals(jsonObject)) { try { j = JSONObject.fromObject(jsonObject); oAuthInfo = new OAuthInfo(); //oAuthInfo.setAccessToken(j.getString("access_token")); //oAuthInfo.setExpiresIn(j.getInt("expires_in")); //oAuthInfo.setRefreshToken(j.getString("refresh_token")); oAuthInfo.setOpenId(j.getString("openid")); //oAuthInfo.setScope(j.getString("scope")); } catch (JSONException e) { oAuthInfo = null; // 获取token失败 System.out.println(e); } } return j.getString("openid"); }