index.vue 10 KB
<template>
  <div class="device-index">
    <div class="sprite-icon icon-device-bg">
      绑定设备SN码和车辆VIN码<br />
      关联硬件设备和车辆信息
    </div>
    <div class="box">
      <div class="step">
        第一步,扫描设备SN码
      </div>
      <div class="sn input-item">
        SN码
        <div class="sn-input input">
          <input
            type="number"
            placeholder="点击手动输入SN码"
            v-model="sn"
            maxlength="11"
            :class="{'inputErr':snErr}"
            @blur="checkInstallation('sn')"
            :pattern="/^[0-9]*[1-9][0-9]*$/"
          >
          <div
            class="err"
            v-show="snErr && sn"
          >{{snText}}</div>
        </div>
        <div
          class="sprite-icon icon-device-scan"
          @click="scanQRCode"
        ></div>
      </div>
    </div>
    <div class="box">
      <div class="step">
        第二步,拍照上传车辆VIN码
      </div>
      <div
        class="vin-box"
        @click="chooseImage"
      >
        <template v-if="vinImg">
          <img :src="vinImg">
        </template>
        <template v-else>
          <div class="add">
            <div class="sprite-icon icon-device-add"></div>
            上传安装设备的车辆VIN码
            <div class="tips">
              注:<div>VIN码是车辆车架号,通常位于<br>汽车前挡风玻璃左/右下角</div>
            </div>
          </div>
        </template>
      </div>
      <div class="vin input-item">
        VIN码
        <div class="vin-input input">
          <input
            type="text"
            placeholder="点击手动输入车辆VIN码"
            v-model="vin"
            maxlength="17"
            :class="{'inputErr':vinErr}"
            @blur="checkInstallation('vin')"
            @input="vinInput(vin)"
          >
          <div
            class="err"
            v-show="vinErr && vin"
          >{{vinText}}</div>
        </div>
      </div>
    </div>
    <div
      :class="['dev-btn',{'btn-disabled':!sn||!vin || snErr || vinErr}]"
      @click="submit"
      @touchstart="touchstart"
      @touchend="touchend"
    >提交</div>
    <device-tabbar
      routeName="device.list"
      :tabIndex="0"
    ></device-tabbar>
  </div>
</template>

<script type='text/ecmascript-6'>
import DeviceTabBar from '@c/DeviceTabBar'
import WxConfig from '@u/WxConfig'
const WX = new WxConfig(['scanQRCode', 'chooseImage', 'getLocation'])
import { common, device } from '@r/api'
import AMap from 'AMap'
const vin_text = '该设备码已被关联,请重新输入或扫描'
const sn_text = '该设备码已被关联,请重新输入或扫描'
export default {
  name: 'index',
  data () {
    return {
      addressInfo: {},
      sn: '',
      vin: '',
      vinImg: '',
      snErr: false,
      vinErr: false,
      vinText: '',
      snText: ''
    }
  },
  components: {
    'device-tabbar': DeviceTabBar
  },
  mounted () {
    this.getLocation()
  },
  watch: {
    'sn' (n) {
      n = n.replace('.', '')
      this.sn = n.substring(0, 11)
      this.snErr = this.sn.length < 11
    },
  },
  methods: {
    vinInput (vin) {
      this.vin = vin.substring(0, 17)
      let rex = /^[A-HJ-NPR-Z\d]{17}$/
      this.vinErr = !rex.test(this.vin)
    },
    scanQRCode () {
      WX.scanQRCode(1).then(res => {
        let { resultStr } = res
        this.sn = resultStr.split(',')[1]
        this.checkInstallation('sn')
      })
    },
    getLocation () {
      WX.getLocation().then(res => {
        AMap.plugin('AMap.Geocoder', () => {
          const geocoder = new AMap.Geocoder()
          let { longitude, latitude } = res
          let lnglat = [longitude, latitude]
          geocoder.getAddress(lnglat, (status, result) => {
            let { info, regeocode: { formattedAddress } } = result
            if (status === 'complete' && info === 'OK') {
              // result为对应的地理位置详细信息
              this.addressInfo = {
                longitude,
                latitude,
                address: formattedAddress
              }
            }
          })
        })
      })
    },
    checkInstallation (name) {
      let value = this[name]
      if (!value) return
      let text = {
        sn: sn_text,
        vin: vin_text
      }
      device.checkInstallation({
        [name]: value,
      }).then(res => {
        if (res) {
          let { code, message } = res
          if (code == 0) {
            this[`${name}Err`] = false
          } else {
            this[`${name}Err`] = true
            this[`${name}Text`] = text[name]
            this.showToast(message)
          }
        } else {
          this.showToast('系统异常')
        }
        console.log(res)
      })
    },
    chooseImage () {
      WX.chooseImage().then(res => {
        let [data] = res
        let { localData } = data
        let suffix = ''
        let imgUrl = ''
        if (localData.indexOf(';') != -1) {
          suffix = localData.split(';')[0]
          imgUrl = localData.split('base64,')[1]
        }
        let type = suffix ? suffix.split('/')[1] : 'jpg'
        if (!imgUrl) imgUrl = localData
        if (type == 'unknown') type = 'jpg'
        if (type.toLowerCase() != 'jpg' && type.toLowerCase() != 'jpeg' && type.toLowerCase() != 'png') {
          this.showToast('图片格式错误')
          return
        }
        this.showLoading('上传中...')
        common.uploadImage({
          file: `${type}@${imgUrl}`
        }).then(img => {
          console.log(img)
          if (img) {
            let { data } = img
            this.showLoading('识别中...')
            device.scanVin({ imageUrl: data }).then(vinRes => {
              if (vinRes) {
                console.log(vinRes)
                let { code } = vinRes
                if (code == 0) {
                  this.vinImg = img.data
                  this.vin = vinRes.data.vin
                  this.checkInstallation('vin')
                } else {
                  this.showToast('识别失败请重试', 'close')
                  this.vinImg = ''
                  this.showInput = false
                }
              } else {
                this.showToast('识别失败请重试', 'close')
                this.vinImg = ''
                this.showInput = false
              }
            })
          } else {
            this.showToast('上传失败')
            this.vinImg = ''
            this.showInput = false
          }
        })
      })
    },

    submit () {
      let { sn, vin, snErr, vinErr, addressInfo } = this
      if (!sn || !vin || snErr || vinErr) return
      console.log('提交')
      let { openid } = this.$store.state
      this.showLoading('绑定中...')
      let params = {
        sn,
        vin,
        openid,
        ...addressInfo
      }
      device.installation(params).then(res => {
        if (res) {
          let { code, message, data } = res
          if (code == 0) {
            this.dialog({
              title: '提示',
              message: '提交成功',
              confirmButtonText: '返回',
              showCancelButton: false
            }).then(() => {
              this.showToast(data)
              Object.assign(this.$data, this.$options.data())
              this.getLocation()
            })
          } else {
            this.showToast(message)
          }
        } else {
          this.showToast('系统异常')
        }
      })
    }
  }
}
</script>

<style scoped lang='scss'>
.device-index {
  padding-bottom: 44px;
  .icon-device-bg {
    font-size: 16px;
    color: #ffffff;
    display: flex;
    align-items: center;
    justify-content: center;
    line-height: 24px;
  }
  .box {
    background: #ffffff;
    box-shadow: 0 2px 4px 0 rgba(0, 0, 0, 0.1);
    margin-bottom: 10px;
    padding: 20px 16px;
    .step {
      padding-left: 20px;
      font-size: 14px;
      color: #333333;
      position: relative;
      margin-bottom: 20px;
      &::before,
      &::after {
        content: "";
        position: absolute;
        top: 50%;
        transform: translateY(-50%);
        border-radius: 100%;
      }
      &::before {
        border: 1px solid #a7d1ff;
        width: 10px;
        height: 10px;
        left: 0;
      }
      &::after {
        background: #4a9dff;
        width: 4px;
        height: 4px;
        left: 4px;
      }
    }
    .input-item {
      padding: 0 21px 0 22px;
      font-size: 16px;
      color: #4a9dff;
      display: grid;
      align-items: center;
      .input {
        border-bottom: 1px solid #666666;
        position: relative;
        padding: 2px 6px;
        input {
          border: none;
          font-size: 12px;
          color: #666666;
          height: 17px;
          width: 100%;
          font-size: 14px;
        }
        .inputErr {
          color: #fa5151;
        }
        .err {
          font-size: 10px;
          color: #fa5151;
          position: absolute;
          left: 0;
          bottom: -15px;
        }
      }
    }
    .sn {
      grid-template-columns: 39px 1fr 20px;
      &-input {
        max-width: 220px;
        margin: 0 20px 0 1px;
      }
    }
    .vin {
      grid-template-columns: 43px 1fr;
    }
    .vin-box {
      width: 300px;
      height: 180px;
      margin: 0 auto 20px;
      box-sizing: border-box;
      border: 1px dotted #979797;
      display: flex;
      align-items: center;
      justify-content: center;
      .add {
        font-size: 16px;
        color: #999999;
        text-align: center;
        .icon-device-add {
          margin: 0 auto 10px;
        }
        .tips {
          margin-top: 12px;
          color: #c6c6c6;
          font-size: 10px;
          display: flex;
          align-items: flex-start;
          justify-content: center;
          div {
            text-align: left;
          }
        }
      }
    }
  }
  .dev-btn {
    width: 198px;
    height: 32px;
    display: grid;
    place-items: center;
    border-radius: 6px;
    font-size: 14px;
    color: #ffffff;
    background: #4fa3ff;
    margin: 20px auto;
  }
  .btn-disabled {
    background: #d2d2d2;
  }
}
</style>