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} core CPU核心信息
* - {string} cores CPU核心数 * - {string} cores CPU核心数
*/ */
export function getCPUInfo(text) { export function getCPUInfo(text = '') {
const cpuInfo = { const cpuInfo = {
company: '', company: '',
model: '', model: '',

View File

@ -87,7 +87,7 @@ const { cpuAndMemAndDisk } = handleServerInfo({
}); });
const slogan = computed(() => props.info?.PublicNote?.customData?.slogan); 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> </script>
<style lang="scss" scoped> <style lang="scss" scoped>

View File

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