我有下面的代码。它看起来又长又复杂,现在我必须添加更多条件。有什么办法可以简化这段代码。我所能想到的就是将 City 和 Menu 开关结合起来,但是如果我这样做了,我必须以一种方式获取 City 的 pk 值,而另一种方式获取 Menu 的 pk 值。javascript 是否提供任何其他方式来做与开关相同的事情?很抱歉列出了长代码,但我认为我应该包括所有内容。
function getParams(entity) {
var store = window.localStorage;
var table = "Content";
switch (entity) {
case "City":
if (store.getItem('AccountID')) {
pk = store.getItem('AccountID') + "04" + "000";
return {
pk: pk,
param: '?pk=' + pk,
table: table,
rc: true
}
} else {
paramOnFailure("Please reselect Account");
return { rc: false }
}
break;
case "Menu":
if (store.getItem('AccountID')) {
pk = store.getItem('AccountID') + "00" + "000";
return {
pk: pk,
param: '?pk=' + pk,
table: table,
rc: true
}
} else {
paramOnFailure("Please reselect Account");
return { rc: false }
}
break;
case "Page":
if (store.getItem('AccountID') && store.getItem('PageID')) {
pk = store.getItem('AccountID') + store.getItem('PageID') + "000";
return {
pk: pk,
param: '?pk=' + pk,
table: table,
rc: true
}
} else {
paramOnFailure("Please reselect Account and Page Type");
return { rc: false }
}
break;
case "Question":
if (store.getItem('AccountID') &&
store.getItem('CityID') &&
store.getItem('TopicID') ) {
pk = store.getItem('AccountID') + store.getItem('CityID');
return {
pk: pk,
param: '?pk=' + pk + '&rk=' + store.getItem('TopicID'),
table: "Question",
rc: true
}
} else {
paramOnFailure("Please reselect Account, City and Topic");
return { rc: false }
}
break;
case "Reference":
if (store.getItem('ReferenceID')) {
pk = store.getItem('ReferenceID');
return {
pk: pk,
param: '?pk=' + pk,
table: "Reference",
rc: true
}
} else {
paramOnFailure("Please reselect Reference");
return { rc: false }
}
break;
case "Topic":
if (store.getItem('AccountID') && store.getItem('CityID')) {
pk = store.getItem('AccountID') + "05" + store.getItem('CityID');
return {
pk: pk,
param: parameters = '?pk=' + pk,
table: table,
rc: true
}
} else {
paramOnFailure("Please reselect Account and City");
return { rc: false }
}
break;
default:
;
}