tool.ts 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. /**
  2. * @description 一些会用到的实用工具
  3. */
  4. import { cloneDeep } from 'lodash-es'
  5. import { CACHE_KEY, useCache } from '@/hooks/web/useCache'
  6. /**
  7. * 计算表格高度 = 页面高度 - 传入高度
  8. */
  9. export const calculateTableHeight = (height: number) => {
  10. return document.body.clientHeight - height
  11. }
  12. /**
  13. * 四舍五入换算方法,把万换算成亿,保留两位小数
  14. */
  15. export const unitConversion = (valueInTenThousand: number): string => {
  16. // 判断值是否大于等于 1 亿
  17. if (valueInTenThousand >= 10000) {
  18. // 将以万为单位的数字除以 10,000 来得到以亿为单位的数字,并保留两位小数
  19. const valueInBillion = valueInTenThousand / 10000
  20. // 使用 toFixed 方法保留两位小数
  21. return valueInBillion.toFixed(2) + ' 亿'
  22. } else {
  23. // 小于 1 亿的情况,以万为单位返回
  24. return `${valueInTenThousand} 万`
  25. }
  26. }
  27. /**
  28. * 数组按字段排序
  29. * @param key 排序字段
  30. */
  31. export const sortByKey = (
  32. dataSource: any[],
  33. key: string,
  34. order: 'desc' | 'asc' = 'desc'
  35. ): any[] => {
  36. const newList = cloneDeep(dataSource)
  37. if (order === 'asc') return newList.sort((x, y) => x[key] - y[key])
  38. return cloneDeep(dataSource).sort((x, y) => y[key] - x[key])
  39. }
  40. /**
  41. * 获取用户信息的方法
  42. */
  43. export const getUserId = () => {
  44. const { wsCache } = useCache()
  45. const userInfo = wsCache.get(CACHE_KEY.USER)
  46. return userInfo.user ?? {}
  47. }