优化这段代码 let that = this if( !/^\+?[1-9][0-9]*$/.test(that.data.cost) ){ wx.showToast({ title: '佣金必须为非零的正整数', icon: 'none', duration: 2000 }) return false; }else { if(that.data.cost > 100 && that.data.cost <= 300){ missionSingleCostWeight = that.data.missionSingleCostWeight_D_value * 0.3 }else if(that.data.cost > 300 && that.data.cost <= 500){ missionSingleCostWeight = that.data.missionSingleCostWeight_D_value * 0.6 }else if(that.data.cost > 500 && that.data.cost <= 1000){ missionSingleCostWeight = that.data.missionSingleCostWeight_D_value * 0.8 }else if(that.data.cost > 1000 && that.data.cost <= 3000){ missionSingleCostWeight = that.data.missionSingleCostWeight_D_value * 1 }else if(that.data.cost > 3000 && that.data.cost <= 5000){ missionSingleCostWeight = that.data.missionSingleCostWeight_D_value * 0.6 }else if(that.data.cost > 5000 && that.data.cost <= 8000){ missionSingleCostWeight = that.data.missionSingleCostWeight_D_value * 0.4 }else if(that.data.cost > 8000 && that.data.cost <= 10000){ missionSingleCostWeight = that.data.missionSingleCostWeight_D_value * 0.2 }else if(that.data.cost > 10000 && that.data.cost <= 15000){ missionSingleCostWeight = that.data.missionSingleCostWeight_D_value * 0.1 }else{ missionSingleCostWeight = that.data.missionSingleCostWeight_D_value * 0 } if (that.data.cost > 0 && that.data.cost <= 500) { costPercent = 0.3 }else if (that.data.cost > 500 && that.data.cost <= 1000) { costPercent = 0.4 }else if (that.data.cost > 1000 && that.data.cost <= 2000) { costPercent = 0.5 }else if (that.data.cost > 2000 && that.data.cost <= 5000) { costPercent = 0.3 }else if (that.data.cost > 5000 && that.data.cost <= 10000) { costPercent = 0.2 }else if (that.data.cost > 10000 && that.data.cost <= 28000) { costPercent = 0.1 }else if (that.data.cost > 28000 && that.data.cost <= 80000) { costPercent = 0.05 }else if (that.data.cost > 80000 && that.data.cost <= 300000) { costPercent = 0 } }
时间: 2023-06-04 15:02:36 浏览: 110
和孩子一起100天拿下中考英语考纲词汇中文版--第2天-1页.pdf
){ wx.showToast({ title: '佣金不能超过100元', icon: 'none', duration: 2000 }) return false; }else{ return true; } }
可以将代码优化为:
const cost = this.data.cost;
if (/^\s*[1-9]\d*$/.test(cost)) {
const num = parseInt(cost);
if (num > 100) {
wx.showToast({
title: '佣金不能超过100元',
icon: 'none',
duration: 2000
})
return false;
} else {
return true;
}
} else {
wx.showToast({
title: '佣金必须为非零的正整数',
icon: 'none',
duration: 2000
})
return false;
}
阅读全文