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