feat(wallet): 新增余额格式化显示字段

在响应对象中添加 balanceDisplay 字段,用于返回“K”缩写格式的大额余额,
This commit is contained in:
2025-12-11 14:38:53 +08:00
parent f937b03940
commit 567a8bf165
3 changed files with 18 additions and 10 deletions

View File

@@ -17,4 +17,10 @@ public class KeyboardUserWalletRespVO {
*/
@Schema(description = "余额")
private BigDecimal balance;
/**
* 格式化后的余额显示
*/
@Schema(description = "格式化后的余额显示")
private String balanceDisplay;
}

View File

@@ -1,8 +1,5 @@
package com.yolo.keyborad.service.impl;
import cn.hutool.core.bean.BeanUtil;
import com.yolo.keyborad.common.ErrorCode;
import com.yolo.keyborad.exception.BusinessException;
import com.yolo.keyborad.model.vo.wallet.KeyboardUserWalletRespVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
@@ -28,12 +25,17 @@ public class KeyboardUserWalletServiceImpl extends ServiceImpl<KeyboardUserWalle
.eq(KeyboardUserWallet::getUserId, userId)
.one();
KeyboardUserWalletRespVO respVO = new KeyboardUserWalletRespVO();
if (wallet == null) {
// 如果用户没有钱包记录返回余额为0
respVO.setBalance(BigDecimal.ZERO);
} else {
respVO.setBalance(wallet.getBalance());
}
BigDecimal balance = (wallet == null) ? BigDecimal.ZERO : wallet.getBalance();
respVO.setBalance(balance);
respVO.setBalanceDisplay(formatBalance(balance));
return respVO;
}
private String formatBalance(BigDecimal balance) {
if (balance.compareTo(new BigDecimal("10000")) >= 0) {
BigDecimal kValue = balance.divide(new BigDecimal("1000"), 2, BigDecimal.ROUND_HALF_UP);
return kValue.stripTrailingZeros().toPlainString() + "K";
}
return balance.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString();
}
}

View File

@@ -87,7 +87,7 @@ sa-token:
# token 最低活跃频率(单位:秒),如果 token 超过此时间没有访问系统就会被冻结,默认-1 代表不限制,永不冻结
active-timeout: -1
# 是否允许同一账号多地同时登录 (为 true 时允许一起登录, 为 false 时新登录挤掉旧登录)
is-concurrent: false
is-concurrent: true
# 在多人登录同一账号时,是否共用一个 token (为 true 时所有登录共用一个 token, 为 false 时每次登录新建一个 token
is-share: false
# token 风格默认可取值uuid、simple-uuid、random-32、random-64、random-128、tik