fix: 修复扣款时浮点数精度导致的余额不足误判和 -0.00 显示问题

This commit is contained in:
小海
2026-01-12 14:03:56 +08:00
parent b76cc583fb
commit 53e730f8d5

View File

@@ -35,14 +35,22 @@ const emit = defineEmits(['close', 'success']); const { t } = useI18n(); const a
const submitting = ref(false); const form = reactive({ amount: 0, notes: '' })
watch(() => props.show, (v) => { if(v) { form.amount = 0; form.notes = '' } })
const calculateNewBalance = () => (props.user ? (props.operation === 'add' ? props.user.balance + form.amount : props.user.balance - form.amount) : 0)
const calculateNewBalance = () => {
if (!props.user) return 0
const result = props.operation === 'add' ? props.user.balance + form.amount : props.user.balance - form.amount
// 避免浮点数精度问题导致的 -0.00 显示
return result === 0 || Object.is(result, -0) ? 0 : result
}
const handleBalanceSubmit = async () => {
if (!props.user) return
if (!form.amount || form.amount <= 0) {
appStore.showError(t('admin.users.amountRequired'))
return
}
if (props.operation === 'subtract' && form.amount > props.user.balance) {
// 使用小数点后两位精度比较,避免浮点数精度问题
const amount = Math.round(form.amount * 100) / 100
const balance = Math.round(props.user.balance * 100) / 100
if (props.operation === 'subtract' && amount > balance) {
appStore.showError(t('admin.users.insufficientBalance'))
return
}