This commit is contained in:
雷欧(林平凡)
2025-04-01 17:40:20 +08:00
parent 0fdc491f9e
commit 0e09457f0e

View File

@@ -27,6 +27,7 @@ import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
@@ -717,13 +718,15 @@ private void handleGiftMoneyFlow(String fromWxid, String message, UserInteractio
}
amount = Double.parseDouble(message);
logger.debug("校验金额:{} 元商品价格的80%{}", amount, 0.8 * Double.parseDouble(state.getCollectedFields().get("price"))); // 新增
// 格式化后输出日志
String formattedAmount = String.format("%.2f", amount);
logger.debug("校验金额:{} 元商品价格的80%{}", formattedAmount, 0.8 * Double.parseDouble(state.getCollectedFields().get("price"))); // 新增
if (amount < 1 || amount > 0.8 * Double.parseDouble(state.getCollectedFields().get("price"))) {
wxUtil.sendTextMessage(fromWxid, "❌ 金额需≥1元且≤商品价格的80%", 1, fromWxid);
return;
}
state.getCollectedFields().put("amount", String.valueOf(amount));
state.getCollectedFields().put("amount", formattedAmount);
state.setCurrentStep(STEP_QUANTITY);
break;
@@ -896,9 +899,21 @@ public boolean stopGiftCoupon(String giftKey) throws Exception {
private boolean isValidAmount(String input) {
return input.matches("^\\d+(\\.\\d{1,2})?$");
private boolean isValidAmount(String input) {
if (Util.isNotEmpty(input)) {
try {
double amount = Double.parseDouble(input);
// 校验是否为两位小数(可选)
BigDecimal bd = new BigDecimal(input);
if (bd.scale() > 2) return false; // 精度超过两位返回 false
return amount >= 1 && amount <= 50;
} catch (NumberFormatException e) {
return false;
}
}
return false;
}