From 8a590a8d3bba55726d69f71545195eb6d1b0f516 Mon Sep 17 00:00:00 2001 From: TsMask <340112800@qq.com> Date: Thu, 1 Feb 2024 09:43:50 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=AD=97=E8=8A=82=E8=BD=AC=E6=8D=A2?= =?UTF-8?q?=E5=B7=A5=E5=85=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/utils/parse-utils.ts | 23 ++++++------------- .../overview/hooks/useUPFTotalFlow.ts | 6 ++--- 2 files changed, 10 insertions(+), 19 deletions(-) diff --git a/src/utils/parse-utils.ts b/src/utils/parse-utils.ts index 327204ae..b04a2cad 100644 --- a/src/utils/parse-utils.ts +++ b/src/utils/parse-utils.ts @@ -154,21 +154,12 @@ export function parseSizeFromKbs(sizeByte: number, timeInterval: number): any { * @param bits 字节Bit大小 * @returns MB */ -export function parseSizeFromBytes(bits: number | string): string { +export function parseSizeFromBits(bits: number | string): string { bits = Number(bits) || 0; - const kb = bits / 1024; - const mb = kb / 1024; - const gb = mb / 1024; - const tb = gb / 1024; - - if (tb >= 1) { - return tb.toFixed(2) + ' TB'; - } else if (gb >= 1) { - return gb.toFixed(2) + ' GB'; - } else if (mb >= 1) { - return mb.toFixed(2) + ' MB'; - } else if (kb >= 1) { - return kb.toFixed(2) + ' KB'; - } - return bits + ' B'; + if (bits <= 0) return '0 B'; + const units = ['B', 'KB', 'MB', 'GB', 'TB']; + const unitIndex = Math.floor(Math.log2(bits) / 10); + const value = (bits / Math.pow(1024, unitIndex)).toFixed(2); + const unti = units[unitIndex]; + return `${value} ${unti}`; } diff --git a/src/views/dashboard/overview/hooks/useUPFTotalFlow.ts b/src/views/dashboard/overview/hooks/useUPFTotalFlow.ts index e0da6a4b..4266cfa8 100644 --- a/src/views/dashboard/overview/hooks/useUPFTotalFlow.ts +++ b/src/views/dashboard/overview/hooks/useUPFTotalFlow.ts @@ -1,4 +1,4 @@ -import { parseSizeFromBytes } from '@/utils/parse-utils'; +import { parseSizeFromBits } from '@/utils/parse-utils'; import { ref } from 'vue'; type TFType = { @@ -33,8 +33,8 @@ export const upfTotalFlow = ref([ /**UPF-总流量数 数据解析 */ export function upfTFParse(data: Record) { let { up, down } = data; - up = parseSizeFromBytes(up); - down = parseSizeFromBytes(down); + up = parseSizeFromBits(up); + down = parseSizeFromBits(down); return { up, down }; }