|
@ -0,0 +1,55 @@ |
|
|
|
|
|
package com.iflytop.colortitration.app.controller; |
|
|
|
|
|
|
|
|
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
|
|
|
|
|
import com.iflytop.colortitration.app.model.dto.LoginDTO; |
|
|
|
|
|
import com.iflytop.colortitration.common.enums.EnableStatus; |
|
|
|
|
|
import com.iflytop.colortitration.common.model.entity.User; |
|
|
|
|
|
import com.iflytop.colortitration.common.result.Result; |
|
|
|
|
|
import com.iflytop.colortitration.common.result.ResultCode; |
|
|
|
|
|
import com.iflytop.colortitration.common.service.UserService; |
|
|
|
|
|
import io.swagger.v3.oas.annotations.Operation; |
|
|
|
|
|
import io.swagger.v3.oas.annotations.tags.Tag; |
|
|
|
|
|
import jakarta.validation.Valid; |
|
|
|
|
|
import lombok.RequiredArgsConstructor; |
|
|
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
|
|
import org.springframework.web.bind.annotation.*; |
|
|
|
|
|
|
|
|
|
|
|
import java.util.Objects; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* 认证控制 |
|
|
|
|
|
*/ |
|
|
|
|
|
@Tag(name = "认证") |
|
|
|
|
|
@RestController |
|
|
|
|
|
@RequestMapping("/api/auth") |
|
|
|
|
|
@RequiredArgsConstructor |
|
|
|
|
|
@Slf4j |
|
|
|
|
|
public class AuthController { |
|
|
|
|
|
|
|
|
|
|
|
private final UserService userService; |
|
|
|
|
|
|
|
|
|
|
|
@Operation(summary = "账号密码登录") |
|
|
|
|
|
@PostMapping("/login") |
|
|
|
|
|
public Result<User> login(@Valid @RequestBody LoginDTO loginDTO) { |
|
|
|
|
|
User user = userService.getOne(new LambdaQueryWrapper<>(User.class).eq(User::getUsername, loginDTO.getUsername())); |
|
|
|
|
|
if (user != null && !Objects.equals(user.getDeleted(), EnableStatus.ENABLE) && user.getPassword().equals(loginDTO.getPassword())) { |
|
|
|
|
|
userService.setCurrentUser(user); |
|
|
|
|
|
user.setPassword(null); |
|
|
|
|
|
return Result.success(user); |
|
|
|
|
|
} |
|
|
|
|
|
return Result.failed(ResultCode.INVALID_CREDENTIALS); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Operation(summary = "用户登出") |
|
|
|
|
|
@PostMapping("/logout") |
|
|
|
|
|
public Result<String> logout() { |
|
|
|
|
|
userService.setCurrentUser(null); |
|
|
|
|
|
return Result.success(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Operation(summary = "获取当前登录用户") |
|
|
|
|
|
@GetMapping("/current") |
|
|
|
|
|
public Result<User> current() { |
|
|
|
|
|
return Result.success(userService.getCurrentUser()); |
|
|
|
|
|
} |
|
|
|
|
|
} |