daily-share icon indicating copy to clipboard operation
daily-share copied to clipboard

JS 最大安全数的问题 (2024-06-15)

Open yaogengzhu opened this issue 8 months ago • 0 comments

最近在一个需求开发中,遇到一个BUG。 JAVA中给了一个超级大的数字过来, axios 请求后,将数字进行了转换,导致前后数据对应不上。

处理方案如下,具体的细节可以百度下!

const jsonBig = require('json-bigint');
async function newRequest(url, method, data, headers = {}) {
  try {
    const baseUrl = 'xxxxx';
    const result = await axios({
      url: baseUrl + url,
      method,
      headers: Object.assign(headers, {
        'Auth-Id': xxxx,
        Authorization: xxxx
      }),
      [method === 'get' ? 'params' : 'data']: data,
      transformResponse: [
        function (data) {
          try {
            return jsonBig({ storeAsString: true }).parse(data);
            // return jsonBig.parse(data);
          } catch (err) {
            return {
              data,
            };
          }
        },
      ],
    });
    return result.data;
  } catch (e) {
    return {
      code: 0,
      message: e.reponse,
    };
  }
}

yaogengzhu avatar Jun 15 '24 07:06 yaogengzhu