diff --git a/build.gradle b/build.gradle index 85473cf..4a835cd 100644 --- a/build.gradle +++ b/build.gradle @@ -40,6 +40,13 @@ dependencies { runtimeOnly 'com.h2database:h2' runtimeOnly 'com.mysql:mysql-connector-j' annotationProcessor 'org.projectlombok:lombok' + + implementation "org.mapstruct:mapstruct:1.5.5.Final" + annotationProcessor "org.mapstruct:mapstruct-processor:1.5.5.Final" + + // If you are using mapstruct in test code + testAnnotationProcessor "org.mapstruct:mapstruct-processor:1.5.5.Final" + testImplementation 'org.springframework.boot:spring-boot-starter-test' } diff --git a/src/main/java/com/lk/paopao/controller/UserController.java b/src/main/java/com/lk/paopao/controller/UserController.java new file mode 100644 index 0000000..5fcff86 --- /dev/null +++ b/src/main/java/com/lk/paopao/controller/UserController.java @@ -0,0 +1,23 @@ +package com.lk.paopao.controller; + +import com.lk.paopao.dto.UserDto; +import com.lk.paopao.dto.rest.response.DataResult; +import com.lk.paopao.dto.rest.response.ResultUtil; +import com.lk.paopao.service.UserService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequestMapping("${app.version}/user") +public class UserController { + + @Autowired + private UserService userService; + + @GetMapping("/info") + public DataResult info() { + return ResultUtil.ok(userService.info()); + } +} diff --git a/src/main/java/com/lk/paopao/dto/UserDto.java b/src/main/java/com/lk/paopao/dto/UserDto.java new file mode 100644 index 0000000..9592fda --- /dev/null +++ b/src/main/java/com/lk/paopao/dto/UserDto.java @@ -0,0 +1,21 @@ +package com.lk.paopao.dto; + + +import lombok.Value; + +import java.io.Serializable; + +/** + * DTO for {@link com.lk.paopao.entity.User} + */ +@Value +public class UserDto implements Serializable { + Long id; + String nickname; + String username; + String phone; + Byte status; + String avatar; + Long balance; + Boolean isAdmin; +} \ No newline at end of file diff --git a/src/main/java/com/lk/paopao/mapper/UserMapper.java b/src/main/java/com/lk/paopao/mapper/UserMapper.java new file mode 100644 index 0000000..6bdaa1f --- /dev/null +++ b/src/main/java/com/lk/paopao/mapper/UserMapper.java @@ -0,0 +1,17 @@ +package com.lk.paopao.mapper; + +import com.lk.paopao.entity.User; +import com.lk.paopao.dto.UserDto; +import org.mapstruct.BeanMapping; +import org.mapstruct.Mapper; +import org.mapstruct.MappingTarget; +import org.mapstruct.NullValuePropertyMappingStrategy; + +@Mapper(componentModel = "spring") +public interface UserMapper { + User toEntity(UserDto userDto); + + UserDto toDto(User user); + + @BeanMapping(nullValuePropertyMappingStrategy = NullValuePropertyMappingStrategy.IGNORE)User partialUpdate(UserDto userDto, @MappingTarget User user); +} diff --git a/src/main/java/com/lk/paopao/service/AuthService.java b/src/main/java/com/lk/paopao/service/AuthService.java index 78cdf13..25e8d1a 100644 --- a/src/main/java/com/lk/paopao/service/AuthService.java +++ b/src/main/java/com/lk/paopao/service/AuthService.java @@ -2,15 +2,18 @@ package com.lk.paopao.service; import com.lk.paopao.dto.rest.request.RegisterRequest; import com.lk.paopao.entity.User; +import com.lk.paopao.exception.AuthFailedException; import com.lk.paopao.exception.ResourceExistedException; import com.lk.paopao.repository.UserRepository; import com.lk.paopao.security.JwtUtils; +import jakarta.security.auth.message.AuthException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.security.authentication.AuthenticationManager; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; import org.springframework.security.core.Authentication; import org.springframework.security.core.context.SecurityContextHolder; +import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -83,5 +86,26 @@ public class AuthService { return token; } + /** + * 获取当前登录的用户详情。 + * @return 当前登录的UserDetails对象 + * @throws AuthFailedException 如果无法获取当前用户详情,则抛出异常 + */ + public UserDetails getCurrentUserDetails() { + Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); + if (authentication.getPrincipal() instanceof UserDetails) { + return (UserDetails) authentication.getPrincipal(); + } + throw new AuthFailedException("找不到用户在线信息"); + } + /** + * 获取当前登录的用户信息。 + * @return 当前登录的用户信息 + * @throws AuthFailedException 如果无法获取当前用户信息,则抛出异常 + */ + public User getCurrentUser() { + // 通过当前用户详情获取用户信息 + return userRepository.findByUsername(getCurrentUserDetails().getUsername()).orElseThrow(()->new AuthFailedException("找不到用户在线信息")); + } } diff --git a/src/main/java/com/lk/paopao/service/UserService.java b/src/main/java/com/lk/paopao/service/UserService.java new file mode 100644 index 0000000..e00b32e --- /dev/null +++ b/src/main/java/com/lk/paopao/service/UserService.java @@ -0,0 +1,23 @@ +package com.lk.paopao.service; + +import com.lk.paopao.dto.UserDto; +import com.lk.paopao.mapper.UserMapper; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +@Service +public class UserService { + @Autowired + private UserMapper userMapper; + @Autowired + private AuthService authService; + + /** + * 获取当前用户的基本信息 + * @return UserDto 当前用户的数据传输对象 + */ + public UserDto info(){ + return userMapper.toDto(authService.getCurrentUser()); + } + +}