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

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
}
/**
 * 校验手机号码格式
 */
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
 */
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)
        }
      }
    })
  }
}

/** 二次确认封装 */
function secondConfirm(that, title, txt, apiModule, apiName, apiParams, type = 'warning', fn) {
  that.$confirm(txt, title, {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type,
    callback: action => {
      if (action == 'confirm') {
        that.$apis[apiModule][apiName](apiParams).then(res => {
          that.$message({
            message: res.code == '0' ? '操作成功' : res.message,
            type: res.code == '0' ? 'success' : 'error'
          })
          if (res.code == '0') {
            fn && fn()
            that.handleSearch()
          }
        })
      }
    }
  })
}

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()
}
 function getArrayIsSubset (arr2, arr1)  {
  if(arr2.length ==0) return false
  return arr2.every(item => {
    return arr1.includes(item)
  })
}
export default {
  debounce,
  dateFormat,
  checkPhone,
  getOptionsList,
  secondConfirm,
  aToExport,
  getArrayIsSubset
}