e554a287f9
- 创建统一的BaseRequest和BaseResponse基础类 - 新增全局异常处理机制 - 重构所有Controller层,移除业务逻辑到Service层 - 统一接口入参和出参格式 - 移除try-catch,使用全局异常处理 - 完善接口文档和参数校验 主要变更: 1. 新增request和response包结构 2. 创建全局异常处理器GlobalExceptionHandler 3. 重构AiChatController、AuthController、UserController等 4. 优化代码规范,提升维护性
44 lines
1.4 KiB
Java
44 lines
1.4 KiB
Java
package com.emotion.controller;
|
|
|
|
import com.emotion.common.Result;
|
|
import com.emotion.dto.request.TokenRequest;
|
|
import com.emotion.dto.response.UserInfoResponse;
|
|
import com.emotion.service.TokenService;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.validation.annotation.Validated;
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
@RestController
|
|
@RequestMapping("/token")
|
|
public class TokenController {
|
|
|
|
@Autowired
|
|
private TokenService tokenService;
|
|
|
|
/**
|
|
* 通过token获取用户信息
|
|
*/
|
|
@PostMapping("/user-info")
|
|
public Result<UserInfoResponse> getUserInfoByToken(@RequestBody @Validated TokenRequest request) {
|
|
UserInfoResponse userInfo = tokenService.getUserInfoByToken(request.getToken());
|
|
return Result.success(userInfo);
|
|
}
|
|
|
|
/**
|
|
* 通过token获取用户名
|
|
*/
|
|
@PostMapping("/username")
|
|
public Result<String> getUsernameByToken(@RequestBody @Validated TokenRequest request) {
|
|
String username = tokenService.getUsernameByToken(request.getToken());
|
|
return Result.success(username);
|
|
}
|
|
|
|
/**
|
|
* 验证token并返回用户ID
|
|
*/
|
|
@PostMapping("/validate")
|
|
public Result<String> validateTokenAndGetUserId(@RequestBody @Validated TokenRequest request) {
|
|
String userId = tokenService.validateTokenAndGetUserId(request.getToken());
|
|
return Result.success(userId);
|
|
}
|
|
} |