huangxw hai 5 meses
pai
achega
74285d4b39
Modificáronse 2 ficheiros con 6 adicións e 14 borrados
  1. 6 13
      src/utils/ruoyi.ts
  2. 0 1
      src/views/models/FileLook.vue

+ 6 - 13
src/utils/ruoyi.ts

@@ -1,3 +1,4 @@
+import NP from 'number-precision';
 // 日期格式化
 export function parseTime(time: any, pattern?: string) {
     if (arguments.length === 0 || !time) {
@@ -256,28 +257,20 @@ export const fileExt = (fileName: string) => {
     // 获取文件后缀, 从后面到第一个点取 不包括.
     return fileName.slice(fileName.lastIndexOf('.') + 1);
 };
-export const changeByte = (byte: number) => {
+export const changeByte = (byte: number): string => {
     let size = '';
     if (byte < 0.1 * 1024) {
         // 小于0.1KB,则转化成B
-        size = `${byte.toFixed(2)}B`;
+        size = `${NP.round(byte, 2)}B`;
     } else if (byte < 0.1 * 1024 * 1024) {
         // 小于0.1MB,则转化成KB
-        size = `${(byte / 1024).toFixed(2)}KB`;
+        size = `${NP.round(byte / 1024, 2)}KB`;
     } else if (byte < 0.1 * 1024 * 1024 * 1024) {
         // 小于0.1GB,则转化成MB
-        size = `${(byte / (1024 * 1024)).toFixed(2)}MB`;
+        size = `${NP.round(byte / (1024 * 1024), 2)}MB`;
     } else {
         // 其他转化成GB
-        size = `${(byte / (1024 * 1024 * 1024)).toFixed(2)}GB`;
-    }
-
-    const sizeStr = `${size}`; // 转成字符串
-    const index = sizeStr.indexOf('.'); // 获取小数点处的索引
-    const dou = sizeStr.substring(index + 1, index + 3); // 获取小数点后两位的值
-    if (dou == '00') {
-        // 判断后两位是否为00,如果是则删除00
-        return sizeStr.substring(0, index);
+        size = `${NP.round(byte / (1024 * 1024 * 1024), 2)}GB`;
     }
     return size;
 };

+ 0 - 1
src/views/models/FileLook.vue

@@ -28,7 +28,6 @@ import { download } from '@/utils/request';
 import { propTypes } from '@/utils/propTypes';
 import { changeByte } from '@/utils/ruoyi';
 import { getFileIconByUrl } from '@/utils/models';
-
 const props = defineProps({
     modelValue: [String, Object, Array],
     span: propTypes.number.def(18),