From bb24e36294077c38129e031a7ab0cf14bc01c636 Mon Sep 17 00:00:00 2001 From: jmal Date: Fri, 18 Jul 2025 17:56:01 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E4=BC=98=E5=8C=96=E7=94=A8=E6=88=B7?= =?UTF-8?q?=E7=A9=BA=E9=97=B4=E5=92=8C=E9=85=8D=E9=A2=9D=E8=AE=A1=E7=AE=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/layout/components/Sidebar/index.vue | 10 +++++----- src/utils/number.js | 21 +++++++++++---------- src/views/setting/sys/cusomerInfo.vue | 13 +++++++------ 3 files changed, 23 insertions(+), 21 deletions(-) diff --git a/src/layout/components/Sidebar/index.vue b/src/layout/components/Sidebar/index.vue index b2b65c9..4b5b1b2 100644 --- a/src/layout/components/Sidebar/index.vue +++ b/src/layout/components/Sidebar/index.vue @@ -406,15 +406,15 @@ export default { if (this.takeUpSpace === 0) { this.takeUpSpace = this.userInfo.takeUpSpace } - const quota = this.userInfo.quota - const space = this.takeUpSpace/1024/1024/1024 - if(space && quota > 0){ - const percentage = Number((space/quota * 100).toFixed(5)) + const spaceBytes = this.takeUpSpace + const quotaBytes = this.userInfo.quota * 1024 * 1024 * 1024 + if(spaceBytes && quotaBytes > 0){ + const percentage = Number((spaceBytes/quotaBytes * 100).toFixed(5)) this.percentage = percentage > 100 ? 100 : percentage if(this.isCollapse){ return `${formatSize(this.takeUpSpace)}` } else { - return `${formatSize(this.takeUpSpace)}/${quota}GB` + return `${formatSize(this.takeUpSpace)}/${formatSize(quotaBytes)}` } } } diff --git a/src/utils/number.js b/src/utils/number.js index 30af345..56660a4 100644 --- a/src/utils/number.js +++ b/src/utils/number.js @@ -127,17 +127,18 @@ export function formatTime(agoTime) { * @param {*} size */ export function formatSize(size) { - if (size === 0) { - return '' - } else if (size < 1024) { - return size + 'B' - } else if (size < 1024 * 1024) { - return (size/1024).toFixed(1) + 'k' - } else if (size < 1024 * 1024 * 1024) { - return (size/(1024 * 1024)).toFixed(1) + 'M' - } else { - return (size/(1024 * 1024 * 1024)).toFixed(1) + 'G' + if (size === 0 || !size) return '0B'; + + const units = ['B', 'K', 'M', 'G', 'T']; + let index = 0; + let result = size; + + while (result >= 1024 && index < units.length - 1) { + result /= 1024; + index++; } + + return result.toFixed(1) + units[index]; } /** diff --git a/src/views/setting/sys/cusomerInfo.vue b/src/views/setting/sys/cusomerInfo.vue index 488a081..87c4335 100644 --- a/src/views/setting/sys/cusomerInfo.vue +++ b/src/views/setting/sys/cusomerInfo.vue @@ -241,12 +241,13 @@ } }, progressFormat() { - const space = this.userInfo.takeUpSpace/1024/1024/1024 - const quota = this.userInfo.quota - if(space && quota > 0){ - const percentage = Number((space/quota * 100).toFixed(1)) - this.percentage = percentage > 100 ? 100 : percentage - return `${this.percentage}% (${formatSize(this.userInfo.takeUpSpace)}/${quota}GB)`; + const spaceBytes = this.userInfo.takeUpSpace; + const quotaBytes = this.userInfo.quota * 1024 * 1024 * 1024; + + if (spaceBytes && quotaBytes > 0) { + const percentage = Number((spaceBytes / quotaBytes * 100).toFixed(1)); + this.percentage = percentage > 100 ? 100 : percentage; + return `${this.percentage}% (${formatSize(spaceBytes)}/${formatSize(quotaBytes)})`; } }, // 设置formData