1:web端请求服务端获取到sessionId,将sessionId设置一个过期时间存入redis
@ApiOperation("前端生成二维码,请求绑定关系,二维码过期时间30秒")
@PostMapping("/request")
public AjaxResult request(HttpServletRequest request) {
String id = request.getSession().getId();
redisService.set(id, id, 30);
return AjaxResult.success("", id);
}
2:轮询,返回给前端信息
@ApiOperation("前端轮询该接口,扫码登录之后返回信息")
@PostMapping("/loopCode")
public AjaxResult loopCode(String sessionId) {
Object o = redisService.get(sessionId);
if (ObjectUtils.isEmpty(o)) {
return AjaxResult.error("二维码已过期");
}
// APP端扫码认证成功之后将1中的缓存sessionId 更新为success或者自定义值
if (o.toString().equals("success")) {
// 将令牌和用户信息返回给web端跳转即可
String mobile = (String) redisService.get(sessionId + "login");
String token = (String) redisService.get(sessionId + mobile);
AppUser user = tokenManage.getUser(token);
Map map = new HashMap();
map.put("token", token);
map.put("user", user);
return AjaxResult.success(map);
}
return new AjaxResult(AjaxResult.Type.SCAN_CODE, "扫码中");
}
3:扫码登录,app请求之后获取到二维码中的sessionId,将sessionId和手机号在缓存中绑定,并修改1中的缓存value
@ApiOperation("扫码登录")
@PostMapping("/scanLogin")
public AjaxResult scanLogin(String mobile, String sessionId) {
Object o = redisService.get(sessionId);
if (ObjectUtils.isEmpty(o)) {
return AjaxResult.error("无效码");
}
AppUser appUser = appUserService.selectAppUserByPhonenumber(mobile);
String token = tokenManage.getToken(appUser);
if (ObjectUtils.isEmpty(token)) {
return new AjaxResult(APP_AUTH_LOGIN, "令牌已过期,请重新登录");
}
redisService.set(sessionId, "success", 20);
redisService.set(sessionId + "login", mobile, 20);
redisService.set(sessionId + mobile, token,20);
return AjaxResult.success();
}
|