facial-recognition-verify.uvue 2.8 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">
A
Anne_LXM 已提交
9
          <input class="uni-input" type="text" v-model="realName" name="real-name"
10
            placeholder="姓名" />
雪洛's avatar
雪洛 已提交
11 12
        </view>
        <view class="uni-btn-v uni-common-mt">
A
Anne_LXM 已提交
13
          <input class="uni-input" type="text" v-model="idCard" name="id-card"
14
            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
        let metaInfo : string
        try {
          metaInfo = uni.getFacialRecognitionMetaInfo();
        } catch (e) {
54
          if ((e as Error).message!.indexOf('Failed resolution of') > -1) {
55 56 57 58 59 60 61 62
            uni.showModal({
              title: '提示',
              content: '注意:目前uni-app x内使用实人认证功能需要打包自定义基座。',
              showCancel: false
            })
          }
          throw e
        }
雪洛's avatar
雪洛 已提交
63 64 65 66 67
        testFacialCo.getCertifyId({
          realName,
          idCard,
          metaInfo
        })
68
          .then((res : UTSJSONObject) : Promise<string> => {
雪洛's avatar
雪洛 已提交
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
            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))
                }
              })
            })
          })
85
          .then((certifyId : string) : Promise<UTSJSONObject> => {
雪洛's avatar
雪洛 已提交
86 87
            return testFacialCo.getAuthResult(certifyId)
          })
88
          .then((res : UTSJSONObject) => {
雪洛's avatar
雪洛 已提交
89 90
            console.log('res', res)
          })
91
          .catch((err : any | null) => {
雪洛's avatar
雪洛 已提交
92 93 94 95 96 97 98 99 100
            console.error('error', err)
          })
      }
    }
  }
</script>

<style>
</style>