bug修复
This commit is contained in:
+17
-13
@@ -13,16 +13,19 @@ import jakarta.validation.Valid;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
@Tag(name = "认证接口")
|
||||
@RestController
|
||||
@RequestMapping("/api/auth")
|
||||
@RequestMapping("/api/public")
|
||||
@RequiredArgsConstructor
|
||||
public class AuthController {
|
||||
|
||||
private final AuthService authService;
|
||||
|
||||
@Operation(summary = "用户登录")
|
||||
@PostMapping("/login")
|
||||
@PostMapping("/user/login")
|
||||
@RateLimit(key = "login", maxCount = 5, duration = 60)
|
||||
public ApiResult<LoginVO> login(@Valid @RequestBody LoginDTO dto) {
|
||||
LoginVO vo = authService.login(dto, WebUtils.getClientIp(), WebUtils.getUserAgent());
|
||||
@@ -38,25 +41,26 @@ public class AuthController {
|
||||
}
|
||||
|
||||
@Operation(summary = "用户注册")
|
||||
@PostMapping("/register")
|
||||
@PostMapping("/user/register")
|
||||
@RateLimit(key = "register", maxCount = 3, duration = 3600)
|
||||
public ApiResult<Void> register(@Valid @RequestBody RegisterDTO dto) {
|
||||
authService.register(dto);
|
||||
return ApiResult.success();
|
||||
}
|
||||
|
||||
@Operation(summary = "获取验证码")
|
||||
@GetMapping("/captcha")
|
||||
public ApiResult<Map<String, String>> captcha() {
|
||||
return ApiResult.success(Map.of(
|
||||
"captchaKey", UUID.randomUUID().toString(),
|
||||
"captchaImage", ""
|
||||
));
|
||||
}
|
||||
|
||||
@Operation(summary = "刷新Token")
|
||||
@PostMapping("/refresh")
|
||||
public ApiResult<LoginVO> refreshToken(@RequestHeader("Refresh-Token") String refreshToken) {
|
||||
@PostMapping("/refresh-token")
|
||||
public ApiResult<LoginVO> refreshToken(@RequestParam String refreshToken) {
|
||||
LoginVO vo = authService.refreshToken(refreshToken);
|
||||
return ApiResult.success(vo);
|
||||
}
|
||||
|
||||
@Operation(summary = "退出登录")
|
||||
@PostMapping("/logout")
|
||||
public ApiResult<Void> logout(@RequestHeader("Authorization") String authHeader) {
|
||||
String token = authHeader.replace("Bearer ", "");
|
||||
authService.logout(token);
|
||||
return ApiResult.success();
|
||||
}
|
||||
}
|
||||
|
||||
+28
-4
@@ -3,6 +3,7 @@ package fun.nojava.module.system.controller;
|
||||
import fun.nojava.common.model.ApiResult;
|
||||
import fun.nojava.common.model.PageResult;
|
||||
import fun.nojava.module.system.dto.UserInfoVO;
|
||||
import fun.nojava.module.system.service.AuthService;
|
||||
import fun.nojava.module.system.service.UserService;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
@@ -12,14 +13,15 @@ import org.springframework.web.bind.annotation.*;
|
||||
|
||||
@Tag(name = "用户接口")
|
||||
@RestController
|
||||
@RequestMapping("/api/user")
|
||||
@RequestMapping("/api/app/user")
|
||||
@RequiredArgsConstructor
|
||||
public class UserController {
|
||||
|
||||
private final UserService userService;
|
||||
private final AuthService authService;
|
||||
|
||||
@Operation(summary = "获取当前用户信息")
|
||||
@GetMapping("/info")
|
||||
@GetMapping("/me")
|
||||
public ApiResult<UserInfoVO> getCurrentUser(@AuthenticationPrincipal Long userId) {
|
||||
return ApiResult.success(userService.getUserInfo(userId));
|
||||
}
|
||||
@@ -29,6 +31,28 @@ public class UserController {
|
||||
public ApiResult<UserInfoVO> getUserInfo(@PathVariable Long id) {
|
||||
return ApiResult.success(userService.getUserInfo(id));
|
||||
}
|
||||
|
||||
@Operation(summary = "关注用户")
|
||||
@PostMapping("/{id}/follow")
|
||||
public ApiResult<Void> followUser(@AuthenticationPrincipal Long userId, @PathVariable Long id) {
|
||||
userService.follow(userId, id);
|
||||
return ApiResult.success();
|
||||
}
|
||||
|
||||
@Operation(summary = "取消关注")
|
||||
@DeleteMapping("/{id}/follow")
|
||||
public ApiResult<Void> unfollowUser(@AuthenticationPrincipal Long userId, @PathVariable Long id) {
|
||||
userService.unfollow(userId, id);
|
||||
return ApiResult.success();
|
||||
}
|
||||
|
||||
@Operation(summary = "退出登录")
|
||||
@PostMapping("/logout")
|
||||
public ApiResult<Void> logout(@RequestHeader("Authorization") String authHeader) {
|
||||
String token = authHeader.replace("Bearer ", "");
|
||||
authService.logout(token);
|
||||
return ApiResult.success();
|
||||
}
|
||||
}
|
||||
|
||||
@Tag(name = "管理员-用户管理")
|
||||
@@ -49,14 +73,14 @@ class AdminUserController {
|
||||
}
|
||||
|
||||
@Operation(summary = "重置密码")
|
||||
@PostMapping("/{id}/reset-password")
|
||||
@PutMapping("/{id}/reset-password")
|
||||
public ApiResult<Void> resetPassword(@PathVariable Long id, @RequestParam String newPassword) {
|
||||
userService.resetPassword(id, newPassword);
|
||||
return ApiResult.success();
|
||||
}
|
||||
|
||||
@Operation(summary = "更新状态")
|
||||
@PostMapping("/{id}/status")
|
||||
@PutMapping("/{id}/status")
|
||||
public ApiResult<Void> updateStatus(@PathVariable Long id, @RequestParam int status) {
|
||||
userService.updateUserStatus(id, status);
|
||||
return ApiResult.success();
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package fun.nojava.module.system.dto;
|
||||
|
||||
import jakarta.validation.constraints.Email;
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import jakarta.validation.constraints.Size;
|
||||
import lombok.Data;
|
||||
@@ -8,8 +7,7 @@ import lombok.Data;
|
||||
@Data
|
||||
public class LoginDTO {
|
||||
|
||||
@NotBlank(message = "邮箱不能为空")
|
||||
@Email(message = "邮箱格式不正确")
|
||||
@NotBlank(message = "账号不能为空")
|
||||
private String email;
|
||||
|
||||
@NotBlank(message = "密码不能为空")
|
||||
|
||||
@@ -6,7 +6,7 @@ import lombok.Data;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@Data
|
||||
@TableName("login_log")
|
||||
@TableName("sys_login_log")
|
||||
public class LoginLog {
|
||||
|
||||
@TableId(type = IdType.AUTO)
|
||||
@@ -14,14 +14,17 @@ public class LoginLog {
|
||||
|
||||
private Long userId;
|
||||
|
||||
@TableField("username")
|
||||
private String email;
|
||||
|
||||
private Integer loginType;
|
||||
|
||||
private String ipAddress;
|
||||
|
||||
@TableField(exist = false)
|
||||
private String userAgent;
|
||||
|
||||
@TableField("result")
|
||||
private Integer status;
|
||||
|
||||
private String failReason;
|
||||
|
||||
@@ -6,7 +6,7 @@ import lombok.Data;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@Data
|
||||
@TableName("user")
|
||||
@TableName("sys_user")
|
||||
public class User {
|
||||
|
||||
@TableId(type = IdType.AUTO)
|
||||
@@ -14,6 +14,10 @@ public class User {
|
||||
|
||||
private String email;
|
||||
|
||||
private String username;
|
||||
|
||||
private String phone;
|
||||
|
||||
private String password;
|
||||
|
||||
private String nickname;
|
||||
@@ -22,6 +26,7 @@ public class User {
|
||||
|
||||
private String bio;
|
||||
|
||||
@TableField("user_type")
|
||||
private Integer type;
|
||||
|
||||
private Integer status;
|
||||
|
||||
@@ -6,7 +6,7 @@ import lombok.Data;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@Data
|
||||
@TableName("user_session")
|
||||
@TableName("sys_user_session")
|
||||
public class UserSession {
|
||||
|
||||
@TableId(type = IdType.AUTO)
|
||||
@@ -14,12 +14,15 @@ public class UserSession {
|
||||
|
||||
private Long userId;
|
||||
|
||||
@TableField("token")
|
||||
private String refreshToken;
|
||||
|
||||
@TableField("user_agent")
|
||||
private String deviceInfo;
|
||||
|
||||
private String ipAddress;
|
||||
|
||||
@TableField("expire_time")
|
||||
private LocalDateTime expiresAt;
|
||||
|
||||
@TableField(fill = FieldFill.INSERT)
|
||||
|
||||
+6
-1
@@ -39,7 +39,12 @@ public class AuthServiceImpl implements AuthService {
|
||||
@Transactional
|
||||
public LoginVO login(LoginDTO dto, String ipAddress, String userAgent) {
|
||||
User user = userMapper.selectOne(
|
||||
new LambdaQueryWrapper<User>().eq(User::getEmail, dto.getEmail())
|
||||
new LambdaQueryWrapper<User>()
|
||||
.eq(User::getEmail, dto.getEmail())
|
||||
.or()
|
||||
.eq(User::getUsername, dto.getEmail())
|
||||
.or()
|
||||
.eq(User::getPhone, dto.getEmail())
|
||||
);
|
||||
|
||||
if (user == null) {
|
||||
|
||||
+20
-1
@@ -7,6 +7,8 @@ import fun.nojava.common.exception.ErrorCode;
|
||||
import fun.nojava.common.model.PageResult;
|
||||
import fun.nojava.module.system.dto.UserInfoVO;
|
||||
import fun.nojava.module.system.entity.User;
|
||||
import fun.nojava.module.system.entity.UserFollow;
|
||||
import fun.nojava.module.system.mapper.UserFollowMapper;
|
||||
import fun.nojava.module.system.mapper.UserMapper;
|
||||
import fun.nojava.module.system.service.UserService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
@@ -20,6 +22,7 @@ import java.util.List;
|
||||
public class UserServiceImpl implements UserService {
|
||||
|
||||
private final UserMapper userMapper;
|
||||
private final UserFollowMapper userFollowMapper;
|
||||
private final PasswordEncoder passwordEncoder;
|
||||
|
||||
@Override
|
||||
@@ -93,14 +96,30 @@ public class UserServiceImpl implements UserService {
|
||||
|
||||
@Override
|
||||
public boolean isFollowing(Long followerId, Long followeeId) {
|
||||
return false;
|
||||
return userFollowMapper.selectCount(
|
||||
new LambdaQueryWrapper<UserFollow>()
|
||||
.eq(UserFollow::getFollowerId, followerId)
|
||||
.eq(UserFollow::getFolloweeId, followeeId)
|
||||
) > 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void follow(Long followerId, Long followeeId) {
|
||||
if (followerId == null || followeeId == null || followerId.equals(followeeId) || isFollowing(followerId, followeeId)) {
|
||||
return;
|
||||
}
|
||||
UserFollow follow = new UserFollow();
|
||||
follow.setFollowerId(followerId);
|
||||
follow.setFolloweeId(followeeId);
|
||||
userFollowMapper.insert(follow);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void unfollow(Long followerId, Long followeeId) {
|
||||
userFollowMapper.delete(
|
||||
new LambdaQueryWrapper<UserFollow>()
|
||||
.eq(UserFollow::getFollowerId, followerId)
|
||||
.eq(UserFollow::getFolloweeId, followeeId)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user