您好,登錄后才能下訂單哦!
本文實例為大家分享了java微信掃描公眾號二維碼實現登陸的具體代碼,供大家參考,具體內容如下
前提條件:
1.微信公眾平臺為服務號,
2.服務號實現了賬號綁定功能,即將open_id 與業務系統中的用戶名有對應關系
具體實現原理:
1.用戶訪問業務系統登陸頁時,調用二維碼接口,獲得二維碼的ticketid,同時將sessionid,ticketid和二維碼的seceneid保存
2.返回登陸頁時,根據ticketid獲得微信二維碼
3.頁面通過ajax發送請求,判斷是否已經掃描成功。
4.公眾平臺服務監測到掃描事件,更新seceneid中掃描二維碼的業務系統用戶名
5.當ajax監測到掃描成功,并返回有業務系統用戶名,即可做模擬登陸!
具體代碼:
根據sceneID獲取,獲取ticketId, sceneId可以為sessionID,或者自定義的其他任何值,但必須保證不重復
注意:這里請求的type可以為臨時二維碼或永久二維碼,具體區別可以參看微信公眾平臺的開發者文檔。
public static String getSceneTicket(String type,String sceneId){ WxScene scene = new WxScene(); scene.setAction_name(type); scene.setSceneId(Integer.parseInt(sceneId)); scene.setExpire_seconds(1800); String jsonScene = JSONObject.fromObject(scene).toString(); String url = WeixinContents.qr_scene_ticket_url.replaceAll("ACCESS_TOKEN", getAccessToken(WeixinContents.appid,WeixinContents.appsecret).getToken()); System.out.println(jsonScene); JSONObject jsonObject = httpRequest(url, "POST", jsonScene); int result = 0; String ticket = ""; if (null != jsonObject) { if (jsonObject.containsKey("errcode")) { result = jsonObject.getInt("errcode"); }else{ ticket = jsonObject.getString("ticket"); } } return ticket; }
2.掃描二維碼登陸的幾個action
@ActionKey("/") @ClearInterceptor(ClearLayer.ALL) public void index() { LoginUser u = (LoginUser)getSessionAttr("LoginUser"); setAttr("root", this.getRequest().getContextPath()); if(null==u){ setAttr("ticketId",wxTicket()); render("/WEB-INF/login.html"); }else{ redirect("/index"); } } private String wxTicket() { int sceneId = Db.queryInt("select SEQ_WX_SCENE.Nextval from dual"); String ticketId = WeixinHttpUtils.getSceneTicket("QR_SCENE", sceneId+""); setSessionAttr("ticketId",ticketId); setSessionAttr("sessionId",this.getRequest().getSession().getId()); String sql = "insert into wx_scence_logon(id,sessionid,ticketid,SCENCE_ID) values(sys_guid(),?,?,?)"; Db.update(sql,this.getRequest().getSession().getId(),ticketId,sceneId); return ticketId; } @ActionKey("/cxTicket") @ClearInterceptor(ClearLayer.ALL) public void cxTicket(){ String ticketId = getPara("ticketId"); String sessionid = getPara("sessionId"); int i=0; while(i<10){ Record r = Db.findFirst("select id from wx_scence_logon l,wx_user u where u.open_id = l.open_id and l.ticketid = ? and l.sessionId = ?",ticketId,sessionid); if(r!=null&&StringUtils.isNotEmpty(r.getStr("id"))){ setAttr("success","1"); setAttr("logonId",r.getStr("id")); break; }else{ setAttr("success","0"); try { Thread.sleep(5000); i++; } catch (InterruptedException e) { e.printStackTrace(); } } } render(new JsonRender().forIE()); } @ActionKey("/ticketLogon") @ClearInterceptor(ClearLayer.ALL) public void ticketLogon(){ String id = getPara("ticketId"); Record r = Db.findFirst("select user_id as username from wx_scence_logon l,wx_user u where u.open_id = l.open_id and l.id = ?",id); if(r!=null&&StringUtils.isNotEmpty(r.getStr("username"))){ String username = r.getStr("username"); LoginUser user = LoginUser.dao.findFirst("select user_id,xm,department_id,departmentname,userpw from gy_user u where u.username = ? and u.userlockstate = '1' ",username); String permSql = "select distinct p.* from hr_user_role t,hr_role r,hr_role_perm m,hr_perms p where t.role_id = r.id and m.role_id = r.id and m.perm_id = p.id and user_id = ?"; List<Record> perms = Db.find(permSql,new Object[]{user.getStr("user_id")}); if(perms!=null&&perms.size()>0){ this.getSession().removeAttribute("USER_PERMS"); setSessionAttr("USER_PERMS",perms); getRequest().getSession().setAttribute("LoginUser",user); } } this.redirect("/main"); }
3. 登陸頁面
二維碼顯示
<img src="https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=${ticketId!}" width="250px"/>
定時查詢掃描狀態
function wxCxTikcet(){ $.getJSON("${root!}/cxTicket",{ticketId:"${ticketId}",sessionId:"${sessionId}"},function(data){ //alert(data.success=="1"); if(data.success=='1'){ _logon(data.logonId); //window.location.href="${root!}/ticketLogon/" rel="external nofollow" +data.logonId; } }); } var t_int = window.setInterval("wxCxTikcet()",5000);
4.公眾平臺代碼
}else if(eventType.equalsIgnoreCase(MessageUtil.EVENT_TYPE_SCAN)){ String scene_id = eventKey; if(Integer.parseInt(eventKey)==0){ respContent = "掃描參數出錯!請刷新重試!"; }else{ respContent = getSceneContent(scene_id,fromUserName); } }
private static String getSceneContent(String sceneId,String fromUserName){ String sql ="select * from WX_SCENCE_LOGON where scence_id = ?"; Record r = Db.findFirst(sql,sceneId); if(r!=null){ String updateSql = "update wx_scence_logon set open_id = ? where id = ?"; Db.update(updateSql,fromUserName,r.getStr("id")); return "您已成功登陸***系統!"; } }
說明,框架使用jfinal 1.5
大家可以參考專題:java二維碼進行學習
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。