This commit is contained in:
@@ -40,32 +40,23 @@
|
||||
|
||||
<script setup>
|
||||
import {
|
||||
ref,
|
||||
computed,
|
||||
watch
|
||||
computed
|
||||
} from 'vue';
|
||||
import {
|
||||
assetApi
|
||||
} from '@/api/index.js';
|
||||
import {
|
||||
useUserStore
|
||||
} from '@/store/index.js';
|
||||
|
||||
const props = defineProps({
|
||||
identifier: {
|
||||
type: String,
|
||||
default: ''
|
||||
assetInfo: {
|
||||
type: Object,
|
||||
default: null
|
||||
}
|
||||
});
|
||||
|
||||
const emit = defineEmits(['packageLoaded']);
|
||||
|
||||
const userStore = useUserStore();
|
||||
|
||||
// 流量数据
|
||||
const usedMB = ref(0);
|
||||
const totalMB = ref(0);
|
||||
const remainMB = ref(0);
|
||||
// 和首页共用最新资产快照,不保留只在首次进入时加载的套餐缓存。
|
||||
const metrics = computed(() => props.assetInfo?.current_package_usage_id
|
||||
? getTrafficMetrics(props.assetInfo)
|
||||
: { used: 0, total: 0, remain: 0 });
|
||||
const usedMB = computed(() => metrics.value.used);
|
||||
const totalMB = computed(() => metrics.value.total);
|
||||
const remainMB = computed(() => metrics.value.remain);
|
||||
|
||||
const toMB = (value) => Number(value) || 0;
|
||||
|
||||
@@ -125,40 +116,6 @@
|
||||
};
|
||||
};
|
||||
|
||||
// 加载套餐流量数据
|
||||
const loadPackageData = async () => {
|
||||
if (userStore.state.loginPending || !userStore.state.token) return;
|
||||
const identifier = props.identifier || userStore.state.identifier;
|
||||
if (!identifier) return;
|
||||
|
||||
try {
|
||||
const data = await assetApi.getInfo(identifier);
|
||||
|
||||
if (data.current_package_usage_id) {
|
||||
const metrics = getTrafficMetrics(data);
|
||||
usedMB.value = metrics.used;
|
||||
totalMB.value = metrics.total;
|
||||
remainMB.value = metrics.remain;
|
||||
emit('packageLoaded', data);
|
||||
} else {
|
||||
usedMB.value = 0;
|
||||
totalMB.value = 0;
|
||||
remainMB.value = 0;
|
||||
emit('packageLoaded', null);
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('加载套餐流量数据失败', e);
|
||||
}
|
||||
};
|
||||
|
||||
// 监听 identifier 变化
|
||||
watch(() => [props.identifier || userStore.state.identifier, userStore.state.token, userStore.state.loginPending], ([newVal, token, loginPending]) => {
|
||||
if (newVal && token && !loginPending) {
|
||||
loadPackageData();
|
||||
}
|
||||
}, {
|
||||
immediate: true
|
||||
});
|
||||
</script>
|
||||
|
||||
<style scoped lang="scss">
|
||||
|
||||
Reference in New Issue
Block a user