From 3504afe58c3eb943fabd099946093fd8220a2be2 Mon Sep 17 00:00:00 2001 From: huangyw <1207046171@qq.com> Date: Tue, 30 Jul 2024 10:49:17 +0800 Subject: [PATCH] =?UTF-8?q?release:=20getLoginQr=E5=90=8E=E7=AB=AF?= =?UTF-8?q?=E4=B8=8D=E5=86=8D=E7=94=9F=E6=88=90=E4=BA=8C=E7=BB=B4=E7=A0=81?= =?UTF-8?q?=EF=BC=8C=E5=89=8D=E7=AB=AF=E4=B8=BB=E5=8A=A8=E7=94=9F=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../luoo/user/controller/LoginController.java | 614 +++++++++--------- 1 file changed, 311 insertions(+), 303 deletions(-) diff --git a/luoo_user/src/main/java/com/luoo/user/controller/LoginController.java b/luoo_user/src/main/java/com/luoo/user/controller/LoginController.java index 582cd7e..30f2f2a 100644 --- a/luoo_user/src/main/java/com/luoo/user/controller/LoginController.java +++ b/luoo_user/src/main/java/com/luoo/user/controller/LoginController.java @@ -24,9 +24,11 @@ import io.swagger.annotations.Api; import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiOperation; +import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang.ObjectUtils; import org.apache.commons.lang.StringUtils; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; @@ -47,157 +49,166 @@ import java.util.concurrent.TimeUnit; */ @RestController @CrossOrigin +@Slf4j @RequestMapping("/user") @Api(tags = "LoginController") public class LoginController extends BaseController { - private static final List DEFAULT_SUPPORTED_COUNTRY_CODES = Arrays - .asList(new CountryCodeDTO("中国", "+86")); - @Autowired - private UserInfoService userInfoService; - - @Autowired - private RedisTemplate redisTemplate; - - @Autowired - private WeChatAouth2Service weChatAouth2Service; - - @Autowired - private JwtUtil jwtUtil; - - /** - * 更新好友粉丝数和用户关注数 - * - * @param - * @return - */ - @PutMapping("/{userid}/{friendid}/{x}") - public void updatefanscountandfollowcount(@PathVariable String userid, @PathVariable String friendid, - @PathVariable int x) { - userInfoService.updatefanscountandfollowcount(x, userid, friendid); - } - /** - * 发送短信验证码 - */ - @ApiOperation(value = "1.发送短信验证码", notes = "有效期15分钟,一个手机号一天内最多发送20个请求") - @PostMapping("/sendsms") - @GlobalInterceptor(frequencyType = RequestFrequencyTypeEnum.DAY, requestFrequencyThreshold = 20) - public Result sendSms(@VerifyParam SendSmsReq sendSmsReq) { - if (sendSmsReq.getMobile().equals("18812345678")) { - /** - * 测试用手机号 - */ - return Result.success(); - } - if (!StringTools.isEmpty(sendSmsReq.getImageCheckCode())) { - // 得到缓存中的验证码 - String redisImageCheckCodeKey = Constants.REDIS_KEY_IMAGE_CHECK_CODE + sendSmsReq.getDeviceId(); - String redisImageCheckCode = (String) redisTemplate.opsForValue().get(redisImageCheckCodeKey); - if (StringTools.isEmpty(redisImageCheckCode)) { - return Result.failed(StatusCode.USER_IMAGE_VERIFICATION_CODE_EXPIRED); - } - if (!redisImageCheckCode.equalsIgnoreCase(sendSmsReq.getImageCheckCode())) { - return Result.failed(StatusCode.USER_IMAGE_VERIFICATION_CODE_MISMATCH); - } - redisTemplate.delete(redisImageCheckCodeKey); - } - - userInfoService.sendSms(sendSmsReq.getDeviceId(), sendSmsReq.getMobile()); - return Result.success(); + private static final List DEFAULT_SUPPORTED_COUNTRY_CODES = Arrays + .asList(new CountryCodeDTO("中国", "+86")); + @Autowired + private UserInfoService userInfoService; + + @Autowired + private RedisTemplate redisTemplate; + + @Autowired + private WeChatAouth2Service weChatAouth2Service; + + @Autowired + private JwtUtil jwtUtil; + + @Value("${download.url}") + private String downloadUrl; + + /** + * 更新好友粉丝数和用户关注数 + * + * @param + * @return + */ + @PutMapping("/{userid}/{friendid}/{x}") + public void updatefanscountandfollowcount(@PathVariable String userid, + @PathVariable String friendid, + @PathVariable int x) { + userInfoService.updatefanscountandfollowcount(x, userid, friendid); + } + + /** + * 发送短信验证码 + */ + @ApiOperation(value = "1.发送短信验证码", notes = "有效期15分钟,一个手机号一天内最多发送20个请求") + @PostMapping("/sendsms") + @GlobalInterceptor(frequencyType = RequestFrequencyTypeEnum.DAY, requestFrequencyThreshold = 20) + public Result sendSms(@VerifyParam SendSmsReq sendSmsReq) { + if (sendSmsReq.getMobile().equals("18812345678")) { + /** + * 测试用手机号 + */ + return Result.success(); } - - @ApiOperation(value = "2.登录/注册", notes = "成功后返回authorization") - @PostMapping("/appLogin") - @GlobalInterceptor(frequencyType = RequestFrequencyTypeEnum.HOUR, requestFrequencyThreshold = 12) - public Result appLogin(HttpServletRequest request, - @VerifyParam LoginReq loginReq) { - if ("18812345678".equals(loginReq.getMobile())) { - UserInfo loginUserInfo = new UserInfo(); - loginUserInfo.setMobile(loginReq.getMobile()); - loginUserInfo.setLastUseDeviceId(loginReq.getDeviceId()); - loginUserInfo.setLastUseDeviceBrand(loginReq.getDeviceBrand()); - loginUserInfo.setLastLoginIp(getIpAddr(request)); - String token = userInfoService.loginOrRegister(loginUserInfo); - return Result.success(token); - } - // 得到缓存中的验证码 - String redisMobileCheckCodeKey = Constants.REDIS_KEY_MOBILE_CHECK_CODE + loginReq.getDeviceId(); - String redisMobileCheckCode = (String) redisTemplate.opsForValue().get(redisMobileCheckCodeKey); - if (StringTools.isEmpty(redisMobileCheckCode)) { - return Result.failed(StatusCode.USER_MOBILE_VERIFICATION_CODE_EXPIRED); - } - if (!redisMobileCheckCode.equals(loginReq.getMobileCheckCode())) { - return Result.failed(StatusCode.USER_MOBILE_VERIFICATION_CODE_MISMATCH); - } - UserInfo loginUserInfo = new UserInfo(); - loginUserInfo.setMobile(loginReq.getMobile()); - loginUserInfo.setLastUseDeviceId(loginReq.getDeviceId()); - loginUserInfo.setLastUseDeviceBrand(loginReq.getDeviceBrand()); - loginUserInfo.setLastLoginIp(getIpAddr(request)); - String token = userInfoService.loginOrRegister(loginUserInfo); - redisTemplate.delete(redisMobileCheckCodeKey); - return Result.success(token); + if (!StringTools.isEmpty(sendSmsReq.getImageCheckCode())) { + // 得到缓存中的验证码 + String redisImageCheckCodeKey = + Constants.REDIS_KEY_IMAGE_CHECK_CODE + sendSmsReq.getDeviceId(); + String redisImageCheckCode = (String) redisTemplate.opsForValue().get(redisImageCheckCodeKey); + if (StringTools.isEmpty(redisImageCheckCode)) { + return Result.failed(StatusCode.USER_IMAGE_VERIFICATION_CODE_EXPIRED); + } + if (!redisImageCheckCode.equalsIgnoreCase(sendSmsReq.getImageCheckCode())) { + return Result.failed(StatusCode.USER_IMAGE_VERIFICATION_CODE_MISMATCH); + } + redisTemplate.delete(redisImageCheckCodeKey); } - @ApiOperation(value = "2.1 appleId登录/注册", notes = "成功后返回authorization") - @PostMapping("/appleIdLogin") - public Result appleIdLogin(HttpServletRequest request, @VerifyParam AppleLoginReq appleLoginReq) { - - - // 无手机号和验证码 - String identityToken = appleLoginReq.getIdentityToken(); - // 解码后的消息体 - JSONObject playloadObj = IOSTokenUtils.parserIdentityToken(identityToken); - Boolean success; - try { - success = IOSTokenUtils.verifyExc(identityToken, playloadObj); - } catch (Exception e) { - return Result.failed(StatusCode.APPLE_lOGIN_FAILED); + userInfoService.sendSms(sendSmsReq.getDeviceId(), sendSmsReq.getMobile()); + return Result.success(); + } + + @ApiOperation(value = "2.登录/注册", notes = "成功后返回authorization") + @PostMapping("/appLogin") + @GlobalInterceptor(frequencyType = RequestFrequencyTypeEnum.HOUR, requestFrequencyThreshold = 12) + public Result appLogin(HttpServletRequest request, + @VerifyParam LoginReq loginReq) { + if ("18812345678".equals(loginReq.getMobile())) { + UserInfo loginUserInfo = new UserInfo(); + loginUserInfo.setMobile(loginReq.getMobile()); + loginUserInfo.setLastUseDeviceId(loginReq.getDeviceId()); + loginUserInfo.setLastUseDeviceBrand(loginReq.getDeviceBrand()); + loginUserInfo.setLastLoginIp(getIpAddr(request)); + String token = userInfoService.loginOrRegister(loginUserInfo); + return Result.success(token); + } + // 得到缓存中的验证码 + String redisMobileCheckCodeKey = Constants.REDIS_KEY_MOBILE_CHECK_CODE + loginReq.getDeviceId(); + String redisMobileCheckCode = (String) redisTemplate.opsForValue().get(redisMobileCheckCodeKey); + if (StringTools.isEmpty(redisMobileCheckCode)) { + return Result.failed(StatusCode.USER_MOBILE_VERIFICATION_CODE_EXPIRED); + } + if (!redisMobileCheckCode.equals(loginReq.getMobileCheckCode())) { + return Result.failed(StatusCode.USER_MOBILE_VERIFICATION_CODE_MISMATCH); + } + UserInfo loginUserInfo = new UserInfo(); + loginUserInfo.setMobile(loginReq.getMobile()); + loginUserInfo.setLastUseDeviceId(loginReq.getDeviceId()); + loginUserInfo.setLastUseDeviceBrand(loginReq.getDeviceBrand()); + loginUserInfo.setLastLoginIp(getIpAddr(request)); + String token = userInfoService.loginOrRegister(loginUserInfo); + redisTemplate.delete(redisMobileCheckCodeKey); + return Result.success(token); + } + + @ApiOperation(value = "2.1 appleId登录/注册", notes = "成功后返回authorization") + @PostMapping("/appleIdLogin") + public Result appleIdLogin(HttpServletRequest request, + @VerifyParam AppleLoginReq appleLoginReq) { + + // 无手机号和验证码 + String identityToken = appleLoginReq.getIdentityToken(); + // 解码后的消息体 + JSONObject playloadObj = IOSTokenUtils.parserIdentityToken(identityToken); + Boolean success; + try { + success = IOSTokenUtils.verifyExc(identityToken, playloadObj); + } catch (Exception e) { + return Result.failed(StatusCode.APPLE_lOGIN_FAILED); // throw new RuntimeException(e); - } + } - if (!success) { - // TODO 校验token失败具体操作 - return Result.failed(StatusCode.APPLE_lOGIN_FAILED); - } - UserInfo loginUserInfo = new UserInfo(); + if (!success) { + // TODO 校验token失败具体操作 + return Result.failed(StatusCode.APPLE_lOGIN_FAILED); + } + UserInfo loginUserInfo = new UserInfo(); // loginUserInfo.setMobile(appleLoginReq.getMobile()); - loginUserInfo.setAppleId(appleLoginReq.getUserIdentifier()); - loginUserInfo.setAppleEmail(playloadObj.get("email") + ""); - loginUserInfo.setLastLoginIp(getIpAddr(request)); - loginUserInfo.setLastUseDeviceId(appleLoginReq.getDeviceId()); - loginUserInfo.setLastUseDeviceBrand(appleLoginReq.getDeviceBrand()); - String token = userInfoService.appleLoginOrRegister(loginUserInfo); - - - return Result.success(token); - - + loginUserInfo.setAppleId(appleLoginReq.getUserIdentifier()); + loginUserInfo.setAppleEmail(playloadObj.get("email") + ""); + loginUserInfo.setLastLoginIp(getIpAddr(request)); + loginUserInfo.setLastUseDeviceId(appleLoginReq.getDeviceId()); + loginUserInfo.setLastUseDeviceBrand(appleLoginReq.getDeviceBrand()); + String token = userInfoService.appleLoginOrRegister(loginUserInfo); + + return Result.success(token); + + + } + + @ApiOperation(value = "2.2 微信wxId登录/注册", notes = "成功后返回authorization") + @PostMapping("/wxIdLogin") + public Result wxIdLogin(HttpServletRequest request, + @VerifyParam WeChatLoginReq weChatLoginReq) { + + // 如果有手机号,是首次微信登录时绑定流程 + if (StringUtils.isNotEmpty(weChatLoginReq.getMobile())) { + //得到缓存中的验证码 + 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)); } - @ApiOperation(value = "2.2 微信wxId登录/注册", notes = "成功后返回authorization") - @PostMapping("/wxIdLogin") - public Result wxIdLogin(HttpServletRequest request, @VerifyParam WeChatLoginReq weChatLoginReq) { - - // 如果有手机号,是首次微信登录时绑定流程 - if (StringUtils.isNotEmpty(weChatLoginReq.getMobile())) { - //得到缓存中的验证码 - 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)); - // 无手机号,非第一次微信登录场景,需要判断当前wxID在数据库中是否有记录,如果有记录登录正常执行,如果无记录,返回状态到上面绑定手机号流程 - return weChatAouth2Service.login(weChatLoginReq, getIpAddr(request)); - - } + } // @ApiOperation(value = "2.1 appleId登录/注册", notes = "成功后返回authorization") // @PostMapping("/appleIdLogin") // @GlobalInterceptor(frequencyType = RequestFrequencyTypeEnum.HOUR, requestFrequencyThreshold = 12) @@ -285,176 +296,173 @@ public class LoginController extends BaseController { // } - /** - * authorization 续期 - */ - @ApiOperation(value = "3.token续期", notes = "有效期7天") - @PostMapping("/autoLogin") - @GlobalInterceptor - public Result autoLogin(HttpServletRequest request, - @RequestHeader(value = "Authorization", required = false) String authorization, - @VerifyParam TokenRefreshReq tokenRefreshReq) { - return Result.success(userInfoService.autoLogin(authorization, tokenRefreshReq.getDeviceId(), tokenRefreshReq.getDeviceBrand(), getIpAddr(request))); + /** + * authorization 续期 + */ + @ApiOperation(value = "3.token续期", notes = "有效期7天") + @PostMapping("/autoLogin") + @GlobalInterceptor + public Result autoLogin(HttpServletRequest request, + @RequestHeader(value = "Authorization", required = false) String authorization, + @VerifyParam TokenRefreshReq tokenRefreshReq) { + return Result.success(userInfoService.autoLogin(authorization, tokenRefreshReq.getDeviceId(), + tokenRefreshReq.getDeviceBrand(), getIpAddr(request))); + } + + /** + * 获取图行验证码 + */ + @ApiOperation(value = "4.获取图形验证码", notes = "4.三次短信验证失败后,获取图形验证码,有效期10分钟") + @ApiImplicitParams({@ApiImplicitParam(name = "deviceId", value = "设备id", required = true)}) + @GetMapping("/imageCheckCode/{deviceId}") + @GlobalInterceptor + public void imageCheckCode(HttpServletResponse response, + @VerifyParam(required = true) @PathVariable("deviceId") String deviceId) throws IOException { + CreateImageCode vCode = new CreateImageCode(130, 38, 5, 10); + response.setHeader("Pragma", "no-cache"); + response.setHeader("Cache-Control", "no-cache"); + response.setDateHeader("Expires", 0); + response.setContentType("image/jpeg"); + String code = vCode.getCode(); + String redisKey = Constants.REDIS_KEY_IMAGE_CHECK_CODE + deviceId; + redisTemplate.opsForValue().set(redisKey, code, 10, TimeUnit.MINUTES); + vCode.write(response.getOutputStream()); + } + + /** + * 4.1获取图形验证码Base64 + * + * @return + */ + @ApiOperation(value = "4.1获取图形验证码Base64", notes = "4.三次短信验证失败后,获取图形验证码Base64,有效期10分钟") + @ApiImplicitParams({@ApiImplicitParam(name = "deviceId", value = "设备id", required = true)}) + @GetMapping("/imageCheckCodeBase64/{deviceId}") + @GlobalInterceptor + public Result imageCheckCodeBase64(HttpServletResponse response, + @VerifyParam(required = true) @PathVariable("deviceId") String deviceId) throws IOException { + CreateImageCode vCode = new CreateImageCode(130, 38, 5, 10); + response.setHeader("Pragma", "no-cache"); + response.setHeader("Cache-Control", "no-cache"); + response.setDateHeader("Expires", 0); + response.setContentType("image/jpeg"); + String code = vCode.getCode(); + String redisKey = Constants.REDIS_KEY_IMAGE_CHECK_CODE + deviceId; + redisTemplate.opsForValue().set(redisKey, code, 10, TimeUnit.MINUTES); + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + vCode.write(bos); + return Result.success(Base64.getEncoder().encodeToString(bos.toByteArray())); + } + + /** + * 获取支持的手机号国家码 + */ + @ApiOperation(value = "5.获取支持的手机号国家码", notes = "v1仅支持'+86'") + @GetMapping("/supportedCountryCode") + public Result> getSupportedCountryCode() { + return Result.success(DEFAULT_SUPPORTED_COUNTRY_CODES); + } + + /** + * 退出登录 + * + * @param authorization + * @return + */ + @ApiOperation(value = "6.退出登录") + @PostMapping("/logout") + @GlobalInterceptor(checkAppUserLogin = true) + public Result logout( + @RequestHeader(value = "Authorization", required = true) String authorization) { + return Result.success(); + } + + /** + * 通过用户名/密码登录 + * + * @return 登录结果 + */ + @ApiOperation(value = "7.通过用户名/密码登录") + @PostMapping("/login/username") + public Result loginByUserName(@Validated @RequestBody UserNameLoginDto userNameLoginDto) { + String userName = userNameLoginDto.getUserName(); + String password = userNameLoginDto.getPassword(); + UserInfo userInfo = userInfoService.findByUserNameAndPassword(userName, password); + if (ObjectUtils.notEqual(UserStatusEnum.ENABLE.getStatus(), userInfo.getStatus())) { + // 用户状态错误 + throw new BizException(ErrorConstants.USER_STATUS_ERROR); } - - /** - * 获取图行验证码 - */ - @ApiOperation(value = "4.获取图形验证码", notes = "4.三次短信验证失败后,获取图形验证码,有效期10分钟") - @ApiImplicitParams({@ApiImplicitParam(name = "deviceId", value = "设备id", required = true)}) - @GetMapping("/imageCheckCode/{deviceId}") - @GlobalInterceptor - public void imageCheckCode(HttpServletResponse response, - @VerifyParam(required = true) @PathVariable("deviceId") String deviceId) throws IOException { - CreateImageCode vCode = new CreateImageCode(130, 38, 5, 10); - response.setHeader("Pragma", "no-cache"); - response.setHeader("Cache-Control", "no-cache"); - response.setDateHeader("Expires", 0); - response.setContentType("image/jpeg"); - String code = vCode.getCode(); - String redisKey = Constants.REDIS_KEY_IMAGE_CHECK_CODE + deviceId; - redisTemplate.opsForValue().set(redisKey, code, 10, TimeUnit.MINUTES); - vCode.write(response.getOutputStream()); - } - - /** - * 4.1获取图形验证码Base64 - * - * @return - */ - @ApiOperation(value = "4.1获取图形验证码Base64", notes = "4.三次短信验证失败后,获取图形验证码Base64,有效期10分钟") - @ApiImplicitParams({@ApiImplicitParam(name = "deviceId", value = "设备id", required = true)}) - @GetMapping("/imageCheckCodeBase64/{deviceId}") - @GlobalInterceptor - public Result imageCheckCodeBase64(HttpServletResponse response, - @VerifyParam(required = true) @PathVariable("deviceId") String deviceId) throws IOException { - CreateImageCode vCode = new CreateImageCode(130, 38, 5, 10); - response.setHeader("Pragma", "no-cache"); - response.setHeader("Cache-Control", "no-cache"); - response.setDateHeader("Expires", 0); - response.setContentType("image/jpeg"); - String code = vCode.getCode(); - String redisKey = Constants.REDIS_KEY_IMAGE_CHECK_CODE + deviceId; - redisTemplate.opsForValue().set(redisKey, code, 10, TimeUnit.MINUTES); - ByteArrayOutputStream bos = new ByteArrayOutputStream(); - vCode.write(bos); - return Result.success(Base64.getEncoder().encodeToString(bos.toByteArray())); - } - - /** - * 获取支持的手机号国家码 - */ - @ApiOperation(value = "5.获取支持的手机号国家码", notes = "v1仅支持'+86'") - @GetMapping("/supportedCountryCode") - public Result> getSupportedCountryCode() { - return Result.success(DEFAULT_SUPPORTED_COUNTRY_CODES); - } - - /** - * 退出登录 - * - * @param authorization - * @return - */ - @ApiOperation(value = "6.退出登录") - @PostMapping("/logout") - @GlobalInterceptor(checkAppUserLogin = true) - public Result logout(@RequestHeader(value = "Authorization", required = true) String authorization) { - return Result.success(); - } - - /** - * 通过用户名/密码登录 - * - * @return 登录结果 - */ - @ApiOperation(value = "7.通过用户名/密码登录") - @PostMapping("/login/username") - public Result loginByUserName(@Validated @RequestBody UserNameLoginDto userNameLoginDto) { - String userName = userNameLoginDto.getUserName(); - String password = userNameLoginDto.getPassword(); - UserInfo userInfo = userInfoService.findByUserNameAndPassword(userName, password); - if (ObjectUtils.notEqual(UserStatusEnum.ENABLE.getStatus(), userInfo.getStatus())) { - // 用户状态错误 - throw new BizException(ErrorConstants.USER_STATUS_ERROR); - } - if (userInfo != null) { - //生成token - String token = jwtUtil.createJWT(userInfo.getId(), userInfo.getUserName(), "artist", ""); - Map map = new HashMap<>(); - map.put("token", token); - map.put("roles", "artist"); - map.put("name", userInfo.getUserName()); - return Result.success(map); - } else { - return Result.failed(StatusCode.USER_NAME_OR_PASSWORD_FAILED); - } - } - - @ApiOperation(value = "8.修改密码") - @PostMapping("/changePassword") - public Result 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 getUuid() { - String uuid = UUID.randomUUID().toString(); - redisTemplate.opsForValue().set(uuid, 0, 60, TimeUnit.SECONDS); - return Result.success(uuid); - } - - @ApiOperation(value = "9.2.校验uuid对应的请求认证状态", notes = "0-未完成 1-已扫码 other-token") - @GetMapping(value = "/check/qr") - public Result getCountryCode(String uuid) { - return Result.success(redisTemplate.opsForValue().get(uuid).toString()); + if (userInfo != null) { + //生成token + String token = jwtUtil.createJWT(userInfo.getId(), userInfo.getUserName(), "artist", ""); + Map map = new HashMap<>(); + map.put("token", token); + map.put("roles", "artist"); + map.put("name", userInfo.getUserName()); + return Result.success(map); + } else { + return Result.failed(StatusCode.USER_NAME_OR_PASSWORD_FAILED); } - - @ApiOperation(value = "9.3.获取登录二维码、放入Token", notes = "前端获取二维码") - @GetMapping(value = "/getLoginQr") - public void createCodeImg(HttpServletRequest request, HttpServletResponse response, String uuid) { - response.setHeader("Pragma", "No-cache"); - response.setHeader("Cache-Control", "no-cache"); - - response.setDateHeader("Expires", 0); - response.setContentType("image/jpeg"); - - try { - - Object o = redisTemplate.opsForValue().get(uuid); - if (o == null) { - throw new BizException("二维码失效!"); - } - if (!String.valueOf(o).equals("0")) { - throw new BizException("登录认证已完成"); - } - - response.setHeader("uuid", uuid); - // 这里是开源工具类 hutool里的QrCodeUtil - // 网址:http://hutool.mydoc.io/ - QrCodeUtil.generate(uuid, 300, 300, "jpg", response.getOutputStream()); - - } catch (Exception e) { - e.printStackTrace(); - } + } + + @ApiOperation(value = "8.修改密码") + @PostMapping("/changePassword") + public Result 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 getUuid() { + String uuid = UUID.randomUUID().toString(); + redisTemplate.opsForValue().set(uuid, 0, 60, TimeUnit.SECONDS); + return Result.success(uuid); + } + + @ApiOperation(value = "9.2.校验uuid对应的请求认证状态", notes = "0-未完成 1-已扫码 other-token") + @GetMapping(value = "/check/qr") + public Result getCountryCode(String uuid) { + return Result.success(redisTemplate.opsForValue().get(uuid).toString()); + } + + @ApiOperation(value = "9.3.获取登录二维码、放入Token", notes = "前端获取二维码") + @GetMapping(value = "/getLoginQr") + public Result createCodeImg(HttpServletRequest request, HttpServletResponse response, + String uuid) { + + try { + + Object o = redisTemplate.opsForValue().get(uuid); + if (o == null) { + throw new BizException("二维码失效!"); + } + if (!String.valueOf(o).equals("0")) { + throw new BizException("登录认证已完成"); + } + + } catch (Exception e) { + e.printStackTrace(); } - @ApiOperation(value = "9.4.确认身份接口:确定身份以及判断是否二维码过期等", notes = "确认身份接口:确定身份以及判断是否二维码过期等") - @PostMapping(value = "/scanOrConfirm") - @ApiImplicitParams({@ApiImplicitParam(name = "token", value = "扫码获得的code", required = true), - @ApiImplicitParam(name = "userId", value = "用户id", required = true), - @ApiImplicitParam(name = "type", value = "type 1-扫码 2-确认", required = true)}) - public Result scanOrConfirm(@RequestParam("token") String token, - @RequestParam("userId") String userId, - @RequestParam("type") Integer type) { - try { - userInfoService.scan(userId, token, type); - return Result.success(); - } catch (Exception e) { - throw new RuntimeException(e); - } + log.info(downloadUrl); + return Result.success(uuid); + } + + @ApiOperation(value = "9.4.确认身份接口:确定身份以及判断是否二维码过期等", notes = "确认身份接口:确定身份以及判断是否二维码过期等") + @PostMapping(value = "/scanOrConfirm") + @ApiImplicitParams({@ApiImplicitParam(name = "token", value = "扫码获得的code", required = true), + @ApiImplicitParam(name = "userId", value = "用户id", required = true), + @ApiImplicitParam(name = "type", value = "type 1-扫码 2-确认", required = true)}) + public Result scanOrConfirm(@RequestParam("token") String token, + @RequestParam("userId") String userId, + @RequestParam("type") Integer type) { + try { + userInfoService.scan(userId, token, type); + return Result.success(); + } catch (Exception e) { + throw new RuntimeException(e); } + } }