网站如何接入微信登录
分类: Java 专栏: java 标签: 网站如何接入微信登录
2023-04-10 13:10:27 228浏览
网站如何接入微信登录
网站接入微信登录分为两种,1是微信内收授权直接登录,2是微信外授权登录需要生成二维码。
先来说说微信登录的流程,需要参数appid,以及授权成功后的回调地址
//我是把微信登录和微信绑定做到了一起,login代表登录,bind代表绑定,该方法主要是扫码登录 @ResponseBody @RequestMapping(value = "/beforeWxLogin", headers = { "X-Requested-With=XMLHttpRequest" }) public MessUtils beforeWxLogin(JfWxLoginLog o, Integer needbinduid, String token, HttpSession session) throws Exception { MessUtils mess = new MessUtils(); //sessionid前段页面生成的字符窜,主要是扫码登录识别身份的的,因为电脑微信扫码是用户手机扫,扫完电脑的页面跳转进入网站,微信页面则提示登录成功或者绑定成功 if (o.getSessionid() == null || o.getSessionid().trim().length() == 0) { mess.setStatus(0); mess.setMsg("参数异常"); return mess; } if (o.getVtype() == null || o.getVtype().trim().length() == 0) { mess.setStatus(0); mess.setMsg("参数异常"); return mess; } if (!o.getVtype().equals("login") && !o.getVtype().equals("bind")) { mess.setStatus(0); mess.setMsg("参数异常"); return mess; } o.setCts(DateUtils.DateToString(new Date())); List<JfWxLoginLog> li = jfWxLoginLogService.getList(o); if (li == null || li.size() == 0) { jfWxLoginLogService.insert(o); } //微信自能带一个参数所以用下划线,授权成功后的回调链接, 带有sessionid表示扫码登录,不带sessionid表示微信内直接登录 String redirect_uri = "https://xxx/xxx/callBack?sessionid=" + o.getSessionid() + "_" + o.getVtype(); if (o.getVtype().equals("bind")) { //绑定账号需要先判断用户是否登录,我的是一体项目,直接session判断,前后端分离可以根据token等判断 } //这个链接就是微信授权登录的链接,如果前端js判断是微信浏览器,则直接让a标签的href等于以下链接,用户点击直接登录 //如果前端是普通浏览器,则利用qrcode.min.js生成二维码,让用户手机扫码登录(本程序根据sessionid是否为空判断是扫码还是微信登录) String wxlogin = "https://open.weixin.qq.com/connect/oauth2/authorize?" + "appid=" + Sys.wxlogin.appid + "&redirect_uri=" + redirect_uri + "&response_type=code" + "&scope=snsapi_userinfo" + "&state=123#wechat_redirect"; mess.setStatus(1); mess.setObj(wxlogin); return mess; }微信
微信授权后回调函数
/** * * 微信登录复杂,涉及到微信内登录和绑定,电脑扫码登录和绑定 微信授权成功的回调函数, 主要获取openid,其他的属于你自己的业务逻辑了,比如第一次登录属于注册,非第一次登录查询,判断当前用户是登录还是绑定,是电脑扫码还是微信内直接登录等 */ @RequestMapping("/xxx/callBack") protected void deGet(String sessionid , HttpServletRequest request, HttpServletResponse response, HttpSession session) throws Exception { // 获取回调地址中的code String code = request.getParameter("code"); if (code == null || code.trim().length() == 0) { System.out.println("wx code error "); request.getRequestDispatcher("/error?msg=参数异常,").forward(request, response); } // 拼接url String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + Sys.wxlogin.appid + "&secret=" + Sys.wxlogin.appsecret + "&code=" + code + "&grant_type=authorization_code"; JSONObject jsonObject = doGetJson(url); // 1.获取微信用户的openid String openID = jsonObject.getString("openid"); if (openID == null || openID.trim().length() == 0) { System.out.println("wx openID error "); request.getRequestDispatcher("/error?msg=参数异常,").forward(request, response); } // 2.获取获取access_token String access_token = jsonObject.getString("access_token"); if (access_token == null || access_token.trim().length() == 0) { System.out.println("wx access_token error "); request.getRequestDispatcher("/error?msg=参数异常,").forward(request, response); } // String infoUrl = "https://api.weixin.qq.com/sns/userinfo?access_token=" + access_token + "&openid=" + openID // + "&lang=zh_CN"; // // 3.获取微信用户信息 // JSONObject userInfo = doGetJson(infoUrl); // // 至此拿到了微信用户的所有信息,剩下的就是业务逻辑处理部分了 // System.out.println("WX login uinfo=" + userInfo.toString()); //数据库查询该openid是否存在,微信登录考虑两种情况,第一次登录属于注册,第二次登录 UsersInfo ui = usersInfoService.getByWXopeind(openID); System.out.println("o.getSessionid()="+sessionid); // 电脑扫码 if (sessionid!=null && sessionid.trim().length()>0) { String s[]=sessionid.split("_"); String sessionid_real=s[0]; String vtype=s[1]; if (vtype.equals("login")) { if (ui != null) {// 非第一次登录 if (ui.getStatus() != 1) { System.out.println("wx login error ,status=0 ---outwx"); request.getRequestDispatcher("/error?msg=该账号已被禁用," + ui.getMsg()).forward(request, response); } else { //登录的操作 return; } } else {// 第一次登录 //注册,添加用户数据的操作 return; } } // 扫码绑定账号 if (vtype.equals("bind")) { Integer uid=Integer.parseInt(s[2]); String token=s[3]; if (uid != null && token != null && token.trim().length() > 0) { JfUsersInfo lu = jfUsersInfoService.getByToken(token, uid); if (lu != null) { if (ui == null) { //绑定微信的操作 } else { System.out.println("wx binded ---outwx"); request.getRequestDispatcher("/error?msg=该微信已绑定,请勿重复操作").forward(request, response); return; } } } System.out.println("wx uid or token is null ---outwx"); request.getRequestDispatcher("/error?msg=先登录账号才可绑定维信哦").forward(request, response); return; } } else { // 微信内 JfUsersInfo uc = (JfUsersInfo) session.getAttribute("user"); if (uc != null) {// 微信内 已登录,可能是绑定WX if (ui != null) { System.out.println("WX bind error ,is binded---inwx"); request.getRequestDispatcher("/error?msg=绑定失败,该微信已被绑定").forward(request, response); return; } else { //绑定微信操作 return; } } else {// 登录,可能是WX第一次登录或者非第一次登录 // 微信内登录 if (ui != null) {// 非第一次登录 if (ui.getStatus() != 1) { System.out.println("wx login error ,status=0 ---inwx"); request.getRequestDispatcher("/error?msg=该账号已被禁用," + ui.getMsg()).forward(request, response); return; } else { //登录成功的一些操作 return; } } else {// 第一次登录 //插入用户数据 response.sendRedirect("/web/gf_login_goto.jsp"); return; } } } }
特别说明:
手机扫码需要写一个ajax轮询,根据sessionid查询数据库来判断用户是否已经成功授权获取openid,来判断网站是否进入个人中心
好博客就要一起分享哦!分享海报
此处可发布评论
评论(0)展开评论
暂无评论,快来写一下吧
展开评论
他的专栏