Преглед на файлове

fix: 引入bignumber解决js计算精度丢失的问题

lanjianrong преди 4 години
родител
ревизия
d148e4fb1f

+ 6 - 5
src/pages/Contract/Content/Income/components/Modal/index.tsx

@@ -14,6 +14,7 @@ import dayjs from 'dayjs'
 import React, { useEffect, useState, useMemo } from 'react'
 import { apiGetReturnWay } from '../Tabs/Receivable/api'
 import styles from './index.module.scss'
+import { BigNumber } from "bignumber.js"
 interface ContractSection {
   id: string;
   treeType: number;
@@ -123,10 +124,10 @@ const ContractModal: React.FC<iModalCommonProps> = ({ modalObj: { type, visible,
   }
 
   // 处理添加回款的金额不应该超出最大值
-  const maxPrice = parseFloat(contractStore.contract.price) - parseFloat(contractType === ContractType.INCOME ? contractStore.contract.returned : contractStore.contract.paid)
+  // const maxPrice = parseFloat(contractStore.contract.price) - parseFloat(contractType === ContractType.INCOME ? contractStore.contract.returned : contractStore.contract.paid)
+  const maxPrice = new BigNumber(contractStore.contract.price).minus(contractType === ContractType.INCOME ? contractStore.contract.returned : contractStore.contract.paid)
 
-
-  const minPrice = parseFloat(contractType === ContractType.INCOME ? row.contractReturned : row.contractsPaid)
+  const minPrice = new BigNumber(contractType === ContractType.INCOME ? row.contractReturned : row.contractsPaid)
   return (
     <Modal
       getContainer={false}
@@ -230,7 +231,7 @@ const ContractModal: React.FC<iModalCommonProps> = ({ modalObj: { type, visible,
             </Form.Item>
             <Form.Item label="合同金额" name="price" rules={[ { required: true, message: '请输入合同金额' }, () => ({
               validator(_, value) {
-                if (value && parseFloat(value) < minPrice) {
+                if (value && (new BigNumber(value).lt(minPrice))) {
                   return Promise.reject(`当前金额不能低于${minPrice}`)
                 }
                 return Promise.resolve()
@@ -316,7 +317,7 @@ const ContractModal: React.FC<iModalCommonProps> = ({ modalObj: { type, visible,
             </Form.Item>
             <Form.Item name="price" label={`${contractType === ContractType.INCOME ? '回款' : '支付'}金额`} rules={[ { required: true, message: `请输入${contractType === ContractType.INCOME ? '回款' : '支付'}金额` }, () => ({
               validator(_, value) {
-                if (value && parseFloat(value) > maxPrice) {
+                if (value && (new BigNumber(value).gt(maxPrice))) {
                   return Promise.reject(`当前金额不能大于${maxPrice}`)
                 }
                 return Promise.resolve()

+ 1 - 3
src/pages/Contract/Content/Income/components/Tabs/Detail/index.tsx

@@ -16,10 +16,8 @@ const Detail:React.FC<DetailProps> = (props) => {
   const type_name = type === ContractType.INCOME ? '回款' : '支付'
   const progress = useMemo(() => {
     const i = new BigNumber(type === ContractType.INCOME ? contract.returned : contract.paid).dividedBy(contract.price).toFixed(2)
-    // const i = parseFloat((parseFloat(type === ContractType.INCOME ? contract.returned : contract.paid) / parseFloat()).toFixed(2))
-
     const j = new BigNumber(1).minus(i)
-    const k: number = parseFloat(contract.price) - parseFloat(type === ContractType.INCOME ? contract.returned : contract.paid)
+    const k = new BigNumber(contract.price).minus(type === ContractType.INCOME ? contract.returned : contract.paid)
     return { returned: new BigNumber(i).multipliedBy(100) + '%', unReturned: new BigNumber(j).multipliedBy(100) + '%', unReturnedMoney: k }
   }, [ contract ])
   return contract.id ? (

+ 2 - 1
src/pages/Quality/Content/List/index.tsx

@@ -50,6 +50,7 @@ const QualityList: React.FC<{}> = () => {
   const columns: ColumnsType<iQualityList> = [
     {
       title: '序号',
+      align: 'center',
       // eslint-disable-next-line react/display-name
       render: (_: string, record: any, i: number) => {
         return <span>{i + 1}</span>
@@ -77,7 +78,7 @@ const QualityList: React.FC<{}> = () => {
     },
     {
       title: '检查日期',
-      dataIndex: 'create_time',
+      dataIndex: 'createTime',
       // eslint-disable-next-line react/display-name
       render: (text: string) => {
         return <span>{dayjsFormat(text, 'YYYY-MM-DD')}</span>

+ 2 - 1
src/pages/Safe/Content/List/index.tsx

@@ -26,6 +26,7 @@ const SafeList: React.FC<{}> = () => {
   const columns: ColumnsType<iSafeList> = [
     {
       title: '序号',
+      align: 'center',
       // eslint-disable-next-line react/display-name
       render: (_: string, record: any, i: number) => {
         return <span>{i + 1}</span>
@@ -53,7 +54,7 @@ const SafeList: React.FC<{}> = () => {
     },
     {
       title: '检查日期',
-      dataIndex: 'create_time',
+      dataIndex: 'createTime',
       // eslint-disable-next-line react/display-name
       render: (text: string) => {
         return <span>{dayjsFormat(text, 'YYYY-MM-DD')}</span>

+ 3 - 12
src/utils/util.ts

@@ -4,6 +4,7 @@ import { message } from 'antd'
 import dayjs from 'dayjs'
 import consts from './consts'
 import history from './history'
+import { BigNumber } from "bignumber.js"
 
 function getCookie(name: string) {
   const prefix = name + '='
@@ -207,21 +208,11 @@ const formatDate = (d: string) => {
 }
 
 // 数字千分位
-const formatMoney = (num: string | number | undefined) => {
+const formatMoney = (num: string | number | undefined | BigNumber.Value) => {
   if (!num) return '0.00'
-  if (typeof num === 'number' && isNaN(num)) {
-    return '0.00'
-  }
-  let newNum = ''
-  if (typeof num === 'string') {
-    newNum = parseFloat(num).toFixed()
-  } else {
-    // number类型
-    newNum = num.toFixed()
-  }
+  const newNum = new BigNumber(num).toFormat()
   return newNum.replace(/\d{1,3}(?=(\d{3})+(\.\d*)?$)/g, '$&,')
 }
-
 // 控制是否有权限进入标段
 const handleIntoBidsection = async (permission_type: 'contract' | 'safe' | 'quality', bid: string, name: string) => {
   await tenderStore.saveTenderPermission()