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

pull/1/head
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.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<UserPageBO> 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<Boolean> updateStatus(@RequestParam("id") Integer id,
@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.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<UsersMobileRegisterVO> mobileRegister(@RequestParam("mobile") String mobile,
@RequestParam("code") String code) {
CommonResult<OAuth2AccessTokenBO> 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<Void> mobileSend(@RequestParam("mobile") String mobile) {
return mobileCodeService.send(mobile);
public CommonResult<Boolean> 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<UsersAccessTokenVO> refreshToken(@RequestParam("refreshToken") String refreshToken) {
CommonResult<OAuth2AccessTokenBO> result = oauth2Service.refreshToken(refreshToken);
return PassportConvert.INSTANCE.convert2(result);
OAuth2AccessTokenBO result = oauth2Service.refreshToken(refreshToken);
return success(PassportConvert.INSTANCE.convert2(result));
}
// TODO 功能:退出,销毁 token

View File

@ -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<UsersUserVO> info() {
CommonResult<UserBO> 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));
}
}

View File

@ -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<UsersMobileRegisterVO> convert(CommonResult<OAuth2AccessTokenBO> oauth2AccessTokenBO);
@Mappings({})
CommonResult<UsersAccessTokenVO> convert2(CommonResult<OAuth2AccessTokenBO> result);
UsersAccessTokenVO convert2(OAuth2AccessTokenBO result);
}

View File

@ -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<AdminsUserPageVO> convert(CommonResult<UserPageBO> result);
AdminsUserPageVO convert(UserPageBO result);
@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.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<OAuth2AuthenticationBO> 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);

View File

@ -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<Void> send(String mobile) throws ServiceException;
void send(String mobile) throws ServiceException;
}

View File

@ -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<OAuth2AccessTokenBO> getAccessToken(String mobile, String code);
OAuth2AccessTokenBO getAccessToken(String mobile, String code);
/**
* 访( accessToken )
@ -15,9 +14,9 @@ public interface OAuth2Service {
* @param accessToken 访
* @return
*/
CommonResult<OAuth2AuthenticationBO> checkToken(String accessToken);
OAuth2AuthenticationBO checkToken(String accessToken);
CommonResult<OAuth2AccessTokenBO> refreshToken(String refreshToken);
OAuth2AccessTokenBO refreshToken(String refreshToken);
// TODO @see 移除 token

View File

@ -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<Boolean> addUserAccessLog(UserAccessLogAddDTO userAccessLogAddDTO);
void addUserAccessLog(UserAccessLogAddDTO userAccessLogAddDTO);
}
}

View File

@ -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<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
* @return
*/
CommonResult<Boolean> updateUser(UserUpdateDTO userUpdateDTO);
Boolean updateUser(UserUpdateDTO userUpdateDTO);
/**
*
@ -27,7 +28,8 @@ public interface UserService {
* @param status
* @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
* @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.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<MobileCodeDO> 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<Void> 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);
}
}

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.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<OAuth2AccessTokenBO> getAccessToken(String mobile, String code) {
public OAuth2AccessTokenBO getAccessToken(String mobile, String code) {
// 校验传入的 mobile 和 code 是否合法
CommonResult<MobileCodeDO> 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<UserDO> 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<OAuth2AuthenticationBO> 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<OAuth2AccessTokenBO> 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);
}
/**

View File

@ -33,7 +33,7 @@ public class UserAccessLogServiceImpl implements UserAccessLogService {
private UserAccessLogMapper userAccessLogMapper;
@Override
public CommonResult<Boolean> 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);
}
}

View File

@ -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<UserDO> 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<UserPageBO> 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<UserBO> 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<Boolean> 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<Boolean> 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<Boolean> 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;
}
}