index.js 2.53 KB
export function debounce(func, wait = 200) {
  let timeout
  return function(event) {
    clearTimeout(timeout)
    timeout = setTimeout(() => {
      func.call(this, event)
    }, wait)
  }
}

export function dateFormat(dateTime, format = 'YYYY-MM-DD HH:mm') {
  const date = new Date(dateTime)
  const config = {
    YYYY: date.getFullYear(),
    MM: date.getMonth() + 1, //getMonth() 方法根据本地时间返回指定日期的月份(从 0 到 11)
    DD: date.getDate(),
    HH: date.getHours(),
    mm: date.getMinutes(),
    ss: date.getSeconds()
  }
  for (const key in config) {
    format = format.replace(key, config[key])
  }
  return format
}
/**
 * 校验手机号码格式
 */
export function checkPhone(mobile) {
  const myreg = /^(13[0-9]|15[0-9]|16[0-9]|17[0-9]|18[0-9]|19[0-9]|14[0-9])[0-9]{8}$/
  return myreg.test(mobile)
}

/**
 * 用于glsx-combi-form的select写入options
 * formName String FormData的属性名
 * hasAll Boolean 是否需要添加“全部”属性
 * refreshSigle String 更新特定的属性的options
 */
export function getOptionsList(that, formName, hasAll = true, refreshSigle) {
  if (refreshSigle) {
    var item = that[formName].find(k => k.name == refreshSigle)
    var index = that[formName].findIndex(k => k.name == refreshSigle)
    that.$set(that[formName][index], 'optionList', that.$common.utils.deepClone(that.baseData[item.optionsName]))
    if (hasAll && that.baseData[item.optionsName].length && that.baseData[item.optionsName][0][that[formName][index].optionLabel] !== '全部') {
      const temp = {}
      temp[that[formName][index].optionId] = ''
      temp[that[formName][index].optionLabel] = '全部'
      that[formName][index].optionList.unshift(temp)
    }
  } else {
    that[formName].forEach((item, index) => {
      if (item.type === 'select' && that.baseData[item.optionsName]?.length) {
        that.$set(that[formName][index], 'optionList', that.$common.utils.deepClone(that.baseData[item.optionsName]))
        if (hasAll && that.baseData[item.optionsName].length && that.baseData[item.optionsName][0][that[formName][index].optionLabel] !== '全部') {
          const temp = {}
          temp[that[formName][index].optionId] = ''
          temp[that[formName][index].optionLabel] = '全部'
          that[formName][index].optionList.unshift(temp)
        }
      }
    })
  }
}

export function aToExport(url) {
  var a = document.createElement('a')
  a.setAttribute('id', 'exportToExport')
  a.setAttribute('href', url)
  a.setAttribute('target', '_blank')
  document.body.appendChild(a)
  a.click()
}