realnameAuth.uvue 11.2 KB
Newer Older
crlfe's avatar
crlfe 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 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 50 51 52 53 54 55 56 57 58 59 60 61 62 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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
<template>
    <view>
        <template v-if="isCertify">
            <view class="list">
                <view class="list__item">
                    <view class="list__title">姓名</view>
                    <view class="list__content">
                        <text class="value">{{realnameInfo['realName']}}</text>
                    </view>
                </view>
                <view class="list__item">
                    <view class="list__title">身份证号码</view>
                    <view class="list__content">
                        <text class="value">{{realnameInfo['identity']}}</text>
                    </view>
                </view>
            </view>
        </template>
        <template v-else>
            <template v-if="verifyFail">
                <view class="fail-tip-content">
                    <view class="face-icon">
                        <image src="@/uni_modules/uni-id-pages-x/static/face-verify-icon.png" class="face-icon-image"/>
                    </view>
                    <view class="error-title">
                        <text class="text">{{verifyFailTitle}}</text>
                    </view>
                    <view class="error-description">
                        <text class="text">{{verifyFailContent}}</text>
                    </view>
                    <button type="primary" @click="retry" v-if="verifyFailCode != 10013">重新开始验证</button>
                    <button type="primary" @click="retry" v-else>返回</button>
                    <view class="dev-tip" v-if="isDev">请在控制台查看详细错误(此提示仅在开发环境展示)</view>
                </view>
            </template>
            <template v-else>
                <uni-id-pages-x-input class="input" v-model=" realName as string" placeholder="姓名" :focus="true"
                                      :maxlength="25"></uni-id-pages-x-input>
                <uni-id-pages-x-input class="input" v-model="idCard as string" placeholder="身份证号码"
                                      :maxlength="25"></uni-id-pages-x-input>
                <uni-id-pages-x-agreements scope="realNameVerify" ref="agreements"
                                           style="margin: 0 10px 10px;"></uni-id-pages-x-agreements>
                <button type="primary" :disabled="!certifyIdNext" @click="getCertifyId" style="margin: 0 10px;">确定
                </button>
            </template>
        </template>
    </view>
</template>

<script lang="uts">
    import {state, mutations} from '@/uni_modules/uni-id-pages-x/store.uts';
    import checkIdCard from "@/uni_modules/uni-id-pages-x/lib/check-id-card.uts";

    const uniIdCo = uniCloud.importObject('uni-id-co')
    const tempFrvInfoKey = 'uni-id-pages-temp-frv'

    export default {
        data() {
            return {
                realName: '',
                idCard: '',
                certifyId: '',
                verifyFail: false,
                verifyFailCode: 0,
                verifyFailTitle: '',
                verifyFailContent: ''
            };
        },
        computed: {
            userInfo(): UTSJSONObject {
                return state.userInfo
            },
            realnameInfo(): UTSJSONObject {
                return this.userInfo.getJSON('realNameInfo') ?? {}
            },
            certifyIdNext(): boolean {
                return this.realName !== '' && this.idCard !== '' && !state.pendingAgreements
            },
            isCertify(): boolean {
                return this.realnameInfo.getNumber('authStatus') == 2
            },
            isDev(): boolean {
                return process.env.NODE_ENV == 'development'
            }
        },
        methods: {
            async getCertifyId(): Promise<void> {
                if (!this.certifyIdNext) return

                if (!checkIdCard(this.idCard)) {
                    uni.showToast({
                        title: "身份证不合法",
                        icon: "none"
                    })
                    return
                }

                const reg = new RegExp('^[\u4e00-\u9fa5]{1,10}(·?[\u4e00-\u9fa5]{1,10}){0,5}', 'g')

                if (
                    typeof this.realName !== 'string' ||
                    this.realName.length < 2 ||
                    !reg.test(this.realName)
                ) {
                    uni.showToast({
                        title: "姓名只能是汉字",
                        icon: "none"
                    })
                    return
                }

                uni.setStorage({
                    key: tempFrvInfoKey,
                    data: {
                        realName: this.realName,
                        idCard: this.idCard
                    }
                });

                const metaInfo = uni.getFacialRecognitionMetaInfo()

                const res: UTSJSONObject = await uniIdCo.getFrvCertifyId({
                    realName: this.realName,
                    idCard: this.idCard,
                    metaInfo
                })

                if (res.getNumber('errCode') == 0) {
                    return uni.showToast({
                        title: res.getString('errMsg') as string,
                        icon: "none"
                    })
                }

                this.certifyId = res.getString('certifyId') as string

                this.startFacialRecognitionVerify()
            },
            startFacialRecognitionVerify() {
                uni.startFacialRecognitionVerify({
                    certifyId: this.certifyId,
                    progressBarColor: "#2979ff",
                    success: () => {
                        this.verifyFail = false
                        this.getFrvAuthResult()
                    },
                    fail: (e: IFacialRecognitionVerifyError) => {
                        let title = "验证失败"
DCloud_JSON's avatar
DCloud_JSON 已提交
149
                        let content: string;
crlfe's avatar
crlfe 已提交
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224

                        console.log(
                            `[frv-debug] certifyId auth error: certifyId -> ${this.certifyId}, error -> ${JSON.stringify(e)}`
                        )

                        switch (e.errCode) {
                            case 10001:
                                content = '认证ID为空'
                                break
                            case 10010:
                                title = '刷脸异常'
                                content = e.cause?.message ?? '错误代码: 10010'
                                break
                            case 10011:
                                title = '验证中断'
                                content = e.cause?.message ?? '错误代码: 10011'
                                break
                            case 10012:
                                content = '网络异常'
                                break
                            case 10020:
                                content = '设备设置时间异常'
                                break
                            case 10013:
                                this.verifyFailCode = e.errCode
                                this.verifyFailContent = e.cause?.message ?? '错误代码: 10013'
                                this.getFrvAuthResult()

                                console.log(
                                    `[frv-debug] 刷脸失败, certifyId -> ${this.certifyId}, 如在开发环境请检查用户的姓名、身份证号与刷脸用户是否为同一用户。如遇到认证ID已使用请检查opendb-frv-logs表中certifyId状态`
                                )
                                return
                            default:
                                title = ''
                                content = `验证未知错误 (${e.errCode})`
                                break
                        }
                        this.verifyFail = true
                        this.verifyFailCode = e.errCode
                        this.verifyFailTitle = title
                        this.verifyFailContent = content
                    }
                })
            },
            async getFrvAuthResult(): Promise<void> {
                const uniIdCo = uniCloud.importObject('uni-id-co', {
                    customUI: true
                })
                try {
                    uni.showLoading({
                        title: "验证中...",
                        mask: false
                    })
                    const res: UTSJSONObject = await uniIdCo.getFrvAuthResult({
                        certifyId: this.certifyId
                    })

                    if (this.verifyFailContent != '') {
                        console.log(`[frv-debug] 客户端刷脸失败,由实人认证服务查询具体原因,原因:${this.verifyFailContent}`)
                    }

                    uni.showModal({
                        content: "实名认证成功",
                        showCancel: false,
                        success: () => {
                            mutations.updateUserInfo({
                                realNameInfo: res
                            })
                            this.verifyFail = false
                        }
                    })

                    uni.removeStorage({
                        key: tempFrvInfoKey
                    })
A
Anne_LXM 已提交
225
                } catch (e) {
A
Anne_LXM 已提交
226
                    const error = e as UniCloudError
crlfe's avatar
crlfe 已提交
227
                    this.verifyFail = true
A
Anne_LXM 已提交
228
                    this.verifyFailTitle = error.errMsg
crlfe's avatar
crlfe 已提交
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
                    console.error(JSON.stringify(e))
                }
                uni.hideLoading()
            },
            retry() {
                if (this.verifyFailCode != 10013) {
                    this.getCertifyId()
                } else {
                    this.verifyFail = false
                }
            },
        }
    }
</script>

<style lang="scss">
  @import url("/uni_modules/uni-id-pages-x/common/common.scss");

  .list {
    position: relative;
    background-color: #fff;
    margin-top: 15px;

    &__item {
      border-bottom: 1px solid #f5f5f5;
      flex-direction: row;
      align-items: center;
      padding: 15px 15px;
    }

    &__title {
      flex: 1;
    }

    &__text {
      font-size: 14px;
      color: #333;
    }

    &__content {
      flex-direction: row;
      justify-content: center;
      align-items: center;

      .value {
        color: #999;
        font-size: 12px;
      }

      .unset {
        color: #aaa;
        font-size: 12px;
      }
    }
  }

  .input {
    background-color: #FFF;
  }

  .uni-label-pointer {
    align-items: center;
    display: flex;
    flex-direction: row;
  }

  .item {
    flex-direction: row;
  }

  .text {
    line-height: 26px;
  }

  .agreements {
    margin-bottom: 20px;
  }

  .fail-tip-content {
    padding: 20px;
  }

  .face-icon {
    width: 100px;
    height: 100px;
    margin: 50px auto 30px;
  }

  .face-icon-image {
    width: 100%;
    height: 100%;
  }

  .error-title {
    .text {
      font-size: 18px;
      text-align: center;
      font-weight: bold;
    }
  }

  .error-description {
    margin: 10px 0 20px;

    .text {
      font-size: 13px;
      color: #999999;
      text-align: center;
    }
  }

  .dev-tip {
    margin-top: 20px;
    font-size: 13px;
    color: #999;
    text-align: center;
  }
</style>