fix: 修复获取CPU信息时的可选链问题,确保在未提供CPU数据时不会抛出错误

This commit is contained in:
hi2hi 2024-12-06 10:04:03 +00:00
parent f2fb191b52
commit 5b2bea95a0
3 changed files with 8 additions and 5 deletions

View File

@ -24,7 +24,7 @@
* - {string} core CPU核心信息
* - {string} cores CPU核心数
*/
export function getCPUInfo(text) {
export function getCPUInfo(text = '') {
const cpuInfo = {
company: '',
model: '',

View File

@ -87,7 +87,7 @@ const { cpuAndMemAndDisk } = handleServerInfo({
});
const slogan = computed(() => props.info?.PublicNote?.customData?.slogan);
const cpuInfo = computed(() => hostUtils.getCPUInfo(props.info.Host.CPU[0]));
const cpuInfo = computed(() => hostUtils.getCPUInfo(props.info?.Host?.CPU?.[0]));
</script>
<style lang="scss" scoped>

View File

@ -13,7 +13,7 @@ export default (params) => {
return {};
}
const cpuInfo = computed(() => {
if (props.info?.Host?.CPU) {
if (props.info?.Host?.CPU?.[0]) {
return hostUtils.getCPUInfo(props.info.Host.CPU[0]);
}
return {};
@ -61,6 +61,8 @@ export default (params) => {
const serverStatusList = computed(() => statusListTpl.split(',').map((i) => {
switch (i) {
case 'cpu':
{
const CoresVal = cpuInfo.value?.cores ? `${cpuInfo.value?.cores}C` : '-';
return {
type: 'cpu',
used: (props.info.State?.CPU || 0).toFixed(1) * 1,
@ -71,10 +73,11 @@ export default (params) => {
valText: `${(props.info.State?.CPU || 0).toFixed(1) * 1}%`,
label: 'CPU',
content: {
default: cpuInfo.value?.core,
mobile: `${cpuInfo.value?.cores}C`,
default: cpuInfo.value?.core || CoresVal,
mobile: CoresVal,
},
};
}
case 'mem':
{
let usedVal;