From 26686f88db7502a2c940f1977910714bdca7ff0f Mon Sep 17 00:00:00 2001 From: TsMask <340112800@qq.com> Date: Fri, 21 Mar 2025 16:39:58 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20SMF=E6=95=B0=E6=8D=AE=E5=8D=95=E4=BD=8D?= =?UTF-8?q?=E8=BD=AC=E6=8D=A2MB=E6=98=BE=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/utils/parse-utils.ts | 24 +++++++++++++++++----- src/views/dashboard/smfCDR/index.vue | 7 ++++--- src/views/dashboard/smfCDRByIMSI/index.vue | 8 ++++---- 3 files changed, 27 insertions(+), 12 deletions(-) diff --git a/src/utils/parse-utils.ts b/src/utils/parse-utils.ts index 49a89ddd..8e13fd43 100644 --- a/src/utils/parse-utils.ts +++ b/src/utils/parse-utils.ts @@ -192,17 +192,31 @@ export function parseSizeFromBits(bits: number | string): string { /** * 字节数转换单位 * @param byte 字节Byte大小 64009540 = 512.08 MB + * @param unit 指定单位 B / KB / MB / GB / TB / PB / EB / ZB / YB * @returns xx B / KB / MB / GB / TB / PB / EB / ZB / YB */ -export function parseSizeFromByte(byte: number | string): string { +export function parseSizeFromByte( + byte: number | string, + unit?: string +): string { byte = Number(byte) || 0; if (byte <= 0) return '0 B'; const units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']; - const unitIndex = Math.floor(Math.log2(byte) / 10); - const unti = units[unitIndex]; + let unitIndex = 0; + let unitStr = 'B'; + if (unit) { + const index = units.indexOf(unit); + if (index > -1) { + unitIndex = index; + unitStr = unit; + } + } else { + unitIndex = Math.floor(Math.log2(byte) / 10); + unitStr = units[unitIndex]; + } const value = byte / Math.pow(1000, unitIndex); if (unitIndex > 0) { - return `${value.toFixed(2)} ${unti}`; + return `${value.toFixed(2)} ${unitStr}`; } - return `${value} ${unti}`; + return `${value} ${unitStr}`; } diff --git a/src/views/dashboard/smfCDR/index.vue b/src/views/dashboard/smfCDR/index.vue index deb1731e..9edb1d71 100644 --- a/src/views/dashboard/smfCDR/index.vue +++ b/src/views/dashboard/smfCDR/index.vue @@ -21,6 +21,7 @@ import PQueue from 'p-queue'; import saveAs from 'file-saver'; import dayjs, { Dayjs } from 'dayjs'; import { useClipboard } from '@vueuse/core'; +import { parseSizeFromByte } from '@/utils/parse-utils'; const { copy } = useClipboard({ legacy: true }); const { t } = useI18n(); const ws = new WS(); @@ -164,7 +165,7 @@ let tableColumns = ref([ } } } - return dataVolumeUplink; + return parseSizeFromByte(dataVolumeUplink, 'MB'); }, }, { @@ -189,7 +190,7 @@ let tableColumns = ref([ } } } - return dataVolumeDownlink; + return parseSizeFromByte(dataVolumeDownlink, 'MB'); }, }, { @@ -214,7 +215,7 @@ let tableColumns = ref([ } } } - return dataTotalVolume; + return parseSizeFromByte(dataTotalVolume, 'MB'); }, }, { diff --git a/src/views/dashboard/smfCDRByIMSI/index.vue b/src/views/dashboard/smfCDRByIMSI/index.vue index 7c99e830..768ba262 100644 --- a/src/views/dashboard/smfCDRByIMSI/index.vue +++ b/src/views/dashboard/smfCDRByIMSI/index.vue @@ -69,8 +69,8 @@ const option = { } else { downlinkValue = params[1].value; } - const uplinkValueF = parseSizeFromByte(uplinkValue); - const downlinkValueF = parseSizeFromByte(downlinkValue); + const uplinkValueF = parseSizeFromByte(uplinkValue, 'MB'); + const downlinkValueF = parseSizeFromByte(downlinkValue, 'MB'); return `
${title}
Downlink: ${downlinkValueF}
@@ -466,8 +466,8 @@ function fnRanderChartDataUpdate() { downlinkTotal += dataVolumeDownlinkYSeriesData[index]; } state.dataUsage = [ - parseSizeFromByte(uplinkTotal), - parseSizeFromByte(downlinkTotal), + parseSizeFromByte(uplinkTotal, 'MB'), + parseSizeFromByte(downlinkTotal, 'MB'), ]; }