调整 User 项目结构
增加管理后台查看 User 分页接口
This commit is contained in:
@@ -2,10 +2,13 @@ package cn.iocoder.mall.user.convert;
|
||||
|
||||
import cn.iocoder.mall.user.dataobject.UserDO;
|
||||
import cn.iocoder.mall.user.service.api.bo.UserBO;
|
||||
import cn.iocoder.mall.user.service.api.dto.UserUpdateDTO;
|
||||
import org.mapstruct.Mapper;
|
||||
import org.mapstruct.Mappings;
|
||||
import org.mapstruct.factory.Mappers;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Mapper
|
||||
public interface UserConvert {
|
||||
|
||||
@@ -14,4 +17,10 @@ public interface UserConvert {
|
||||
@Mappings({})
|
||||
UserBO convert(UserDO userDO);
|
||||
|
||||
@Mappings({})
|
||||
UserDO convert(UserUpdateDTO userUpdateDTO);
|
||||
|
||||
@Mappings({})
|
||||
List<UserBO> convert(List<UserDO> userDOs);
|
||||
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package cn.iocoder.mall.user.dao;
|
||||
|
||||
import cn.iocoder.mall.user.dataobject.OAuth2AccessTokenDO;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
@@ -10,4 +11,6 @@ public interface OAuth2AccessTokenMapper {
|
||||
|
||||
OAuth2AccessTokenDO selectByTokenId(String tokenId);
|
||||
|
||||
void updateToInvalidByUserId(@Param("userId") Integer userId);
|
||||
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package cn.iocoder.mall.user.dao;
|
||||
|
||||
import cn.iocoder.mall.user.dataobject.OAuth2RefreshTokenDO;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
@@ -8,4 +9,6 @@ public interface OAuth2RefreshTokenMapper {
|
||||
|
||||
void insert(OAuth2RefreshTokenDO entity);
|
||||
|
||||
void updateToInvalidByUserId(@Param("userId") Integer userId);
|
||||
|
||||
}
|
||||
@@ -1,13 +1,26 @@
|
||||
package cn.iocoder.mall.user.dao;
|
||||
|
||||
import cn.iocoder.mall.user.dataobject.UserDO;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Repository
|
||||
public interface UserMapper {
|
||||
|
||||
void insert(UserDO entity);
|
||||
|
||||
UserDO selectByMobile(String mobile);
|
||||
int update(UserDO entity);
|
||||
|
||||
UserDO selectByMobile(@Param("mobile") String mobile);
|
||||
|
||||
UserDO selectById(@Param("id") Integer id);
|
||||
|
||||
List<UserDO> selectListByNicknameLike(@Param("nickname") String nickname,
|
||||
@Param("offset") Integer offset,
|
||||
@Param("limit") Integer limit);
|
||||
|
||||
Integer selectCountByNicknameLike(@Param("nickname") String nickname);
|
||||
|
||||
}
|
||||
@@ -8,7 +8,7 @@ public class MobileCodeDO {
|
||||
/**
|
||||
* 编号
|
||||
*/
|
||||
private Long id;
|
||||
private Integer id;
|
||||
/**
|
||||
* 手机号
|
||||
*/
|
||||
@@ -28,7 +28,7 @@ public class MobileCodeDO {
|
||||
/**
|
||||
* 注册的用户编号
|
||||
*/
|
||||
private Long usedUid;
|
||||
private Integer usedUserId;
|
||||
/**
|
||||
* 创建时间
|
||||
*/
|
||||
@@ -38,11 +38,11 @@ public class MobileCodeDO {
|
||||
*/
|
||||
private Date usedTime;
|
||||
|
||||
public Long getId() {
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public MobileCodeDO setId(Long id) {
|
||||
public MobileCodeDO setId(Integer id) {
|
||||
this.id = id;
|
||||
return this;
|
||||
}
|
||||
@@ -83,12 +83,12 @@ public class MobileCodeDO {
|
||||
return this;
|
||||
}
|
||||
|
||||
public Long getUsedUid() {
|
||||
return usedUid;
|
||||
public Integer getUsedUserId() {
|
||||
return usedUserId;
|
||||
}
|
||||
|
||||
public MobileCodeDO setUsedUid(Long usedUid) {
|
||||
this.usedUid = usedUid;
|
||||
public MobileCodeDO setUsedUserId(Integer usedUserId) {
|
||||
this.usedUserId = usedUserId;
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
@@ -15,7 +15,7 @@ public class OAuth2AccessTokenDO {
|
||||
/**
|
||||
* 用户编号
|
||||
*/
|
||||
private Long uid;
|
||||
private Integer userId;
|
||||
/**
|
||||
* 过期时间
|
||||
*/
|
||||
@@ -47,12 +47,12 @@ public class OAuth2AccessTokenDO {
|
||||
return this;
|
||||
}
|
||||
|
||||
public Long getUid() {
|
||||
return uid;
|
||||
public Integer getUserId() {
|
||||
return userId;
|
||||
}
|
||||
|
||||
public OAuth2AccessTokenDO setUid(Long uid) {
|
||||
this.uid = uid;
|
||||
public OAuth2AccessTokenDO setUserId(Integer userId) {
|
||||
this.userId = userId;
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
@@ -16,7 +16,7 @@ public class OAuth2RefreshTokenDO {
|
||||
/**
|
||||
* 用户编号
|
||||
*/
|
||||
private Long uid;
|
||||
private Integer userId;
|
||||
/**
|
||||
* 是否有效
|
||||
*/
|
||||
@@ -39,12 +39,12 @@ public class OAuth2RefreshTokenDO {
|
||||
return this;
|
||||
}
|
||||
|
||||
public Long getUid() {
|
||||
return uid;
|
||||
public Integer getUserId() {
|
||||
return userId;
|
||||
}
|
||||
|
||||
public OAuth2RefreshTokenDO setUid(Long uid) {
|
||||
this.uid = uid;
|
||||
public OAuth2RefreshTokenDO setUserId(Integer userId) {
|
||||
this.userId = userId;
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
@@ -12,7 +12,7 @@ public class UserDO extends BaseDO {
|
||||
/**
|
||||
* 用户编号
|
||||
*/
|
||||
private Long id;
|
||||
private Integer id;
|
||||
/**
|
||||
* 手机号
|
||||
*/
|
||||
@@ -25,12 +25,19 @@ public class UserDO extends BaseDO {
|
||||
* 头像
|
||||
*/
|
||||
private String avatar;
|
||||
/**
|
||||
* 账号状态
|
||||
*
|
||||
* 1 - 开启
|
||||
* 2 - 禁用
|
||||
*/
|
||||
private Integer status;
|
||||
|
||||
public Long getId() {
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public UserDO setId(Long id) {
|
||||
public UserDO setId(Integer id) {
|
||||
this.id = id;
|
||||
return this;
|
||||
}
|
||||
@@ -62,4 +69,13 @@ public class UserDO extends BaseDO {
|
||||
return this;
|
||||
}
|
||||
|
||||
public Integer getStatus() {
|
||||
return status;
|
||||
}
|
||||
|
||||
public UserDO setStatus(Integer status) {
|
||||
this.status = status;
|
||||
return this;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -10,7 +10,7 @@ public class UserRegisterDO {
|
||||
/**
|
||||
* 用户编号
|
||||
*/
|
||||
private Long id;
|
||||
private Integer id;
|
||||
/**
|
||||
* 创建时间
|
||||
*/
|
||||
@@ -21,11 +21,11 @@ public class UserRegisterDO {
|
||||
// TODO 芋艿 方式,手机注册、qq 等等
|
||||
|
||||
|
||||
public Long getId() {
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public UserRegisterDO setId(Long id) {
|
||||
public UserRegisterDO setId(Integer id) {
|
||||
this.id = id;
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -68,10 +68,10 @@ public class MobileCodeServiceImpl implements MobileCodeService {
|
||||
* 更新手机验证码已使用
|
||||
*
|
||||
* @param id 验证码编号
|
||||
* @param uid 用户编号
|
||||
* @param userId 用户编号
|
||||
*/
|
||||
public void useMobileCode(Long id, Long uid) {
|
||||
MobileCodeDO update = new MobileCodeDO().setId(id).setUsed(true).setUsedUid(uid).setUsedTime(new Date());
|
||||
public void useMobileCode(Integer id, Integer userId) {
|
||||
MobileCodeDO update = new MobileCodeDO().setId(id).setUsed(true).setUsedUserId(userId).setUsedTime(new Date());
|
||||
mobileCodeMapper.update(update);
|
||||
}
|
||||
|
||||
|
||||
@@ -94,19 +94,32 @@ public class OAuth2ServiceImpl implements OAuth2Service {
|
||||
return CommonResult.success(OAuth2Convert.INSTANCE.convertToAuthentication(accessTokenDO));
|
||||
}
|
||||
|
||||
private OAuth2AccessTokenDO createOAuth2AccessToken(Long uid, String refreshToken) {
|
||||
/**
|
||||
* 移除用户对应的 Token
|
||||
*
|
||||
* @param userId 管理员编号
|
||||
*/
|
||||
@Transactional
|
||||
public void removeToken(Integer userId) {
|
||||
// 设置 access token 失效
|
||||
oauth2AccessTokenMapper.updateToInvalidByUserId(userId);
|
||||
// 设置 refresh token 失效
|
||||
oauth2RefreshTokenMapper.updateToInvalidByUserId(userId);
|
||||
}
|
||||
|
||||
private OAuth2AccessTokenDO createOAuth2AccessToken(Integer uid, String refreshToken) {
|
||||
OAuth2AccessTokenDO accessToken = new OAuth2AccessTokenDO().setId(generateAccessToken())
|
||||
.setRefreshToken(refreshToken)
|
||||
.setUid(uid)
|
||||
.setUserId(uid)
|
||||
.setExpiresTime(new Date(System.currentTimeMillis() + accessTokenExpireTimeMillis))
|
||||
.setValid(true);
|
||||
oauth2AccessTokenMapper.insert(accessToken);
|
||||
return accessToken;
|
||||
}
|
||||
|
||||
private OAuth2RefreshTokenDO createOAuth2RefreshToken(Long uid) {
|
||||
private OAuth2RefreshTokenDO createOAuth2RefreshToken(Integer uid) {
|
||||
OAuth2RefreshTokenDO refreshToken = new OAuth2RefreshTokenDO().setId(generateRefreshToken())
|
||||
.setUid(uid)
|
||||
.setUserId(uid)
|
||||
.setExpiresTime(new Date(System.currentTimeMillis() + refreshTokenExpireTimeMillis))
|
||||
.setValid(true);
|
||||
oauth2RefreshTokenMapper.insert(refreshToken);
|
||||
|
||||
@@ -1,13 +1,21 @@
|
||||
package cn.iocoder.mall.user.service;
|
||||
|
||||
import cn.iocoder.common.framework.constant.SysErrorCodeEnum;
|
||||
import cn.iocoder.common.framework.dataobject.BaseDO;
|
||||
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.convert.UserConvert;
|
||||
import cn.iocoder.mall.user.dao.UserMapper;
|
||||
import cn.iocoder.mall.user.dao.UserRegisterMapper;
|
||||
import cn.iocoder.mall.user.dataobject.UserDO;
|
||||
import cn.iocoder.mall.user.dataobject.UserRegisterDO;
|
||||
import cn.iocoder.mall.user.service.api.UserService;
|
||||
import cn.iocoder.mall.user.service.api.bo.UserPageBO;
|
||||
import cn.iocoder.mall.user.service.api.constant.UserConstants;
|
||||
import cn.iocoder.mall.user.service.api.constant.UserErrorCodeEnum;
|
||||
import cn.iocoder.mall.user.service.api.dto.UserPageDTO;
|
||||
import cn.iocoder.mall.user.service.api.dto.UserUpdateDTO;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
@@ -25,7 +33,7 @@ public class UserServiceImpl implements UserService {
|
||||
@Autowired
|
||||
private UserRegisterMapper userRegisterMapper;
|
||||
@Autowired
|
||||
private MobileCodeServiceImpl mobileCodeService;
|
||||
private OAuth2ServiceImpl oAuth2Service;
|
||||
|
||||
public UserDO getUser(String mobile) {
|
||||
return userMapper.selectByMobile(mobile);
|
||||
@@ -33,14 +41,16 @@ public class UserServiceImpl implements UserService {
|
||||
|
||||
@Transactional
|
||||
public CommonResult<UserDO> createUser(String mobile) {
|
||||
// TODO 芋艿,校验手机格式
|
||||
if (!ValidationUtil.isMobile(mobile)) {
|
||||
return CommonResult.error(SysErrorCodeEnum.VALIDATION_REQUEST_PARAM_ERROR.getCode(), "手机格式不正确"); // TODO 有点搓
|
||||
}
|
||||
// 校验用户是否已经存在
|
||||
if (getUser(mobile) != null) {
|
||||
return ServiceExceptionUtil.error(UserErrorCodeEnum.USER_MOBILE_ALREADY_REGISTERED.getCode());
|
||||
}
|
||||
// 创建用户
|
||||
UserDO userDO = new UserDO().setMobile(mobile);
|
||||
userDO.setCreateTime(new Date());
|
||||
UserDO userDO = new UserDO().setMobile(mobile).setStatus(UserConstants.STATUS_ENABLE);
|
||||
userDO.setCreateTime(new Date()).setDeleted(BaseDO.DELETED_NO);
|
||||
userMapper.insert(userDO);
|
||||
// 插入注册信息
|
||||
createUserRegister(userDO);
|
||||
@@ -54,4 +64,81 @@ public class UserServiceImpl implements UserService {
|
||||
userRegisterMapper.insert(userRegisterDO);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CommonResult<UserPageBO> getUserPage(UserPageDTO userPageDTO) {
|
||||
UserPageBO userPageBO = new UserPageBO();
|
||||
// 查询分页数据
|
||||
int offset = userPageDTO.getPageNo() * userPageDTO.getPageSize();
|
||||
userPageBO.setUsers(UserConvert.INSTANCE.convert(userMapper.selectListByNicknameLike(userPageDTO.getNickname(),
|
||||
offset, userPageDTO.getPageSize())));
|
||||
// 查询分页总数
|
||||
userPageBO.setCount(userMapper.selectCountByNicknameLike(userPageDTO.getNickname()));
|
||||
return CommonResult.success(userPageBO);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CommonResult<Boolean> updateUser(UserUpdateDTO userUpdateDTO) {
|
||||
// 校验用户存在
|
||||
if (userMapper.selectById(userUpdateDTO.getId()) == null) {
|
||||
return ServiceExceptionUtil.error(UserErrorCodeEnum.USER_NOT_EXISTS.getCode());
|
||||
}
|
||||
// 更新用户
|
||||
UserDO updateUser = UserConvert.INSTANCE.convert(userUpdateDTO);
|
||||
userMapper.update(updateUser);
|
||||
// 返回成功
|
||||
return CommonResult.success(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 有点搓
|
||||
}
|
||||
// 校验用户存在
|
||||
UserDO user = userMapper.selectById(userId);
|
||||
if (user == null) {
|
||||
return ServiceExceptionUtil.error(UserErrorCodeEnum.USER_NOT_EXISTS.getCode());
|
||||
}
|
||||
// 如果状态相同,则返回错误
|
||||
if (status.equals(user.getStatus())) {
|
||||
return ServiceExceptionUtil.error(UserErrorCodeEnum.USER_STATUS_EQUALS.getCode());
|
||||
}
|
||||
// 更新管理员状态
|
||||
UserDO updateUser = new UserDO().setId(userId).setStatus(status);
|
||||
userMapper.update(updateUser);
|
||||
// 如果是关闭管理员,则标记 token 失效。否则,管理员还可以继续蹦跶
|
||||
if (UserConstants.STATUS_DISABLE.equals(status)) {
|
||||
oAuth2Service.removeToken(userId);
|
||||
}
|
||||
// 返回成功
|
||||
return CommonResult.success(true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CommonResult<Boolean> updateUserMobile(Integer userId, String mobile) {
|
||||
if (!ValidationUtil.isMobile(mobile)) {
|
||||
return CommonResult.error(SysErrorCodeEnum.VALIDATION_REQUEST_PARAM_ERROR.getCode(), "手机格式不正确"); // TODO 有点搓
|
||||
}
|
||||
// 校验用户存在
|
||||
UserDO user = userMapper.selectById(userId);
|
||||
if (user == null) {
|
||||
return ServiceExceptionUtil.error(UserErrorCodeEnum.USER_NOT_EXISTS.getCode());
|
||||
}
|
||||
// 如果状态相同,则返回错误
|
||||
if (mobile.equals(user.getMobile())) {
|
||||
return ServiceExceptionUtil.error(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);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user