用户模块:用户注销

This commit is contained in:
huangge1199 2025-06-30 15:57:41 +08:00
parent 4670360495
commit 43bcec3cc0
3 changed files with 32 additions and 0 deletions

View File

@ -60,6 +60,16 @@ public class UserController {
return R.ok(userService.getLoginUserVO(user));
}
/**
* 用户注销
*/
@PostMapping("/logout")
public R<Boolean> userLogout(HttpServletRequest request) {
ThrowUtils.throwIf(request == null, ErrorCode.PARAMS_ERROR);
boolean result = userService.userLogout(request);
return R.ok(result);
}
}

View File

@ -57,4 +57,13 @@ public interface UserService extends IService<User> {
*/
User getLoginUser(HttpServletRequest request);
/**
* 用户注销
*
* @param request 请求
* @return 是否注销成功
*/
boolean userLogout(HttpServletRequest request);
}

View File

@ -133,6 +133,19 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User>
return currentUser;
}
@Override
public boolean userLogout(HttpServletRequest request) {
// 先判断是否已登录
Object userObj = request.getSession().getAttribute(UserConstant.USER_LOGIN_STATE);
if (userObj == null) {
throw new MyException(ErrorCode.OPERATION_ERROR, "未登录");
}
// 移除登录态
request.getSession().removeAttribute(UserConstant.USER_LOGIN_STATE);
return true;
}
}