亲测通过
1,pc端生成带有当前会话的sessionid的url(通过微信来扫描)
2,扫描后,微信浏览器将访问url,将微信浏览器中的sessionid改成通过url传过来的session(pc端),然后引导用户进入微信网页授权地址,参考 http://mp.weixin.qq.com/wiki/9/01f711493b5a02f24b04365ac5d8fd95.html
3,在微信浏览器中得到openid,并存入session
private String sid; private String state; @Action("weixinloginerweima") public void weixinloginerweima() throws Exception{ if (sid==null&&state==null) { write("sid参数错误"); return; } if (sid==null) { sid=state; } String appid = "wxXXXXXXXXXX"; String app_secret = "XXXXXXXXXXXXXX"; cookie("JSESSIONID", sid,null); if (!sid.equals(session.getId())||code==null) { String path = request.getContextPath(); String getWebUrl= "http://"+request.getServerName()+("/".equals(path)?"":path); String localurl = getWebUrl+"/hindex/weixinloginerweima"; String weixinurl = "https://open.weixin.qq.com/connect/oauth2/authorize?appid="+appid+"&redirect_uri="+URLEncoder.encode(localurl, "utf8")+"&response_type=code&scope=snsapi_base&state="+sid+"#wechat_redirect"; response.sendRedirect(weixinurl); }else{ write(code); Map<String, String> param = new HashMap<>(); param.put("appid", appid); param.put("secret", app_secret); param.put("code", code); param.put("grant_type", "authorization_code"); JSONObject jsonObject = HttpUtil.get("https://api.weixin.qq.com/sns/oauth2/access_token", param ); String weixinid = jsonObject.getString("openid"); if (weixinid==null) { write(jsonObject.toJSONString()); return; } Map<String, Object> param1 = new HashMap<>(); param1.put("weixinid", weixinid); List<User> users =easyService.find(User.class, param1 , null, null); if (users!=null&&users.size()>0) { session.setAttribute("user", users.get(0)); response.sendRedirect(getWebUrl()+"/hindex/weixinlogin?sta=1"); }else{ session.setAttribute("weixinid", weixinid); response.sendRedirect(getWebUrl()+"/hindex/weixinlogin?sta=2");//在微信上提示在pc端绑定账号 } }
4,在pc端,用ajax检测session是否已有openid,如果有,则进行,绑定或者注册新用户处理
@Action("checkweixinid") public void checkweixinid() { Object object = session.getAttribute("weixinid"); if (object==null) { write("2"); }else{ write("1"); } }
<a href="javascript:;" style="position:relative;" onclick=‘toWeiXinLogin()‘> <span id="weixinloginerweima" style="left:10px;display:none;position: absolute; height: 200px; width: 200px; top: 24px; z-index: 88;"></span> <img src="weixinlogin.png"/> </a>
var erweima = false; function toWeiXinLogin(){ if(erweima===false){ erweima = true; $.getJSON("${mcbn}/hindex/getweixinurl",function(data){ var qrcode = new QRCode(document.getElementById("weixinloginerweima"), { width : 200, height : 200 }); qrcode.makeCode(data.url); weixinloginerweimashow(); }); }else{ weixinloginerweimashow(); } } function checkweixinid(){ $.ajax({ "url":"${mcbn}/hindex/checkweixinid", "success":function(data){ if(data==1){ location.href="${mcbn}/hindex/bangding"; }else{ var display = $("#weixinloginerweima").css("display"); if(display=="none"){ }else{ setTimeout(checkweixinid,3000); } } } }); } function weixinloginerweimashow(){ var display = $("#weixinloginerweima").css("display"); if(display=="none"){ $("#weixinloginerweima").show(); setTimeout(checkweixinid,3000); }else{ $("#weixinloginerweima").hide(); } }
java网站,修改cookie中的jsessionid可改变会话的session,在地址后面加过;jsession=sessionid,未成功
php中,可以直接调用session_id($sessionid)