release- 扫码登录新增已扫码未登录状态

release-2024-08-08
huangyawei 4 months ago
parent c6aa15cfe6
commit 183731df5d

@ -42,163 +42,162 @@ import java.util.concurrent.TimeUnit;
/** /**
* *
*
* @author Administrator
* *
* @author Administrator
*/ */
@RestController @RestController
@CrossOrigin @CrossOrigin
@RequestMapping("/user") @RequestMapping("/user")
@Api(tags = "LoginController") @Api(tags = "LoginController")
public class LoginController extends BaseController { public class LoginController extends BaseController {
private static final List<CountryCodeDTO> DEFAULT_SUPPORTED_COUNTRY_CODES = Arrays private static final List<CountryCodeDTO> DEFAULT_SUPPORTED_COUNTRY_CODES = Arrays
.asList(new CountryCodeDTO("中国", "+86")); .asList(new CountryCodeDTO("中国", "+86"));
@Autowired @Autowired
private UserInfoService userInfoService; private UserInfoService userInfoService;
@Autowired @Autowired
private RedisTemplate redisTemplate; private RedisTemplate redisTemplate;
@Autowired @Autowired
private WeChatAouth2Service weChatAouth2Service; private WeChatAouth2Service weChatAouth2Service;
@Autowired @Autowired
private JwtUtil jwtUtil; private JwtUtil jwtUtil;
/** /**
* *
* *
* @param * @param
* @return * @return
*/ */
@PutMapping("/{userid}/{friendid}/{x}") @PutMapping("/{userid}/{friendid}/{x}")
public void updatefanscountandfollowcount(@PathVariable String userid, @PathVariable String friendid, public void updatefanscountandfollowcount(@PathVariable String userid, @PathVariable String friendid,
@PathVariable int x) { @PathVariable int x) {
userInfoService.updatefanscountandfollowcount(x, userid, friendid); userInfoService.updatefanscountandfollowcount(x, userid, friendid);
} }
/** /**
* *
*/ */
@ApiOperation(value = "1.发送短信验证码", notes = "有效期15分钟一个手机号一天内最多发送20个请求") @ApiOperation(value = "1.发送短信验证码", notes = "有效期15分钟一个手机号一天内最多发送20个请求")
@PostMapping("/sendsms") @PostMapping("/sendsms")
@GlobalInterceptor(frequencyType = RequestFrequencyTypeEnum.DAY, requestFrequencyThreshold = 20) @GlobalInterceptor(frequencyType = RequestFrequencyTypeEnum.DAY, requestFrequencyThreshold = 20)
public Result<Void> sendSms(@VerifyParam SendSmsReq sendSmsReq) { public Result<Void> sendSms(@VerifyParam SendSmsReq sendSmsReq) {
if(sendSmsReq.getMobile().equals("18812345678")){ if (sendSmsReq.getMobile().equals("18812345678")) {
/** /**
* *
*/ */
return Result.success(); return Result.success();
} }
if (!StringTools.isEmpty(sendSmsReq.getImageCheckCode())) { if (!StringTools.isEmpty(sendSmsReq.getImageCheckCode())) {
// 得到缓存中的验证码 // 得到缓存中的验证码
String redisImageCheckCodeKey = Constants.REDIS_KEY_IMAGE_CHECK_CODE + sendSmsReq.getDeviceId(); String redisImageCheckCodeKey = Constants.REDIS_KEY_IMAGE_CHECK_CODE + sendSmsReq.getDeviceId();
String redisImageCheckCode = (String) redisTemplate.opsForValue().get(redisImageCheckCodeKey); String redisImageCheckCode = (String) redisTemplate.opsForValue().get(redisImageCheckCodeKey);
if (StringTools.isEmpty(redisImageCheckCode)) { if (StringTools.isEmpty(redisImageCheckCode)) {
return Result.failed(StatusCode.USER_IMAGE_VERIFICATION_CODE_EXPIRED); return Result.failed(StatusCode.USER_IMAGE_VERIFICATION_CODE_EXPIRED);
} }
if (!redisImageCheckCode.equalsIgnoreCase(sendSmsReq.getImageCheckCode())) { if (!redisImageCheckCode.equalsIgnoreCase(sendSmsReq.getImageCheckCode())) {
return Result.failed(StatusCode.USER_IMAGE_VERIFICATION_CODE_MISMATCH); return Result.failed(StatusCode.USER_IMAGE_VERIFICATION_CODE_MISMATCH);
} }
redisTemplate.delete(redisImageCheckCodeKey); redisTemplate.delete(redisImageCheckCodeKey);
} }
userInfoService.sendSms(sendSmsReq.getDeviceId(), sendSmsReq.getMobile()); userInfoService.sendSms(sendSmsReq.getDeviceId(), sendSmsReq.getMobile());
return Result.success(); return Result.success();
} }
@ApiOperation(value = "2.登录/注册", notes = "成功后返回authorization") @ApiOperation(value = "2.登录/注册", notes = "成功后返回authorization")
@PostMapping("/appLogin") @PostMapping("/appLogin")
@GlobalInterceptor(frequencyType = RequestFrequencyTypeEnum.HOUR, requestFrequencyThreshold = 12) @GlobalInterceptor(frequencyType = RequestFrequencyTypeEnum.HOUR, requestFrequencyThreshold = 12)
public Result<String> appLogin(HttpServletRequest request, public Result<String> appLogin(HttpServletRequest request,
@VerifyParam LoginReq loginReq) { @VerifyParam LoginReq loginReq) {
if("18812345678".equals(loginReq.getMobile())) { if ("18812345678".equals(loginReq.getMobile())) {
UserInfo loginUserInfo = new UserInfo(); UserInfo loginUserInfo = new UserInfo();
loginUserInfo.setMobile(loginReq.getMobile()); loginUserInfo.setMobile(loginReq.getMobile());
loginUserInfo.setLastUseDeviceId(loginReq.getDeviceId()); loginUserInfo.setLastUseDeviceId(loginReq.getDeviceId());
loginUserInfo.setLastUseDeviceBrand(loginReq.getDeviceBrand()); loginUserInfo.setLastUseDeviceBrand(loginReq.getDeviceBrand());
loginUserInfo.setLastLoginIp(getIpAddr(request)); loginUserInfo.setLastLoginIp(getIpAddr(request));
String token = userInfoService.loginOrRegister(loginUserInfo); String token = userInfoService.loginOrRegister(loginUserInfo);
return Result.success(token); return Result.success(token);
} }
// 得到缓存中的验证码 // 得到缓存中的验证码
String redisMobileCheckCodeKey = Constants.REDIS_KEY_MOBILE_CHECK_CODE + loginReq.getDeviceId(); String redisMobileCheckCodeKey = Constants.REDIS_KEY_MOBILE_CHECK_CODE + loginReq.getDeviceId();
String redisMobileCheckCode = (String) redisTemplate.opsForValue().get(redisMobileCheckCodeKey); String redisMobileCheckCode = (String) redisTemplate.opsForValue().get(redisMobileCheckCodeKey);
if (StringTools.isEmpty(redisMobileCheckCode)) { if (StringTools.isEmpty(redisMobileCheckCode)) {
return Result.failed(StatusCode.USER_MOBILE_VERIFICATION_CODE_EXPIRED); return Result.failed(StatusCode.USER_MOBILE_VERIFICATION_CODE_EXPIRED);
} }
if (!redisMobileCheckCode.equals(loginReq.getMobileCheckCode())) { if (!redisMobileCheckCode.equals(loginReq.getMobileCheckCode())) {
return Result.failed(StatusCode.USER_MOBILE_VERIFICATION_CODE_MISMATCH); return Result.failed(StatusCode.USER_MOBILE_VERIFICATION_CODE_MISMATCH);
} }
UserInfo loginUserInfo = new UserInfo(); UserInfo loginUserInfo = new UserInfo();
loginUserInfo.setMobile(loginReq.getMobile()); loginUserInfo.setMobile(loginReq.getMobile());
loginUserInfo.setLastUseDeviceId(loginReq.getDeviceId()); loginUserInfo.setLastUseDeviceId(loginReq.getDeviceId());
loginUserInfo.setLastUseDeviceBrand(loginReq.getDeviceBrand()); loginUserInfo.setLastUseDeviceBrand(loginReq.getDeviceBrand());
loginUserInfo.setLastLoginIp(getIpAddr(request)); loginUserInfo.setLastLoginIp(getIpAddr(request));
String token = userInfoService.loginOrRegister(loginUserInfo); String token = userInfoService.loginOrRegister(loginUserInfo);
redisTemplate.delete(redisMobileCheckCodeKey); redisTemplate.delete(redisMobileCheckCodeKey);
return Result.success(token); return Result.success(token);
} }
@ApiOperation(value = "2.1 appleId登录/注册", notes = "成功后返回authorization")
@PostMapping("/appleIdLogin") @ApiOperation(value = "2.1 appleId登录/注册", notes = "成功后返回authorization")
public Result<String> appleIdLogin(HttpServletRequest request,@VerifyParam AppleLoginReq appleLoginReq) { @PostMapping("/appleIdLogin")
public Result<String> appleIdLogin(HttpServletRequest request, @VerifyParam AppleLoginReq appleLoginReq) {
// 无手机号和验证码
String identityToken = appleLoginReq.getIdentityToken(); // 无手机号和验证码
// 解码后的消息体 String identityToken = appleLoginReq.getIdentityToken();
JSONObject playloadObj = IOSTokenUtils.parserIdentityToken(identityToken); // 解码后的消息体
Boolean success; JSONObject playloadObj = IOSTokenUtils.parserIdentityToken(identityToken);
try { Boolean success;
success = IOSTokenUtils.verifyExc(identityToken, playloadObj); try {
} catch (Exception e) { success = IOSTokenUtils.verifyExc(identityToken, playloadObj);
return Result.failed(StatusCode.APPLE_lOGIN_FAILED); } catch (Exception e) {
return Result.failed(StatusCode.APPLE_lOGIN_FAILED);
// throw new RuntimeException(e); // throw new RuntimeException(e);
} }
if (!success) { if (!success) {
// TODO 校验token失败具体操作 // TODO 校验token失败具体操作
return Result.failed(StatusCode.APPLE_lOGIN_FAILED); return Result.failed(StatusCode.APPLE_lOGIN_FAILED);
} }
UserInfo loginUserInfo = new UserInfo(); UserInfo loginUserInfo = new UserInfo();
// loginUserInfo.setMobile(appleLoginReq.getMobile()); // loginUserInfo.setMobile(appleLoginReq.getMobile());
loginUserInfo.setAppleId(appleLoginReq.getUserIdentifier()); loginUserInfo.setAppleId(appleLoginReq.getUserIdentifier());
loginUserInfo.setAppleEmail(playloadObj.get("email")+""); loginUserInfo.setAppleEmail(playloadObj.get("email") + "");
loginUserInfo.setLastLoginIp(getIpAddr(request)); loginUserInfo.setLastLoginIp(getIpAddr(request));
loginUserInfo.setLastUseDeviceId(appleLoginReq.getDeviceId()); loginUserInfo.setLastUseDeviceId(appleLoginReq.getDeviceId());
loginUserInfo.setLastUseDeviceBrand(appleLoginReq.getDeviceBrand()); loginUserInfo.setLastUseDeviceBrand(appleLoginReq.getDeviceBrand());
String token = userInfoService.appleLoginOrRegister(loginUserInfo); String token = userInfoService.appleLoginOrRegister(loginUserInfo);
return Result.success(token); return Result.success(token);
}
} @ApiOperation(value = "2.2 微信wxId登录/注册", notes = "成功后返回authorization")
@PostMapping("/wxIdLogin")
public Result<String> wxIdLogin(HttpServletRequest request, @VerifyParam WeChatLoginReq weChatLoginReq) {
@ApiOperation(value = "2.2 微信wxId登录/注册", notes = "成功后返回authorization") // 如果有手机号,是首次微信登录时绑定流程
@PostMapping("/wxIdLogin") if (StringUtils.isNotEmpty(weChatLoginReq.getMobile())) {
public Result<String> wxIdLogin(HttpServletRequest request,@VerifyParam WeChatLoginReq weChatLoginReq) { //得到缓存中的验证码
String redisMobileCheckCodeKey = Constants.REDIS_KEY_MOBILE_CHECK_CODE + weChatLoginReq.getDeviceId();
String redisMobileCheckCode = (String) redisTemplate.opsForValue().get(redisMobileCheckCodeKey);
if (StringTools.isEmpty(redisMobileCheckCode)) {
return Result.failed(StatusCode.USER_MOBILE_VERIFICATION_CODE_EXPIRED);
}
if (!redisMobileCheckCode.equals(weChatLoginReq.getMobileCheckCode())) {
return Result.failed(StatusCode.USER_MOBILE_VERIFICATION_CODE_MISMATCH);
}
return weChatAouth2Service.register(weChatLoginReq, getIpAddr(request));
}
// 如果有手机号,是首次微信登录时绑定流程 // 无手机号,非第一次微信登录场景,需要判断当前wxID在数据库中是否有记录如果有记录登录正常执行如果无记录返回状态到上面绑定手机号流程
if(StringUtils.isNotEmpty(weChatLoginReq.getMobile())) { return weChatAouth2Service.login(weChatLoginReq, getIpAddr(request));
//得到缓存中的验证码
String redisMobileCheckCodeKey = Constants.REDIS_KEY_MOBILE_CHECK_CODE + weChatLoginReq.getDeviceId();
String redisMobileCheckCode = (String) redisTemplate.opsForValue().get(redisMobileCheckCodeKey);
if (StringTools.isEmpty(redisMobileCheckCode)) {
return Result.failed(StatusCode.USER_MOBILE_VERIFICATION_CODE_EXPIRED);
}
if (!redisMobileCheckCode.equals(weChatLoginReq.getMobileCheckCode())) {
return Result.failed(StatusCode.USER_MOBILE_VERIFICATION_CODE_MISMATCH);
}
return weChatAouth2Service.register(weChatLoginReq,getIpAddr(request));
}
// 无手机号,非第一次微信登录场景,需要判断当前wxID在数据库中是否有记录如果有记录登录正常执行如果无记录返回状态到上面绑定手机号流程
return weChatAouth2Service.login(weChatLoginReq,getIpAddr(request));
}
}
// @ApiOperation(value = "2.1 appleId登录/注册", notes = "成功后返回authorization") // @ApiOperation(value = "2.1 appleId登录/注册", notes = "成功后返回authorization")
// @PostMapping("/appleIdLogin") // @PostMapping("/appleIdLogin")
// @GlobalInterceptor(frequencyType = RequestFrequencyTypeEnum.HOUR, requestFrequencyThreshold = 12) // @GlobalInterceptor(frequencyType = RequestFrequencyTypeEnum.HOUR, requestFrequencyThreshold = 12)
@ -286,168 +285,173 @@ public class LoginController extends BaseController {
// } // }
/**
/** * authorization
* authorization */
*/ @ApiOperation(value = "3.token续期", notes = "有效期7天")
@ApiOperation(value = "3.token续期", notes = "有效期7天") @PostMapping("/autoLogin")
@PostMapping("/autoLogin") @GlobalInterceptor
@GlobalInterceptor public Result<String> autoLogin(HttpServletRequest request,
public Result<String> autoLogin(HttpServletRequest request, @RequestHeader(value = "Authorization", required = false) String authorization,
@RequestHeader(value = "Authorization", required = false) String authorization, @VerifyParam TokenRefreshReq tokenRefreshReq) {
@VerifyParam TokenRefreshReq tokenRefreshReq) { return Result.success(userInfoService.autoLogin(authorization, tokenRefreshReq.getDeviceId(), tokenRefreshReq.getDeviceBrand(), getIpAddr(request)));
return Result.success(userInfoService.autoLogin(authorization, tokenRefreshReq.getDeviceId(), tokenRefreshReq.getDeviceBrand(), getIpAddr(request))); }
}
/**
/** *
* */
*/ @ApiOperation(value = "4.获取图形验证码", notes = "4.三次短信验证失败后获取图形验证码有效期10分钟")
@ApiOperation(value = "4.获取图形验证码", notes = "4.三次短信验证失败后获取图形验证码有效期10分钟") @ApiImplicitParams({@ApiImplicitParam(name = "deviceId", value = "设备id", required = true)})
@ApiImplicitParams({ @ApiImplicitParam(name = "deviceId", value = "设备id", required = true) }) @GetMapping("/imageCheckCode/{deviceId}")
@GetMapping("/imageCheckCode/{deviceId}") @GlobalInterceptor
@GlobalInterceptor public void imageCheckCode(HttpServletResponse response,
public void imageCheckCode(HttpServletResponse response, @VerifyParam(required = true) @PathVariable("deviceId") String deviceId) throws IOException {
@VerifyParam(required = true) @PathVariable("deviceId") String deviceId) throws IOException { CreateImageCode vCode = new CreateImageCode(130, 38, 5, 10);
CreateImageCode vCode = new CreateImageCode(130, 38, 5, 10); response.setHeader("Pragma", "no-cache");
response.setHeader("Pragma", "no-cache"); response.setHeader("Cache-Control", "no-cache");
response.setHeader("Cache-Control", "no-cache"); response.setDateHeader("Expires", 0);
response.setDateHeader("Expires", 0); response.setContentType("image/jpeg");
response.setContentType("image/jpeg"); String code = vCode.getCode();
String code = vCode.getCode(); String redisKey = Constants.REDIS_KEY_IMAGE_CHECK_CODE + deviceId;
String redisKey = Constants.REDIS_KEY_IMAGE_CHECK_CODE + deviceId; redisTemplate.opsForValue().set(redisKey, code, 10, TimeUnit.MINUTES);
redisTemplate.opsForValue().set(redisKey, code, 10, TimeUnit.MINUTES); vCode.write(response.getOutputStream());
vCode.write(response.getOutputStream()); }
}
/** /**
* 4.1Base64 * 4.1Base64
* @return *
*/ * @return
@ApiOperation(value = "4.1获取图形验证码Base64", notes = "4.三次短信验证失败后获取图形验证码Base64有效期10分钟") */
@ApiImplicitParams({ @ApiImplicitParam(name = "deviceId", value = "设备id", required = true) }) @ApiOperation(value = "4.1获取图形验证码Base64", notes = "4.三次短信验证失败后获取图形验证码Base64有效期10分钟")
@GetMapping("/imageCheckCodeBase64/{deviceId}") @ApiImplicitParams({@ApiImplicitParam(name = "deviceId", value = "设备id", required = true)})
@GlobalInterceptor @GetMapping("/imageCheckCodeBase64/{deviceId}")
public Result<String> imageCheckCodeBase64(HttpServletResponse response, @GlobalInterceptor
@VerifyParam(required = true) @PathVariable("deviceId") String deviceId) throws IOException { public Result<String> imageCheckCodeBase64(HttpServletResponse response,
CreateImageCode vCode = new CreateImageCode(130, 38, 5, 10); @VerifyParam(required = true) @PathVariable("deviceId") String deviceId) throws IOException {
response.setHeader("Pragma", "no-cache"); CreateImageCode vCode = new CreateImageCode(130, 38, 5, 10);
response.setHeader("Cache-Control", "no-cache"); response.setHeader("Pragma", "no-cache");
response.setDateHeader("Expires", 0); response.setHeader("Cache-Control", "no-cache");
response.setContentType("image/jpeg"); response.setDateHeader("Expires", 0);
String code = vCode.getCode(); response.setContentType("image/jpeg");
String redisKey = Constants.REDIS_KEY_IMAGE_CHECK_CODE + deviceId; String code = vCode.getCode();
redisTemplate.opsForValue().set(redisKey, code, 10, TimeUnit.MINUTES); String redisKey = Constants.REDIS_KEY_IMAGE_CHECK_CODE + deviceId;
redisTemplate.opsForValue().set(redisKey, code, 10, TimeUnit.MINUTES);
ByteArrayOutputStream bos = new ByteArrayOutputStream(); ByteArrayOutputStream bos = new ByteArrayOutputStream();
vCode.write(bos); vCode.write(bos);
return Result.success(Base64.getEncoder().encodeToString(bos.toByteArray())); return Result.success(Base64.getEncoder().encodeToString(bos.toByteArray()));
} }
/** /**
* *
*/ */
@ApiOperation(value = "5.获取支持的手机号国家码", notes = "v1仅支持'+86'") @ApiOperation(value = "5.获取支持的手机号国家码", notes = "v1仅支持'+86'")
@GetMapping("/supportedCountryCode") @GetMapping("/supportedCountryCode")
public Result<List<CountryCodeDTO>> getSupportedCountryCode() { public Result<List<CountryCodeDTO>> getSupportedCountryCode() {
return Result.success(DEFAULT_SUPPORTED_COUNTRY_CODES); return Result.success(DEFAULT_SUPPORTED_COUNTRY_CODES);
} }
/**
* 退 /**
* @param authorization * 退
* @return *
*/ * @param authorization
@ApiOperation(value = "6.退出登录") * @return
@PostMapping("/logout") */
@GlobalInterceptor(checkAppUserLogin = true) @ApiOperation(value = "6.退出登录")
public Result<Void> logout(@RequestHeader(value = "Authorization", required = true) String authorization){ @PostMapping("/logout")
return Result.success(); @GlobalInterceptor(checkAppUserLogin = true)
} public Result<Void> logout(@RequestHeader(value = "Authorization", required = true) String authorization) {
return Result.success();
/** }
* /
* /**
* @return * /
*/ *
@ApiOperation(value = "7.通过用户名/密码登录") * @return
@PostMapping("/login/username") */
public Result loginByUserName(@Validated @RequestBody UserNameLoginDto userNameLoginDto) { @ApiOperation(value = "7.通过用户名/密码登录")
String userName = userNameLoginDto.getUserName(); @PostMapping("/login/username")
String password = userNameLoginDto.getPassword(); public Result loginByUserName(@Validated @RequestBody UserNameLoginDto userNameLoginDto) {
UserInfo userInfo = userInfoService.findByUserNameAndPassword(userName, password); String userName = userNameLoginDto.getUserName();
if(ObjectUtils.notEqual(UserStatusEnum.ENABLE.getStatus(), userInfo.getStatus())){ String password = userNameLoginDto.getPassword();
// 用户状态错误 UserInfo userInfo = userInfoService.findByUserNameAndPassword(userName, password);
throw new BizException(ErrorConstants.USER_STATUS_ERROR); if (ObjectUtils.notEqual(UserStatusEnum.ENABLE.getStatus(), userInfo.getStatus())) {
} // 用户状态错误
if (userInfo !=null) { throw new BizException(ErrorConstants.USER_STATUS_ERROR);
//生成token }
String token = jwtUtil.createJWT(userInfo.getId(),userInfo.getUserName(),"artist",""); if (userInfo != null) {
Map<String, Object> map = new HashMap<>(); //生成token
map.put("token",token); String token = jwtUtil.createJWT(userInfo.getId(), userInfo.getUserName(), "artist", "");
map.put("roles","artist"); Map<String, Object> map = new HashMap<>();
map.put("name",userInfo.getUserName()); map.put("token", token);
return Result.success(map); map.put("roles", "artist");
} else { map.put("name", userInfo.getUserName());
return Result.failed(StatusCode.USER_NAME_OR_PASSWORD_FAILED); return Result.success(map);
} } else {
} return Result.failed(StatusCode.USER_NAME_OR_PASSWORD_FAILED);
}
@ApiOperation(value = "8.修改密码") }
@PostMapping("/changePassword")
public Result<Void> changePassword(@Validated @RequestBody UserNameLoginDto changePasswordDto) { @ApiOperation(value = "8.修改密码")
userInfoService.changePassword(changePasswordDto.getUserName(), changePasswordDto.getPassword()); @PostMapping("/changePassword")
return Result.success(); public Result<Void> changePassword(@Validated @RequestBody UserNameLoginDto changePasswordDto) {
} userInfoService.changePassword(changePasswordDto.getUserName(), changePasswordDto.getPassword());
return Result.success();
@ApiOperation(value = "9.1.获取UUID", notes = "获取UUID") }
@GetMapping(value = "/uuid")
public Result<String> getUuid() { @ApiOperation(value = "9.1.获取UUID", notes = "获取UUID")
String uuid = UUID.randomUUID().toString(); @GetMapping(value = "/uuid")
redisTemplate.opsForValue().set(uuid, 0, 60, TimeUnit.SECONDS); public Result<String> getUuid() {
return Result.success(uuid); String uuid = UUID.randomUUID().toString();
} redisTemplate.opsForValue().set(uuid, 0, 60, TimeUnit.SECONDS);
return Result.success(uuid);
@ApiOperation(value = "9.2.校验uuid对应的请求认证状态", notes = "0是未完成其余是已完成状态的token") }
@GetMapping(value = "/check/qr")
public Result<String> getCountryCode(String uuid) { @ApiOperation(value = "9.2.校验uuid对应的请求认证状态", notes = "0-未完成 1-已扫码 other-token")
return Result.success(redisTemplate.opsForValue().get(uuid).toString()); @GetMapping(value = "/check/qr")
} public Result<String> getCountryCode(String uuid) {
return Result.success(redisTemplate.opsForValue().get(uuid).toString());
@ApiOperation(value = "9.3.获取登录二维码、放入Token", notes = "前端获取二维码") }
@GetMapping(value = "/getLoginQr")
public void createCodeImg(HttpServletRequest request, HttpServletResponse response, String uuid){ @ApiOperation(value = "9.3.获取登录二维码、放入Token", notes = "前端获取二维码")
response.setHeader("Pragma", "No-cache"); @GetMapping(value = "/getLoginQr")
response.setHeader("Cache-Control", "no-cache"); public void createCodeImg(HttpServletRequest request, HttpServletResponse response, String uuid) {
response.setHeader("Pragma", "No-cache");
response.setDateHeader("Expires", 0); response.setHeader("Cache-Control", "no-cache");
response.setContentType("image/jpeg");
response.setDateHeader("Expires", 0);
try { response.setContentType("image/jpeg");
Object o = redisTemplate.opsForValue().get(uuid); try {
if (o == null) {
throw new BizException("二维码失效!"); Object o = redisTemplate.opsForValue().get(uuid);
} if (o == null) {
if(!String.valueOf(o).equals("0")) { throw new BizException("二维码失效!");
throw new BizException("登录认证已完成"); }
} if (!String.valueOf(o).equals("0")) {
throw new BizException("登录认证已完成");
response.setHeader("uuid", uuid); }
// 这里是开源工具类 hutool里的QrCodeUtil
// 网址http://hutool.mydoc.io/ response.setHeader("uuid", uuid);
QrCodeUtil.generate(uuid, 300, 300, "jpg",response.getOutputStream()); // 这里是开源工具类 hutool里的QrCodeUtil
// 网址http://hutool.mydoc.io/
} catch (Exception e) { QrCodeUtil.generate(uuid, 300, 300, "jpg", response.getOutputStream());
e.printStackTrace();
} } catch (Exception e) {
} e.printStackTrace();
}
@ApiOperation(value = "9.4.确认身份接口:确定身份以及判断是否二维码过期等", notes = "确认身份接口:确定身份以及判断是否二维码过期等") }
@PostMapping(value = "/bindUserIdAndToken")
public Result<String> bindUserIdAndToken(@RequestParam("token") String token , @ApiOperation(value = "9.4.确认身份接口:确定身份以及判断是否二维码过期等", notes = "确认身份接口:确定身份以及判断是否二维码过期等")
@RequestParam("userId") String userId){ @PostMapping(value = "/scanOrConfirm")
try { public Result<Void> scanOrConfirm(@RequestParam("token") String token,
return Result.success(userInfoService.bindUserIdAndToken(userId,token)); @RequestParam("userId") String userId,
} catch (Exception e) { @RequestParam("type") Integer type) {
throw new RuntimeException(e); try {
} userInfoService.scan(userId, token, type);
} return Result.success();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
} }

@ -710,7 +710,15 @@ public class UserInfoService {
userInfoDao.save(userInfoByUserName); userInfoDao.save(userInfoByUserName);
} }
public String bindUserIdAndToken(String userId, String token) throws Exception { /**
*
*
* @param userId id
* @param token uuid
* @param type 1- 2-
* @return token
*/
public void scan(String userId, String token, Integer type) throws Exception {
Object o = redisTemplate.opsForValue().get(token); Object o = redisTemplate.opsForValue().get(token);
if (o == null) { if (o == null) {
throw new BizException("二维码失效!"); throw new BizException("二维码失效!");
@ -719,9 +727,15 @@ public class UserInfoService {
throw new BizException("登录认证已完成"); throw new BizException("登录认证已完成");
} }
UserInfo loginUserInfo = userInfoDao.findById(userId).get(); if(type == 1) {
String authorization = loginOrRegister(loginUserInfo); // 扫码
redisTemplate.opsForValue().set(token, authorization, 60, TimeUnit.SECONDS); redisTemplate.opsForValue().set(token, 1, 60, TimeUnit.SECONDS);
return authorization; } else {
// 确认
UserInfo loginUserInfo = userInfoDao.findById(userId).get();
String authorization = loginOrRegister(loginUserInfo);
redisTemplate.opsForValue().set(token, authorization, 60, TimeUnit.SECONDS);
}
} }
} }

Loading…
Cancel
Save