跳转至

条件判断优化

案例1

  • 优化前
if (newData.editorId === this.userInfo.id || (newData.assistantEditorId === this.userInfo.id && newData.isAssistantEditor) && (newData.status === "Created" || newData.status === "Pending_edit" || newData.status === "Editing" || newData.status === "Pending_modify" || newData.status === "Modifying")) {
          this.isRefuellingEngineer = true;
        }
  • 优化后
// 定义一个数组来包含所有允许的状态
const allowStatuses = ["Created", "Pending_edit", "Editing", "Pending_modify", "Modifying"];

// 检查用户是否是主编辑或助理编辑,并且状态是否允许
const isEditor = newData.editorId === this.userInfo.id;
const isAssistantEditor = newData.assistantEditorId === this.userInfo.id && newData.isAssistantEditor;

// 如果用户是主编辑或助理编辑,并且状态是允许的,设置this.isRefuellingEngineer为true
if ((isEditor || isAssistantEditor) && allowStatuses.includes(newData.status)) {
  this.isRefuellingEngineer = true;
}

案例2

  • 优化前
if (newData.verifierId === this.userInfo.id && (newData.status === "Pending_verify" || newData.status === "Verifying")) {
          this.isVerificationEngineer = true;
        }
  • 优化后
// 定义一个数组来包含所有允许的状态
const verificationStatuses = ["Pending_verify", "Verifying"];

// 如果用户是验证工程师,并且状态是允许的,设置this.isVerificationEngineer为true
if (newData.verifierId === this.userInfo.id && verificationStatuses.includes(newData.status)) {
  this.isVerificationEngineer = true;
}