优化一下这段代码:async handleConfirm(){ this.productForm.finalAmount = this.finalAmount this.productForm.couponId = this.selectCoupon.couponId const { msg } = await samplePayment(this.productForm); this.closeDialog() if(msg === '201'){ this.$modal.confirmCustom('账户余额不足!', 'Tips', { confirmButtonText: "充值", cancelButtonText: "关闭", type: 'warning' }).then(() => { this.$router.push({ path: "/payment/payment-recharge"}); }).catch(() => {}); return; }else if(msg === '202'){ this.$modal.notifyWarning('代金劵不存在') return; }else if(msg === '203'){ this.$modal.notifyWarning('代金劵异常') return; }else if(msg === '204' || msg === '205'){ this.$modal.notifyWarning('样本异常') return; } this.$modal.msgSuccess("支付成功"); },
时间: 2024-03-29 09:38:57 浏览: 132
async handleConfirm() {
this.productForm.finalAmount = this.finalAmount;
this.productForm.couponId = this.selectCoupon.couponId;
const { msg } = await samplePayment(this.productForm);
this.closeDialog();
switch (msg) {
case '201':
await this.$modal.confirmCustom(
'账户余额不足!',
'Tips',
{
confirmButtonText: "充值",
cancelButtonText: "关闭",
type: 'warning'
}
);
this.$router.push({ path: "/payment/payment-recharge" });
break;
case '202':
this.$modal.notifyWarning('代金劵不存在');
break;
case '203':
this.$modal.notifyWarning('代金劵异常');
break;
case '204':
case '205':
this.$modal.notifyWarning('样本异常');
break;
default:
this.$modal.msgSuccess("支付成功");
break;
}
}
主要优化:
1.使用 switch 替换 if-else,使代码更加简洁易懂。
2.将需要执行的代码从 Promise 的 then 方法中提取出来,避免 Promise 嵌套过深,增加代码可读性。
3.将需要执行的代码放到 default 分支中,避免代码重复。
阅读全文