梦应逍遥 发表于 2024-12-29 14:24:13

uniapp 前端解决精度丢失的问题 (后端返回分布式id)

缘故原由: 后端使用分布式id,  id为19位数,导致精度丢失 ,前端解决方法
https://i-blog.csdnimg.cn/direct/c0464df89f17454b9f23c482a66bc6f7.png
这个是通过欣赏器请求返来的数据,这个时间id 数据已经丢失了,在数据库查询不到,在调获详情接口的时间会有问题
实际的:
https://i-blog.csdnimg.cn/direct/0ecb01a3fd0946a8a1f27fda56b8a90f.png
解决办法:
1. 通过  JSONbig  插件  
npm install json-bigint 2.封装 请求   longAxios.js  uni.request  (   在axios 里面可以使用transformResponse 来修改,uni.request 没有,使用 success 来获取)   
import JSONbig from 'json-bigint';
import {
        getToken
} from '@/utils/auth';
import config from '@/config';

const timeout = 10000;
const baseUrl = config.baseUrl;

const longAxios = options => {
        const {
                url,
                method,
                data,
                params, // 接收 params
                header,
                type,
                key,
                receive
        } = options;

        return new Promise((resolve, reject) => {
                // 构建请求 URL
                let requestUrl = `${baseUrl}${url}`;

                // 如果有 params,构建查询字符串
                if (params) {
                        const queryParams = new URLSearchParams(params).toString();
                        requestUrl += `?${queryParams}`; // 将查询参数添加到 URL
                }

                uni.request({
                        url: requestUrl,
                        method,
                        data,
                        header: {
                                Authorization: 'Bearer ' + getToken(),
                                ...header // 如果有额外的头部信息,可以合并
                        },
                        dataType: 'String', // 将接收的数据转换成字符串类型,而不直接解析
                        success: res => {
                                try {
                                        // 如果大数字类型转换成功则返回转换的数据结果
                                        res.data = JSONbig.parse(res.data)
                                } catch (err) {
                                        // 如果转换失败,则包装为统一数据格式并返回
                                        resolve(JSON.parse(res.data));
                                }
                               
                                // data = JSONbig.parse(data);
                                if (type == 'array') {
                                        res.data.filter((item) => {
                                                item = JSONbig.parse(item).toString()
                                                return item
                                        })
                                } else if (type == 'rows') {
                                        res.data.rows.filter((item) => {
                                                item = JSONbig.parse(item).toString()
                                                return item
                                        })
                                } else {
                                        res.data = JSONbig.parse(res.data).toString()
                                }
                                console.log(res)
                               // 返回结果
                                  if (receive === 'rows') {
                                        resolve(res.data.rows); // 确保 resolve 返回正确的数据
                                  } else {
                                        resolve(res.data);
                                  }

                        },
                        fail: err => {
                                reject(err);
                        },
                });
        });
}

export default longAxios; 3.封装api(根据自己的业务来修改, 留意key 值,我这里是id, 返回的是rows )
https://i-blog.csdnimg.cn/direct/209ca6566b52493db7de7a0b399208b2.png
import longAxios from '@/utils/longAxios';


export function $listTree(params) {

    return longAxios({
      url: `/lims/custom/listTree`,
      method: 'get',
      params,
      key: 'id',
      type: 'rows',
      receive: 'rows'
      });

}
4.正常调用就可以了
const res = await $customListData(this.queryParams)

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页: [1]
查看完整版本: uniapp 前端解决精度丢失的问题 (后端返回分布式id)