Parcourir la source

refactor(工具函数): 将文件大小格式化功能提取为公共工具函数

yz il y a 1 mois
Parent
commit
af94a002b9
2 fichiers modifiés avec 5 ajouts et 32 suppressions
  1. 3 14
      src/views/claim/index.vue
  2. 2 18
      src/views/order/image-store-apply/index.vue

+ 3 - 14
src/views/claim/index.vue

@@ -204,6 +204,7 @@
 
 <script>
 import { getClaimList, getClaimDetail, getClaimAttachments, getClaimAuditList, addClaimAudit, updateClaimAudit, removeClaimAudit } from '@/api/claim/index'
+import { formatFileSize } from '@/util/util'
 import { mapGetters } from 'vuex'
 
 export default {
@@ -627,20 +628,8 @@ export default {
       window.open(file.fileUrl)
     },
 
-    /**
-     * 格式化文件大小
-     * @param {number} size - 文件大小(字节)
-     * @returns {string} 格式化后的文件大小
-     */
-    formatFileSize(size) {
-      if (size < 1024) {
-        return size + ' B'
-      } else if (size < 1024 * 1024) {
-        return (size / 1024).toFixed(2) + ' KB'
-      } else {
-        return (size / (1024 * 1024)).toFixed(2) + ' MB'
-      }
-    },
+    // 添加公共方法引用
+    formatFileSize,
 
     /**
      * 获取审核状态类型

+ 2 - 18
src/views/order/image-store-apply/index.vue

@@ -397,6 +397,7 @@
 
 <script>
 import { getList, getDetail, update, getAttachmentList } from '@/api/order/image-store-apply'
+import { formatFileSize } from '@/util/util'
 import { mapGetters } from 'vuex'
 
 /**
@@ -780,24 +781,7 @@ export default {
       })
     },
 
-    /**
-     * 格式化文件大小
-     * @param {number} size - 文件大小(字节)
-     * @returns {string} 格式化后的文件大小
-     */
-    formatFileSize(size) {
-      if (!size) return '0 B'
-      const units = ['B', 'KB', 'MB', 'GB']
-      let index = 0
-      let fileSize = size
-
-      while (fileSize >= 1024 && index < units.length - 1) {
-        fileSize /= 1024
-        index++
-      }
-
-      return `${fileSize.toFixed(2)} ${units[index]}`
-    },
+    formatFileSize,
 
     /**
      * 判断是否为图片文件