forked from gxwebsoft/mp-10550
fix(dealer): 修复经销商提现功能中的金额处理问题
- 添加 normalizeMoneyString 函数统一处理后端返回的金额数据类型 - 使用 normalizeMoneyString 替代直接访问 dealerUser.money 确保金额始终为字符串 - 修改金额验证逻辑确保数值转换的准确性 - 更新格式化金额函数支持未知类型输入并添加数值有效性检查 - 修复 Radio.Group 控件值更新时表单字段同步问题
This commit is contained in:
@@ -25,6 +25,12 @@ interface WithdrawRecordWithDetails extends ShopDealerWithdraw {
|
||||
accountDisplay?: string
|
||||
}
|
||||
|
||||
// Some backends may return money fields as number; keep internal usage always as string.
|
||||
const normalizeMoneyString = (money: unknown) => {
|
||||
if (money === null || money === undefined || money === '') return '0.00'
|
||||
return typeof money === 'string' ? money : String(money)
|
||||
}
|
||||
|
||||
const DealerWithdraw: React.FC = () => {
|
||||
const [activeTab, setActiveTab] = useState<string | number>('0')
|
||||
const [selectedAccount, setSelectedAccount] = useState('')
|
||||
@@ -52,7 +58,7 @@ const DealerWithdraw: React.FC = () => {
|
||||
const fetchBalance = useCallback(async () => {
|
||||
console.log(dealerUser, 'dealerUser...')
|
||||
try {
|
||||
setAvailableAmount(dealerUser?.money || '0.00')
|
||||
setAvailableAmount(normalizeMoneyString(dealerUser?.money))
|
||||
} catch (error) {
|
||||
console.error('获取余额失败:', error)
|
||||
}
|
||||
@@ -163,8 +169,8 @@ const DealerWithdraw: React.FC = () => {
|
||||
}
|
||||
|
||||
// 验证提现金额
|
||||
const amount = parseFloat(values.amount)
|
||||
const available = parseFloat(availableAmount.replace(/,/g, ''))
|
||||
const amount = parseFloat(String(values.amount))
|
||||
const available = parseFloat(normalizeMoneyString(availableAmount).replace(/,/g, ''))
|
||||
|
||||
if (isNaN(amount) || amount <= 0) {
|
||||
Taro.showToast({
|
||||
@@ -266,13 +272,13 @@ const DealerWithdraw: React.FC = () => {
|
||||
}
|
||||
|
||||
const setAllAmount = () => {
|
||||
formRef.current?.setFieldsValue({amount: availableAmount.replace(/,/g, '')})
|
||||
formRef.current?.setFieldsValue({amount: normalizeMoneyString(availableAmount).replace(/,/g, '')})
|
||||
}
|
||||
|
||||
// 格式化金额
|
||||
const formatMoney = (money?: string) => {
|
||||
if (!money) return '0.00'
|
||||
return parseFloat(money).toFixed(2)
|
||||
const formatMoney = (money?: unknown) => {
|
||||
const n = parseFloat(normalizeMoneyString(money).replace(/,/g, ''))
|
||||
return Number.isFinite(n) ? n.toFixed(2) : '0.00'
|
||||
}
|
||||
|
||||
const renderWithdrawForm = () => (
|
||||
@@ -320,8 +326,8 @@ const DealerWithdraw: React.FC = () => {
|
||||
type="number"
|
||||
onChange={(value) => {
|
||||
// 实时验证提现金额
|
||||
const amount = parseFloat(value)
|
||||
const available = parseFloat(availableAmount.replace(/,/g, ''))
|
||||
const amount = parseFloat(String(value))
|
||||
const available = parseFloat(normalizeMoneyString(availableAmount).replace(/,/g, ''))
|
||||
if (!isNaN(amount) && amount > available) {
|
||||
// 可以在这里添加实时提示,但不阻止输入
|
||||
}
|
||||
@@ -354,7 +360,15 @@ const DealerWithdraw: React.FC = () => {
|
||||
</View>
|
||||
|
||||
<Form.Item name="accountType" label="提现方式" required>
|
||||
<Radio.Group value={selectedAccount} onChange={() => setSelectedAccount}>
|
||||
<Radio.Group
|
||||
value={selectedAccount}
|
||||
onChange={(value) => {
|
||||
const next = String(value)
|
||||
setSelectedAccount(next)
|
||||
// Ensure Form gets the field value even when Radio.Group is controlled.
|
||||
formRef.current?.setFieldsValue({accountType: next})
|
||||
}}
|
||||
>
|
||||
<Cell.Group>
|
||||
<Cell>
|
||||
<Radio value="wechat">微信钱包</Radio>
|
||||
|
||||
Reference in New Issue
Block a user