小程序实现人脸识别的项目实践

 更新时间:2023年10月11日 10:08:21   作者:前端小灰狼  
人脸识别在现在很多地方都可以用的到,例如支付,解锁等,本文就来介绍一下小程序实现人脸识别,具有一定的参考价值,感兴趣的可以了解一下

调用api   wx.startFacialRecognitionVerify

第一步:

// 修改方法
      expertUpdate() {
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)
            })
          }
        })
      },
      

到此这篇关于小程序实现人脸识别的项目实践的文章就介绍到这了,更多相关小程序 人脸识别内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • 基于JS对象创建常用方式及原理分析

    基于JS对象创建常用方式及原理分析

    下面小编就为大家带来一篇基于JS对象创建常用方式及原理分析。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-06-06
  • js实现页面导航层级指示效果

    js实现页面导航层级指示效果

    这篇文章主要为大家详细介绍了js实现页面导航层级指示效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-08-08
  • 学习javascript的闭包,原型,和匿名函数之旅

    学习javascript的闭包,原型,和匿名函数之旅

    Javascript中有几个非常重要的语言特性——对象、原型继承、闭包。其中闭包 对于那些使用传统静态语言C/C++的程序员来说是一个新的语言特性,本文给大家介绍js的闭包,原型,和匿名函数之旅,感兴趣的朋友一起学习吧
    2015-10-10
  • js实现限定范围拖拽的示例

    js实现限定范围拖拽的示例

    这篇文章主要介绍了js实现限定范围拖拽的示例,帮助大家更好的制作js特效,美化自己的网页,感兴趣的朋友可以了解下
    2020-10-10
  • JavaScript对象的四种创建方法

    JavaScript对象的四种创建方法

    这篇文章主要介绍了JavaScript对象的四种创建方法,首先我们通过确定一个对象的属性和方法展开主题相关内容,需要的小伙伴可以参考一下
    2022-08-08
  • JavaScript实现浅拷贝与深拷贝的方法分析

    JavaScript实现浅拷贝与深拷贝的方法分析

    这篇文章主要介绍了JavaScript实现浅拷贝与深拷贝的方法,结合实例形式总结分析了JavaScript浅拷贝与深拷贝的定义与使用方法,需要的朋友可以参考下
    2018-07-07
  • 微信小程序中悬浮窗功能的实现代码

    微信小程序中悬浮窗功能的实现代码

    悬浮窗就是图中微信图标的按钮,采用fixed定位,可拖动和点击。本文给大家分享一个比较常见的常见,通过实例代码给大家介绍微信小程序中悬浮窗功能的实现,一起看看吧
    2019-08-08
  • 通过身份证号得到出生日期和性别的js代码

    通过身份证号得到出生日期和性别的js代码

    主要是通过判断指定位数的数字,来判断并加以算法实现男女性别的判断。
    2009-11-11
  • JS给按钮添加跳转功能类似a标签

    JS给按钮添加跳转功能类似a标签

    这篇文章主要介绍了JS给按钮添加跳转功能类似a标签,需要的朋友可以参考下
    2017-05-05
  • jsonp跨域获取百度联想词的方法分析

    jsonp跨域获取百度联想词的方法分析

    这篇文章主要介绍了jsonp跨域获取百度联想词的方法,结合实例形式分析了jsonp的原理及跨域获取百度联想词的相关操作技巧,需要的朋友可以参考下
    2019-05-05

最新评论