帮我优化这段代码 handleWorkOrderMessage() { // 待办理、待审核二级弹窗 console.log(this.pWorkOrder.orderState); if (this.pWorkOrder.orderState === '20') { this.popupSitch.description.show = true; this.popupSitch.description.label = '经办意见'; this.description = '按要求完成,下单!'; this.orderState = '30'; } // 接单 if (this.pWorkOrder.orderState === '30') { this.popupSitch.description.show = true; this.popupSitch.description.label = '经办意见'; this.description = '按要求完成,接单!'; this.orderState = '40'; } // 反馈 if (this.pWorkOrder.orderState === '40') { this.popupSitch.description.show = true; this.popupSitch.description.label = '经办意见'; this.popupSitch.image.label = '反馈图片'; this.popupSitch.image.show = true; this.description = '按要求完成,反馈!'; this.orderState = '50'; } // 审核关闭 if (this.pWorkOrder.orderState === '50') { this.popupSitch.description.show = true; this.popupSitch.description.label = '经办意见'; this.description = '按要求完成,审核关闭!'; this.orderState = '60'; } this.$refs.orderPopup.open(); },
时间: 2024-01-09 21:04:13 浏览: 28
可以考虑使用 switch case 语句来替换 if else 语句,这样代码更加简洁易读。同时,可以将重复的代码提取成函数,避免代码冗余。下面是重构后的代码:
```
handleWorkOrderMessage() {
const { orderState } = this.pWorkOrder;
switch (orderState) {
case '20':
this.showDescriptionPopup('按要求完成,下单!', '30');
break;
case '30':
this.showDescriptionPopup('按要求完成,接单!', '40');
break;
case '40':
this.showImagePopup('按要求完成,反馈!', '50');
break;
case '50':
this.showDescriptionPopup('按要求完成,审核关闭!', '60');
break;
default:
break;
}
this.$refs.orderPopup.open();
},
showDescriptionPopup(description, orderState) {
this.popupSitch.description.show = true;
this.popupSitch.description.label = '经办意见';
this.description = description;
this.orderState = orderState;
},
showImagePopup(description, orderState) {
this.popupSitch.description.show = true;
this.popupSitch.description.label = '经办意见';
this.popupSitch.image.label = '反馈图片';
this.popupSitch.image.show = true;
this.description = description;
this.orderState = orderState;
},
```
可以看到,重构后的代码更加简洁易读,同时避免了重复的代码。