feat: SMF数据单位转换MB显示

This commit is contained in:
TsMask
2025-03-21 16:39:58 +08:00
parent fb3f1daecf
commit 26686f88db
3 changed files with 27 additions and 12 deletions

View File

@@ -192,17 +192,31 @@ export function parseSizeFromBits(bits: number | string): string {
/** /**
* 字节数转换单位 * 字节数转换单位
* @param byte 字节Byte大小 64009540 = 512.08 MB * @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 * @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; byte = Number(byte) || 0;
if (byte <= 0) return '0 B'; if (byte <= 0) return '0 B';
const units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']; const units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
const unitIndex = Math.floor(Math.log2(byte) / 10); let unitIndex = 0;
const unti = units[unitIndex]; 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); const value = byte / Math.pow(1000, unitIndex);
if (unitIndex > 0) { if (unitIndex > 0) {
return `${value.toFixed(2)} ${unti}`; return `${value.toFixed(2)} ${unitStr}`;
} }
return `${value} ${unti}`; return `${value} ${unitStr}`;
} }

View File

@@ -21,6 +21,7 @@ import PQueue from 'p-queue';
import saveAs from 'file-saver'; import saveAs from 'file-saver';
import dayjs, { Dayjs } from 'dayjs'; import dayjs, { Dayjs } from 'dayjs';
import { useClipboard } from '@vueuse/core'; import { useClipboard } from '@vueuse/core';
import { parseSizeFromByte } from '@/utils/parse-utils';
const { copy } = useClipboard({ legacy: true }); const { copy } = useClipboard({ legacy: true });
const { t } = useI18n(); const { t } = useI18n();
const ws = new WS(); const ws = new WS();
@@ -164,7 +165,7 @@ let tableColumns = ref<ColumnsType>([
} }
} }
} }
return dataVolumeUplink; return parseSizeFromByte(dataVolumeUplink, 'MB');
}, },
}, },
{ {
@@ -189,7 +190,7 @@ let tableColumns = ref<ColumnsType>([
} }
} }
} }
return dataVolumeDownlink; return parseSizeFromByte(dataVolumeDownlink, 'MB');
}, },
}, },
{ {
@@ -214,7 +215,7 @@ let tableColumns = ref<ColumnsType>([
} }
} }
} }
return dataTotalVolume; return parseSizeFromByte(dataTotalVolume, 'MB');
}, },
}, },
{ {

View File

@@ -69,8 +69,8 @@ const option = {
} else { } else {
downlinkValue = params[1].value; downlinkValue = params[1].value;
} }
const uplinkValueF = parseSizeFromByte(uplinkValue); const uplinkValueF = parseSizeFromByte(uplinkValue, 'MB');
const downlinkValueF = parseSizeFromByte(downlinkValue); const downlinkValueF = parseSizeFromByte(downlinkValue, 'MB');
return ` return `
<div style="font-weight: bold;">${title}</div> <div style="font-weight: bold;">${title}</div>
<div>Downlink: ${downlinkValueF}</div> <div>Downlink: ${downlinkValueF}</div>
@@ -466,8 +466,8 @@ function fnRanderChartDataUpdate() {
downlinkTotal += dataVolumeDownlinkYSeriesData[index]; downlinkTotal += dataVolumeDownlinkYSeriesData[index];
} }
state.dataUsage = [ state.dataUsage = [
parseSizeFromByte(uplinkTotal), parseSizeFromByte(uplinkTotal, 'MB'),
parseSizeFromByte(downlinkTotal), parseSizeFromByte(downlinkTotal, 'MB'),
]; ];
} }