2
0

fix:套餐流量计算修改

This commit is contained in:
zhongzm
2024-12-25 19:55:12 +08:00
parent 1c81fa2ec0
commit 7cc1af269b

View File

@@ -5,8 +5,6 @@ import { fetchPackageList, submitOrder } from '@/service/api/auth';
import { message } from 'ant-design-vue'; import { message } from 'ant-design-vue';
const { t } = useI18n(); const { t } = useI18n();
// 注入更新仪表盘的方法
const updateDashboard = inject('updateDashboard') as () => Promise<void>;
interface PackageOption { interface PackageOption {
id: string; id: string;
@@ -48,25 +46,35 @@ const formatValidityPeriod = (num: number, type: number): string => {
}; };
// 流量单位转换函数 // 流量单位转换函数
const formatTraffic = (trafficKB: number): string => { const formatTraffic = (bytes: number): string => {
const KB_TO_MB = 1024; // 处理 0 值、undefined 或 null 的情况
const KB_TO_GB = 1024 * 1024; if (!bytes || bytes === 0) {
const KB_TO_TB = 1024 * 1024 * 1024; return '0B';
}
if (trafficKB >= KB_TO_TB) { const B_TO_KB = 1024;
// KB -> TB (除以 1024^3) const B_TO_MB = 1024 * 1024;
return `${(trafficKB / KB_TO_TB).toFixed(2)}TB`; const B_TO_GB = 1024 * 1024 * 1024;
const B_TO_TB = 1024 * 1024 * 1024 * 1024;
if (bytes >= B_TO_TB) {
// B -> TB
return `${(bytes / B_TO_TB).toFixed(2)}TB`;
} }
if (trafficKB >= KB_TO_GB) { if (bytes >= B_TO_GB) {
// KB -> GB (除以 1024^2) // B -> GB
return `${(trafficKB / KB_TO_GB).toFixed(2)}GB`; return `${(bytes / B_TO_GB).toFixed(2)}GB`;
} }
if (trafficKB >= KB_TO_MB) { if (bytes >= B_TO_MB) {
// KB -> MB (除以 1024) // B -> MB
return `${(trafficKB / KB_TO_MB).toFixed(2)}MB`; return `${(bytes / B_TO_MB).toFixed(2)}MB`;
} }
// 小于1MB的情况保持KB单位 if (bytes >= B_TO_KB) {
return `${trafficKB.toFixed(2)}KB`; // B -> KB
return `${(bytes / B_TO_KB).toFixed(2)}KB`;
}
// 小于1KB的情况保持B单位
return `${bytes.toFixed(2)}B`;
}; };
const packageOptions = ref<PackageOption[]>([]); const packageOptions = ref<PackageOption[]>([]);
@@ -121,7 +129,10 @@ const selectPackage = (option: PackageOption) => {
selectedPackage.value = option; selectedPackage.value = option;
}; };
// 添加办理套餐的方法 // 注入更新仪表盘的方法
const updateDashboard = inject('updateDashboard') as () => Promise<void>;
// 修改套餐办理方法
const handleSubmitOrder = async () => { const handleSubmitOrder = async () => {
try { try {
await submitOrder({ await submitOrder({
@@ -129,7 +140,7 @@ const handleSubmitOrder = async () => {
packageId: selectedPackage.value.id packageId: selectedPackage.value.id
}); });
message.success('套餐办理成功!'); message.success('套餐办理成功!');
// 更新表盘数据 // 更新<EFBFBD><EFBFBD>表盘数据
await updateDashboard(); await updateDashboard();
} catch (error) { } catch (error) {
message.error('套餐办理失败,请重试!'); message.error('套餐办理失败,请重试!');
@@ -149,12 +160,12 @@ onMounted(async () => {
<div class="price"> <div class="price">
<span class="currency">¥</span> <span class="currency">¥</span>
<span class="amount">{{ selectedPackage.price }}</span> <span class="amount">{{ selectedPackage.price }}</span>
<span class="period"></span> <span class="period">/</span>
</div> </div>
<div class="subtitle">{{ selectedPackage.packageName }}</div> <div class="subtitle">{{ selectedPackage.packageName }}</div>
</div> </div>
<!-- 套餐选 --> <!-- 套餐选 -->
<div class="package-options"> <div class="package-options">
<h3 class="section-title">{{ t('page.setmeal.changablelevel') }}</h3> <h3 class="section-title">{{ t('page.setmeal.changablelevel') }}</h3>
<div class="options-grid"> <div class="options-grid">