From ec9622ad892e85ef0643792f54c0647f3621a408 Mon Sep 17 00:00:00 2001 From: YunaiV Date: Wed, 8 May 2019 19:20:25 +0800 Subject: [PATCH] =?UTF-8?q?-=20=E5=90=8E=E7=AB=AF=EF=BC=9AUser=20=E6=A8=A1?= =?UTF-8?q?=E5=9D=97=EF=BC=8Cservice=20=E5=A6=82=E6=9E=9C=E9=80=BB?= =?UTF-8?q?=E8=BE=91=E6=9C=89=E9=97=AE=E9=A2=98=EF=BC=8C=E6=8A=9B=E5=87=BA?= =?UTF-8?q?=E5=BC=82=E5=B8=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../admins/AdminsUserController.java | 10 ++-- .../controller/users/PassportController.java | 15 +++--- .../controller/users/UserController.java | 10 ++-- .../application/convert/PassportConvert.java | 6 +-- .../user/application/convert/UserConvert.java | 11 ++-- .../interceptor/UserSecurityInterceptor.java | 7 +-- .../mall/user/api/MobileCodeService.java | 3 +- .../iocoder/mall/user/api/OAuth2Service.java | 7 ++- .../mall/user/api/UserAccessLogService.java | 5 +- .../cn/iocoder/mall/user/api/UserService.java | 16 +++--- .../biz/service/MobileCodeServiceImpl.java | 47 ++++++++-------- .../user/biz/service/OAuth2ServiceImpl.java | 48 +++++++---------- .../biz/service/UserAccessLogServiceImpl.java | 4 +- .../user/biz/service/UserServiceImpl.java | 53 ++++++++----------- 14 files changed, 108 insertions(+), 134 deletions(-) diff --git a/user/user-application/src/main/java/cn/iocoder/mall/user/application/controller/admins/AdminsUserController.java b/user/user-application/src/main/java/cn/iocoder/mall/user/application/controller/admins/AdminsUserController.java index 41b936241..483af9637 100644 --- a/user/user-application/src/main/java/cn/iocoder/mall/user/application/controller/admins/AdminsUserController.java +++ b/user/user-application/src/main/java/cn/iocoder/mall/user/application/controller/admins/AdminsUserController.java @@ -14,6 +14,8 @@ import io.swagger.annotations.ApiOperation; import org.apache.dubbo.config.annotation.Reference; import org.springframework.web.bind.annotation.*; +import static cn.iocoder.common.framework.vo.CommonResult.success; + @RestController @RequestMapping("/admins/user") @Api("用户模块") @@ -37,9 +39,9 @@ public class AdminsUserController { UserPageDTO userPageDTO = new UserPageDTO().setNickname(nickname).setStatus(status) .setPageNo(pageNo).setPageSize(pageSize); // 查询分页 - CommonResult result = userService.getUserPage(userPageDTO); + UserPageBO result = userService.getUserPage(userPageDTO); // 转换结果 - return UserConvert.INSTANCE.convert(result); + return success(UserConvert.INSTANCE.convert(result)); } @PostMapping("/update") @@ -54,7 +56,7 @@ public class AdminsUserController { @RequestParam("avatar") String avatar) { UserUpdateDTO userUpdateDTO = new UserUpdateDTO().setId(id).setNickname(nickname).setNickname(nickname).setAvatar(avatar); // 更新 - return userService.updateUser(userUpdateDTO); + return success(userService.updateUser(userUpdateDTO)); } @PostMapping("/update_status") @@ -65,7 +67,7 @@ public class AdminsUserController { }) public CommonResult updateStatus(@RequestParam("id") Integer id, @RequestParam("status") Integer status) { - return userService.updateUserStatus(id, status); + return success(userService.updateUserStatus(id, status)); } } diff --git a/user/user-application/src/main/java/cn/iocoder/mall/user/application/controller/users/PassportController.java b/user/user-application/src/main/java/cn/iocoder/mall/user/application/controller/users/PassportController.java index 5ae7c958d..80db85f14 100644 --- a/user/user-application/src/main/java/cn/iocoder/mall/user/application/controller/users/PassportController.java +++ b/user/user-application/src/main/java/cn/iocoder/mall/user/application/controller/users/PassportController.java @@ -19,6 +19,8 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; +import static cn.iocoder.common.framework.vo.CommonResult.success; + @RestController @RequestMapping("users/passport") @Api("Passport 模块") @@ -47,16 +49,17 @@ public class PassportController { }) public CommonResult mobileRegister(@RequestParam("mobile") String mobile, @RequestParam("code") String code) { - CommonResult result = oauth2Service.getAccessToken(mobile, code); - return PassportConvert.INSTANCE.convert(result); + OAuth2AccessTokenBO result = oauth2Service.getAccessToken(mobile, code); + return success(PassportConvert.INSTANCE.convert(result)); } @PermitAll @PostMapping("mobile/send_register_code") @ApiOperation(value = "发送手机验证码") @ApiImplicitParam(name = "mobile", value = "手机号", required = true, example = "15601691300") - public CommonResult mobileSend(@RequestParam("mobile") String mobile) { - return mobileCodeService.send(mobile); + public CommonResult mobileSend(@RequestParam("mobile") String mobile) { + mobileCodeService.send(mobile); + return success(true); } // TODO 芋艿,改绑手机号 @@ -78,8 +81,8 @@ public class PassportController { @PermitAll @PostMapping("/refresh_token") // TODO 功能:刷新 token public CommonResult refreshToken(@RequestParam("refreshToken") String refreshToken) { - CommonResult result = oauth2Service.refreshToken(refreshToken); - return PassportConvert.INSTANCE.convert2(result); + OAuth2AccessTokenBO result = oauth2Service.refreshToken(refreshToken); + return success(PassportConvert.INSTANCE.convert2(result)); } // TODO 功能:退出,销毁 token diff --git a/user/user-application/src/main/java/cn/iocoder/mall/user/application/controller/users/UserController.java b/user/user-application/src/main/java/cn/iocoder/mall/user/application/controller/users/UserController.java index e36bc8ec1..1714c2a06 100644 --- a/user/user-application/src/main/java/cn/iocoder/mall/user/application/controller/users/UserController.java +++ b/user/user-application/src/main/java/cn/iocoder/mall/user/application/controller/users/UserController.java @@ -12,6 +12,8 @@ import io.swagger.annotations.ApiOperation; import org.apache.dubbo.config.annotation.Reference; import org.springframework.web.bind.annotation.*; +import static cn.iocoder.common.framework.vo.CommonResult.success; + @RestController @RequestMapping("/users/user") @Api("用户模块") @@ -23,8 +25,8 @@ public class UserController { @GetMapping("/info") @ApiOperation(value = "用户信息") public CommonResult info() { - CommonResult userResult = userService.getUser(UserSecurityContextHolder.getContext().getUserId()); - return UserConvert.INSTANCE.convert2(userResult); + UserBO userResult = userService.getUser(UserSecurityContextHolder.getContext().getUserId()); + return success(UserConvert.INSTANCE.convert2(userResult)); } @PostMapping("/update_avatar") @@ -34,7 +36,7 @@ public class UserController { UserUpdateDTO userUpdateDTO = new UserUpdateDTO().setId(UserSecurityContextHolder.getContext().getUserId()) .setAvatar(avatar); // 更新头像 - return userService.updateUser(userUpdateDTO); + return success(userService.updateUser(userUpdateDTO)); } @PostMapping("/update_nickname") @@ -44,7 +46,7 @@ public class UserController { UserUpdateDTO userUpdateDTO = new UserUpdateDTO().setId(UserSecurityContextHolder.getContext().getUserId()) .setNickname(nickname); // 更新头像 - return userService.updateUser(userUpdateDTO); + return success(userService.updateUser(userUpdateDTO)); } } diff --git a/user/user-application/src/main/java/cn/iocoder/mall/user/application/convert/PassportConvert.java b/user/user-application/src/main/java/cn/iocoder/mall/user/application/convert/PassportConvert.java index 889c9878a..917b9bde7 100644 --- a/user/user-application/src/main/java/cn/iocoder/mall/user/application/convert/PassportConvert.java +++ b/user/user-application/src/main/java/cn/iocoder/mall/user/application/convert/PassportConvert.java @@ -1,6 +1,5 @@ package cn.iocoder.mall.user.application.convert; -import cn.iocoder.common.framework.vo.CommonResult; import cn.iocoder.mall.user.api.bo.OAuth2AccessTokenBO; import cn.iocoder.mall.user.application.vo.users.UsersAccessTokenVO; import cn.iocoder.mall.user.application.vo.users.UsersMobileRegisterVO; @@ -17,9 +16,6 @@ public interface PassportConvert { UsersMobileRegisterVO convert(OAuth2AccessTokenBO oauth2AccessTokenBO); @Mappings({}) - CommonResult convert(CommonResult oauth2AccessTokenBO); - - @Mappings({}) - CommonResult convert2(CommonResult result); + UsersAccessTokenVO convert2(OAuth2AccessTokenBO result); } diff --git a/user/user-application/src/main/java/cn/iocoder/mall/user/application/convert/UserConvert.java b/user/user-application/src/main/java/cn/iocoder/mall/user/application/convert/UserConvert.java index 9b4637a52..89327c2ad 100644 --- a/user/user-application/src/main/java/cn/iocoder/mall/user/application/convert/UserConvert.java +++ b/user/user-application/src/main/java/cn/iocoder/mall/user/application/convert/UserConvert.java @@ -1,10 +1,9 @@ package cn.iocoder.mall.user.application.convert; -import cn.iocoder.common.framework.vo.CommonResult; -import cn.iocoder.mall.user.application.vo.admins.AdminsUserPageVO; -import cn.iocoder.mall.user.application.vo.users.UsersUserVO; import cn.iocoder.mall.user.api.bo.UserBO; import cn.iocoder.mall.user.api.bo.UserPageBO; +import cn.iocoder.mall.user.application.vo.admins.AdminsUserPageVO; +import cn.iocoder.mall.user.application.vo.users.UsersUserVO; import org.mapstruct.Mapper; import org.mapstruct.Mappings; import org.mapstruct.factory.Mappers; @@ -15,9 +14,9 @@ public interface UserConvert { UserConvert INSTANCE = Mappers.getMapper(UserConvert.class); @Mappings({}) - CommonResult convert(CommonResult result); + AdminsUserPageVO convert(UserPageBO result); @Mappings({}) - CommonResult convert2(CommonResult result); + UsersUserVO convert2(UserBO result); -} \ No newline at end of file +} diff --git a/user/user-sdk/src/main/java/cn/iocoder/mall/user/sdk/interceptor/UserSecurityInterceptor.java b/user/user-sdk/src/main/java/cn/iocoder/mall/user/sdk/interceptor/UserSecurityInterceptor.java index e3acf0fda..4deb50eff 100644 --- a/user/user-sdk/src/main/java/cn/iocoder/mall/user/sdk/interceptor/UserSecurityInterceptor.java +++ b/user/user-sdk/src/main/java/cn/iocoder/mall/user/sdk/interceptor/UserSecurityInterceptor.java @@ -2,7 +2,6 @@ package cn.iocoder.mall.user.sdk.interceptor; import cn.iocoder.common.framework.exception.ServiceException; import cn.iocoder.common.framework.util.HttpUtil; -import cn.iocoder.common.framework.vo.CommonResult; import cn.iocoder.mall.user.api.OAuth2Service; import cn.iocoder.mall.user.api.bo.OAuth2AuthenticationBO; import cn.iocoder.mall.user.sdk.annotation.PermitAll; @@ -31,11 +30,7 @@ public class UserSecurityInterceptor extends HandlerInterceptorAdapter { String accessToken = HttpUtil.obtainAccess(request); OAuth2AuthenticationBO authentication = null; if (accessToken != null) { - CommonResult result = oauth2Service.checkToken(accessToken); - if (result.isError()) { // TODO 芋艿,如果访问的地址无需登录,这里也不用抛异常 - throw new ServiceException(result.getCode(), result.getMessage()); - } - authentication = result.getData(); + authentication = oauth2Service.checkToken(accessToken); // TODO 芋艿,如果访问的地址无需登录,这里也不用抛异常 // 添加到 SecurityContext UserSecurityContext context = new UserSecurityContext(authentication.getUserId()); UserSecurityContextHolder.setContext(context); diff --git a/user/user-service-api/src/main/java/cn/iocoder/mall/user/api/MobileCodeService.java b/user/user-service-api/src/main/java/cn/iocoder/mall/user/api/MobileCodeService.java index 64037eccf..022953a25 100644 --- a/user/user-service-api/src/main/java/cn/iocoder/mall/user/api/MobileCodeService.java +++ b/user/user-service-api/src/main/java/cn/iocoder/mall/user/api/MobileCodeService.java @@ -1,7 +1,6 @@ package cn.iocoder.mall.user.api; import cn.iocoder.common.framework.exception.ServiceException; -import cn.iocoder.common.framework.vo.CommonResult; public interface MobileCodeService { @@ -10,6 +9,6 @@ public interface MobileCodeService { * * @param mobile 手机号 */ - CommonResult send(String mobile) throws ServiceException; + void send(String mobile) throws ServiceException; } diff --git a/user/user-service-api/src/main/java/cn/iocoder/mall/user/api/OAuth2Service.java b/user/user-service-api/src/main/java/cn/iocoder/mall/user/api/OAuth2Service.java index cfdd792a7..2a3ae3870 100644 --- a/user/user-service-api/src/main/java/cn/iocoder/mall/user/api/OAuth2Service.java +++ b/user/user-service-api/src/main/java/cn/iocoder/mall/user/api/OAuth2Service.java @@ -1,13 +1,12 @@ package cn.iocoder.mall.user.api; -import cn.iocoder.common.framework.vo.CommonResult; import cn.iocoder.mall.user.api.bo.OAuth2AccessTokenBO; import cn.iocoder.mall.user.api.bo.OAuth2AuthenticationBO; public interface OAuth2Service { - CommonResult getAccessToken(String mobile, String code); + OAuth2AccessTokenBO getAccessToken(String mobile, String code); /** * 校验访问令牌,获取身份信息( 不包括 accessToken 等等 ) @@ -15,9 +14,9 @@ public interface OAuth2Service { * @param accessToken 访问令牌 * @return 授权信息 */ - CommonResult checkToken(String accessToken); + OAuth2AuthenticationBO checkToken(String accessToken); - CommonResult refreshToken(String refreshToken); + OAuth2AccessTokenBO refreshToken(String refreshToken); // TODO @see 移除 token diff --git a/user/user-service-api/src/main/java/cn/iocoder/mall/user/api/UserAccessLogService.java b/user/user-service-api/src/main/java/cn/iocoder/mall/user/api/UserAccessLogService.java index 5e454a244..972d6e195 100644 --- a/user/user-service-api/src/main/java/cn/iocoder/mall/user/api/UserAccessLogService.java +++ b/user/user-service-api/src/main/java/cn/iocoder/mall/user/api/UserAccessLogService.java @@ -1,10 +1,9 @@ package cn.iocoder.mall.user.api; -import cn.iocoder.common.framework.vo.CommonResult; import cn.iocoder.mall.user.api.dto.UserAccessLogAddDTO; public interface UserAccessLogService { - CommonResult addUserAccessLog(UserAccessLogAddDTO userAccessLogAddDTO); + void addUserAccessLog(UserAccessLogAddDTO userAccessLogAddDTO); -} \ No newline at end of file +} diff --git a/user/user-service-api/src/main/java/cn/iocoder/mall/user/api/UserService.java b/user/user-service-api/src/main/java/cn/iocoder/mall/user/api/UserService.java index aa59d7103..b97c94aeb 100644 --- a/user/user-service-api/src/main/java/cn/iocoder/mall/user/api/UserService.java +++ b/user/user-service-api/src/main/java/cn/iocoder/mall/user/api/UserService.java @@ -1,6 +1,7 @@ package cn.iocoder.mall.user.api; -import cn.iocoder.common.framework.vo.CommonResult; +import cn.iocoder.common.framework.constant.CommonStatusEnum; +import cn.iocoder.common.framework.validator.InEnum; import cn.iocoder.mall.user.api.bo.UserBO; import cn.iocoder.mall.user.api.bo.UserPageBO; import cn.iocoder.mall.user.api.dto.UserPageDTO; @@ -8,9 +9,9 @@ import cn.iocoder.mall.user.api.dto.UserUpdateDTO; public interface UserService { - CommonResult getUserPage(UserPageDTO userPageDTO); + UserPageBO getUserPage(UserPageDTO userPageDTO); - CommonResult getUser(Integer userId); + UserBO getUser(Integer userId); /** * 更新用户基本信息 @@ -18,7 +19,7 @@ public interface UserService { * @param userUpdateDTO 更新 DTO * @return 更新结果 */ - CommonResult updateUser(UserUpdateDTO userUpdateDTO); + Boolean updateUser(UserUpdateDTO userUpdateDTO); /** * 更新用户状态 @@ -27,7 +28,8 @@ public interface UserService { * @param status 状态 * @return 更新结果 */ - CommonResult updateUserStatus(Integer userId, Integer status); + Boolean updateUserStatus(Integer userId, + @InEnum(value = CommonStatusEnum.class, message = "修改状态必须是 {value}") Integer status); /** * 更新用户手机号 @@ -36,6 +38,6 @@ public interface UserService { * @param mobile 手机号 * @return 更新结果 */ - CommonResult updateUserMobile(Integer userId, String mobile); + Boolean updateUserMobile(Integer userId, String mobile); -} \ No newline at end of file +} diff --git a/user/user-service-impl/src/main/java/cn/iocoder/mall/user/biz/service/MobileCodeServiceImpl.java b/user/user-service-impl/src/main/java/cn/iocoder/mall/user/biz/service/MobileCodeServiceImpl.java index 618384369..9b06bbc00 100644 --- a/user/user-service-impl/src/main/java/cn/iocoder/mall/user/biz/service/MobileCodeServiceImpl.java +++ b/user/user-service-impl/src/main/java/cn/iocoder/mall/user/biz/service/MobileCodeServiceImpl.java @@ -3,11 +3,10 @@ package cn.iocoder.mall.user.biz.service; import cn.iocoder.common.framework.constant.SysErrorCodeEnum; import cn.iocoder.common.framework.util.ServiceExceptionUtil; import cn.iocoder.common.framework.util.ValidationUtil; -import cn.iocoder.common.framework.vo.CommonResult; -import cn.iocoder.mall.user.biz.dao.MobileCodeMapper; -import cn.iocoder.mall.user.biz.dataobject.MobileCodeDO; import cn.iocoder.mall.user.api.MobileCodeService; import cn.iocoder.mall.user.api.constant.UserErrorCodeEnum; +import cn.iocoder.mall.user.biz.dao.MobileCodeMapper; +import cn.iocoder.mall.user.biz.dataobject.MobileCodeDO; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; @@ -49,24 +48,23 @@ public class MobileCodeServiceImpl implements MobileCodeService { * @param code 验证码 * @return 手机验证码信息 */ - public CommonResult validLastMobileCode(String mobile, String code) { - + public MobileCodeDO validLastMobileCode(String mobile, String code) { // TODO: 2019-04-09 Sin 暂时先忽略掉验证码校验 - return CommonResult.success(new MobileCodeDO().setCode(code).setCreateTime(new Date()).setId(1)); -// MobileCodeDO mobileCodePO = mobileCodeMapper.selectLast1ByMobile(mobile); -// if (mobileCodePO == null) { // 若验证码不存在,抛出异常 -// return ServiceExceptionUtil.error(UserErrorCodeEnum.MOBILE_CODE_NOT_FOUND.getCode()); -// } -// if (System.currentTimeMillis() - mobileCodePO.getCreateTime().getTime() >= codeExpireTimes) { // 验证码已过期 -// return ServiceExceptionUtil.error(UserErrorCodeEnum.MOBILE_CODE_EXPIRED.getCode()); -// } -// if (mobileCodePO.getUsed()) { // 验证码已使用 -// return ServiceExceptionUtil.error(UserErrorCodeEnum.MOBILE_CODE_USED.getCode()); -// } -// if (!mobileCodePO.getCode().equals(code)) { -// return ServiceExceptionUtil.error(UserErrorCodeEnum.MOBILE_CODE_NOT_CORRECT.getCode()); -// } -// return CommonResult.success(mobileCodePO); +// return new MobileCodeDO().setCode(code).setCreateTime(new Date()).setId(1); + MobileCodeDO mobileCodePO = mobileCodeMapper.selectLast1ByMobile(mobile); + if (mobileCodePO == null) { // 若验证码不存在,抛出异常 + throw ServiceExceptionUtil.exception(UserErrorCodeEnum.MOBILE_CODE_NOT_FOUND.getCode()); + } + if (System.currentTimeMillis() - mobileCodePO.getCreateTime().getTime() >= codeExpireTimes) { // 验证码已过期 + throw ServiceExceptionUtil.exception(UserErrorCodeEnum.MOBILE_CODE_EXPIRED.getCode()); + } + if (mobileCodePO.getUsed()) { // 验证码已使用 + throw ServiceExceptionUtil.exception(UserErrorCodeEnum.MOBILE_CODE_USED.getCode()); + } + if (!mobileCodePO.getCode().equals(code)) { + throw ServiceExceptionUtil.exception(UserErrorCodeEnum.MOBILE_CODE_NOT_CORRECT.getCode()); + } + return mobileCodePO; } /** @@ -81,18 +79,18 @@ public class MobileCodeServiceImpl implements MobileCodeService { } // TODO 芋艿,后面要返回有效时间 - public CommonResult send(String mobile) { + public void send(String mobile) { if (!ValidationUtil.isMobile(mobile)) { - return CommonResult.error(SysErrorCodeEnum.VALIDATION_REQUEST_PARAM_ERROR.getCode(), "手机格式不正确"); // TODO 有点搓 + throw ServiceExceptionUtil.exception(SysErrorCodeEnum.VALIDATION_REQUEST_PARAM_ERROR.getCode(), "手机格式不正确"); // TODO 有点搓 } // 校验是否可以发送验证码 MobileCodeDO lastMobileCodePO = mobileCodeMapper.selectLast1ByMobile(mobile); if (lastMobileCodePO != null) { if (lastMobileCodePO.getTodayIndex() >= sendMaximumQuantityPerDay) { // 超过当天发送的上限。 - return ServiceExceptionUtil.error(UserErrorCodeEnum.MOBILE_CODE_EXCEED_SEND_MAXIMUM_QUANTITY_PER_DAY.getCode()); + throw ServiceExceptionUtil.exception(UserErrorCodeEnum.MOBILE_CODE_EXCEED_SEND_MAXIMUM_QUANTITY_PER_DAY.getCode()); } if (System.currentTimeMillis() - lastMobileCodePO.getCreateTime().getTime() < sendFrequency) { // 发送过于频繁 - return ServiceExceptionUtil.error(UserErrorCodeEnum.MOBILE_CODE_SEND_TOO_FAST.getCode()); + throw ServiceExceptionUtil.exception(UserErrorCodeEnum.MOBILE_CODE_SEND_TOO_FAST.getCode()); } // TODO 提升,每个 IP 每天可发送数量 // TODO 提升,每个 IP 每小时可发送数量 @@ -104,7 +102,6 @@ public class MobileCodeServiceImpl implements MobileCodeService { .setUsed(false).setCreateTime(new Date()); mobileCodeMapper.insert(newMobileCodePO); // TODO 发送验证码短信 - return CommonResult.success(null); } } diff --git a/user/user-service-impl/src/main/java/cn/iocoder/mall/user/biz/service/OAuth2ServiceImpl.java b/user/user-service-impl/src/main/java/cn/iocoder/mall/user/biz/service/OAuth2ServiceImpl.java index bea7708ca..b4880370c 100644 --- a/user/user-service-impl/src/main/java/cn/iocoder/mall/user/biz/service/OAuth2ServiceImpl.java +++ b/user/user-service-impl/src/main/java/cn/iocoder/mall/user/biz/service/OAuth2ServiceImpl.java @@ -2,18 +2,17 @@ package cn.iocoder.mall.user.biz.service; import cn.iocoder.common.framework.exception.ServiceException; import cn.iocoder.common.framework.util.ServiceExceptionUtil; -import cn.iocoder.common.framework.vo.CommonResult; +import cn.iocoder.mall.user.api.OAuth2Service; +import cn.iocoder.mall.user.api.bo.OAuth2AccessTokenBO; +import cn.iocoder.mall.user.api.bo.OAuth2AuthenticationBO; +import cn.iocoder.mall.user.api.constant.UserErrorCodeEnum; +import cn.iocoder.mall.user.biz.convert.OAuth2Convert; import cn.iocoder.mall.user.biz.dao.OAuth2AccessTokenMapper; import cn.iocoder.mall.user.biz.dao.OAuth2RefreshTokenMapper; import cn.iocoder.mall.user.biz.dataobject.MobileCodeDO; import cn.iocoder.mall.user.biz.dataobject.OAuth2AccessTokenDO; import cn.iocoder.mall.user.biz.dataobject.OAuth2RefreshTokenDO; import cn.iocoder.mall.user.biz.dataobject.UserDO; -import cn.iocoder.mall.user.api.OAuth2Service; -import cn.iocoder.mall.user.api.bo.OAuth2AccessTokenBO; -import cn.iocoder.mall.user.api.bo.OAuth2AuthenticationBO; -import cn.iocoder.mall.user.api.constant.UserErrorCodeEnum; -import cn.iocoder.mall.user.biz.convert.OAuth2Convert; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; @@ -52,20 +51,13 @@ public class OAuth2ServiceImpl implements OAuth2Service { @Override @Transactional - public CommonResult getAccessToken(String mobile, String code) { + public OAuth2AccessTokenBO getAccessToken(String mobile, String code) { // 校验传入的 mobile 和 code 是否合法 - CommonResult result = mobileCodeService.validLastMobileCode(mobile, code); - if (result.isError()) { - return CommonResult.error(result); - } + MobileCodeDO mobileCodeDO = mobileCodeService.validLastMobileCode(mobile, code); // 获取用户 UserDO userDO = userService.getUser(mobile); if (userDO == null) { // 用户不存在,则进行创建用户 - CommonResult createResult = userService.createUser(mobile); - if (createResult.isError()) { - return CommonResult.error(createResult); - } - userDO = createResult.getData(); + userDO = userService.createUser(mobile); Assert.notNull(userDO, "创建用户必然成功"); } // 创建刷新令牌 @@ -73,46 +65,46 @@ public class OAuth2ServiceImpl implements OAuth2Service { // 创建访问令牌 OAuth2AccessTokenDO oauth2AccessTokenDO = createOAuth2AccessToken(userDO.getId(), oauth2RefreshTokenDO.getId()); // 标记已使用 - mobileCodeService.useMobileCode(result.getData().getId(), userDO.getId()); + mobileCodeService.useMobileCode(mobileCodeDO.getId(), userDO.getId()); // 转换返回 - return CommonResult.success(OAuth2Convert.INSTANCE.convertToAccessTokenWithExpiresIn(oauth2AccessTokenDO)); + return OAuth2Convert.INSTANCE.convertToAccessTokenWithExpiresIn(oauth2AccessTokenDO); } @Override - public CommonResult checkToken(String accessToken) throws ServiceException { + public OAuth2AuthenticationBO checkToken(String accessToken) throws ServiceException { OAuth2AccessTokenDO accessTokenDO = oauth2AccessTokenMapper.selectByTokenId(accessToken); if (accessTokenDO == null) { // 不存在 - return ServiceExceptionUtil.error(UserErrorCodeEnum.OAUTH_INVALID_ACCESS_TOKEN_NOT_FOUND.getCode()); + throw ServiceExceptionUtil.exception(UserErrorCodeEnum.OAUTH_INVALID_ACCESS_TOKEN_NOT_FOUND.getCode()); } if (accessTokenDO.getExpiresTime().getTime() < System.currentTimeMillis()) { // 已过期 - return ServiceExceptionUtil.error(UserErrorCodeEnum.OAUTH_INVALID_ACCESS_TOKEN_EXPIRED.getCode()); + throw ServiceExceptionUtil.exception(UserErrorCodeEnum.OAUTH_INVALID_ACCESS_TOKEN_EXPIRED.getCode()); } if (!accessTokenDO.getValid()) { // 无效 - return ServiceExceptionUtil.error(UserErrorCodeEnum.OAUTH_INVALID_ACCESS_TOKEN_INVALID.getCode()); + throw ServiceExceptionUtil.exception(UserErrorCodeEnum.OAUTH_INVALID_ACCESS_TOKEN_INVALID.getCode()); } // 转换返回 - return CommonResult.success(OAuth2Convert.INSTANCE.convertToAuthentication(accessTokenDO)); + return OAuth2Convert.INSTANCE.convertToAuthentication(accessTokenDO); } @Override - public CommonResult refreshToken(String refreshToken) { + public OAuth2AccessTokenBO refreshToken(String refreshToken) { OAuth2RefreshTokenDO refreshTokenDO = oauth2RefreshTokenMapper.selectById(refreshToken); // 校验刷新令牌是否合法 if (refreshTokenDO == null) { // 不存在 - return ServiceExceptionUtil.error(UserErrorCodeEnum.OAUTH_INVALID_REFRESH_TOKEN_NOT_FOUND.getCode()); + throw ServiceExceptionUtil.exception(UserErrorCodeEnum.OAUTH_INVALID_REFRESH_TOKEN_NOT_FOUND.getCode()); } if (refreshTokenDO.getExpiresTime().getTime() < System.currentTimeMillis()) { // 已过期 - return ServiceExceptionUtil.error(UserErrorCodeEnum.OAUTH_INVALID_REFRESH_TOKEN_EXPIRED.getCode()); + throw ServiceExceptionUtil.exception(UserErrorCodeEnum.OAUTH_INVALID_REFRESH_TOKEN_EXPIRED.getCode()); } if (!refreshTokenDO.getValid()) { // 无效 - return ServiceExceptionUtil.error(UserErrorCodeEnum.OAUTH_INVALID_REFRESH_TOKEN_INVALID.getCode()); + throw ServiceExceptionUtil.exception(UserErrorCodeEnum.OAUTH_INVALID_REFRESH_TOKEN_INVALID.getCode()); } // 标记 refreshToken 对应的 accessToken 都不合法 oauth2AccessTokenMapper.updateToInvalidByRefreshToken(refreshToken); // 创建访问令牌 OAuth2AccessTokenDO oauth2AccessTokenDO = createOAuth2AccessToken(refreshTokenDO.getUserId(), refreshTokenDO.getId()); // 转换返回 - return CommonResult.success(OAuth2Convert.INSTANCE.convertToAccessTokenWithExpiresIn(oauth2AccessTokenDO)); + return OAuth2Convert.INSTANCE.convertToAccessTokenWithExpiresIn(oauth2AccessTokenDO); } /** diff --git a/user/user-service-impl/src/main/java/cn/iocoder/mall/user/biz/service/UserAccessLogServiceImpl.java b/user/user-service-impl/src/main/java/cn/iocoder/mall/user/biz/service/UserAccessLogServiceImpl.java index d80e84f0f..07eb4b1be 100644 --- a/user/user-service-impl/src/main/java/cn/iocoder/mall/user/biz/service/UserAccessLogServiceImpl.java +++ b/user/user-service-impl/src/main/java/cn/iocoder/mall/user/biz/service/UserAccessLogServiceImpl.java @@ -33,7 +33,7 @@ public class UserAccessLogServiceImpl implements UserAccessLogService { private UserAccessLogMapper userAccessLogMapper; @Override - public CommonResult addUserAccessLog(UserAccessLogAddDTO userAccessLogAddDTO) { + public void addUserAccessLog(UserAccessLogAddDTO userAccessLogAddDTO) { // 创建 UserAccessLogDO UserAccessLogDO accessLog = UserAccessLogConvert.INSTANCE.convert(userAccessLogAddDTO); accessLog.setCreateTime(new Date()); @@ -49,8 +49,6 @@ public class UserAccessLogServiceImpl implements UserAccessLogService { } // 插入 userAccessLogMapper.insert(accessLog); - // 返回成功 - return CommonResult.success(true); } } diff --git a/user/user-service-impl/src/main/java/cn/iocoder/mall/user/biz/service/UserServiceImpl.java b/user/user-service-impl/src/main/java/cn/iocoder/mall/user/biz/service/UserServiceImpl.java index 9aa8967a1..f9fd61acb 100644 --- a/user/user-service-impl/src/main/java/cn/iocoder/mall/user/biz/service/UserServiceImpl.java +++ b/user/user-service-impl/src/main/java/cn/iocoder/mall/user/biz/service/UserServiceImpl.java @@ -1,10 +1,10 @@ package cn.iocoder.mall.user.biz.service; +import cn.iocoder.common.framework.constant.CommonStatusEnum; import cn.iocoder.common.framework.constant.DeletedStatusEnum; import cn.iocoder.common.framework.constant.SysErrorCodeEnum; import cn.iocoder.common.framework.util.ServiceExceptionUtil; import cn.iocoder.common.framework.util.ValidationUtil; -import cn.iocoder.common.framework.vo.CommonResult; import cn.iocoder.mall.user.api.UserService; import cn.iocoder.mall.user.api.bo.UserBO; import cn.iocoder.mall.user.api.bo.UserPageBO; @@ -42,13 +42,13 @@ public class UserServiceImpl implements UserService { } @Transactional - public CommonResult createUser(String mobile) { + public UserDO createUser(String mobile) { if (!ValidationUtil.isMobile(mobile)) { - return CommonResult.error(SysErrorCodeEnum.VALIDATION_REQUEST_PARAM_ERROR.getCode(), "手机格式不正确"); // TODO 有点搓 + throw ServiceExceptionUtil.exception(SysErrorCodeEnum.VALIDATION_REQUEST_PARAM_ERROR.getCode(), "手机格式不正确"); // TODO 有点搓 } // 校验用户是否已经存在 if (getUser(mobile) != null) { - return ServiceExceptionUtil.error(UserErrorCodeEnum.USER_MOBILE_ALREADY_REGISTERED.getCode()); + throw ServiceExceptionUtil.exception(UserErrorCodeEnum.USER_MOBILE_ALREADY_REGISTERED.getCode()); } // 创建用户 UserDO userDO = new UserDO().setMobile(mobile).setStatus(UserConstants.STATUS_ENABLE); @@ -58,7 +58,7 @@ public class UserServiceImpl implements UserService { // 插入注册信息 createUserRegister(userDO); // 转换返回 - return CommonResult.success(userDO); + return userDO; } private void createUserRegister(UserDO userDO) { @@ -68,7 +68,7 @@ public class UserServiceImpl implements UserService { } @Override - public CommonResult getUserPage(UserPageDTO userPageDTO) { + public UserPageBO getUserPage(UserPageDTO userPageDTO) { UserPageBO userPageBO = new UserPageBO(); // 查询分页数据 int offset = (userPageDTO.getPageNo() - 1) * userPageDTO.getPageSize(); @@ -77,77 +77,68 @@ public class UserServiceImpl implements UserService { offset, userPageDTO.getPageSize()))); // 查询分页总数 userPageBO.setTotal(userMapper.selectCountByNicknameLike(userPageDTO.getNickname(), userPageDTO.getStatus())); - return CommonResult.success(userPageBO); + return userPageBO; } @Override - public CommonResult getUser(Integer userId) { - return CommonResult.success(UserConvert.INSTANCE.convert(userMapper.selectById(userId))); + public UserBO getUser(Integer userId) { + return UserConvert.INSTANCE.convert(userMapper.selectById(userId)); } @Override - public CommonResult updateUser(UserUpdateDTO userUpdateDTO) { + public Boolean updateUser(UserUpdateDTO userUpdateDTO) { // 校验用户存在 if (userMapper.selectById(userUpdateDTO.getId()) == null) { - return ServiceExceptionUtil.error(UserErrorCodeEnum.USER_NOT_EXISTS.getCode()); + throw ServiceExceptionUtil.exception(UserErrorCodeEnum.USER_NOT_EXISTS.getCode()); } // 更新用户 UserDO updateUser = UserConvert.INSTANCE.convert(userUpdateDTO); userMapper.update(updateUser); // 返回成功 - return CommonResult.success(true); + return true; } @Override - public CommonResult updateUserStatus(Integer userId, Integer status) { - // 校验参数 - if (!isValidStatus(status)) { - return CommonResult.error(SysErrorCodeEnum.VALIDATION_REQUEST_PARAM_ERROR.getCode(), "变更状态必须是开启(1)或关闭(2)"); // TODO 有点搓 - } + public Boolean updateUserStatus(Integer userId, Integer status) { // 校验用户存在 UserDO user = userMapper.selectById(userId); if (user == null) { - return ServiceExceptionUtil.error(UserErrorCodeEnum.USER_NOT_EXISTS.getCode()); + throw ServiceExceptionUtil.exception(UserErrorCodeEnum.USER_NOT_EXISTS.getCode()); } // 如果状态相同,则返回错误 if (status.equals(user.getStatus())) { - return ServiceExceptionUtil.error(UserErrorCodeEnum.USER_STATUS_EQUALS.getCode()); + throw ServiceExceptionUtil.exception((UserErrorCodeEnum.USER_STATUS_EQUALS.getCode())); } // 更新管理员状态 UserDO updateUser = new UserDO().setId(userId).setStatus(status); userMapper.update(updateUser); // 如果是关闭管理员,则标记 token 失效。否则,管理员还可以继续蹦跶 - if (UserConstants.STATUS_DISABLE.equals(status)) { + if (CommonStatusEnum.DISABLE.getValue().equals(status)) { oAuth2Service.removeToken(userId); } // 返回成功 - return CommonResult.success(true); + return true; } @Override - public CommonResult updateUserMobile(Integer userId, String mobile) { + public Boolean updateUserMobile(Integer userId, String mobile) { if (!ValidationUtil.isMobile(mobile)) { - return CommonResult.error(SysErrorCodeEnum.VALIDATION_REQUEST_PARAM_ERROR.getCode(), "手机格式不正确"); // TODO 有点搓 + throw ServiceExceptionUtil.exception(SysErrorCodeEnum.VALIDATION_REQUEST_PARAM_ERROR.getCode(), "手机格式不正确"); // TODO 有点搓 } // 校验用户存在 UserDO user = userMapper.selectById(userId); if (user == null) { - return ServiceExceptionUtil.error(UserErrorCodeEnum.USER_NOT_EXISTS.getCode()); + throw ServiceExceptionUtil.exception(UserErrorCodeEnum.USER_NOT_EXISTS.getCode()); } // 如果状态相同,则返回错误 if (mobile.equals(user.getMobile())) { - return ServiceExceptionUtil.error(UserErrorCodeEnum.USER_MOBILE_EQUALS.getCode()); + throw ServiceExceptionUtil.exception(UserErrorCodeEnum.USER_MOBILE_EQUALS.getCode()); } // 更新管理员状态 UserDO updateUser = new UserDO().setId(userId).setMobile(mobile); userMapper.update(updateUser); // 返回成功 - return CommonResult.success(true); - } - - private boolean isValidStatus(Integer status) { - return UserConstants.STATUS_ENABLE.equals(status) - || UserConstants.STATUS_DISABLE.equals(status); + return true; } }