refactor: 金币默认值改为数据库层面设置
- UserInfo实体类coins字段添加默认值DEFAULT 3 - 移除前后端硬编码的金币默认值 - 新增consumeCoins API接口用于金币消耗 - coinUtils.js改为从服务器获取金币数据 - 所有测算页面checkCoinsAndConsume改为异步调用 - VIP用户测算不消耗金币
This commit is contained in:
1 parent
b9a4a9150e
commit
f5f214a100
14 files changed
+141
-38
No files matched your search
@@ -266,4 +266,31 @@ public class UserController {
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@PostMapping("/consumeCoins")
|
||||
public Map<String, Object> consumeCoins(@RequestBody Map<String, Object> params) {
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
try {
|
||||
String userId = params.get("userId").toString();
|
||||
Integer amount = params.get("amount") != null ? Integer.parseInt(params.get("amount").toString()) : 1;
|
||||
String type = params.get("type") != null ? params.get("type").toString() : "unknown";
|
||||
String description = params.get("description") != null ? params.get("description").toString() : "金币消耗";
|
||||
String relatedId = params.get("relatedId") != null ? params.get("relatedId").toString() : null;
|
||||
|
||||
UserInfo user = userInfoService.consumeCoins(userId, amount, type, description, relatedId);
|
||||
if (user != null) {
|
||||
Map<String, Object> data = new HashMap<>();
|
||||
data.put("coins", user.getCoins());
|
||||
result.put("success", true);
|
||||
result.put("data", data);
|
||||
} else {
|
||||
result.put("success", false);
|
||||
result.put("error", "用户不存在或金币不足");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
result.put("success", false);
|
||||
result.put("error", e.getMessage());
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -31,6 +31,7 @@ public class UserInfo {
|
||||
|
||||
private Integer vipLevel;
|
||||
|
||||
@Column(columnDefinition = "INT DEFAULT 3")
|
||||
private Integer coins;
|
||||
|
||||
private Integer streakDays;
|
||||
@@ -45,7 +46,6 @@ public class UserInfo {
|
||||
createTime = LocalDateTime.now();
|
||||
updateTime = createTime;
|
||||
vipLevel = 0;
|
||||
coins = 3;
|
||||
streakDays = 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -28,7 +28,6 @@ public class UserInfoService {
|
||||
user.setUsername(generateUsername());
|
||||
user.setUserId(generateUserId());
|
||||
user.setVipLevel(0);
|
||||
user.setCoins(3);
|
||||
user.setStreakDays(0);
|
||||
|
||||
return userInfoRepository.save(user);
|
||||
@@ -119,6 +118,21 @@ public class UserInfoService {
|
||||
return false;
|
||||
}
|
||||
|
||||
public UserInfo consumeCoins(String userId, Integer amount, String type, String description, String relatedId) {
|
||||
UserInfo user = userInfoRepository.findByUserId(userId);
|
||||
if (user != null) {
|
||||
if (user.getVipLevel() != null && user.getVipLevel() > 0) {
|
||||
return user;
|
||||
}
|
||||
if (user.getCoins() < amount) {
|
||||
return null;
|
||||
}
|
||||
user.setCoins(user.getCoins() - amount);
|
||||
return userInfoRepository.save(user);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private String generateUsername() {
|
||||
String prefix = "玄机用户";
|
||||
String suffix = generateRandomString(6);
|
||||
|
||||
Reference in new issue
Block a user