feat:jiekou

This commit is contained in:
xiang
2026-03-21 20:38:38 +08:00
parent 4ea369b33d
commit ace15206ec
5 changed files with 54 additions and 1 deletions

View File

@@ -7,6 +7,7 @@ import com.xiang.xservice.auth.api.dto.resp.LoginResp;
import com.xiang.xservice.auth.api.dto.resp.RegisterResp;
import com.xiang.xservice.basic.common.resp.Result;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
@@ -16,4 +17,8 @@ public interface TokenApi {
Result<LoginResp> login(@RequestBody @NotNull(message = "请求参数不能为空") @Valid LoginRequest request);
Result<RegisterResp> register(@RequestBody @Valid @NotNull(message = "请求参数不能为空") RegisterRequest request);
Result<Long> getUserId(@RequestParam(value = "token", required = true) String token);
Result<Long> getTenantId(@RequestParam(value = "token", required = true)String token);
}

View File

@@ -12,7 +12,8 @@ public enum Code01UserErrorCode implements BaseErrorCode {
USER_EXISTS("A1000102", "用户已存在"),
USER_LOGIN_ERROR("A1000103", "用户登录失败!"),
USER_REGISTER_ERROR("1000104", "用户注册失败!"),
REFRESH_TOKEN_NOT_EXISTS("1000105", "refreshToken不匹配")
REFRESH_TOKEN_NOT_EXISTS("1000105", "refreshToken不匹配"),
TOKEN_NOT_VALID("1000106", "token校验失败"),
;
private final String code;

View File

@@ -13,8 +13,10 @@ import com.xiang.xservice.basic.common.resp.Result;
import com.xiang.xservice.basic.exception.BusinessException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.validation.Valid;
@@ -73,6 +75,26 @@ public class TokenController implements TokenApi {
return Result.error("操作失败");
}
@Override
@GetMapping("/private/user/getUserId")
public Result<Long> getUserId(@RequestParam("token") String token) {
try {
return Result.data(userService.getUserId(token));
} catch (Exception e) {
return Result.error(e.getMessage());
}
}
@Override
@GetMapping("/private/user/getTenantId")
public Result<Long> getTenantId(@RequestParam("token") String token) {
try {
return Result.data(userService.getTenantId(token));
} catch (Exception e) {
return Result.error(e.getMessage());
}
}
@PostMapping("/private/auth/getInfo")
public Result<UserDTO> getUserInfo(@RequestBody @Valid @NotNull(message = "请求参数不能为空") UserQueryRequest request) {
return Result.data(userService.getUserDetail(request.getUsername()));

View File

@@ -43,4 +43,7 @@ public interface XUserService {
LoginResp refresh(RefreshRequest request);
Boolean updateStatus(Long id, Integer status);
Long getUserId(String token);
Long getTenantId(String token);
}

View File

@@ -413,4 +413,26 @@ public class XUserServiceImpl implements XUserService {
user.setStatus(status);
return userMapper.update(user) > 0;
}
@Override
public Long getUserId(String token) {
try {
Jwt jwt = jwtDecoder.decode(token);
Object userId = jwt.getClaim("userId");
return (long) userId;
} catch (Exception e) {
throw new BusinessException(Code01UserErrorCode.TOKEN_NOT_VALID);
}
}
@Override
public Long getTenantId(String token) {
try {
Jwt jwt = jwtDecoder.decode(token);
Object userId = jwt.getClaim("tenantId");
return (long) userId;
} catch (Exception e) {
throw new BusinessException(Code01UserErrorCode.TOKEN_NOT_VALID);
}
}
}