优化建议:

  1. 将函数拆分成多个小函数,方便维护和重用。
  2. 简化if-else语句,避免过多嵌套,提高代码可读性。
  3. 缓存变量,避免重复计算。
  4. 使用三元表达式替换部分if-else语句,提高代码简洁性。
  5. 避免在函数中直接调用uni.showToast()等UI操作,应该将UI操作封装成一个单独的函数。

修改后的代码如下:

// 校验总人数 function validateTotalNum(selected, id) { if (this.workerList.length < this.dataList.needWorkerNum - this.dataList.arriveNum) { if (!selected) { this.pointList[id].selected = !this.pointList[id].selected; this.workerList.push(this.pointList[id]); } else { this.pointList[id].selected = !this.pointList[id].selected; this.workerList = this.workerList.filter(worker => worker.id !== id); } } else { uni.showToast({ title: '人员超出限制', duration: 2000 }); } }

// 校验男女人数 function validateGenderNum(selected, id) { if (this.pointList[id].sex == '男') { if (!selected) { if (this.currentMale == 0) { uni.showToast({ title: '男性人员超出限制', duration: 2000 }); return; } this.pointList[id].selected = !this.pointList[id].selected; this.workerList.push(this.pointList[id]); this.currentMale--; } else { this.pointList[id].selected = !this.pointList[id].selected; this.workerList = this.workerList.filter(worker => worker.id !== id); this.currentMale++; } } else { if (!selected) { if (this.currentWomen == 0) { uni.showToast({ title: '女性人员超出限制', duration: 2000 }); return; } this.pointList[id].selected = !this.pointList[id].selected; this.workerList.push(this.pointList[id]); this.currentWomen--; } else { this.pointList[id].selected = !this.pointList[id].selected; this.workerList = this.workerList.filter(worker => worker.id !== id); this.currentWomen++; } } }

// 点击选择人员 function personSelect(e) { const selected = this.pointList[e].selected; const id = this.pointList[e].id; // 没有性别要求 if (this.dataList.needMaleNum === null) { validateTotalNum.call(this, selected, id); } else { validateGenderNum.call(this, selected, id); }

优化jspersonSelecte 				没有性别要求				if thisdataListneedMaleNum === null 					校验总人数 如果没有性别要求则校验总人数即可如果有则校验需要的男女数与总人数					if thisworkerListlength thisdataListneedWorkerNum - thisdataListarriveNum 						如果没

原文地址: https://www.cveoy.top/t/topic/gdhA 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录