From 6fa8d539549cd8d417876a6299ba6bd972b720ee Mon Sep 17 00:00:00 2001 From: wangqing <408564126@qq.com> Date: Tue, 26 Mar 2024 13:08:50 +0800 Subject: [PATCH] feat:appleId login && logOff --- luoo_common/src/main/java/api/StatusCode.java | 4 +- .../luoo/user/controller/LoginController.java | 152 +++++++++++++++--- .../luoo/user/controller/MyController.java | 43 +++++ .../luoo/user/dto/request/AppleLoginReq.java | 6 + .../com/luoo/user/dto/request/LogoffReq.java | 35 ++++ .../luoo/user/service/UserInfoService.java | 3 + 6 files changed, 220 insertions(+), 23 deletions(-) create mode 100644 luoo_user/src/main/java/com/luoo/user/dto/request/LogoffReq.java diff --git a/luoo_common/src/main/java/api/StatusCode.java b/luoo_common/src/main/java/api/StatusCode.java index a09598e..cd4487b 100644 --- a/luoo_common/src/main/java/api/StatusCode.java +++ b/luoo_common/src/main/java/api/StatusCode.java @@ -19,7 +19,9 @@ public enum StatusCode implements IErrorCode { USER_NICK_NAME_HAS_BEEN_EXIST(10006, "昵称已经存在"), USER_INVALID_USER_ID(10007, "无效用户id"), USER_SENSITIVE_INFO(10008, "不能提交敏感信息"), - APPLE_lOGIN_FAILED(10008, "apple id登录失败"), + APPLE_lOGIN_FAILED(10009, "apple id登录失败"), + + APPLEID_MOBILE_UNBINDED(10010,"请先绑定appleid和手机号"), // music 模块错误码以20XXX,不足5位补0; 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 52be0e7..bcb3f1d 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 @@ -14,6 +14,7 @@ import javax.servlet.http.HttpServletResponse; import com.alibaba.fastjson.JSONObject; import com.luoo.user.dto.request.AppleLoginReq; import com.luoo.user.util.IOSTokenUtils; +import org.apache.commons.lang.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.web.bind.annotation.*; @@ -82,6 +83,12 @@ public class LoginController extends BaseController { @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(); @@ -104,6 +111,15 @@ public class LoginController extends BaseController { @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); @@ -122,35 +138,127 @@ public class LoginController extends BaseController { redisTemplate.delete(redisMobileCheckCodeKey); return Result.success(token); } - @ApiOperation(value = "2.1 appleId登录/注册", notes = "成功后返回authorization") @PostMapping("/appleIdLogin") @GlobalInterceptor(frequencyType = RequestFrequencyTypeEnum.HOUR, requestFrequencyThreshold = 12) - public Result appleIdLogin(HttpServletRequest request,@RequestBody 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); + 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(); +// loginUserInfo.setMobile(appleLoginReq.getMobile()); + loginUserInfo.setAppleId(appleLoginReq.getUserIdentifier()); + loginUserInfo.setLastLoginIp(getIpAddr(request)); + loginUserInfo.setLastUseDeviceId(appleLoginReq.getDeviceId()); + loginUserInfo.setLastUseDeviceBrand(appleLoginReq.getDeviceBrand()); + String token = userInfoService.appleLoginOrRegister(loginUserInfo); + + + return Result.success(token); + - if (!success) { - // TODO 校验token失败具体操作 - return Result.failed(StatusCode.APPLE_lOGIN_FAILED); - } - UserInfo loginUserInfo = new UserInfo(); - loginUserInfo.setLastLoginIp(getIpAddr(request)); - loginUserInfo.setLastUseDeviceId(appleLoginReq.getDeviceId()); - loginUserInfo.setLastUseDeviceBrand("iPhone"); - String token = userInfoService.appleLoginOrRegister(loginUserInfo); - return Result.success(token); } +// @ApiOperation(value = "2.1 appleId登录/注册", notes = "成功后返回authorization") +// @PostMapping("/appleIdLogin") +// @GlobalInterceptor(frequencyType = RequestFrequencyTypeEnum.HOUR, requestFrequencyThreshold = 12) +// public Result appleIdLogin(HttpServletRequest request,@VerifyParam AppleLoginReq appleLoginReq) { +// +// if("18812345678".equals(appleLoginReq.getMobile())) { +// UserInfo loginUserInfo = new UserInfo(); +// loginUserInfo.setMobile(appleLoginReq.getMobile()); +// loginUserInfo.setLastUseDeviceId(appleLoginReq.getDeviceId()); +// loginUserInfo.setLastUseDeviceBrand(appleLoginReq.getDeviceBrand()); +// loginUserInfo.setLastLoginIp(getIpAddr(request)); +// String token = userInfoService.loginOrRegister(loginUserInfo); +// return Result.success(token); +// } +// +// // 如果有手机号和验证码 +// if(StringUtils.isNotEmpty(appleLoginReq.getMobile()) && StringUtils.isNotEmpty(appleLoginReq.getMobileCheckCode())) { +// // 得到缓存中的验证码 +// String redisMobileCheckCodeKey = Constants.REDIS_KEY_MOBILE_CHECK_CODE + appleLoginReq.getDeviceId(); +// String redisMobileCheckCode = (String) redisTemplate.opsForValue().get(redisMobileCheckCodeKey); +// if (StringTools.isEmpty(redisMobileCheckCode)) { +// return Result.failed(StatusCode.USER_MOBILE_VERIFICATION_CODE_EXPIRED); +// } +// if (!redisMobileCheckCode.equals(appleLoginReq.getMobileCheckCode())) { +// return Result.failed(StatusCode.USER_MOBILE_VERIFICATION_CODE_MISMATCH); +// } +// 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(); +// loginUserInfo.setMobile(appleLoginReq.getMobile()); +// loginUserInfo.setAppleId(appleLoginReq.getUserIdentifier()); +// loginUserInfo.setLastLoginIp(getIpAddr(request)); +// loginUserInfo.setLastUseDeviceId(appleLoginReq.getDeviceId()); +// loginUserInfo.setLastUseDeviceBrand(appleLoginReq.getDeviceBrand()); +// String token = userInfoService.loginOrRegister(loginUserInfo); +// redisTemplate.delete(redisMobileCheckCodeKey); +// +// return Result.success(token); +// } else { +// +// // 无手机号和验证码 +// 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(); +// loginUserInfo.setMobile(appleLoginReq.getMobile()); +// loginUserInfo.setAppleId(appleLoginReq.getUserIdentifier()); +// loginUserInfo.setLastLoginIp(getIpAddr(request)); +// loginUserInfo.setLastUseDeviceId(appleLoginReq.getDeviceId()); +// loginUserInfo.setLastUseDeviceBrand(appleLoginReq.getDeviceBrand()); +// String token = userInfoService.appleLoginOrRegister(loginUserInfo); +// +// if("未注册".equals(token)) { +// return Result.failed(StatusCode.APPLEID_MOBILE_UNBINDED); +// } +// return Result.success(token); +// } +// +// +// } diff --git a/luoo_user/src/main/java/com/luoo/user/controller/MyController.java b/luoo_user/src/main/java/com/luoo/user/controller/MyController.java index 6af9403..b0528d4 100644 --- a/luoo_user/src/main/java/com/luoo/user/controller/MyController.java +++ b/luoo_user/src/main/java/com/luoo/user/controller/MyController.java @@ -15,6 +15,8 @@ import java.util.stream.Collectors; import com.github.houbb.sensitive.word.bs.SensitiveWordBs; import com.luoo.user.dao.UserMessageDao; import com.luoo.user.dto.TotalCommentVo; +import com.luoo.user.dto.request.LoginReq; +import com.luoo.user.dto.request.LogoffReq; import com.luoo.user.pojo.*; import com.luoo.user.service.*; import org.apache.commons.lang.StringUtils; @@ -443,6 +445,47 @@ public class MyController extends BaseController { return thanks; } + // 查询我收到的评论 + @ApiOperation(value = "12.账户与安全", notes = "账户与安全") + @GetMapping("/accountSafety") + public Result getAccountSafety(@RequestHeader(value = "Authorization", required = true) String authorization) { + //验证是否登录,并且拿到ID + UserLoginDto userLoginDto = getUserLoginDto(authorization); + if (null == userLoginDto) { + return Result.unauthorized(null); + } + String userId = userLoginDto.getUserId(); + UserInfo userInfo = userInfoService.findById(userId); + return Result.success(userInfo); + } + + + @ApiOperation(value = "12.1 注销账号", notes = "注销账号") + @PostMapping ("/logOff") + public Result logOff(@RequestHeader(value = "Authorization", required = true) String authorization, @VerifyParam LogoffReq logoffReq) { + //验证是否登录,并且拿到ID + UserLoginDto userLoginDto = getUserLoginDto(authorization); + if (null == userLoginDto) { + return Result.unauthorized(null); + } + String userId = userLoginDto.getUserId(); + // 得到缓存中的验证码 + String redisMobileCheckCodeKey = Constants.REDIS_KEY_MOBILE_CHECK_CODE + logoffReq.getDeviceId(); + String redisMobileCheckCode = (String) redisTemplate.opsForValue().get(redisMobileCheckCodeKey); + if (StringTools.isEmpty(redisMobileCheckCode)) { + return Result.failed(StatusCode.USER_MOBILE_VERIFICATION_CODE_EXPIRED); + } + if (!redisMobileCheckCode.equals(logoffReq.getMobileCheckCode())) { + return Result.failed(StatusCode.USER_MOBILE_VERIFICATION_CODE_MISMATCH); + } + + // 删除账号 + userInfoService.deleteById(userId); + return Result.success(); + } + + + private Integer getIndex(UserInfo u) { return Integer.valueOf(Arrays.stream(u.getBadges().split(",")).filter(s->s.startsWith(String.valueOf(UserBadgeEnum.CONTRIBUTOR.getCode()))).findFirst().get().split("_")[1]); } diff --git a/luoo_user/src/main/java/com/luoo/user/dto/request/AppleLoginReq.java b/luoo_user/src/main/java/com/luoo/user/dto/request/AppleLoginReq.java index 055c89c..f7efdd8 100644 --- a/luoo_user/src/main/java/com/luoo/user/dto/request/AppleLoginReq.java +++ b/luoo_user/src/main/java/com/luoo/user/dto/request/AppleLoginReq.java @@ -17,6 +17,12 @@ import java.io.Serializable; @ApiModel public class AppleLoginReq implements Serializable { private static final long serialVersionUID = 1L; +// +// @ApiModelProperty(name = "mobile", value = "手机号", required = false) +// private String mobile; +// +// @ApiModelProperty(name = "mobileCheckCode", value = "6位验证码", required = false) +// String mobileCheckCode; @ApiModelProperty(name = "deviceId", value = "设备id", required = true) @VerifyParam(required = true) diff --git a/luoo_user/src/main/java/com/luoo/user/dto/request/LogoffReq.java b/luoo_user/src/main/java/com/luoo/user/dto/request/LogoffReq.java new file mode 100644 index 0000000..683edac --- /dev/null +++ b/luoo_user/src/main/java/com/luoo/user/dto/request/LogoffReq.java @@ -0,0 +1,35 @@ +package com.luoo.user.dto.request; + +import annotation.VerifyParam; +import enums.VerifyRegexEnum; +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; +import lombok.Getter; +import lombok.Setter; + +import java.io.Serializable; + +/** + * 期刊查询参数 + */ +@Getter +@Setter +@ApiModel +public class LogoffReq implements Serializable { + private static final long serialVersionUID = 1L; + @ApiModelProperty(name = "mobile", value = "手机号", required = true) + @VerifyParam(required = true, regex = VerifyRegexEnum.MOBILE) + private String mobile; + + @ApiModelProperty(name = "mobileCheckCode", value = "6位验证码", required = true) + @VerifyParam(required = true, regex = VerifyRegexEnum.MOBILE_CHECK_CODE) + String mobileCheckCode; + + @ApiModelProperty(name = "deviceId", value = "设备id", required = true) + @VerifyParam(required = true) + private String deviceId; + + @ApiModelProperty(name = "deviceBrand", value = "设备品牌", required = false) + String deviceBrand; + +} diff --git a/luoo_user/src/main/java/com/luoo/user/service/UserInfoService.java b/luoo_user/src/main/java/com/luoo/user/service/UserInfoService.java index 9a2136b..910d49d 100644 --- a/luoo_user/src/main/java/com/luoo/user/service/UserInfoService.java +++ b/luoo_user/src/main/java/com/luoo/user/service/UserInfoService.java @@ -206,7 +206,9 @@ public class UserInfoService { userInfo.setAvatar(Constants.DEFAULT_USER_AVATAR); userInfo.setThumbnail(Constants.DEFAULT_USER_THUMBNAIL); userInfo.setSignature(Constants.DEFAULT_USER_SIGNATURE); + } else { + userInfo.setAppleId(loginUserInfo.getAppleId()); userInfo.setLastUseDeviceId(loginUserInfo.getLastUseDeviceId()); userInfo.setLastUseDeviceBrand(loginUserInfo.getLastUseDeviceBrand()); userInfo.setLastLoginIp(loginUserInfo.getLastLoginIp()); @@ -224,6 +226,7 @@ public class UserInfoService { public String appleLoginOrRegister(UserInfo loginUserInfo) { UserInfo userInfo = userInfoDao.findByAppleId(loginUserInfo.getAppleId()); if (null == userInfo) { +// return "未注册"; userInfo = loginUserInfo; userInfo.setId(String.valueOf(idWorker.nextId())); userInfo.setNickName("雀乐-" + NickNameUtil.getRandomNickName());