123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- import {array, func, object, string} from "@/uni_modules/uv-ui-tools/libs/function/test";
- import _ from "lodash";
- import mxConfig from "@/common/mxConfig";
- export const sizeFormat = function (size, decimals = 2) {
- if (size === 0) return '0 B';
- const k = 1024; // 转换单位
- const units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']; // 单位数组
- const i = Math.floor(Math.log(size) / Math.log(k)); // 计算当前单位的索引
- // 转换为对应单位,保留指定的小数位
- const formattedSize = parseFloat((size / Math.pow(k, i)).toFixed(decimals));
- return `${formattedSize} ${units[i]}`;
- }
- export const getSizeOfObject = function (obj) {
- // console.log('getSizeOfObject', obj)
- if (!object(obj) && !array(obj)) return 0
- return new Blob([JSON.stringify(obj)]).size
- }
- export const blockRunner = async function (blockFn) {
- if (func(blockFn)) {
- const result = blockFn()
- if (_.isBoolean(result) && !result) return Promise.reject(`blockFn returns false`)
- else if (result instanceof Promise) await result
- }
- return true // 其它情况默认通过
- }
- /*
- * @description 拼接OSS路径
- * @param {src} 原始路径
- * */
- export const combineOssFile = function (src) {
- if (!string(src)) return src
- const lowerCaseStr = src.toLowerCase()
- if (lowerCaseStr.startsWith('http')) return src
- if (['.jpg', '.png', '.gif', '.jpeg'].every(suffix => !lowerCaseStr.includes(suffix))) return src
- const baseUrl = mxConfig.ossFileBase || ''
- const comb = baseUrl.endsWith('/') || src.startsWith('/') ? '' : '/'
- return [baseUrl, comb, src].join('')
- }
- /**
- * @description 使用这个方法让属性定义看起来更紧凑
- * @param defaultValue
- * @param {StringConstructor|NumberConstructor|BooleanConstructor|ObjectConstructor|FunctionConstructor|ArrayConstructor|Array} type, `String` as default.
- * */
- export function createPropDefine(defaultValue, type = String, validator = undefined) {
- return {
- type: type,
- default: () => defaultValue,
- validator
- }
- }
- /*
- * @description 这个方法是兼容vue2的依赖注入的,vue3中请使用provideLocal&injectLocal来保证更好的可读性
- * @param defVal - 默认值
- * */
- export function createInjectDefaultClosure(defVal) {
- return {
- default: function () {
- return () => defVal
- }
- }
- }
- /***
- * @description 将数字序号转化为字母A/B/C...
- * @param {Number} index 从0开始的序号
- * @return {String} A,B,C...
- * */
- export function numberToLetter(index) {
- return String.fromCharCode(65 + index)
- }
- export function numberToChinese(num) {
- const chineseDigits = ["零", "一", "二", "三", "四", "五", "六", "七", "八", "九"];
- const units = ["", "十", "百", "千", "万", "亿"];
- if (num === 0) {
- return "零";
- }
- let result = "";
- const numStr = String(num);
- const length = numStr.length;
- for (let i = 0; i < length; i++) {
- const digit = parseInt(numStr[i]);
- const unitIndex = length - i - 1;
- if (digit !== 0) {
- result += chineseDigits[digit] + units[unitIndex];
- } else {
- if (!result.endsWith("零")) {
- result += "零";
- }
- }
- }
- // 去掉多余的零
- result = result.replace(/零+$/, "");
- // 特殊处理 “一十”
- result = result.replace(/^一十/, "十");
- return result;
- }
- export const groupByObjectKey = (array, iteratee) => {
- // lodash的groupBy返回的是object, 只能用String/Number/Symbol作为键值,
- // 这是JS本身的特性决定的,如果要以object为键值,用Map当作容器比较合适。
- const result = new Map()
- array.forEach((item, index) => {
- const key = iteratee(item, index)
- const group = result.has(key) ? result.get(key) : []
- group.push(item)
- if (!result.has(key)) result.set(key, group)
- })
- return result
- }
|