feat: 添加获取当前用户积分余额的接口

This commit is contained in:
zyh
2025-08-29 23:55:34 +08:00
parent 64f9e6ef17
commit cc69ac1fee
6 changed files with 291 additions and 1 deletions

View File

@@ -3,6 +3,7 @@ package com.gameplatform.server.controller.admin;
import com.gameplatform.server.model.dto.account.*;
import com.gameplatform.server.model.dto.common.PageResult;
import com.gameplatform.server.service.account.AccountService;
import com.gameplatform.server.security.JwtService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
@@ -16,9 +17,11 @@ import reactor.core.publisher.Mono;
@Tag(name = "管理员账户管理", description = "管理员账户的增删改查操作")
public class AccountController {
private final AccountService accountService;
private final JwtService jwtService;
public AccountController(AccountService accountService) {
public AccountController(AccountService accountService, JwtService jwtService) {
this.accountService = accountService;
this.jwtService = jwtService;
}
@GetMapping
@@ -72,5 +75,27 @@ public class AccountController {
public Mono<Void> resetPassword(@Parameter(description = "账户ID") @PathVariable Long id, @Valid @RequestBody ResetPasswordRequest req) {
return accountService.resetPassword(id, req.getNewPassword(), Boolean.TRUE.equals(req.getForceLogout()));
}
@GetMapping("/me/points-balance")
@Operation(summary = "获取当前用户积分余额", description = "根据token解析用户ID并获取当前用户的积分余额")
public Mono<PointsBalanceResponse> getCurrentUserPointsBalance(
@Parameter(hidden = true) @RequestHeader("Authorization") String authHeader) {
return Mono.fromCallable(() -> {
if (authHeader == null || !authHeader.startsWith("Bearer ")) {
throw new IllegalArgumentException("Authorization header is required");
}
String token = authHeader.substring(7);
io.jsonwebtoken.Claims claims = jwtService.parse(token);
Long userId = claims.get("userId", Long.class);
if (userId == null) {
throw new IllegalArgumentException("Invalid token: userId not found");
}
return userId;
})
.flatMap(accountService::getCurrentUserPointsBalance);
}
}

View File

@@ -0,0 +1,50 @@
package com.gameplatform.server.model.dto.account;
import io.swagger.v3.oas.annotations.media.Schema;
@Schema(description = "用户积分余额响应")
public class PointsBalanceResponse {
@Schema(description = "用户ID")
private Long userId;
@Schema(description = "用户名")
private String username;
@Schema(description = "用户类型")
private String userType;
@Schema(description = "积分余额")
private Long pointsBalance;
public Long getUserId() {
return userId;
}
public void setUserId(Long userId) {
this.userId = userId;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getUserType() {
return userType;
}
public void setUserType(String userType) {
this.userType = userType;
}
public Long getPointsBalance() {
return pointsBalance;
}
public void setPointsBalance(Long pointsBalance) {
this.pointsBalance = pointsBalance;
}
}

View File

@@ -4,6 +4,7 @@ import com.gameplatform.server.mapper.account.UserAccountMapper;
import com.gameplatform.server.model.dto.account.AccountCreateRequest;
import com.gameplatform.server.model.dto.account.AccountResponse;
import com.gameplatform.server.model.dto.account.AccountUpdateRequest;
import com.gameplatform.server.model.dto.account.PointsBalanceResponse;
import com.gameplatform.server.model.dto.common.PageResult;
import com.gameplatform.server.model.entity.account.UserAccount;
import org.springframework.security.crypto.password.PasswordEncoder;
@@ -144,6 +145,24 @@ public class AccountService {
.then();
}
public Mono<PointsBalanceResponse> getCurrentUserPointsBalance(Long userId) {
return Mono.fromCallable(() -> {
UserAccount account = mapper.selectById(userId);
if (account == null) {
throw new IllegalArgumentException("用户不存在");
}
PointsBalanceResponse response = new PointsBalanceResponse();
response.setUserId(account.getId());
response.setUsername(account.getUsername());
response.setUserType(account.getUserType());
response.setPointsBalance(account.getPointsBalance());
return response;
})
.subscribeOn(Schedulers.boundedElastic());
}
private AccountResponse toResp(UserAccount a) {
if (a == null) return null;
AccountResponse r = new AccountResponse();