diff --git a/src/views/refund/addCoinRefund.vue b/src/views/refund/addCoinRefund.vue index c7e398c..586c8bc 100644 --- a/src/views/refund/addCoinRefund.vue +++ b/src/views/refund/addCoinRefund.vue @@ -322,35 +322,61 @@ const handleSelectionChange = (selectedOption) => { } console.log('选择的商品', selectedOption); } -// 验证输入的金币数量是否超过商品花费的金币数量 -const validateGoldInput = (type, value) => { +// 统一的输入处理函数 +const handleGoldInput = (type, value) => { + // 1. 过滤非法字符(只允许数字和小数点) + let filtered = value.replace(/[^\d.]/g, ''); + + // 2. 防止多个小数点 + const dotCount = (filtered.match(/\./g) || []).length; + if (dotCount > 1) { + // 保留第一个小数点,移除后续的小数点 + const parts = filtered.split('.'); + filtered = parts[0] + '.' + parts.slice(1).join(''); + } + + // 3. 限制小数点后两位 + if (filtered.includes('.')) { + const [integer, decimal] = filtered.split('.'); + if (decimal.length > 2) { + filtered = `${integer}.${decimal.slice(0, 2)}`; + } + } + + // 4. 检查是否以小数点开头,如果是,在前面添加0 + if (filtered.startsWith('.')) { + filtered = '0' + filtered; + } + + // 5. 更新值 + addRefund.value[type] = filtered; + + // 6. 验证逻辑(这里直接应用修正后的值) const maxValue = selectedGoodsGold.value[type]; - const inputValue = Number(value); + const inputValue = Number(filtered || 0); + if (isNaN(inputValue)) { - return 0; + return; } + if (inputValue > maxValue) { - // 当输入金额大于商品金额时,显示提示信息 + // 修正为最大值并设置回输入框 + const correctedValue = maxValue.toFixed(2); + addRefund.value[type] = correctedValue; ElMessage.warning('所填金额大于该类金币余额'); - return maxValue; } - return Math.min(inputValue, maxValue); -} - -// 处理永久金币输入 -const handlePermanentGoldInput = (value) => { - addRefund.value.permanentGold = validateGoldInput('permanentGold', value); -} - -// 处理免费金币输入 -const handleFreeGoldInput = (value) => { - addRefund.value.freeGold = validateGoldInput('freeGold', value); + + if (inputValue < 0) { + // 修正为0并设置回输入框 + addRefund.value[type] = '0'; + ElMessage.warning('不能输入负数'); + } } -// 处理任务金币输入 -const handleTaskGoldInput = (value) => { - addRefund.value.taskGold = validateGoldInput('taskGold', value); -} +// 使用示例 +const handlePermanentGoldInput = (value) => handleGoldInput('permanentGold', value); +const handleFreeGoldInput = (value) => handleGoldInput('freeGold', value); +const handleTaskGoldInput = (value) => handleGoldInput('taskGold', value); // 计算总金币 const calculatedRechargeGoods = computed(() => { @@ -442,9 +468,9 @@ onMounted(async function () { style="width: 100px" :disabled="addRe.typeR === '0' ? true : false" @input="handlePermanentGoldInput($event)" - type="number" + > - +

@@ -467,7 +493,7 @@ onMounted(async function () { style="float: left; width: 100px" :disabled="addRe.typeR === '0' ? true : false" @input="handleTaskGoldInput($event)" - type="number" + />