index.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import {array, func, object, string} from "@/uni_modules/uv-ui-tools/libs/function/test";
  2. import _ from "lodash";
  3. import mxConfig from "@/common/mxConfig";
  4. export const sizeFormat = function (size, decimals = 2) {
  5. if (size === 0) return '0 B';
  6. const k = 1024; // 转换单位
  7. const units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']; // 单位数组
  8. const i = Math.floor(Math.log(size) / Math.log(k)); // 计算当前单位的索引
  9. // 转换为对应单位,保留指定的小数位
  10. const formattedSize = parseFloat((size / Math.pow(k, i)).toFixed(decimals));
  11. return `${formattedSize} ${units[i]}`;
  12. }
  13. export const getSizeOfObject = function (obj) {
  14. // console.log('getSizeOfObject', obj)
  15. if (!object(obj) && !array(obj)) return 0
  16. return new Blob([JSON.stringify(obj)]).size
  17. }
  18. export const blockRunner = async function (blockFn) {
  19. if (func(blockFn)) {
  20. const result = blockFn()
  21. if (_.isBoolean(result) && !result) return Promise.reject(`blockFn returns false`)
  22. else if (result instanceof Promise) await result
  23. }
  24. return true // 其它情况默认通过
  25. }
  26. /*
  27. * @description 拼接OSS路径
  28. * @param {src} 原始路径
  29. * */
  30. export const combineOssFile = function (src) {
  31. if (!string(src)) return src
  32. const lowerCaseStr = src.toLowerCase()
  33. if (lowerCaseStr.startsWith('http')) return src
  34. if (['.jpg', '.png', '.gif', '.jpeg'].every(suffix => !lowerCaseStr.includes(suffix))) return src
  35. const baseUrl = mxConfig.ossFileBase || ''
  36. const comb = baseUrl.endsWith('/') || src.startsWith('/') ? '' : '/'
  37. return [baseUrl, comb, src].join('')
  38. }
  39. /**
  40. * @description 使用这个方法让属性定义看起来更紧凑
  41. * @param defaultValue
  42. * @param {StringConstructor|NumberConstructor|BooleanConstructor|ObjectConstructor|FunctionConstructor|ArrayConstructor|Array} type, `String` as default.
  43. * */
  44. export function createPropDefine(defaultValue, type = String, validator = undefined) {
  45. return {
  46. type: type,
  47. default: () => defaultValue,
  48. validator
  49. }
  50. }
  51. /*
  52. * @description 这个方法是兼容vue2的依赖注入的,vue3中请使用provideLocal&injectLocal来保证更好的可读性
  53. * @param defVal - 默认值
  54. * */
  55. export function createInjectDefaultClosure(defVal) {
  56. return {
  57. default: function () {
  58. return () => defVal
  59. }
  60. }
  61. }
  62. /***
  63. * @description 将数字序号转化为字母A/B/C...
  64. * @param {Number} index 从0开始的序号
  65. * @return {String} A,B,C...
  66. * */
  67. export function numberToLetter(index) {
  68. return String.fromCharCode(65 + index)
  69. }
  70. export function numberToChinese(num) {
  71. const chineseDigits = ["零", "一", "二", "三", "四", "五", "六", "七", "八", "九"];
  72. const units = ["", "十", "百", "千", "万", "亿"];
  73. if (num === 0) {
  74. return "零";
  75. }
  76. let result = "";
  77. const numStr = String(num);
  78. const length = numStr.length;
  79. for (let i = 0; i < length; i++) {
  80. const digit = parseInt(numStr[i]);
  81. const unitIndex = length - i - 1;
  82. if (digit !== 0) {
  83. result += chineseDigits[digit] + units[unitIndex];
  84. } else {
  85. if (!result.endsWith("零")) {
  86. result += "零";
  87. }
  88. }
  89. }
  90. // 去掉多余的零
  91. result = result.replace(/零+$/, "");
  92. // 特殊处理 “一十”
  93. result = result.replace(/^一十/, "十");
  94. return result;
  95. }
  96. export const groupByObjectKey = (array, iteratee) => {
  97. // lodash的groupBy返回的是object, 只能用String/Number/Symbol作为键值,
  98. // 这是JS本身的特性决定的,如果要以object为键值,用Map当作容器比较合适。
  99. const result = new Map()
  100. array.forEach((item, index) => {
  101. const key = iteratee(item, index)
  102. const group = result.has(key) ? result.get(key) : []
  103. group.push(item)
  104. if (!result.has(key)) result.set(key, group)
  105. })
  106. return result
  107. }