2024-04-09 13:24:01 +00:00
|
|
|
|
package com.example.catchTheLetters.controller;
|
|
|
|
|
|
2024-04-10 07:03:57 +00:00
|
|
|
|
import com.example.catchTheLetters.model.dto.LoginDto;
|
|
|
|
|
import com.example.catchTheLetters.model.dto.RegisterDto;
|
2024-04-09 13:24:01 +00:00
|
|
|
|
import com.example.catchTheLetters.entity.User;
|
2024-04-10 02:48:38 +00:00
|
|
|
|
import com.example.catchTheLetters.model.vo.UserVo;
|
2024-04-09 13:24:01 +00:00
|
|
|
|
import com.example.catchTheLetters.service.AuthService;
|
|
|
|
|
import com.example.catchTheLetters.utils.R;
|
|
|
|
|
import io.swagger.annotations.Api;
|
|
|
|
|
import io.swagger.annotations.ApiOperation;
|
2024-04-09 17:06:54 +00:00
|
|
|
|
import io.swagger.annotations.ApiParam;
|
2024-04-09 13:24:01 +00:00
|
|
|
|
import jakarta.annotation.Resource;
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
2024-04-09 17:06:54 +00:00
|
|
|
|
import org.springframework.web.bind.annotation.*;
|
2024-04-09 13:24:01 +00:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 认证控制器
|
2024-04-10 07:15:36 +00:00
|
|
|
|
*
|
2024-04-09 13:24:01 +00:00
|
|
|
|
* @author spyn
|
|
|
|
|
*/
|
|
|
|
|
@RestController
|
|
|
|
|
@Slf4j
|
|
|
|
|
@Api(tags = "认证API")
|
|
|
|
|
@RequestMapping("/auth")
|
|
|
|
|
public class AuthController {
|
|
|
|
|
|
|
|
|
|
@Resource
|
|
|
|
|
private AuthService authService;
|
|
|
|
|
|
|
|
|
|
@ApiOperation(value = "用户登录")
|
|
|
|
|
@PostMapping("/login")
|
|
|
|
|
public R<String> login(@RequestBody LoginDto loginDto) {
|
2024-04-10 02:48:38 +00:00
|
|
|
|
return authService.login(loginDto);
|
2024-04-09 13:24:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ApiOperation(value = "用户注销")
|
2024-04-10 10:44:02 +00:00
|
|
|
|
@GetMapping("/logout")
|
|
|
|
|
public R logout(@RequestHeader("token") String token) {
|
2024-04-09 13:24:01 +00:00
|
|
|
|
return authService.logout(token) ? R.ok() : R.fail();
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-09 17:06:54 +00:00
|
|
|
|
@ApiOperation(value = "验证token(不会返回密码)")
|
2024-04-10 10:44:02 +00:00
|
|
|
|
@GetMapping("/verify")
|
2024-04-11 11:26:45 +00:00
|
|
|
|
public R<UserVo> verify(@RequestHeader("token") String token) {
|
2024-04-11 11:50:18 +00:00
|
|
|
|
var user = authService.verify(token);
|
|
|
|
|
return user == null ? R.fail() : R.ok(user.toVo());
|
2024-04-09 13:24:01 +00:00
|
|
|
|
}
|
2024-04-09 13:49:20 +00:00
|
|
|
|
|
|
|
|
|
@ApiOperation(value = "用户注册")
|
|
|
|
|
@PostMapping("/register")
|
2024-04-26 11:44:35 +00:00
|
|
|
|
public R<String> register(@RequestBody RegisterDto registerDto) {
|
2024-04-10 02:48:38 +00:00
|
|
|
|
return authService.register(registerDto);
|
2024-04-09 13:49:20 +00:00
|
|
|
|
}
|
2024-04-09 17:06:54 +00:00
|
|
|
|
|
|
|
|
|
@ApiOperation(value = "用户信息查询(不会返回密码,查不出status为0的用户)")
|
|
|
|
|
@GetMapping("/info")
|
|
|
|
|
@ApiParam(name = "id", value = "用户ID(不传就是拿token查自己)")
|
2024-04-10 10:06:01 +00:00
|
|
|
|
public R<User> info(@RequestParam(required = false) String id, @RequestHeader("token") String token) {
|
2024-04-10 07:15:36 +00:00
|
|
|
|
if (id != null) {
|
2024-04-10 02:48:38 +00:00
|
|
|
|
return R.ok(authService.getUserInfo(id));
|
|
|
|
|
}
|
|
|
|
|
return R.ok(authService.verify(token));
|
2024-04-09 17:06:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ApiOperation(value = "用户信息修改")
|
2024-04-11 11:26:45 +00:00
|
|
|
|
@PutMapping("/update")
|
|
|
|
|
public R<UserVo> update(@RequestBody User user, @RequestHeader("token") String token) {
|
|
|
|
|
return authService.update(user, token);
|
2024-04-09 17:06:54 +00:00
|
|
|
|
}
|
2024-04-09 13:24:01 +00:00
|
|
|
|
}
|