Vue项目中常见的公共方法汇总(五):通用校验处理

// 初始化时间(通用型)
    initTm(queryForm, isTwo = true) {
      const limitTm = window.HT_EnvConfig.limitTm || 8
      let day = 0
      if (moment().hours() < limitTm) {
        day = 1
      }
      let fmtStr = 'YYYY-MM-DD '
      if (limitTm < 10) {
        fmtStr += '0' + limitTm + ':00'
      } else {
        fmtStr += limitTm + ':00'
      }
      queryForm.stm = moment()
        .subtract(day, 'days')
        .format(fmtStr)

      if (isTwo) {
        queryForm.etm = moment()
          .add(moment().minutes() > 0 ? 1 : 0, 'hours')
          .format('YYYY-MM-DD HH:00')
      }
    },
  // 初始化时间(昨日8点到现在)
    initTmR(queryForm, isTwo = true) {
      let day = 0
      const limitTm = window.HT_EnvConfig.limitTmR || 8
      if (moment().hours() < limitTm) {
        day = 1
      }
      let fmtStr = 'YYYY-MM-DD '
      if (limitTm < 10) {
        fmtStr += '0' + limitTm + ':00'
      } else {
        fmtStr += limitTm + ':00'
      }
      queryForm.stm = moment()
        .subtract(day, 'days')
        .format(fmtStr)

      if (isTwo) {
        queryForm.etm = moment()
          .add(moment().minutes() > 0 ? 1 : 0, 'hours')
          .format('YYYY-MM-DD HH:00')
      }
    },
// 初始化时间(昨日6点到现在)
    initTmQ(queryForm, isTwo = true) {
      let day = 0
      const limitTm = window.HT_EnvConfig.limitTmQ || 8
      if (moment().hours() < limitTm) {
        day = 1
      }
      let fmtStr = 'YYYY-MM-DD '
      if (limitTm < 10) {
        fmtStr += '0' + limitTm + ':00'
      } else {
        fmtStr += limitTm + ':00'
      }
      queryForm.stm = moment()
        .subtract(day, 'days')
        .format(fmtStr)

      if (isTwo) {
        queryForm.etm = moment()
          .add(moment().minutes() > 0 ? 1 : 0, 'hours')
          .format('YYYY-MM-DD HH:00')
      }
    },
  // 时间校验
    valiTm(queryForm) {
      var etm = moment(queryForm.etm)
      var stm = moment(queryForm.stm)
      if (etm.diff(stm) < 0) {
        this.$message({
          message: '开始时间不能大于结束时间!',
          type: 'warning'
        })
        return false
      }

      if (etm.diff(stm, 'days', true) > 31) {
        this.$message({
          message: '开始时间和结束时间间隔不能超过31天!',
          type: 'warning'
        })
        return false
      }

      return true
    },

// input框中数值类型校验
 isNumber(val, isStrict = false) {
      // 非负浮点数
      const regPos = /^\d+(\.\d+)?$/
      // 负浮点数
      const regNeg = /^(-(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)))$/

      // 是否严格区分
      if (isStrict) {
        if (regPos.test(val)) {
          return true
        } else {
          return false
        }
      } else {
        if (regPos.test(val) || regNeg.test(val)) {
          return true
        } else {
          return false
        }
      }
    },

// 转换数值类型
 toNumber(str, dec = 1) {
      const r = parseFloat(str)
      if (isNaN(r)) {
        return 0
      } else {
        return parseFloat(r.toFixed(dec))
      }
    },
 // 保留0位小数(结果是字符型),页面显示的时候使用
    toDecStr0(str) {
      // 异常值判断
      if (str === null || str === undefined || str === '' || str === ' ') return '-'

      const r = parseInt(str)
      if (isNaN(r)) {
        return '-'
      } else {
        return r
      }
    },
 // 保留一位小数(结果是字符型),页面显示的时候使用
    toDecStr1(str, trim0 = true) {
      // 异常值判断
      if (str === null || str === undefined || str === '' || str === ' ') return '-'

      const dec = 1
      const r = parseFloat(str)
      if (isNaN(r)) {
        return '-'
      } else {
        let res = r.toFixed(dec)
        if (trim0 === false) {
          return res
        }

        // 删除尾部的‘0’
        if (res.endsWith('0')) {
          res = res.substring(0, res.lastIndexOf('0'))
        }

        // 删除尾部的‘.’
        if (res.endsWith('.')) {
          res = res.substring(0, res.lastIndexOf('.'))
        }
        return res
      }
    },
 // 保留两位小数(结果是字符型),页面显示的时候使用
    toDecStr2(str, trim0 = true) {
      // 异常值判断
      if (str === null || str === undefined || str === '' || str === ' ') return '-'

      const dec = 2
      const r = parseFloat(str)
      if (isNaN(r)) {
        return '-'
      } else {
        let res = r.toFixed(dec)
        if (trim0 === false) {
          return res
        }

        // 删除尾部的‘0’
        if (res.endsWith('00')) {
          res = res.substring(0, res.lastIndexOf('0') - 1)
        } else if (res.endsWith('0')) {
          res = res.substring(0, res.lastIndexOf('0'))
        }

        // 删除尾部的‘.’
        if (res.endsWith('.')) {
          res = res.substring(0, res.lastIndexOf('.'))
        }
        return res
      }
    },
 // 保留三位小数(结果是字符型),页面显示的时候使用
    toDecStr3(str, trim0 = true) {
      // 异常值判断
      if (str === null || str === undefined || str === '' || str === ' ') return '-'

      const dec = 3
      const r = parseFloat(str)
      if (isNaN(r)) {
        return '-'
      } else {
        let res = r.toFixed(dec)
        if (trim0 === false) {
          return res
        }

        // 删除尾部的‘0’
        if (res.endsWith('000')) {
          res = res.substring(0, res.lastIndexOf('0') - 2)
        } else if (res.endsWith('00')) {
          res = res.substring(0, res.lastIndexOf('0') - 1)
        } else if (res.endsWith('0')) {
          res = res.substring(0, res.lastIndexOf('0'))
        }

        // 删除尾部的‘.’
        if (res.endsWith('.')) {
          res = res.substring(0, res.lastIndexOf('.'))
        }
        return res
      }
    }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值