96 lines
3.2 KiB
Java
96 lines
3.2 KiB
Java
package com.example.catchTheLetters.controller;
|
||
|
||
import com.example.catchTheLetters.model.dto.LoginDto;
|
||
import com.example.catchTheLetters.model.dto.RegisterDto;
|
||
import com.example.catchTheLetters.entity.User;
|
||
import com.example.catchTheLetters.model.vo.UserVo;
|
||
import com.example.catchTheLetters.service.AuthService;
|
||
import com.example.catchTheLetters.service.EmailService;
|
||
import com.example.catchTheLetters.utils.R;
|
||
import io.swagger.annotations.Api;
|
||
import io.swagger.annotations.ApiOperation;
|
||
import io.swagger.annotations.ApiParam;
|
||
import jakarta.annotation.Resource;
|
||
import jakarta.validation.constraints.Email;
|
||
import lombok.extern.slf4j.Slf4j;
|
||
import org.springframework.web.bind.annotation.*;
|
||
import org.springframework.web.multipart.MultipartFile;
|
||
|
||
/**
|
||
* 认证控制器
|
||
*
|
||
* @author spyn
|
||
*/
|
||
@RestController
|
||
@Slf4j
|
||
@Api(tags = "认证API")
|
||
@RequestMapping("/auth")
|
||
public class AuthController {
|
||
|
||
@Resource
|
||
private AuthService authService;
|
||
|
||
@Resource
|
||
private EmailService emailService;
|
||
|
||
@ApiOperation(value = "用户登录")
|
||
@PostMapping("/login")
|
||
public R<String> login(@RequestBody LoginDto loginDto) {
|
||
return authService.login(loginDto);
|
||
}
|
||
|
||
@ApiOperation(value = "用户注销")
|
||
@GetMapping("/logout")
|
||
public R logout(@RequestHeader("token") String token) {
|
||
return authService.logout(token) ? R.ok() : R.fail();
|
||
}
|
||
|
||
@ApiOperation(value = "验证token(不会返回密码)")
|
||
@GetMapping("/verify")
|
||
public R<UserVo> verify(@RequestHeader("token") String token) {
|
||
var user = authService.verify(token);
|
||
return user == null ? R.fail() : R.ok(user.toVo());
|
||
}
|
||
|
||
@ApiOperation(value = "用户注册")
|
||
@PostMapping("/register")
|
||
public R<String> register(@RequestBody RegisterDto registerDto) {
|
||
return authService.register(registerDto);
|
||
}
|
||
|
||
@ApiOperation(value = "用户信息查询(不会返回密码,查不出status为0的用户)")
|
||
@GetMapping("/info")
|
||
@ApiParam(name = "id", value = "用户ID(不传就是拿token查自己)")
|
||
public R<User> info(@RequestParam(required = false) String id, @RequestHeader("token") String token) {
|
||
if (id != null) {
|
||
return R.ok(authService.getUserInfo(id));
|
||
}
|
||
return R.ok(authService.verify(token));
|
||
}
|
||
|
||
@ApiOperation(value = "用户信息修改")
|
||
@PutMapping("/update")
|
||
public R<UserVo> update(@RequestBody User user, @RequestHeader("token") String token) {
|
||
return authService.update(user, token);
|
||
}
|
||
|
||
@ApiOperation(value = "发送邮件")
|
||
@PostMapping("/email")
|
||
public R<String> emailSend(@RequestHeader("token") String token, String email) {
|
||
return emailService.sendEmail(email);
|
||
}
|
||
|
||
|
||
@ApiOperation(value = "验证邮箱")
|
||
@PostMapping("/verify-email")
|
||
public R<String> emailVerify(@RequestHeader("token") String token, String email, String code) {
|
||
return emailService.verifyEmail(email, code);
|
||
}
|
||
|
||
@ApiOperation(value = "上传头像文件")
|
||
@PostMapping("/upload-avatar")
|
||
public R<String> uploadAvatar(@RequestHeader("token") String token, MultipartFile file) {
|
||
return authService.uploadAvatar(token, file);
|
||
}
|
||
}
|