Author: Sue Xiaoxiao

Updated on August 19, 2020

Intercepts the specified parameter value in the URL path

GetQueryString(name) {
  var reg = new RegExp("(^ | &)" + name + "= (/ ^ & *) (& | $)");
  var r = window.location.search.substr(1).match(reg);
  if(r ! =null) return unescape(r[2]);
  return null;
}
Copy the code

Set the page distance to the specified location

setScrollTop(value) {
  setTimeout(() = > {
    window.scrollTo({
      top: value,
      behavior: "smooth"
    });
  }, 500);
}
Copy the code

Get top distance

getScrollTop() {
  return (
    window.pageYOffset ||
    document.documentElement.scrollTop ||
    document.body.scrollTop
  );
}
Copy the code

Judge terminal device

testMobileType() {
  const u = navigator.userAgent
  const isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1 / / android terminal
  constisiOS = !! u.match(/\(i[^;] +; ( U;) ? CPU.+Mac OS X/) / / ios terminal
  if (isAndroid) {
    return 'android'
  } else if (isiOS) {
    return 'ios'
  } else {
    return 'pc'}}Copy the code

Image stabilization

debounce(fn, delay) {
  var delay1 = delay || 1000
  var timer
  return function () {
    var th = this
    var args = arguments
    if (timer) {
      clearTimeout(timer)
    }
    timer = setTimeout(function () {
      timer = null
      fn.apply(th, args)
    }, delay1)
  }
}
Copy the code

The throttle

throttle(fn, interval) {
  var last
  var timer
  var interval1 = interval || 2000
  return function () {
    var th = this
    var args = arguments
    var now = +new Date(a)if (last && now - last < interval) {
      clearTimeout(timer)
      timer = setTimeout(function () {
        last = now
        fn.apply(th, args)
      }, interval1)
    } else {
      last = now
      fn.apply(th, args)
    }
  }
}
Copy the code

The time stamp turns in seconds

formatSecondsData(value) {
  var secondTime = parseInt(value); / / SEC.
  var minuteTime = 0; / / points
  var hourTime = 0; / / hour
  if (secondTime > 60) { // If the number of seconds is greater than 60, convert the number of seconds to an integer
    // Get minutes, divide by 60 to get the integer minutes
    minuteTime = parseInt(secondTime / 60);
    // Get the number of seconds. The number of seconds is the integer number of seconds
    secondTime = parseInt(secondTime % 60);
    // If minutes are greater than 60, convert minutes to hours
    if (minuteTime > 60) {
      Divide minutes by 60 to get integer hours
      hourTime = parseInt(minuteTime / 60);
      // Get the amount of measure taken after hours. Divide minutes by 60
      minuteTime = parseInt(minuteTime % 60); }}var result = "" + parseInt(secondTime) + "Seconds";

  if (minuteTime > 0) {
    result = "" + parseInt(minuteTime) + "Points" + result;
  }
  if (hourTime > 0) {
    result = "" + parseInt(hourTime) + "Hour" + result;
  }
  return result;
}
Copy the code

Verify mobile phone number

isPhone(val) {
        var patrn = /^(((1[3456789][0-9]{1})|(15[0-9]{1}))+\d{8})$/
        if(! patrn.test(val) || val ===' ') {
          return false
        } else {
          return true}}Copy the code

Check the license plate number

isCarNum(val) {
        var patrn = / ^ ([beijing-tianjin Shanghai yu ji yu cloud liao black xiang anhui new GuiGan of hubei province can Sue your yue jin meng shan ji min qinghai-tibet plain NingQiong that brought A - Z] {1} {1} [a-z] [A - HJ - NP - Z0-9] {4} [A - HJ - NP - Z0-9 hang cop Hong Kong and Macao] {1}) $/
        var patrn2 = / ^ ([beijing-tianjin Shanghai yu ji yu cloud liao black xiang anhui new GuiGan of hubei province can Sue your yue jin meng shan ji min qinghai-tibet plain NingQiong that brought A - Z] {1} {1} [a-z] ([[0-9] {5} [DF]) | ([DF], [A - HJ - NP - Z0-9]] [0-9] {4}))) $/
        if(! patrn.test(val) && ! patrn2.test(val)) {return false
        } else {
          return true}}Copy the code

Check frame number

isVehicle(val) {
        var patrn = /^[A-HJ-NP-Za-hj-np-z0-9]+$/
        if(! patrn.test(val) || val ===' ') {
          return false
        } else {
          return true}}Copy the code

Check id Card Number

isSfz(idcard) {
        var Errors = [1.'Wrong number of digits'.'Wrong date of birth'.'Wrong ID card number'.'Id card area illegal']
        var area = {
          11: 'Beijing'.12: 'tianjin'.13: 'hebei'.14: 'the shanxi'.15: Inner Mongolia.21: 'the liaoning'.22: 'jilin'.23: 'Heilongjiang'.31: 'Shanghai'.32: 'jiangsu'.33: 'zhejiang'.34: 'anhui'.35: 'fujian'.36: 'jiangxi'.37: 'shandong'.41: 'henan'.42: 'hubei'.43: 'in hunan province'.44: 'in guangdong'.45: 'the guangxi'.46: 'hainan'.50: 'chongqing'.51: 'sichuan'.52: 'guizhou'.53: 'yunnan'.54: 'Tibet'.61: 'the shaanxi'.62: 'gansu'.63: 'the qinghai'.64: 'the ningxia'.65: 'xinjiang'.71: 'Taiwan'.81: 'Hong Kong'.82: 'the'.91: 'foreign'
        }
        var Y, JYM
        var S, M, ereg, Err
        var arr = []
        arr = idcard.split(' ')
        if (area[parseInt(idcard.substr(0.2= =))]null) {
          Err = Errors[4]
          return Err
        }
        switch (idcard.length) {
          case 15:
            if ((parseInt(idcard.substr(6.2)) + 1900) % 4= = =0| | ((parseInt(idcard.substr(6.2)) + 1900) % 100= = =0 && (parseInt(idcard.substr(6.2)) + 1900) % 4= = =0)) {
              ereg = / ^ (1-9] [0-9] {5} [0-9] {2} ((01 | | 07 | 3 | 05 08 | | 10 12) (0 [1-9] | [1-2] [0-9] [0, 1]) | 3 | (04 | | | 06 09 11) (0 [1-9] | [1-2] [0-9] 30) | | 02 (0 | [[1-9] 1-2] [0-9] [0-9] {3})) $/ // Test the validity of the birth date
            } else {
              ereg = / ^ (1-9] [0-9] {5} [0-9] {2} ((01 | | 07 | 3 | 05 08 | | 10 12) (0 [1-9] | [1-2] [0-9] [0, 1]) | 3 | (04 | | | 06 09 11) (0 [1-9] | [1-2] [0-9] 30) | | 02 (0 [1-9] | 1 2 [[0-9] | 0 to 8]) [0-9] {3} $/ // Test the validity of the birth date
            }
            if (ereg.test(idcard)) {
              Err = Errors[0]}else {
              Err = Errors[2]}return Err
          case 18:
            if (parseInt(idcard.substr(6.4)) % 4= = =0| | -parseInt(idcard.substr(6.4)) % 100= = =0 && parseInt(idcard.substr(6.4)) % 4= = =0)) {
              ereg = / ^ (1-9] [0-9] {5} [0-9] {2} (19 (01 | | 07 | 3 | 05 08 | | 10 12) (0 [1-9] | [1-2] [0-9] | 3 [0, 1]) | (04 | | | 06 09 11) (0 [1-9] | [1-2] [0-9] 30) | | 02 (0 [1-9] |[1-2][0-9]))[0-9]{3}[0-9Xx]$/ // A valid regular expression for leap year birth dates
            } else {
              ereg = / ^ (1-9] [0-9] {5} [0-9] {2} (19 (01 | | 07 | 3 | 05 08 | | 10 12) (0 [1-9] | [1-2] [0-9] | 3 [0, 1]) | (04 | | | 06 09 11) (0 [1-9] | [1-2] [0-9] 30) | | 02 (0 [1-9] |1[0-9]|2[0-8]))[0-9]{3}[0-9Xx]$/ // A regular expression for the validity of a regular year birth date
            }
            if (ereg.test(idcard)) {
              S = (parseInt(arr[0]) + parseInt(arr[10)) *7 + (parseInt(arr[1]) + parseInt(arr[11)) *9 + (parseInt(arr[2]) + parseInt(arr[12)) *10 + (parseInt(arr[3]) + parseInt(arr[13)) *5 + (parseInt(arr[4]) + parseInt(arr[14)) *8 + (parseInt(arr[5]) + parseInt(arr[15)) *4 + (parseInt(arr[6]) + parseInt(arr[16)) *2 + parseInt(arr[7]) * 1 + parseInt(arr[8]) * 6 + parseInt(arr[9]) * 3
              Y = S % 11
              M = 'F'
              JYM = '10X98765432'
              M = JYM.substr(Y, 1)
              if (M === arr[17]) {
                Err = Errors[0]}else {
                Err = Errors[3]}}else {
              Err = Errors[2]}return Err
          default:
            Err = Errors[1]
            return Err
        }
      }
Copy the code

The effect input box is an expression

isEmojiCharacter(substring) {
        for (var i = 0; i < substring.length; i++) {
          const hs = substring.charCodeAt(i)
          if (hs >= 0xd800 && hs <= 0xdbff) {
            if (substring.length > 1) {
              const ls = substring.charCodeAt(i + 1)
              var uc = ((hs - 0xd800) * 0x400) + (ls - 0xdc00) + 0x10000
              if (uc >= 0x1d000 && uc <= 0x1f77f) {
                return true}}}else if (substring.length > 1) {
            const ls = substring.charCodeAt(i + 1)
            if (ls === 0x20e3) {
              return true}}else {
            if (hs >= 0x2100 && hs <= 0x27ff) {
              return true
            } else if (hs >= 0x2B05 && hs <= 0x2b07) {
              return true
            } else if (hs >= 0x2934 && hs <= 0x2935) {
              return true
            } else if (hs >= 0x3297 && hs <= 0x3299) {
              return true
            } else if (hs === 0xa9 || hs === 0xae || hs === 0x303d || hs === 0x3030 ||
              hs === 0x2b55 || hs === 0x2b1c || hs === 0x2b1b || hs === 0x2b50) {
              return true}}}}Copy the code

Date formatting

dateFormat(date, fmt) {
        var o = {
          'M+': date.getMonth() + 1.'d+': date.getDate(),
          'h+': date.getHours(),
          'm+': date.getMinutes(),
          's+': date.getSeconds(),
          'q+': Math.floor((date.getMonth() + 3) / 3),
          S: date.getMilliseconds()
        }
        if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (date.getFullYear() + ' ').substr(4 - RegExp.$1.length))
        for (var k in o) {
          if (new RegExp('(' + k + ') ').test(fmt)) fmt = fmt.replace(RegExp. $1,RegExp.$1.length === 1)? (o[k]) : (('00' + o[k]).substr((' ' + o[k]).length)))
        }
        return fmt
      }
Copy the code

Time formatting

 TimeFormat(val, s) {
        if(! val) {return ' '
        }
        if (s) {
          val = val * 1000
        }
        var date = new Date(val)
        var year = date.getFullYear()
        var month = date.getMonth() + 1
        var day = date.getDate()
        var hour = date.getHours()
        var min = date.getMinutes()
        var second = date.getSeconds()
        month = month < 10 ? '0' + month : month
        day = day < 10 ? '0' + day : day
        hour = hour < 10 ? '0' + hour : hour
        min = min < 10 ? '0' + min : min
        second = second < 10 ? '0' + second : second
        return year + The '-' + month + The '-' + day + ' ' + hour + ':' + min + ':' + second
      }
Copy the code

The compressed image

dealImage(path, obj, callback) {
        var img = new Image()
        img.src = path
        img.onload = function () {
          var that = this
          // Compress proportionally by default
          var w = that.width
          var h = that.height
          var scale = w / h
          w = obj.width || w
          h = obj.height || w / scale
          var quality = 0.5 // The default image quality is 0.7
          / / generated canvas
          var canvas = document.createElement('canvas')
          var ctx = canvas.getContext('2d')
          // Create the properties node
          var anw = document.createAttribute('width')
          anw.nodeValue = w
          var anh = document.createAttribute('height')
          anh.nodeValue = h
          canvas.setAttributeNode(anw)
          canvas.setAttributeNode(anh)
          ctx.drawImage(that, 0.0, w, h)
          // Image quality
          if (obj.quality && obj.quality <= 1 && obj.quality > 0) {
            quality = obj.quality
          }
          // The smaller the quality value, the more blurred the image is drawn
          var base64 = canvas.toDataURL('image/jpeg', quality)
          // The callback function returns the base64 value
          callback(base64)
        }
      }
Copy the code

Date year Month day

getDate(n) {
        var date = new Date()
        date.setDate(date.getDate() + n)
        var year = date.getFullYear()
        var month = date.getMonth() + 1
        var day = date.getDate()
        month = month < 10 ? '0' + month : month
        day = day < 10 ? '0' + day : day
        return year + The '-' + month + The '-' + day
      }
Copy the code

The image is converted to base64

getImgData(img, dir, next) {
        var image = new Image()
        image.onload = function () {
          var degree = 0
          var drawWidth
          var drawHeight
          var width
          var height
          drawWidth = this.naturalWidth
          drawHeight = this.naturalHeight
          // Change the size of the image
          var canvas = document.createElement('canvas')
          canvas.width = width = drawWidth
          canvas.height = height = drawHeight
          var context = canvas.getContext('2d')
          // Determine the image orientation, reset the canvas size, and determine the rotation Angle. The default mode of the iPhone is landscape shooting with the home button on the right
          switch (dir) {
            // iPhone shot in landscape with the home button on the left
            case 3:
              degree = 180
              drawWidth = -width
              drawHeight = -height
              break
              // iPhone portrait shot with the home button below (normal holding direction)
            case 6:
              canvas.width = height
              canvas.height = width
              degree = 90
              drawWidth = width
              drawHeight = -height
              break
              // iPhone portrait shot with the home button at the top
            case 8:
              canvas.width = height
              canvas.height = width
              degree = 270
              drawWidth = -width
              drawHeight = height
              break
          }
          // Use canvas rotation correction
          context.rotate((degree * Math.PI) / 180)
          context.drawImage(this.0.0, drawWidth, drawHeight)
          // return the correction image
          next(canvas.toDataURL('image/jpeg'.0.8))
        }
        image.src = img
      }
Copy the code