Преглед изворни кода

feat(util): 添加格式化文件大小工具函数并复用

yz пре 2 месеци
родитељ
комит
221dba62b1
2 измењених фајлова са 17 додато и 14 уклоњено
  1. 15 0
      src/util/util.js
  2. 2 14
      src/views/order/marketing-activity/index.vue

+ 15 - 0
src/util/util.js

@@ -365,3 +365,18 @@ export const downloadFileBase64 = (path, name) => {
     }
     }
   };
   };
 }
 }
+
+/**
+ * 格式化文件大小
+ * @param {number} bytes - 文件大小(字节)
+ * @returns {string} 格式化后的文件大小
+ */
+export const formatFileSize = (bytes) => {
+  if (!bytes || bytes === 0) return '0 B'
+
+  const k = 1024
+  const sizes = ['B', 'KB', 'MB', 'GB', 'TB']
+  const i = Math.floor(Math.log(bytes) / Math.log(k))
+
+  return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i]
+}

+ 2 - 14
src/views/order/marketing-activity/index.vue

@@ -341,6 +341,7 @@ import {
     saveApprovalRecord,
     saveApprovalRecord,
     getActivityAttachments
     getActivityAttachments
 } from '@/api/order/marketing-activity'
 } from '@/api/order/marketing-activity'
+import { formatFileSize } from '@/util/util'
 import { mapGetters } from 'vuex'
 import { mapGetters } from 'vuex'
 
 
 /**
 /**
@@ -846,20 +847,7 @@ export default {
             }
             }
         },
         },
 
 
-        /**
-         * 格式化文件大小
-         * @param {number} bytes - 文件大小(字节)
-         * @returns {string} 格式化后的文件大小
-         */
-        formatFileSize(bytes) {
-            if (!bytes || bytes === 0) return '0 B'
-
-            const k = 1024
-            const sizes = ['B', 'KB', 'MB', 'GB', 'TB']
-            const i = Math.floor(Math.log(bytes) / Math.log(k))
-
-            return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i]
-        },
+        formatFileSize,
 
 
         /**
         /**
          * 获取文件类型图标
          * 获取文件类型图标