common.js 3.4 KB
import axios from 'axios'
export const Common = {
  // 提示框
  confirmDialog: (tipData) => {
    tipData.that.$confirm(tipData.tipText, tipData.tipTitle, {
      confirmButtonText: tipData.confirmButtonText || '确定',
      cancelButtonText: tipData.cancelButtonText || '取消',
      type: tipData.type,
      showCancelButton: tipData.showCancelButton
    }).then(() => {
      tipData.callBack ? tipData.callBack() : ''
    }).catch(() => {
      tipData.errorCallBack ? tipData.errorCallBack() : ''
    })
  },
  /**
	 * 导出excel
	 * */
  exportExcel: function (options) {
    if (!options) {
      return false
    }
    console.log(options.data)
    axios({
      method: options.method || 'POST',
      url: options.url,
      params: options.data,
      responseType: 'blob',
      headers: {
        Authorization: localStorage.getItem('token') || ''
      }
    }).then(response => {
      if (!response) {
        return
      }
      let url = window.URL.createObjectURL(new Blob([response.data], {
        type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8'
      }))
      let link = document.createElement('a')
      link.style.display = 'none'
      link.href = url
      link.setAttribute('download', options.fileName || 'excel.xlsx')
      document.body.appendChild(link)
      link.click()
      if (options.success) {
        (options.success)()
      }
    }).catch((error) => {
      if (options.errorFunc) {
        (options.errorFunc)(error)
      }
    })
  },
  /**
	 * 正整数
	 * */
  checkRegExpNumber(rule, value, callback) {
    var rxp = /^\+?[1-9][0-9]*$/
    if (!rule.required) {
      if (!value) {
        callback()
      }else if (!rxp.test(value)) {
        return callback(new Error('只支持正整数'))
      }
    }
    if (!value) {
      return callback(new Error('请输入正整数'))
    }else {
      callback()
    }
  },
  /**
	 * 手机号
	 * */
  checkRegExpPhoneTel(rule, value, callback) {
    var rxpPhone = /^1[3456789]\d{9}$/
    var rxpTel = /^(\(\d{3,4}\)|\d{3,4}-|\s)?\d{7,14}$/
    if (!rule.required) {
      if (!value) {
        callback()
      }else if (!rxpPhone.test(value)) {
        return callback(new Error('请输入正确的手机号码'))
      }
    }
    if (!value) {
      return callback(new Error('请输入手机号码或固话'))
    }else if (!rxpPhone.test(value) && !rxpTel.test(value)) {
      return callback(new Error('请输入正确的手机号码或固话'))
    }else {
      callback()
    }
  },
  /** 
   * 中文英文数字下划线括号
   */
  checkName(rule, value, callback) {
    var rxp = /^[\u4e00-\u9fa5_a-zA-Z0-9.\\()\\()]+$/
    if (!rule.required) {
      if (!value) {
        callback()
      }else if (!rxp.test(value)) {
        return callback(new Error('请输入中文英文数字下划线或括号'))
      }
    }
    if (!value) {
      return callback(new Error('请输入'))
    }else {
      callback()
    }
  },
  /**
   * 车架号的验证
   */
  checkvin(rule, value, callback) {
    let reg = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{17}$/
    if (!reg.test(value)) {
      callback(new Error('车架号必须是17位数字+字母组合'))
    } else callback()
  },
  /**
   * 设备号的验证
   */
  checksn(rule, value, callback) {
    let reg = /[0-9]{8,15}/g
    if (value && !reg.test(value)) {
      callback(new Error('设备号必须是8-15位数字'))
    } else callback()
  }
}