gerson
2024-09-07 37179a65229455f540dc3ca62d31c4716e15d12d
src/utils/util.ts
@@ -436,17 +436,13 @@
      }
      const res = await deleteApi(
         {
            ID: row.id,
            id: row.id,
            ...rawExtraParams,
         },
         req
      );
      if (res.Data) {
         ElMessage.success(`删除${label}成功`);
         callback(row.id);
      } else {
         ElMessage.error(`删除${label}失败`);
      }
      ElMessage.success(`删除${label}成功`);
      callback(row.id);
   });
};
@@ -579,7 +575,23 @@
      }, wait);
   };
};
/**
 * 文件大小字节转换为XXX
 * @param size 字节大小
 * @returns {string|*}
 */
export const convertFileSize = (size) => {
   if (!size && size !== 0) return '';
   if (size < pow1024(1)) return size + ' B';
   if (size < pow1024(2)) return (size / pow1024(1)).toFixed(2) + ' KB';
   if (size < pow1024(3)) return (size / pow1024(2)).toFixed(2) + ' MB';
   if (size < pow1024(4)) return (size / pow1024(3)).toFixed(2) + ' GB';
   return (size / pow1024(4)).toFixed(2) + ' TB';
};
// 求次幂
function pow1024(num) {
   return Math.pow(1024, num);
}
/**
 *
 * @param {*} func 节流函数
@@ -688,11 +700,37 @@
/**
 * 保留指定精度小数位,且不补零
 * @param num
 * @param precision
 * @returns
 * @param num
 * @param precision
 * @returns
 */
export const toMyFixed = (num, precision) => {
   if (num == null) return '';
   return num.toFixed(precision).replace(/\.?0+$/, '');
};
type GetTextWidthOption = {
   size?: string;
   family?: string;
};
export function getTextWidth(text: string, option: GetTextWidthOption) {
   if (!text) return 0;
   const { size = '14px', family = 'Microsoft YaHei' } = option;
   const spanEle = document.createElement('span');
   document.body.appendChild(spanEle);
   spanEle.style.font = 'times new roman';
   spanEle.style.fontSize = size;
   spanEle.style.height = 'auto';
   spanEle.style.width = 'auto';
   spanEle.style.position = 'absolute';
   spanEle.style.whiteSpace = 'no-wrap';
   spanEle.innerHTML = text;
   const width = spanEle.clientWidth;
   document.body.removeChild(spanEle);
   return width;
}