记项目 常用js方法

//string格式化
repalceHtml (str) {
  let s1 = str.replace(/<\/?.+?>/g, '')
  let s2 = s1.replace(/ /g, '')// 去html
  let s3 = s2.replace(/ /ig, '') // 转为空格
  // let s4 = s3.replace(/\s*/g, '')// 去空格;
  return s3
}
// 百分数转小数
toPoint (percent) {
  let str = percent.replace('%', '')
  str = str / 100
  return str
}
// 大写金额
DX (n) {
  if (!/^(0|[1-9]\d*)(\.\d+)?$/.test(n)) { return '数据非法' }
  let unit = '千百拾亿千百拾万千百拾元角分'
  let str = ''
  n += '00'
  let p = n.indexOf('.')
  if (p >= 0) { n = n.substring(0, p) + n.substr(p + 1, 2) }
  unit = unit.substr(unit.length - n.length)
  for (let i = 0; i < n.length; i++) { str += '零壹贰叁肆伍陆柒捌玖'.charAt(n.charAt(i)) + unit.charAt(i) }
  return str.replace(/零(千|百|拾|角)/g, '零').replace(/(零)+/g, '零').replace(/零(万|亿|元)/g, '$1').replace(/(亿)万|壹(拾)/g, '$1$2').replace(/^元零?|零分/g, '').replace(/元$/g, '元整')
}
// 手机号正则
let reg = 11 && /^((13|14|15|17|18)[0-9]{1}\d{8})$/
if (!reg.test(this.form.account)) {
  this.$message.warning('手机号码格式不匹配!')
  return false
}
// 数组转对象
array2obj (array, key) {
  let resObj = {}
  for (let i = 0; i < array.length; i++) {
    resObj[array[i][key]] = array[i]
  }
  return resObj
}
// 数组转树状结构
let newArr = roleTree.data.treeList
if (newArr.length !== 0) {
  newArr.forEach(item => {
    const pid = item.pid
    if (pid !== '') {
      newArr.forEach(ele => {
        if (ele.id === pid) {
          let childArray = ele.children
          if (!childArray) {
            childArray = []
          }
          childArray.push(item)
          ele.children = childArray
        }
      })
    }
  })
}
// 文件下载
let fileName = data.data.replace(/redirect:/, '') // 文件地址
let downName = row.name // 文件下载名称
const blob = new Blob([fileName])
if (window.navigator.msSaveOrOpenBlob) {
  // 兼容IE10
  navigator.msSaveBlob(blob, downName)
} else {
  //  chrome/firefox
  let aTag = document.createElement('a')
  aTag.download = downName
  aTag.href = URL.createObjectURL(blob)
  aTag.click()
  URL.revokeObjectURL(aTag.href)
}
// 状态转换
convertStatus (status) {
  switch (status) {
    case 'NORMAL':
      return '正常'
    case 'REST':
      return '停业'
    case 'VIOLATION':
      return '违规'
    case 'TO_CHECK':
      return '待审核'
    case 'NO_PASS':
      return '未通过'
  }
}

isAbled (state) {
  if (state === 'TO_PAY') {
    return '待付款'
  } else if (state === 'TO_CHECK') {
    return '待审批'
  } else if (state === 'TO_DELIVERY') {
    return '调剂中'
  } else if (state === 'TO_RECEIVED') {
    return '待收货'
  } else if (state === 'TO_APPRAISE') {
    return '待评价'
  } else if (state === 'COMPLETED') {
    return '交易成功'
  } else if (state === 'REFUNDING') {
    return '退款中'
  } else if (state === 'REFUND_COMPLETE') {
    return '退款成功'
  } else if (state === 'CLOSED') {
    return '交易关闭'
  }
}

item.state = this.isAbled(item.state)

未完待续。。。

你可能感兴趣的:(js)