小程序实现人脸识别功能

调用api   wx.startFacialRecognitionVerify

第一步:

      // 修改方法
      expertUpdate() {

       let _this = this
       wx.startFacialRecognitionVerify({
          name: _this.registerForm.realName, //身份证名称
          idCardNumber: _this.registerForm.idCard, //身份证号码
          checkAliveType: 1, //屏幕闪烁(人脸核验的交互方式,默认0,读数字)
          success(res) {
            
         console.log(res)  //认证结果
         if(res.errCode == 0){
          //识别成功  这个时候可以调后端的接口 (带着返的res.verifyResult)
          _this.verifyUser(res.verifyResult)
        }else{
        	tipInfo("识别失败")
        }
          },
          complete(res) {
          console.log(res)
          },
          fail(e) {
            console.log("err", err)//失败处理方法
           wx.showToast('请保持光线充足,面部正对手机,且无遮挡')
          }
   
        })
}


第二步:调方法 校验后端的接口

 // 人脸识别  根据上一个方法把verify_result传过来  //后端的校验接口


      verifyUser(verify_result) {
        //看后端实际要求 要传身份证号码不
        let obj = {
          uuid: this.infoForm.idCard,
          verify_result: verify_result
        }

      //后端接口=>verifyUser
        verifyUser(obj).then(res => {
          if (res.code == '0') {
            this.$refs.uForm.validate().then(res => {
              let obj = {
                pkid: uni.getStorageSync('expert_info').pkid,
                memberId: uni.getStorageSync('expert_info').memberId,
                avatar: this.fileList1[0].url,
                realName: this.infoForm.realName,
                orgName: this.infoForm.orgName,
                idCard: this.infoForm.idCard,
                province: this.infoForm.province,
                city: this.infoForm.city,
                district: this.infoForm.district,
                phone: this.infoForm.phone,
                professorLevel: this.infoForm.professorLevel,
                adept: this.infoForm.adept,
                intro: this.infoForm.intro,
                smsCode: this.infoForm.smsCode,
                annex: this.fileList2,
              }
             //修改方法
              expertUpdate(obj).then(res => {
                console.log(res, '修改成功了吗');
                if (res.code == '0') {
                  uni.$u.toast('修改成功', 5000)
                  uni.navigateBack()
                  //修改成功后 是返回上一步  还是跳转其他页面 根据实际情况
                } else {
                  uni.$u.toast(res.msg, 5000)
                }
              })
              console.log(res);
      
            }).catch(error => {
              console.log(error);
              uni.$u.toast('请先按要求填写', 5000)
            })
          }
        })
      
      },

注释:完整方法  这个是实现小程序个人信息完善,加了一个判断,如果 输入框没有值则需要走人脸识别验证方法  如果有值 只是修改其他项 就不需要验证  修改完成之后 名字和身份证号码直接禁用

 // 修改方法
      expertUpdate() {
        if (this.fileList1.length == 0) {
          uni.$u.toast('请上传头像')
          return false
        }
        // 判断 realName 是否为空
        if (!this.infoForm.realName) {
          uni.$u.toast('请填写姓名');
          return false;
        }
        // 验证 realName 是否为中文
        const chineseRegex = /^[\u4e00-\u9fa5]+$/;
        if (!chineseRegex.test(this.infoForm.realName)) {
          uni.$u.toast('姓名必须为中文');
          return false;
        }
        // 判断 idCard 是否为空
        if (!this.infoForm.idCard) {
          uni.$u.toast('请填写身份证号码');
          return false;
        }
        // 验证 idCard 是否符合身份证标准
        const idCardRegex = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
        if (!idCardRegex.test(this.infoForm.idCard)) {
          uni.$u.toast('身份证号格式不正确');
          return false;
        }
        if (this.fileList2.length == 0) {
          uni.$u.toast('请上传附件')
          return false
        }
        if (this.infoForm.intro.length > 200) {
          uni.$u.toast('简介字数不能超过200字!')
          return false
        }
        if (!this.flag) {
          let _this = this
          wx.startFacialRecognitionVerify({
            name: _this.infoForm.realName,
            idCardNumber: _this.infoForm.idCard,
            checkAliveType: 1,
            success(res) {
              console.log(res)
              _this.verifyUser(res.verifyResult)
              // console.log(res)
              // uni.navigateBack()
            },
            complete(res) {
              console.log(res)
            },
            fail(e) {
              // console.log(res)
              // console.log(_this.infoForm.realName)
              // console.log(_this.infoForm.idCard)
              console.log(e, 'fail')
            }
          })
        } else {
          this.$refs.uForm.validate().then(res => {
            let obj = {
              pkid: uni.getStorageSync('expert_info').pkid,
              memberId: uni.getStorageSync('expert_info').memberId,
              avatar: this.fileList1[0].url,
              realName: this.infoForm.realName,
              orgName: this.infoForm.orgName,
              idCard: this.infoForm.idCard,
              province: this.infoForm.province,
              city: this.infoForm.city,
              district: this.infoForm.district,
              phone: this.infoForm.phone,
              professorLevel: this.infoForm.professorLevel,
              adept: this.infoForm.adept,
              intro: this.infoForm.intro,
              smsCode: this.infoForm.smsCode,
              annex: this.fileList2,
            }
            expertUpdate(obj).then(res => {
              console.log(res, '修改成功了吗');
              if (res.code == '0') {
                uni.$u.toast('修改成功', 5000)
                uni.navigateBack()
                // this.getExpertInfo()
              } else {
                uni.$u.toast(res.msg, 5000)
              }
            })
            console.log(res);

          }).catch(error => {
            console.log(error);
            uni.$u.toast('请先按要求填写', 5000)
          })
        }
      },
      
      // 人脸识别
      verifyUser(verify_result) {
        let obj = {
          uuid: this.infoForm.idCard,
          verify_result: verify_result
        }
        verifyUser(obj).then(res => {
          if (res.code == '0') {
            this.$refs.uForm.validate().then(res => {
              let obj = {
                pkid: uni.getStorageSync('expert_info').pkid,
                memberId: uni.getStorageSync('expert_info').memberId,
                avatar: this.fileList1[0].url,
                realName: this.infoForm.realName,
                orgName: this.infoForm.orgName,
                idCard: this.infoForm.idCard,
                province: this.infoForm.province,
                city: this.infoForm.city,
                district: this.infoForm.district,
                phone: this.infoForm.phone,
                professorLevel: this.infoForm.professorLevel,
                adept: this.infoForm.adept,
                intro: this.infoForm.intro,
                smsCode: this.infoForm.smsCode,
                annex: this.fileList2,
              }
              expertUpdate(obj).then(res => {
                console.log(res, '修改成功了吗');
                if (res.code == '0') {
                  uni.$u.toast('修改成功', 5000)
                  uni.navigateBack()
                  // this.getExpertInfo()
                } else {
                  uni.$u.toast(res.msg, 5000)
                }
              })
              console.log(res);
      
            }).catch(error => {
              console.log(error);
              uni.$u.toast('请先按要求填写', 5000)
            })
          }
        })
      
      },
      

猜你喜欢

转载自blog.csdn.net/shi15926lei/article/details/133719995
今日推荐