- 后端:User 模块,service 如果逻辑有问题,抛出异常

This commit is contained in:
YunaiV 2019-05-08 19:20:25 +08:00
parent 6169709e76
commit ec9622ad89
14 changed files with 108 additions and 134 deletions

View File

@ -14,6 +14,8 @@ import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.Reference; import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import static cn.iocoder.common.framework.vo.CommonResult.success;
@RestController @RestController
@RequestMapping("/admins/user") @RequestMapping("/admins/user")
@Api("用户模块") @Api("用户模块")
@ -37,9 +39,9 @@ public class AdminsUserController {
UserPageDTO userPageDTO = new UserPageDTO().setNickname(nickname).setStatus(status) UserPageDTO userPageDTO = new UserPageDTO().setNickname(nickname).setStatus(status)
.setPageNo(pageNo).setPageSize(pageSize); .setPageNo(pageNo).setPageSize(pageSize);
// 查询分页 // 查询分页
CommonResult<UserPageBO> result = userService.getUserPage(userPageDTO); UserPageBO result = userService.getUserPage(userPageDTO);
// 转换结果 // 转换结果
return UserConvert.INSTANCE.convert(result); return success(UserConvert.INSTANCE.convert(result));
} }
@PostMapping("/update") @PostMapping("/update")
@ -54,7 +56,7 @@ public class AdminsUserController {
@RequestParam("avatar") String avatar) { @RequestParam("avatar") String avatar) {
UserUpdateDTO userUpdateDTO = new UserUpdateDTO().setId(id).setNickname(nickname).setNickname(nickname).setAvatar(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") @PostMapping("/update_status")
@ -65,7 +67,7 @@ public class AdminsUserController {
}) })
public CommonResult<Boolean> updateStatus(@RequestParam("id") Integer id, public CommonResult<Boolean> updateStatus(@RequestParam("id") Integer id,
@RequestParam("status") Integer status) { @RequestParam("status") Integer status) {
return userService.updateUserStatus(id, status); return success(userService.updateUserStatus(id, status));
} }
} }

View File

@ -19,6 +19,8 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import static cn.iocoder.common.framework.vo.CommonResult.success;
@RestController @RestController
@RequestMapping("users/passport") @RequestMapping("users/passport")
@Api("Passport 模块") @Api("Passport 模块")
@ -47,16 +49,17 @@ public class PassportController {
}) })
public CommonResult<UsersMobileRegisterVO> mobileRegister(@RequestParam("mobile") String mobile, public CommonResult<UsersMobileRegisterVO> mobileRegister(@RequestParam("mobile") String mobile,
@RequestParam("code") String code) { @RequestParam("code") String code) {
CommonResult<OAuth2AccessTokenBO> result = oauth2Service.getAccessToken(mobile, code); OAuth2AccessTokenBO result = oauth2Service.getAccessToken(mobile, code);
return PassportConvert.INSTANCE.convert(result); return success(PassportConvert.INSTANCE.convert(result));
} }
@PermitAll @PermitAll
@PostMapping("mobile/send_register_code") @PostMapping("mobile/send_register_code")
@ApiOperation(value = "发送手机验证码") @ApiOperation(value = "发送手机验证码")
@ApiImplicitParam(name = "mobile", value = "手机号", required = true, example = "15601691300") @ApiImplicitParam(name = "mobile", value = "手机号", required = true, example = "15601691300")
public CommonResult<Void> mobileSend(@RequestParam("mobile") String mobile) { public CommonResult<Boolean> mobileSend(@RequestParam("mobile") String mobile) {
return mobileCodeService.send(mobile); mobileCodeService.send(mobile);
return success(true);
} }
// TODO 芋艿改绑手机号 // TODO 芋艿改绑手机号
@ -78,8 +81,8 @@ public class PassportController {
@PermitAll @PermitAll
@PostMapping("/refresh_token") // TODO 功能刷新 token @PostMapping("/refresh_token") // TODO 功能刷新 token
public CommonResult<UsersAccessTokenVO> refreshToken(@RequestParam("refreshToken") String refreshToken) { public CommonResult<UsersAccessTokenVO> refreshToken(@RequestParam("refreshToken") String refreshToken) {
CommonResult<OAuth2AccessTokenBO> result = oauth2Service.refreshToken(refreshToken); OAuth2AccessTokenBO result = oauth2Service.refreshToken(refreshToken);
return PassportConvert.INSTANCE.convert2(result); return success(PassportConvert.INSTANCE.convert2(result));
} }
// TODO 功能退出销毁 token // TODO 功能退出销毁 token

View File

@ -12,6 +12,8 @@ import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.Reference; import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import static cn.iocoder.common.framework.vo.CommonResult.success;
@RestController @RestController
@RequestMapping("/users/user") @RequestMapping("/users/user")
@Api("用户模块") @Api("用户模块")
@ -23,8 +25,8 @@ public class UserController {
@GetMapping("/info") @GetMapping("/info")
@ApiOperation(value = "用户信息") @ApiOperation(value = "用户信息")
public CommonResult<UsersUserVO> info() { public CommonResult<UsersUserVO> info() {
CommonResult<UserBO> userResult = userService.getUser(UserSecurityContextHolder.getContext().getUserId()); UserBO userResult = userService.getUser(UserSecurityContextHolder.getContext().getUserId());
return UserConvert.INSTANCE.convert2(userResult); return success(UserConvert.INSTANCE.convert2(userResult));
} }
@PostMapping("/update_avatar") @PostMapping("/update_avatar")
@ -34,7 +36,7 @@ public class UserController {
UserUpdateDTO userUpdateDTO = new UserUpdateDTO().setId(UserSecurityContextHolder.getContext().getUserId()) UserUpdateDTO userUpdateDTO = new UserUpdateDTO().setId(UserSecurityContextHolder.getContext().getUserId())
.setAvatar(avatar); .setAvatar(avatar);
// 更新头像 // 更新头像
return userService.updateUser(userUpdateDTO); return success(userService.updateUser(userUpdateDTO));
} }
@PostMapping("/update_nickname") @PostMapping("/update_nickname")
@ -44,7 +46,7 @@ public class UserController {
UserUpdateDTO userUpdateDTO = new UserUpdateDTO().setId(UserSecurityContextHolder.getContext().getUserId()) UserUpdateDTO userUpdateDTO = new UserUpdateDTO().setId(UserSecurityContextHolder.getContext().getUserId())
.setNickname(nickname); .setNickname(nickname);
// 更新头像 // 更新头像
return userService.updateUser(userUpdateDTO); return success(userService.updateUser(userUpdateDTO));
} }
} }

View File

@ -1,6 +1,5 @@
package cn.iocoder.mall.user.application.convert; 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.api.bo.OAuth2AccessTokenBO;
import cn.iocoder.mall.user.application.vo.users.UsersAccessTokenVO; import cn.iocoder.mall.user.application.vo.users.UsersAccessTokenVO;
import cn.iocoder.mall.user.application.vo.users.UsersMobileRegisterVO; import cn.iocoder.mall.user.application.vo.users.UsersMobileRegisterVO;
@ -17,9 +16,6 @@ public interface PassportConvert {
UsersMobileRegisterVO convert(OAuth2AccessTokenBO oauth2AccessTokenBO); UsersMobileRegisterVO convert(OAuth2AccessTokenBO oauth2AccessTokenBO);
@Mappings({}) @Mappings({})
CommonResult<UsersMobileRegisterVO> convert(CommonResult<OAuth2AccessTokenBO> oauth2AccessTokenBO); UsersAccessTokenVO convert2(OAuth2AccessTokenBO result);
@Mappings({})
CommonResult<UsersAccessTokenVO> convert2(CommonResult<OAuth2AccessTokenBO> result);
} }

View File

@ -1,10 +1,9 @@
package cn.iocoder.mall.user.application.convert; 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.UserBO;
import cn.iocoder.mall.user.api.bo.UserPageBO; 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.Mapper;
import org.mapstruct.Mappings; import org.mapstruct.Mappings;
import org.mapstruct.factory.Mappers; import org.mapstruct.factory.Mappers;
@ -15,9 +14,9 @@ public interface UserConvert {
UserConvert INSTANCE = Mappers.getMapper(UserConvert.class); UserConvert INSTANCE = Mappers.getMapper(UserConvert.class);
@Mappings({}) @Mappings({})
CommonResult<AdminsUserPageVO> convert(CommonResult<UserPageBO> result); AdminsUserPageVO convert(UserPageBO result);
@Mappings({}) @Mappings({})
CommonResult<UsersUserVO> convert2(CommonResult<UserBO> result); UsersUserVO convert2(UserBO result);
} }

View File

@ -2,7 +2,6 @@ package cn.iocoder.mall.user.sdk.interceptor;
import cn.iocoder.common.framework.exception.ServiceException; import cn.iocoder.common.framework.exception.ServiceException;
import cn.iocoder.common.framework.util.HttpUtil; 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.OAuth2Service;
import cn.iocoder.mall.user.api.bo.OAuth2AuthenticationBO; import cn.iocoder.mall.user.api.bo.OAuth2AuthenticationBO;
import cn.iocoder.mall.user.sdk.annotation.PermitAll; import cn.iocoder.mall.user.sdk.annotation.PermitAll;
@ -31,11 +30,7 @@ public class UserSecurityInterceptor extends HandlerInterceptorAdapter {
String accessToken = HttpUtil.obtainAccess(request); String accessToken = HttpUtil.obtainAccess(request);
OAuth2AuthenticationBO authentication = null; OAuth2AuthenticationBO authentication = null;
if (accessToken != null) { if (accessToken != null) {
CommonResult<OAuth2AuthenticationBO> result = oauth2Service.checkToken(accessToken); authentication = oauth2Service.checkToken(accessToken); // TODO 芋艿如果访问的地址无需登录这里也不用抛异常
if (result.isError()) { // TODO 芋艿如果访问的地址无需登录这里也不用抛异常
throw new ServiceException(result.getCode(), result.getMessage());
}
authentication = result.getData();
// 添加到 SecurityContext // 添加到 SecurityContext
UserSecurityContext context = new UserSecurityContext(authentication.getUserId()); UserSecurityContext context = new UserSecurityContext(authentication.getUserId());
UserSecurityContextHolder.setContext(context); UserSecurityContextHolder.setContext(context);

View File

@ -1,7 +1,6 @@
package cn.iocoder.mall.user.api; package cn.iocoder.mall.user.api;
import cn.iocoder.common.framework.exception.ServiceException; import cn.iocoder.common.framework.exception.ServiceException;
import cn.iocoder.common.framework.vo.CommonResult;
public interface MobileCodeService { public interface MobileCodeService {
@ -10,6 +9,6 @@ public interface MobileCodeService {
* *
* @param mobile 手机号 * @param mobile 手机号
*/ */
CommonResult<Void> send(String mobile) throws ServiceException; void send(String mobile) throws ServiceException;
} }

View File

@ -1,13 +1,12 @@
package cn.iocoder.mall.user.api; 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.OAuth2AccessTokenBO;
import cn.iocoder.mall.user.api.bo.OAuth2AuthenticationBO; import cn.iocoder.mall.user.api.bo.OAuth2AuthenticationBO;
public interface OAuth2Service { public interface OAuth2Service {
CommonResult<OAuth2AccessTokenBO> getAccessToken(String mobile, String code); OAuth2AccessTokenBO getAccessToken(String mobile, String code);
/** /**
* 校验访问令牌获取身份信息( 不包括 accessToken 等等 ) * 校验访问令牌获取身份信息( 不包括 accessToken 等等 )
@ -15,9 +14,9 @@ public interface OAuth2Service {
* @param accessToken 访问令牌 * @param accessToken 访问令牌
* @return 授权信息 * @return 授权信息
*/ */
CommonResult<OAuth2AuthenticationBO> checkToken(String accessToken); OAuth2AuthenticationBO checkToken(String accessToken);
CommonResult<OAuth2AccessTokenBO> refreshToken(String refreshToken); OAuth2AccessTokenBO refreshToken(String refreshToken);
// TODO @see 移除 token // TODO @see 移除 token

View File

@ -1,10 +1,9 @@
package cn.iocoder.mall.user.api; package cn.iocoder.mall.user.api;
import cn.iocoder.common.framework.vo.CommonResult;
import cn.iocoder.mall.user.api.dto.UserAccessLogAddDTO; import cn.iocoder.mall.user.api.dto.UserAccessLogAddDTO;
public interface UserAccessLogService { public interface UserAccessLogService {
CommonResult<Boolean> addUserAccessLog(UserAccessLogAddDTO userAccessLogAddDTO); void addUserAccessLog(UserAccessLogAddDTO userAccessLogAddDTO);
} }

View File

@ -1,6 +1,7 @@
package cn.iocoder.mall.user.api; 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.UserBO;
import cn.iocoder.mall.user.api.bo.UserPageBO; import cn.iocoder.mall.user.api.bo.UserPageBO;
import cn.iocoder.mall.user.api.dto.UserPageDTO; import cn.iocoder.mall.user.api.dto.UserPageDTO;
@ -8,9 +9,9 @@ import cn.iocoder.mall.user.api.dto.UserUpdateDTO;
public interface UserService { public interface UserService {
CommonResult<UserPageBO> getUserPage(UserPageDTO userPageDTO); UserPageBO getUserPage(UserPageDTO userPageDTO);
CommonResult<UserBO> getUser(Integer userId); UserBO getUser(Integer userId);
/** /**
* 更新用户基本信息 * 更新用户基本信息
@ -18,7 +19,7 @@ public interface UserService {
* @param userUpdateDTO 更新 DTO * @param userUpdateDTO 更新 DTO
* @return 更新结果 * @return 更新结果
*/ */
CommonResult<Boolean> updateUser(UserUpdateDTO userUpdateDTO); Boolean updateUser(UserUpdateDTO userUpdateDTO);
/** /**
* 更新用户状态 * 更新用户状态
@ -27,7 +28,8 @@ public interface UserService {
* @param status 状态 * @param status 状态
* @return 更新结果 * @return 更新结果
*/ */
CommonResult<Boolean> 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 手机号 * @param mobile 手机号
* @return 更新结果 * @return 更新结果
*/ */
CommonResult<Boolean> updateUserMobile(Integer userId, String mobile); Boolean updateUserMobile(Integer userId, String mobile);
} }

View File

@ -3,11 +3,10 @@ package cn.iocoder.mall.user.biz.service;
import cn.iocoder.common.framework.constant.SysErrorCodeEnum; import cn.iocoder.common.framework.constant.SysErrorCodeEnum;
import cn.iocoder.common.framework.util.ServiceExceptionUtil; import cn.iocoder.common.framework.util.ServiceExceptionUtil;
import cn.iocoder.common.framework.util.ValidationUtil; 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.MobileCodeService;
import cn.iocoder.mall.user.api.constant.UserErrorCodeEnum; 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.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -49,24 +48,23 @@ public class MobileCodeServiceImpl implements MobileCodeService {
* @param code 验证码 * @param code 验证码
* @return 手机验证码信息 * @return 手机验证码信息
*/ */
public CommonResult<MobileCodeDO> validLastMobileCode(String mobile, String code) { public MobileCodeDO validLastMobileCode(String mobile, String code) {
// TODO: 2019-04-09 Sin 暂时先忽略掉验证码校验 // TODO: 2019-04-09 Sin 暂时先忽略掉验证码校验
return CommonResult.success(new MobileCodeDO().setCode(code).setCreateTime(new Date()).setId(1)); // return new MobileCodeDO().setCode(code).setCreateTime(new Date()).setId(1);
// MobileCodeDO mobileCodePO = mobileCodeMapper.selectLast1ByMobile(mobile); MobileCodeDO mobileCodePO = mobileCodeMapper.selectLast1ByMobile(mobile);
// if (mobileCodePO == null) { // 若验证码不存在抛出异常 if (mobileCodePO == null) { // 若验证码不存在抛出异常
// return ServiceExceptionUtil.error(UserErrorCodeEnum.MOBILE_CODE_NOT_FOUND.getCode()); throw ServiceExceptionUtil.exception(UserErrorCodeEnum.MOBILE_CODE_NOT_FOUND.getCode());
// } }
// if (System.currentTimeMillis() - mobileCodePO.getCreateTime().getTime() >= codeExpireTimes) { // 验证码已过期 if (System.currentTimeMillis() - mobileCodePO.getCreateTime().getTime() >= codeExpireTimes) { // 验证码已过期
// return ServiceExceptionUtil.error(UserErrorCodeEnum.MOBILE_CODE_EXPIRED.getCode()); throw ServiceExceptionUtil.exception(UserErrorCodeEnum.MOBILE_CODE_EXPIRED.getCode());
// } }
// if (mobileCodePO.getUsed()) { // 验证码已使用 if (mobileCodePO.getUsed()) { // 验证码已使用
// return ServiceExceptionUtil.error(UserErrorCodeEnum.MOBILE_CODE_USED.getCode()); throw ServiceExceptionUtil.exception(UserErrorCodeEnum.MOBILE_CODE_USED.getCode());
// } }
// if (!mobileCodePO.getCode().equals(code)) { if (!mobileCodePO.getCode().equals(code)) {
// return ServiceExceptionUtil.error(UserErrorCodeEnum.MOBILE_CODE_NOT_CORRECT.getCode()); throw ServiceExceptionUtil.exception(UserErrorCodeEnum.MOBILE_CODE_NOT_CORRECT.getCode());
// } }
// return CommonResult.success(mobileCodePO); return mobileCodePO;
} }
/** /**
@ -81,18 +79,18 @@ public class MobileCodeServiceImpl implements MobileCodeService {
} }
// TODO 芋艿后面要返回有效时间 // TODO 芋艿后面要返回有效时间
public CommonResult<Void> send(String mobile) { public void send(String mobile) {
if (!ValidationUtil.isMobile(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); MobileCodeDO lastMobileCodePO = mobileCodeMapper.selectLast1ByMobile(mobile);
if (lastMobileCodePO != null) { if (lastMobileCodePO != null) {
if (lastMobileCodePO.getTodayIndex() >= sendMaximumQuantityPerDay) { // 超过当天发送的上限 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) { // 发送过于频繁 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 每天可发送数量
// TODO 提升每个 IP 每小时可发送数量 // TODO 提升每个 IP 每小时可发送数量
@ -104,7 +102,6 @@ public class MobileCodeServiceImpl implements MobileCodeService {
.setUsed(false).setCreateTime(new Date()); .setUsed(false).setCreateTime(new Date());
mobileCodeMapper.insert(newMobileCodePO); mobileCodeMapper.insert(newMobileCodePO);
// TODO 发送验证码短信 // TODO 发送验证码短信
return CommonResult.success(null);
} }
} }

View File

@ -2,18 +2,17 @@ package cn.iocoder.mall.user.biz.service;
import cn.iocoder.common.framework.exception.ServiceException; import cn.iocoder.common.framework.exception.ServiceException;
import cn.iocoder.common.framework.util.ServiceExceptionUtil; 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.OAuth2AccessTokenMapper;
import cn.iocoder.mall.user.biz.dao.OAuth2RefreshTokenMapper; import cn.iocoder.mall.user.biz.dao.OAuth2RefreshTokenMapper;
import cn.iocoder.mall.user.biz.dataobject.MobileCodeDO; import cn.iocoder.mall.user.biz.dataobject.MobileCodeDO;
import cn.iocoder.mall.user.biz.dataobject.OAuth2AccessTokenDO; import cn.iocoder.mall.user.biz.dataobject.OAuth2AccessTokenDO;
import cn.iocoder.mall.user.biz.dataobject.OAuth2RefreshTokenDO; import cn.iocoder.mall.user.biz.dataobject.OAuth2RefreshTokenDO;
import cn.iocoder.mall.user.biz.dataobject.UserDO; 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.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -52,20 +51,13 @@ public class OAuth2ServiceImpl implements OAuth2Service {
@Override @Override
@Transactional @Transactional
public CommonResult<OAuth2AccessTokenBO> getAccessToken(String mobile, String code) { public OAuth2AccessTokenBO getAccessToken(String mobile, String code) {
// 校验传入的 mobile code 是否合法 // 校验传入的 mobile code 是否合法
CommonResult<MobileCodeDO> result = mobileCodeService.validLastMobileCode(mobile, code); MobileCodeDO mobileCodeDO = mobileCodeService.validLastMobileCode(mobile, code);
if (result.isError()) {
return CommonResult.error(result);
}
// 获取用户 // 获取用户
UserDO userDO = userService.getUser(mobile); UserDO userDO = userService.getUser(mobile);
if (userDO == null) { // 用户不存在则进行创建用户 if (userDO == null) { // 用户不存在则进行创建用户
CommonResult<UserDO> createResult = userService.createUser(mobile); userDO = userService.createUser(mobile);
if (createResult.isError()) {
return CommonResult.error(createResult);
}
userDO = createResult.getData();
Assert.notNull(userDO, "创建用户必然成功"); Assert.notNull(userDO, "创建用户必然成功");
} }
// 创建刷新令牌 // 创建刷新令牌
@ -73,46 +65,46 @@ public class OAuth2ServiceImpl implements OAuth2Service {
// 创建访问令牌 // 创建访问令牌
OAuth2AccessTokenDO oauth2AccessTokenDO = createOAuth2AccessToken(userDO.getId(), oauth2RefreshTokenDO.getId()); 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 @Override
public CommonResult<OAuth2AuthenticationBO> checkToken(String accessToken) throws ServiceException { public OAuth2AuthenticationBO checkToken(String accessToken) throws ServiceException {
OAuth2AccessTokenDO accessTokenDO = oauth2AccessTokenMapper.selectByTokenId(accessToken); OAuth2AccessTokenDO accessTokenDO = oauth2AccessTokenMapper.selectByTokenId(accessToken);
if (accessTokenDO == null) { // 不存在 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()) { // 已过期 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()) { // 无效 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 @Override
public CommonResult<OAuth2AccessTokenBO> refreshToken(String refreshToken) { public OAuth2AccessTokenBO refreshToken(String refreshToken) {
OAuth2RefreshTokenDO refreshTokenDO = oauth2RefreshTokenMapper.selectById(refreshToken); OAuth2RefreshTokenDO refreshTokenDO = oauth2RefreshTokenMapper.selectById(refreshToken);
// 校验刷新令牌是否合法 // 校验刷新令牌是否合法
if (refreshTokenDO == null) { // 不存在 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()) { // 已过期 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()) { // 无效 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 都不合法 // 标记 refreshToken 对应的 accessToken 都不合法
oauth2AccessTokenMapper.updateToInvalidByRefreshToken(refreshToken); oauth2AccessTokenMapper.updateToInvalidByRefreshToken(refreshToken);
// 创建访问令牌 // 创建访问令牌
OAuth2AccessTokenDO oauth2AccessTokenDO = createOAuth2AccessToken(refreshTokenDO.getUserId(), refreshTokenDO.getId()); OAuth2AccessTokenDO oauth2AccessTokenDO = createOAuth2AccessToken(refreshTokenDO.getUserId(), refreshTokenDO.getId());
// 转换返回 // 转换返回
return CommonResult.success(OAuth2Convert.INSTANCE.convertToAccessTokenWithExpiresIn(oauth2AccessTokenDO)); return OAuth2Convert.INSTANCE.convertToAccessTokenWithExpiresIn(oauth2AccessTokenDO);
} }
/** /**

View File

@ -33,7 +33,7 @@ public class UserAccessLogServiceImpl implements UserAccessLogService {
private UserAccessLogMapper userAccessLogMapper; private UserAccessLogMapper userAccessLogMapper;
@Override @Override
public CommonResult<Boolean> addUserAccessLog(UserAccessLogAddDTO userAccessLogAddDTO) { public void addUserAccessLog(UserAccessLogAddDTO userAccessLogAddDTO) {
// 创建 UserAccessLogDO // 创建 UserAccessLogDO
UserAccessLogDO accessLog = UserAccessLogConvert.INSTANCE.convert(userAccessLogAddDTO); UserAccessLogDO accessLog = UserAccessLogConvert.INSTANCE.convert(userAccessLogAddDTO);
accessLog.setCreateTime(new Date()); accessLog.setCreateTime(new Date());
@ -49,8 +49,6 @@ public class UserAccessLogServiceImpl implements UserAccessLogService {
} }
// 插入 // 插入
userAccessLogMapper.insert(accessLog); userAccessLogMapper.insert(accessLog);
// 返回成功
return CommonResult.success(true);
} }
} }

View File

@ -1,10 +1,10 @@
package cn.iocoder.mall.user.biz.service; 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.DeletedStatusEnum;
import cn.iocoder.common.framework.constant.SysErrorCodeEnum; import cn.iocoder.common.framework.constant.SysErrorCodeEnum;
import cn.iocoder.common.framework.util.ServiceExceptionUtil; import cn.iocoder.common.framework.util.ServiceExceptionUtil;
import cn.iocoder.common.framework.util.ValidationUtil; 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.UserService;
import cn.iocoder.mall.user.api.bo.UserBO; import cn.iocoder.mall.user.api.bo.UserBO;
import cn.iocoder.mall.user.api.bo.UserPageBO; import cn.iocoder.mall.user.api.bo.UserPageBO;
@ -42,13 +42,13 @@ public class UserServiceImpl implements UserService {
} }
@Transactional @Transactional
public CommonResult<UserDO> createUser(String mobile) { public UserDO createUser(String mobile) {
if (!ValidationUtil.isMobile(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) { 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); UserDO userDO = new UserDO().setMobile(mobile).setStatus(UserConstants.STATUS_ENABLE);
@ -58,7 +58,7 @@ public class UserServiceImpl implements UserService {
// 插入注册信息 // 插入注册信息
createUserRegister(userDO); createUserRegister(userDO);
// 转换返回 // 转换返回
return CommonResult.success(userDO); return userDO;
} }
private void createUserRegister(UserDO userDO) { private void createUserRegister(UserDO userDO) {
@ -68,7 +68,7 @@ public class UserServiceImpl implements UserService {
} }
@Override @Override
public CommonResult<UserPageBO> getUserPage(UserPageDTO userPageDTO) { public UserPageBO getUserPage(UserPageDTO userPageDTO) {
UserPageBO userPageBO = new UserPageBO(); UserPageBO userPageBO = new UserPageBO();
// 查询分页数据 // 查询分页数据
int offset = (userPageDTO.getPageNo() - 1) * userPageDTO.getPageSize(); int offset = (userPageDTO.getPageNo() - 1) * userPageDTO.getPageSize();
@ -77,77 +77,68 @@ public class UserServiceImpl implements UserService {
offset, userPageDTO.getPageSize()))); offset, userPageDTO.getPageSize())));
// 查询分页总数 // 查询分页总数
userPageBO.setTotal(userMapper.selectCountByNicknameLike(userPageDTO.getNickname(), userPageDTO.getStatus())); userPageBO.setTotal(userMapper.selectCountByNicknameLike(userPageDTO.getNickname(), userPageDTO.getStatus()));
return CommonResult.success(userPageBO); return userPageBO;
} }
@Override @Override
public CommonResult<UserBO> getUser(Integer userId) { public UserBO getUser(Integer userId) {
return CommonResult.success(UserConvert.INSTANCE.convert(userMapper.selectById(userId))); return UserConvert.INSTANCE.convert(userMapper.selectById(userId));
} }
@Override @Override
public CommonResult<Boolean> updateUser(UserUpdateDTO userUpdateDTO) { public Boolean updateUser(UserUpdateDTO userUpdateDTO) {
// 校验用户存在 // 校验用户存在
if (userMapper.selectById(userUpdateDTO.getId()) == null) { 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); UserDO updateUser = UserConvert.INSTANCE.convert(userUpdateDTO);
userMapper.update(updateUser); userMapper.update(updateUser);
// 返回成功 // 返回成功
return CommonResult.success(true); return true;
} }
@Override @Override
public CommonResult<Boolean> updateUserStatus(Integer userId, Integer status) { public Boolean updateUserStatus(Integer userId, Integer status) {
// 校验参数
if (!isValidStatus(status)) {
return CommonResult.error(SysErrorCodeEnum.VALIDATION_REQUEST_PARAM_ERROR.getCode(), "变更状态必须是开启1或关闭2"); // TODO 有点搓
}
// 校验用户存在 // 校验用户存在
UserDO user = userMapper.selectById(userId); UserDO user = userMapper.selectById(userId);
if (user == null) { if (user == null) {
return ServiceExceptionUtil.error(UserErrorCodeEnum.USER_NOT_EXISTS.getCode()); throw ServiceExceptionUtil.exception(UserErrorCodeEnum.USER_NOT_EXISTS.getCode());
} }
// 如果状态相同则返回错误 // 如果状态相同则返回错误
if (status.equals(user.getStatus())) { 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); UserDO updateUser = new UserDO().setId(userId).setStatus(status);
userMapper.update(updateUser); userMapper.update(updateUser);
// 如果是关闭管理员则标记 token 失效否则管理员还可以继续蹦跶 // 如果是关闭管理员则标记 token 失效否则管理员还可以继续蹦跶
if (UserConstants.STATUS_DISABLE.equals(status)) { if (CommonStatusEnum.DISABLE.getValue().equals(status)) {
oAuth2Service.removeToken(userId); oAuth2Service.removeToken(userId);
} }
// 返回成功 // 返回成功
return CommonResult.success(true); return true;
} }
@Override @Override
public CommonResult<Boolean> updateUserMobile(Integer userId, String mobile) { public Boolean updateUserMobile(Integer userId, String mobile) {
if (!ValidationUtil.isMobile(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); UserDO user = userMapper.selectById(userId);
if (user == null) { if (user == null) {
return ServiceExceptionUtil.error(UserErrorCodeEnum.USER_NOT_EXISTS.getCode()); throw ServiceExceptionUtil.exception(UserErrorCodeEnum.USER_NOT_EXISTS.getCode());
} }
// 如果状态相同则返回错误 // 如果状态相同则返回错误
if (mobile.equals(user.getMobile())) { 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); UserDO updateUser = new UserDO().setId(userId).setMobile(mobile);
userMapper.update(updateUser); userMapper.update(updateUser);
// 返回成功 // 返回成功
return CommonResult.success(true); return true;
}
private boolean isValidStatus(Integer status) {
return UserConstants.STATUS_ENABLE.equals(status)
|| UserConstants.STATUS_DISABLE.equals(status);
} }
} }