LicensePlate.js 3.16 KB
// components/LicensePlate/LicensePlate.js
const suffix = '港澳学挂',
  city = '京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼',
  platNumberRex = /^([京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼][A-Z][A-HJ-NP-Z0-9]{4,5}[A-HJ-NP-Z0-9挂学港澳])$/
let timer
Component({
  options: {
    styleIsolation: 'shared'
  },
  /**
   * 组件的属性列表
   */
  properties: {
    showNavHeight:Boolean,
    navheight:Number
  },

  /**
   * 组件的初始数据
   */
  data: {
    plateNoErr: true,
    plateIndex: 0,
    plateNo: new Array(8).fill(''),
    hidden: true
  },

  /**
   * 组件的方法列表
   */
  methods: {
    noScroll(){},
    changeIndex(e) {
      let {
        index
      } = e.currentTarget.dataset, {
        plateIndex
      } = this.data
      if (plateIndex == index) return
      plateIndex = index
      this.setData({
        plateIndex
      })
    },
    changePlateNo(e) {
      let {
        value
      } = e.currentTarget.dataset, {
        plateIndex
      } = this.data
      const index = plateIndex
      if (plateIndex < 7) ++plateIndex
      this.setData({
        [`plateNo[${index}]`]: value,
        plateIndex
      })
      this.check()
    },
    async paste() {
      let {
        data
      } = await wx.getClipboardData(),
        value = data.toUpperCase(),
        plateNo = [...value].filter(e => e && e != '' && /[\u4e00-\u9fa5\w]/.test(e))
      while (plateNo.length < 8) plateNo.push('')
      plateNo = plateNo.splice(0, 8)
      if (platNumberRex.test(plateNo.join(''))) {
        this.setData({
          plateNo
        })
        this.check()
      }
    },
    del() {
      let {
        plateIndex,
        plateNo
      } = this.data
      const index = plateIndex
      if(!plateNo[index] && index == 0) return
      if (plateIndex > 0) --plateIndex
      this.setData({
        [`plateNo[${index}]`]: '',
        plateIndex
      })
      this.check()
    },
    check() {
      let {
        plateNo
      } = this.data,
        plateNoErr = false
      /**车牌后缀 */
      if (plateNo[6] && plateNo[7] && suffix.includes(plateNo[6])) this.setData({
        [`plateNo[${7}]`]: ''
      })
      if (!platNumberRex.test(plateNo.join(''))) plateNoErr = true
      this.setData({
        plateNoErr,
        keyboardDisabled: suffix.includes(plateNo[6])
      })
      wx.vibrateShort()
    },
    toggle() {
      let {
        hidden
      } = this.data
      this.setData({
        hidden: !hidden
      })
      if (!hidden) {
        if (timer) clearTimeout(timer)
        this.setData({
          animation: true
        }, () => {
          timer = setTimeout(() => {
            this.setData({
              animation: false
            })
          }, 500)
        })
      }

    },
    done() {
      let {
        plateNo
      } = this.data,
        value = plateNo.join('')
      this.triggerEvent('done', {
        value
      })
      this.toggle()
    }
  },
  lifetimes: {
    ready() {
      this.setData({
        cityList: [...city],
        letter: [...'ABCDEFGHJKLMNPQRSTUVWXYZ'],
        suffix: [...suffix]
      })
    }
  }
})