init
This commit is contained in:
@@ -0,0 +1,80 @@
|
||||
package com.tailbet.controller;
|
||||
|
||||
import com.tailbet.model.dto.LoginDTO;
|
||||
import com.tailbet.model.dto.RegisterDTO;
|
||||
import com.tailbet.model.dto.UpdateUserDTO;
|
||||
import com.tailbet.model.vo.LoginUserVo;
|
||||
import com.tailbet.model.vo.R;
|
||||
import com.tailbet.model.vo.UserContext;
|
||||
import com.tailbet.service.IUserService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
/**
|
||||
* 用户Controller
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/api/user")
|
||||
@RequiredArgsConstructor
|
||||
public class UserController {
|
||||
|
||||
private final IUserService userService;
|
||||
|
||||
/**
|
||||
* 注册
|
||||
*/
|
||||
@PostMapping("/register")
|
||||
public R<?> register(@RequestBody RegisterDTO dto) {
|
||||
try {
|
||||
userService.register(dto.getUsername(), dto.getPassword());
|
||||
return R.ok("注册成功");
|
||||
} catch (RuntimeException e) {
|
||||
return R.fail(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 登录
|
||||
*/
|
||||
@PostMapping("/login")
|
||||
public R<?> login(@RequestBody LoginDTO dto) {
|
||||
try {
|
||||
String token = userService.login(dto.getUsername(), dto.getPassword());
|
||||
return R.ok("登录成功", java.util.Map.of("token", token));
|
||||
} catch (RuntimeException e) {
|
||||
return R.fail(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前用户信息
|
||||
*/
|
||||
@GetMapping("/info")
|
||||
public R<?> getInfo() {
|
||||
LoginUserVo user = UserContext.getUser();
|
||||
return R.ok(user);
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新用户信息
|
||||
*/
|
||||
@PutMapping("/update")
|
||||
public R<?> update(@RequestBody UpdateUserDTO dto) {
|
||||
try {
|
||||
Long userId = UserContext.getUserId();
|
||||
userService.updateUserInfo(userId, dto);
|
||||
return R.ok("更新成功");
|
||||
} catch (RuntimeException e) {
|
||||
return R.fail(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 退出登录
|
||||
*/
|
||||
@PostMapping("/logout")
|
||||
public R<?> logout() {
|
||||
// 可以在这里清理Redis中的token
|
||||
return R.ok("退出成功");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user