【新增】用户注销接口
This commit is contained in:
13
src/main/java/com/corewing/app/dto/api/LogoffRequest.java
Normal file
13
src/main/java/com/corewing/app/dto/api/LogoffRequest.java
Normal file
@@ -0,0 +1,13 @@
|
||||
package com.corewing.app.dto.api;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import javax.validation.constraints.NotBlank;
|
||||
|
||||
@Data
|
||||
public class LogoffRequest {
|
||||
|
||||
@NotBlank(message = "验证码不能为空")
|
||||
private String code;
|
||||
|
||||
}
|
||||
2
src/main/java/com/corewing/app/dto/api/SendCodeRequest.java
Normal file → Executable file
2
src/main/java/com/corewing/app/dto/api/SendCodeRequest.java
Normal file → Executable file
@@ -17,7 +17,7 @@ public class SendCodeRequest {
|
||||
private String account;
|
||||
|
||||
/**
|
||||
* 验证码类型:register-注册, login-登录, reset-重置密码, forget-忘记密码
|
||||
* 验证码类型:register-注册, login-登录, reset-重置密码, forget-忘记密码,logoff-注销
|
||||
*/
|
||||
@NotBlank(message = "验证码类型不能为空")
|
||||
private String type;
|
||||
|
||||
11
src/main/java/com/corewing/app/modules/app/AppUserController.java
Normal file → Executable file
11
src/main/java/com/corewing/app/modules/app/AppUserController.java
Normal file → Executable file
@@ -13,6 +13,7 @@ import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.validation.Valid;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@@ -203,4 +204,14 @@ public class AppUserController {
|
||||
data.put("username", user.getUsername());
|
||||
return Result.success(I18nUtil.getMessage("user.login.success"), data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 注销用户
|
||||
* @param logoffRequest
|
||||
* @return
|
||||
*/
|
||||
@PostMapping("/logoff")
|
||||
public Result<String> logoff(@RequestBody @Valid LogoffRequest logoffRequest) {
|
||||
return Result.isBool(userService.logoff(logoffRequest));
|
||||
}
|
||||
}
|
||||
|
||||
8
src/main/java/com/corewing/app/service/UserService.java
Normal file → Executable file
8
src/main/java/com/corewing/app/service/UserService.java
Normal file → Executable file
@@ -4,6 +4,7 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import com.corewing.app.dto.api.CodeLoginRequest;
|
||||
import com.corewing.app.dto.api.ForgetPasswordRequest;
|
||||
import com.corewing.app.dto.api.LogoffRequest;
|
||||
import com.corewing.app.dto.biz.user.BizUserStatusRequest;
|
||||
import com.corewing.app.dto.biz.user.ResetPasswordRequest;
|
||||
import com.corewing.app.entity.User;
|
||||
@@ -129,4 +130,11 @@ public interface UserService extends IService<User> {
|
||||
* @return
|
||||
*/
|
||||
String codeLogin(CodeLoginRequest codeLoginRequest);
|
||||
|
||||
/**
|
||||
* 注销用户
|
||||
* @param logoffRequest
|
||||
* @return
|
||||
*/
|
||||
boolean logoff(LogoffRequest logoffRequest);
|
||||
}
|
||||
|
||||
37
src/main/java/com/corewing/app/service/impl/UserServiceImpl.java
Normal file → Executable file
37
src/main/java/com/corewing/app/service/impl/UserServiceImpl.java
Normal file → Executable file
@@ -8,6 +8,7 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.corewing.app.common.page.PageContext;
|
||||
import com.corewing.app.dto.api.CodeLoginRequest;
|
||||
import com.corewing.app.dto.api.ForgetPasswordRequest;
|
||||
import com.corewing.app.dto.api.LogoffRequest;
|
||||
import com.corewing.app.dto.biz.user.BizUserStatusRequest;
|
||||
import com.corewing.app.dto.biz.user.ResetPasswordRequest;
|
||||
import com.corewing.app.entity.User;
|
||||
@@ -34,12 +35,14 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements Us
|
||||
private final Ip2RegionUtil ip2RegionUtil;
|
||||
private final UserMapper userMapper;
|
||||
private final RedisUtil redisUtil;
|
||||
private final UserService userService;
|
||||
|
||||
public UserServiceImpl(VerifyCodeService verifyCodeService, Ip2RegionUtil ip2RegionUtil, UserMapper userMapper, RedisUtil redisUtil) {
|
||||
public UserServiceImpl(VerifyCodeService verifyCodeService, Ip2RegionUtil ip2RegionUtil, UserMapper userMapper, RedisUtil redisUtil, UserService userService) {
|
||||
this.verifyCodeService = verifyCodeService;
|
||||
this.ip2RegionUtil = ip2RegionUtil;
|
||||
this.userMapper = userMapper;
|
||||
this.redisUtil = redisUtil;
|
||||
this.userService = userService;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -294,4 +297,36 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements Us
|
||||
StpUtil.login(user.getId());
|
||||
return StpUtil.getTokenValue();
|
||||
}
|
||||
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
@Override
|
||||
public boolean logoff(LogoffRequest request) {
|
||||
User user = this.getById(StpUtil.getLoginId().toString());
|
||||
// 校验验证码
|
||||
if(user == null) {
|
||||
throw new RuntimeException(I18nUtil.getMessage("error.user.not.found"));
|
||||
}
|
||||
String codeKey = "";
|
||||
Object checkCode = null;
|
||||
if(user.getEmail() != null) {
|
||||
codeKey = String.format("verify_code:%s:%s", "logoff", user.getEmail());
|
||||
checkCode = redisUtil.get(codeKey);
|
||||
}
|
||||
|
||||
if(checkCode == null) {
|
||||
if(user.getTelephone() != null) {
|
||||
codeKey = String.format("verify_code:%s:%s", "logoff", user.getTelephone());
|
||||
checkCode = redisUtil.get(codeKey);
|
||||
}
|
||||
}
|
||||
|
||||
if(checkCode == null || !request.getCode().equalsIgnoreCase(checkCode.toString())) {
|
||||
throw new RuntimeException(I18nUtil.getMessage("error.verify.code.invalid"));
|
||||
}
|
||||
StpUtil.logout();
|
||||
|
||||
// 删除记录
|
||||
userService.removeById(user.getId());
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user