声明:这边所谓的第三方登录,不是通过网页授权的方式,而是利用生成带参数的二维码,将微信号和平台账号绑定实现。如果你要做的是授权登录,请查看
https://blog.csdn.net/wrongyao/article/details/80229986
微信第三方登录功能实现,大致分为两个步骤,绑定和登录,下面先来了解一下绑定。
1、思路概括
注:先用平台用户的userid生成二维码,(详见生成带参数的二维码的帖子),微信扫码以后,需要关注微信公众号,首先请求会先到微信服务器,然后微信服务器会向我服务器推送消息,消息中有userid和openid(这个公众号下这个微信的唯一标识),然后我们可以将这个userid和openid存到关联表中,绑定成功。
1、利用0050的帖子我们可以生成带有userid的二维码,这边只附上简要代码
@Overridepublic String getBindQrCode() {String sessionId = request.getRequestedSessionId();//获取当前登录的用户,注意要用sessionid做区分String loginJsonStr = redisService.get("loginJson&" + sessionId);StringBuffer sceneStr = new StringBuffer("bind&");if (StringUtils.isNotEmpty(loginJsonStr)) {LoginJson loginJson = GsonUtil.fromJson(loginJsonStr, LoginJson.class);SysUser sysUser = loginJson.getSysUser();sceneStr.append(sysUser.getSysUserId());}return getQcode(sceneStr.toString());}/*** 获取二维码** @param sceneStr* @return*/private String getQcode(String sceneStr) {String getTicketUrl = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=" + AccessToken.ACCESS_TOKEN + "";// 临时整形参数值String ticketParam = "{\"expire_seconds\": 120, \"action_name\": \"QR_STR_SCENE\", \"action_info\": {\"scene\": {\"scene_str\": \"" + sceneStr + "\"}}}";String ticketStr = HttpRequest.post(getTicketUrl, ticketParam, null, Constant.RequestType.APPLICATION_JSON);System.out.println(ticketStr);Map<String, String> ticketMap = GsonUtil.fromJson(ticketStr, Map.class);String ticket = ticketMap.get("ticket");return "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=" + ticket;}
大致步骤:获取当前登录用户userid,用这个userid生成 scene_str
注意点:这边用的是redis做的缓存,不要用session,也可以用别的nosql数据库,具体原因会在下篇帖子登录的时候做详细说明。
2、将上述生成的二维码在合适的地方展示即可。注意是在登录之后。
3、扫码推送
扫码微信服务器向我服务器推送的报文
Ticket||gQHO7jwAAAAAAAAAAS5odHRwOi8vd2VpeGluLnFxLmNvbS9xLzAyaXN0X2hHSlJmM2sxT1BfRXhyY2cAAgQ7P_hbAwR4AAAA
CreateTime||1541947203
EventKey||bind&1
Event||SCAN
ToUserName||gh_b348cb411e6c
FromUserName||oLXXD1ElSeoNELQP1EhUkvlRF9SM
MsgType||event
eventkey就是我们之前放入的scene_str
4、绑定
@Override@Transactionalpublic String userBindWechat(Map<String, String> requestMap, long sysUserId) {String openId = requestMap.get("FromUserName");List<Condition> conditions = new ArrayList<>();conditions.add(new Condition(Connector.and, "open_id", Operator.equal, openId));List<SysWechat> sysWechats = sysWechatMapper.select(conditions, SysWechat.class);SysWechat sysWechat = sysWechats.get(0);long sysWechatId = sysWechat.getSysWechatId();conditions.clear();// 查询当前微信号是否已经绑定conditions.add(new Condition(Connector.and, "sys_wechat_id", Operator.equal, sysWechatId));List<SysUserWechat> sysUserWechats = sysUserWechatMapper.select(conditions, SysUserWechat.class);if (null != sysUserWechats && sysUserWechats.size() > 0) {return WechatUtil.replyTextMessage(requestMap.get("FromUserName"), requestMap.get("ToUserName"), wechatMessage.getWechatHaveBindedReplyMessage());}SysUserWechat sysUserWechat = new SysUserWechat();sysUserWechat.setSysUserId(sysUserId);sysUserWechat.setSysWechatId(sysWechatId);sysUserWechatMapper.insert(sysUserWechat);return WechatUtil.replyTextMessage(requestMap.get("FromUserName"), requestMap.get("ToUserName"), wechatMessage.getBindSuccessReplyMessage());}
大致步骤:根据微信服务器向我服务器推送的报文,获取userid,openid将这两个信息,关联起来即为绑定成功。
上述就是扫码绑定的大致思路,如有不妥的地方,欢迎大家斧正,谢谢!