refactor: 金币默认值改为数据库层面设置

- UserInfo实体类coins字段添加默认值DEFAULT 3
- 移除前后端硬编码的金币默认值
- 新增consumeCoins API接口用于金币消耗
- coinUtils.js改为从服务器获取金币数据
- 所有测算页面checkCoinsAndConsume改为异步调用
- VIP用户测算不消耗金币
This commit is contained in:
H1d3rOne committed 2026-03-10 03:19:40 +08:00
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;
}
}
+1 -1
View File
@@ -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);
@@ -221,7 +221,7 @@ export default {
uni.setStorageSync('userId', res.data.userId)
uni.setStorageSync('userInfo', JSON.stringify(res.data))
uni.setStorageSync('userCoins', res.data.coins || 3)
uni.setStorageSync('userCoins', res.data.coins || 0)
uni.setStorageSync('vipLevel', res.data.vipLevel || 0)
uni.setStorageSync('username', res.data.nickname || this.nickname)
uni.setStorageSync('userAvatar', res.data.avatarUrl || this.avatarUrl)
+1 -1
View File
@@ -114,7 +114,7 @@ export default {
async calculate() {
if (this.loading) return
const checkResult = coinUtils.checkCoinsAndConsume()
const checkResult = await coinUtils.checkCoinsAndConsume()
if (!checkResult.canUse) {
return
}
@@ -140,7 +140,7 @@ export default {
async calculate() {
if (this.loading) return
const checkResult = coinUtils.checkCoinsAndConsume()
const checkResult = await coinUtils.checkCoinsAndConsume()
if (!checkResult.canUse) {
return
}
@@ -136,7 +136,7 @@ export default {
async calculate() {
if (this.loading) return
const checkResult = coinUtils.checkCoinsAndConsume()
const checkResult = await coinUtils.checkCoinsAndConsume()
if (!checkResult.canUse) {
return
}
@@ -154,7 +154,7 @@ export default {
async calculate() {
if (this.loading) return
const checkResult = coinUtils.checkCoinsAndConsume()
const checkResult = await coinUtils.checkCoinsAndConsume()
if (!checkResult.canUse) {
return
}
+1 -1
View File
@@ -172,7 +172,7 @@ export default {
async calculate() {
if (this.loading) return
const checkResult = coinUtils.checkCoinsAndConsume()
const checkResult = await coinUtils.checkCoinsAndConsume()
if (!checkResult.canUse) {
return
}
+1 -1
View File
@@ -79,7 +79,7 @@ export default {
async startDivine() {
if (this.loading) return
const checkResult = coinUtils.checkCoinsAndConsume()
const checkResult = await coinUtils.checkCoinsAndConsume()
if (!checkResult.canUse) {
return
}
@@ -92,7 +92,7 @@ export default {
async calculate() {
if (this.loading) return
const checkResult = coinUtils.checkCoinsAndConsume()
const checkResult = await coinUtils.checkCoinsAndConsume()
if (!checkResult.canUse) {
return
}
+1 -1
View File
@@ -114,7 +114,7 @@ export default {
async calculate() {
if (this.loading) return
const checkResult = coinUtils.checkCoinsAndConsume()
const checkResult = await coinUtils.checkCoinsAndConsume()
if (!checkResult.canUse) {
return
}
+13
View File
@@ -394,6 +394,19 @@ const userApi = {
method: 'POST',
data: { userId: userId }
})
},
consumeCoins: (userId, amount, type, description, relatedId) => {
return request({
url: '/user/consumeCoins',
method: 'POST',
data: {
userId: userId,
amount: amount,
type: type,
description: description,
relatedId: relatedId
}
})
}
}
+76 -27
View File
@@ -1,15 +1,48 @@
import api from '@/utils/api.js'
const checkCoinsAndConsume = () => {
const checkCoinsAndConsume = async () => {
const vipLevel = uni.getStorageSync('vipLevel') || 0
if (vipLevel > 0) {
return { canUse: true, needCoins: false }
}
try {
const userInfoStr = uni.getStorageSync('userInfo')
if (!userInfoStr) {
return { canUse: false, needCoins: true }
}
const userInfo = JSON.parse(userInfoStr)
const result = await api.userApi.getUserInfo(userInfo.userId)
if (result && result.success && result.data) {
const coins = result.data.coins || 0
uni.setStorageSync('userCoins', coins)
if (coins < 1) {
uni.showModal({
title: '金币不足',
content: '您的金币不足,请前往任务中心获取金币或开通会员',
confirmText: '去获取',
cancelText: '取消',
success: (res) => {
if (res.confirm) {
uni.navigateTo({ url: '/pages/mine/coins' })
}
}
})
return { canUse: false, needCoins: true }
}
return { canUse: true, needCoins: false }
}
} catch (e) {
console.error('检查金币失败:', e)
}
let coins = uni.getStorageSync('userCoins')
if (coins === null || coins === undefined || coins === '') {
coins = 3
uni.setStorageSync('userCoins', coins)
coins = 0
} else {
coins = parseInt(coins)
}
@@ -38,37 +71,53 @@ const consumeCoin = async (type = 'bazi', description = '测算消费', relatedI
return
}
let coins = uni.getStorageSync('userCoins')
if (coins === null || coins === undefined || coins === '') {
coins = 3
} else {
coins = parseInt(coins)
}
coins -= 1
uni.setStorageSync('userCoins', coins)
try {
const userInfoStr = uni.getStorageSync('userInfo')
if (userInfoStr) {
const userInfo = JSON.parse(userInfoStr)
await api.userApi.updateCoins(userInfo.userId, coins)
const recordData = {
userId: userInfo.userId,
amount: 1,
type: type,
description: description,
relatedId: relatedId
}
await api.coinConsumeApi.save(recordData)
if (!userInfoStr) {
console.error('用户信息不存在')
return
}
const userInfo = JSON.parse(userInfoStr)
const result = await api.userApi.consumeCoins(userInfo.userId, 1, type, description, relatedId)
if (result && result.success) {
const newCoins = result.data?.coins || 0
uni.setStorageSync('userCoins', newCoins)
console.log('金币消耗成功,剩余:', newCoins)
} else {
console.error('金币消耗失败:', result)
}
} catch (e) {
console.error('更新金币失败:', e)
console.error('消耗金币失败:', e)
}
}
const getCoins = async () => {
try {
const userInfoStr = uni.getStorageSync('userInfo')
if (!userInfoStr) {
return 0
}
const userInfo = JSON.parse(userInfoStr)
const result = await api.userApi.getUserInfo(userInfo.userId)
if (result && result.success && result.data) {
const coins = result.data.coins || 0
uni.setStorageSync('userCoins', coins)
return coins
}
} catch (e) {
console.error('获取金币失败:', e)
}
return uni.getStorageSync('userCoins') || 0
}
export default {
checkCoinsAndConsume,
consumeCoin
consumeCoin,
getCoins
}