小程序实现人脸识别功能

news/2024/7/20 2:48:48 标签: 小程序, javascript, 前端

调用api   wx.startFacialRecognitionVerify

第一步:

javascript">// 修改方法
      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('请保持光线充足,面部正对手机,且无遮挡')
          }
   
        })


}


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

javascript"> // 人脸识别  根据上一个方法把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)
            })
          }
        })
      
      },

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

javascript"> // 修改方法
      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)
            })
          }
        })
      
      },
      


http://www.niftyadmin.cn/n/5076571.html

相关文章

UniApp项目实践HelloUni继续快速小步快跑中,前面是大上海吗

效果镇楼 本来想着稍微刷点课程就完活的,结果还是到这个点了。宝贵的时间啊 直接代码干就是了,参考如下链接; UniApp实践业务项目

Linux实验一(常见命令与场景) 基本命令权限管理

实验一 Linux基本命令: 权限管理 一、实验目的: 通过实验熟悉Linux操作系统环境,掌握基本的Linux命令的使用。 二、实验要求: (1)掌握Linux/bash环境下命令的使用 (2) 掌握Linux用户权限管理 三、实验内容 一: 观察系统的结构和文件属性 以超级用户身份登录系统…

使用Pyhton执行JavaScript-pyexecjs

安装 pip install pyexecjs使用案例 import execjs print(execjs.eval("abc zxc".split(" ")))# 调用变量名 text execjs.compile(open(rtext.js).read()) print(text.eval(d))执行call function # text.js 文件 var t 1; function add(a, b) {return a…

Cmd报错:No module named ‘pip’

目录 1、问题描述2、问题原因3、问题解决 1、问题描述 今天在cmd命令行安装Twisted的扩展包whl文件时报错: ...... ModuleNotFoundError: No module named pip2、问题原因 升级pip时命令使用错误 3、问题解决 1) 重装pip python -m ensurepip2&#x…

基于matlab统计Excel文件一列数据中每个数字出现的频次和频率

一、需求描述 如上表所示,在excel文件中,有一列数,统计出该列数中,每个数出现的次数和频率。最后,将统计结果输出到新的excel文件中。 二、程序讲解 第一步:选择excel文件; [Filename, Pathn…

Jakob Jenkov 个人博客 Jackson 部分(译文)

介绍 JSON 是 JavaScript Object Notation 的缩写,JSON 是一种非常流行的数据交换格式,它通常被用来在浏览器和 web 服务器之间传递数据。因为浏览器原生支持解析 JSON 为 JavaScript 对象。在服务器,JSON 需要使用 JSON API 来解析和生成&a…

Linux——多线程1

目录 一.理解线程的概念 Linux线程概念 二.线程的优点 三.线程的缺点 四.线程用途 五. Linux进程VS线程 一.理解线程的概念 教材观点: 线程是一种执行分支,执行粒度比进程更细,调度成本更低。线程是进程内部的一个执行流。 内核观点: …

将远程的文件删除 git

在 Windows 上&#xff0c;路径通常使用正斜杠 / 分隔而不是反斜杠 \&#xff0c;所以需要将反斜杠替换为正斜杠。 git rm --cached <文件路径>git commit -m "删除文件"git push origin <分支名> 也就是 git rm --cached src/views/Screen/MapPopupWi…