import { queryResultLoadingAndInfo } from '@/common/methods/request/resultInfo'; import { getDecimalsNum } from '@/utils/number'; import { ref } from 'vue'; /** * 请求表格数据通用方法 * @param ishandleFloatErr 是否处理浮点失真问题 * @returns */ export function queryTableList(ishandleFloatErr = false, num?: number) { // 加载状态 const loading = ref(false); // 表格数据 const tableList = ref([]); function queryTable(fn: Function, param?: any): Promise { console.log('表格数据查询参数:', param); return queryResultLoadingAndInfo(fn, loading, param) .then(res => { if (!res) return [] tableList.value.length = 0; const result = res.map((e: any, i: number) => { // 统一把空字符转 -- Object.keys(e).forEach(el => { if (e[el] === '') { e[el] = '--' } }) // 统一处理key,表格需要key,单击事件和双击需要依据就是这个key,如果服务返回了key这个字段,表格事情需要展开列表需要额外处理 if (Reflect.has(e, 'key')) { return e } else { return { ...e, key: String(i) }; } }) if (ishandleFloatErr) { // 折中方案:处理浮点失真,如果页面卡顿,则需要服务处理 result?.forEach((e: T, i: number) => { for (const item in e) { const decimal = (e as any).decimalplace const count = decimal ? decimal : num e[item] = getDecimalsNum(e[item], count, decimal) } }); } tableList.value = result return result }) } return { loading, tableList, queryTable } }