/** * @description 一些会用到的实用工具 */ import { cloneDeep } from 'lodash-es' import { CACHE_KEY, useCache } from '@/hooks/web/useCache' /** * 计算表格高度 = 页面高度 - 传入高度 */ export const calculateTableHeight = (height: number) => { return document.body.clientHeight - height } /** * 四舍五入换算方法,把万换算成亿,保留两位小数 */ export const unitConversion = (valueInTenThousand: number): string => { // 判断值是否大于等于 1 亿 if (valueInTenThousand >= 10000) { // 将以万为单位的数字除以 10,000 来得到以亿为单位的数字,并保留两位小数 const valueInBillion = valueInTenThousand / 10000 // 使用 toFixed 方法保留两位小数 return valueInBillion.toFixed(2) + ' 亿' } else { // 小于 1 亿的情况,以万为单位返回 return `${valueInTenThousand} 万` } } /** * 数组按字段排序 * @param key 排序字段 */ export const sortByKey = ( dataSource: any[], key: string, order: 'desc' | 'asc' = 'desc' ): any[] => { const newList = cloneDeep(dataSource) if (order === 'asc') return newList.sort((x, y) => x[key] - y[key]) return cloneDeep(dataSource).sort((x, y) => y[key] - x[key]) } /** * 获取用户信息的方法 */ export const getUserId = () => { const { wsCache } = useCache() const userInfo = wsCache.get(CACHE_KEY.USER) return userInfo.user ?? {} }