facial-recognition-verify.uvue 2.9 KB
Newer Older
雪洛's avatar
雪洛 已提交
1 2 3 4 5 6 7 8
<template>
  <!-- #ifdef APP -->
  <scroll-view style="flex:1">
  <!-- #endif -->
    <view>
      <page-head :title="title"></page-head>
      <view class="uni-padding-wrap uni-common-mt">
        <view class="uni-btn-v uni-common-mt">
9 10
          <input class="uni-input uni-input-default" type="text" v-model="realName" name="real-name" id="real-name"
            placeholder="姓名" />
雪洛's avatar
雪洛 已提交
11 12
        </view>
        <view class="uni-btn-v uni-common-mt">
13 14
          <input class="uni-input uni-input-default" type="text" v-model="idCard" name="id-card" id="id-card"
            placeholder="身份证号" />
雪洛's avatar
雪洛 已提交
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
        </view>
        <view class="uni-btn-v uni-common-mt">
          <button type="primary" @click="facialRecognition">开始人脸识别</button>
        </view>
      </view>
    </view>
  <!-- #ifdef APP -->
  </scroll-view>
  <!-- #endif -->
</template>

<script>
  export default {
    data() {
      return {
        title: '实人认证',
        realName: '',
        idCard: ''
      }
    },
    onReady() {
    },
    methods: {
      facialRecognition() {
        const realName = this.realName.trim()
        const idCard = this.idCard.trim()
        if (realName == '' || idCard == '') {
          uni.showModal({
            title: '错误',
            content: '姓名和身份证号不可为空',
            showCancel: false
          })
          return
        }
        const testFacialCo = uniCloud.importObject('facial-recognition-co')
50 51 52 53 54 55 56 57 58 59 60 61 62
        let metaInfo : string
        try {
          metaInfo = uni.getFacialRecognitionMetaInfo();
        } catch (e) {
          if (e.message!.indexOf('Failed resolution of') > -1) {
            uni.showModal({
              title: '提示',
              content: '注意:目前uni-app x内使用实人认证功能需要打包自定义基座。',
              showCancel: false
            })
          }
          throw e
        }
雪洛's avatar
雪洛 已提交
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
        testFacialCo.getCertifyId({
          realName,
          idCard,
          metaInfo
        })
          .then<string>((res : UTSJSONObject) : Promise<string> => {
            const certifyId = res['certifyId'] as string
            return new Promise((
              resolve : (res : string) => void,
              reject : (err : Error) => void
            ) => {
              uni.startFacialRecognitionVerify({
                certifyId,
                success() {
                  resolve(certifyId)
                },
                fail(err) {
                  reject(new Error(err.errMsg))
                }
              })
            })
          })
          .then<UTSJSONObject>((certifyId : string) : Promise<UTSJSONObject> => {
            return testFacialCo.getAuthResult(certifyId)
          })
          .then<void>((res : UTSJSONObject) => {
            console.log('res', res)
          })
          .catch<void>((err : any | null) => {
            console.error('error', err)
          })
      }
    }
  }
</script>

<style>
</style>