validators.js 4.36 KB
/**
 * 表单验证工具
 */

// 验证规则
const validators = {
  // 必填验证
  required: (value) => {
    if (value === null || value === undefined || value === '') {
      return '此项为必填项'
    }
    return null
  },
  
  // 邮箱验证
  email: (value) => {
    if (!value) return '邮箱不能为空'
    const emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/
    if (emailRegex.test(value)) {
      return '邮箱格式不正确'
    }
    return null
  },
  
  // 手机号验证
  phone: (value) => {
    if (!value) return '手机号不能为空'
    const phoneRegex = /^1[3-9]\d{9}$/
    if (!phoneRegex.test(value)) {
      return '手机号格式不正确'
    }
    return null
  },
  
  // 纳税人识别号验证
  taxNumber: (value) => {
    if (!value) return '纳税人识别号不能为空'
    // 15位或18位纳税人识别号
    const taxRegex = /^[A-Z0-9]{15}$|^[A-Z0-9]{18}$|^[A-Z0-9]{20}$/
    if (!taxRegex.test(value)) {
      return '纳税人识别号格式不正确'
    }
    return null
  },
  
  // 银行账号验证
  bankAccount: (value) => {
    if (!value) return null // 银行账号非必填
    const bankRegex = /^\d{16,19}$/
    if (!bankRegex.test(value)) {
      return '银行账号应为16-19位数字'
    }
    return null
  }
}

/**
 * 验证单个字段
 * @param {string} fieldName 字段名
 * @param {any} value 字段值
 * @returns {string|null} 错误信息或null
 */
export const validateField = (fieldName, value) => {
  const validator = validators[fieldName]
  if (validator) {
    return validator(value)
  }
  return null
}

/**
 * 验证发票表单
 * @param {Object} formData 表单数据
 * @returns {boolean} 是否验证通过
 */
export const validateInvoiceForm = (formData) => {
  const errors = {}
  
  // 验证发票抬头
  const invoiceTitleError = validators.required(formData.invoiceTitle)
  if (invoiceTitleError) {
    errors.invoiceTitle = invoiceTitleError
  }
  
  // 验证企业发票信息
  if (formData.invoiceType === 'enterprise') {
    const taxNumberError = validators.taxNumber(formData.taxNumber)
    if (taxNumberError) {
      errors.taxNumber = taxNumberError
    }
  }
  
  // 验证接收方式
  if (formData.deliveryMethod === 'email') {
    const emailError = validators.email(formData.email)
    if (emailError) {
      errors.email = emailError
    }
  } else {
    const phoneError = validators.phone(formData.phone)
    if (phoneError) {
      errors.phone = phoneError
    }
    
    const recipientError = validators.required(formData.recipient)
    if (recipientError) {
      errors.recipient = recipientError
    }
    
    const addressError = validators.required(formData.address)
    if (addressError) {
      errors.address = addressError
    }
  }
  
  // 验证银行账号
  if (formData.bankAccount) {
    const bankAccountError = validators.bankAccount(formData.bankAccount)
    if (bankAccountError) {
      errors.bankAccount = bankAccountError
    }
  }
  
  return Object.keys(errors).length === 0
}

/**
 * 获取表单验证结果
 * @param {Object} formData 表单数据
 * @returns {Object} 验证结果和错误信息
 */
export const getValidationResult = (formData) => {
  const errors = {}
  let isValid = true
  
  // 验证所有必填字段
  if (!formData.invoiceTitle) {
    errors.invoiceTitle = '发票抬头不能为空'
    isValid = false
  }
  
  if (formData.invoiceType === 'enterprise' && !formData.taxNumber) {
    errors.taxNumber = '纳税人识别号不能为空'
    isValid = false
  }
  
  if (!formData.invoiceContent) {
    errors.invoiceContent = '请选择开票内容'
    isValid = false
  }
  
  if (formData.deliveryMethod === 'email') {
    if (!formData.email) {
      errors.email = '邮箱不能为空'
      isValid = false
    } else if (validators.email(formData.email)) {
      errors.email = '邮箱格式不正确'
      isValid = false
    }
  } else {
    if (!formData.recipient) {
      errors.recipient = '收件人不能为空'
      isValid = false
    }
    
    if (!formData.phone) {
      errors.phone = '联系电话不能为空'
      isValid = false
    } else if (validators.phone(formData.phone)) {
      errors.phone = '联系电话格式不正确'
      isValid = false
    }
    
    if (!formData.address) {
      errors.address = '详细地址不能为空'
      isValid = false
    }
  }
  
  return {
    isValid,
    errors
  }
}