当前位置:   article > 正文

小程序实现人脸识别功能_wx.startfacialrecognitionverify

wx.startfacialrecognitionverify

调用api   wx.startFacialRecognitionVerify

第一步:

  1. // 修改方法
  2. expertUpdate() {
  3. let _this = this
  4. wx.startFacialRecognitionVerify({
  5. name: _this.registerForm.realName, //身份证名称
  6. idCardNumber: _this.registerForm.idCard, //身份证号码
  7. checkAliveType: 1, //屏幕闪烁(人脸核验的交互方式,默认0,读数字)
  8. success(res) {
  9. console.log(res) //认证结果
  10. if(res.errCode == 0){
  11. //识别成功 这个时候可以调后端的接口 (带着返的res.verifyResult)
  12. _this.verifyUser(res.verifyResult)
  13. }else{
  14. tipInfo("识别失败")
  15. }
  16. },
  17. complete(res) {
  18. console.log(res)
  19. },
  20. fail(e) {
  21. console.log("err", err)//失败处理方法
  22. wx.showToast('请保持光线充足,面部正对手机,且无遮挡')
  23. }
  24. })
  25. }

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

  1. // 人脸识别 根据上一个方法把verify_result传过来 //后端的校验接口
  2. verifyUser(verify_result) {
  3. //看后端实际要求 要传身份证号码不
  4. let obj = {
  5. uuid: this.infoForm.idCard,
  6. verify_result: verify_result
  7. }
  8. //后端接口=>verifyUser
  9. verifyUser(obj).then(res => {
  10. if (res.code == '0') {
  11. this.$refs.uForm.validate().then(res => {
  12. let obj = {
  13. pkid: uni.getStorageSync('expert_info').pkid,
  14. memberId: uni.getStorageSync('expert_info').memberId,
  15. avatar: this.fileList1[0].url,
  16. realName: this.infoForm.realName,
  17. orgName: this.infoForm.orgName,
  18. idCard: this.infoForm.idCard,
  19. province: this.infoForm.province,
  20. city: this.infoForm.city,
  21. district: this.infoForm.district,
  22. phone: this.infoForm.phone,
  23. professorLevel: this.infoForm.professorLevel,
  24. adept: this.infoForm.adept,
  25. intro: this.infoForm.intro,
  26. smsCode: this.infoForm.smsCode,
  27. annex: this.fileList2,
  28. }
  29. //修改方法
  30. expertUpdate(obj).then(res => {
  31. console.log(res, '修改成功了吗');
  32. if (res.code == '0') {
  33. uni.$u.toast('修改成功', 5000)
  34. uni.navigateBack()
  35. //修改成功后 是返回上一步 还是跳转其他页面 根据实际情况
  36. } else {
  37. uni.$u.toast(res.msg, 5000)
  38. }
  39. })
  40. console.log(res);
  41. }).catch(error => {
  42. console.log(error);
  43. uni.$u.toast('请先按要求填写', 5000)
  44. })
  45. }
  46. })
  47. },

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

  1. // 修改方法
  2. expertUpdate() {
  3. if (this.fileList1.length == 0) {
  4. uni.$u.toast('请上传头像')
  5. return false
  6. }
  7. // 判断 realName 是否为空
  8. if (!this.infoForm.realName) {
  9. uni.$u.toast('请填写姓名');
  10. return false;
  11. }
  12. // 验证 realName 是否为中文
  13. const chineseRegex = /^[\u4e00-\u9fa5]+$/;
  14. if (!chineseRegex.test(this.infoForm.realName)) {
  15. uni.$u.toast('姓名必须为中文');
  16. return false;
  17. }
  18. // 判断 idCard 是否为空
  19. if (!this.infoForm.idCard) {
  20. uni.$u.toast('请填写身份证号码');
  21. return false;
  22. }
  23. // 验证 idCard 是否符合身份证标准
  24. const idCardRegex = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
  25. if (!idCardRegex.test(this.infoForm.idCard)) {
  26. uni.$u.toast('身份证号格式不正确');
  27. return false;
  28. }
  29. if (this.fileList2.length == 0) {
  30. uni.$u.toast('请上传附件')
  31. return false
  32. }
  33. if (this.infoForm.intro.length > 200) {
  34. uni.$u.toast('简介字数不能超过200字!')
  35. return false
  36. }
  37. if (!this.flag) {
  38. let _this = this
  39. wx.startFacialRecognitionVerify({
  40. name: _this.infoForm.realName,
  41. idCardNumber: _this.infoForm.idCard,
  42. checkAliveType: 1,
  43. success(res) {
  44. console.log(res)
  45. _this.verifyUser(res.verifyResult)
  46. // console.log(res)
  47. // uni.navigateBack()
  48. },
  49. complete(res) {
  50. console.log(res)
  51. },
  52. fail(e) {
  53. // console.log(res)
  54. // console.log(_this.infoForm.realName)
  55. // console.log(_this.infoForm.idCard)
  56. console.log(e, 'fail')
  57. }
  58. })
  59. } else {
  60. this.$refs.uForm.validate().then(res => {
  61. let obj = {
  62. pkid: uni.getStorageSync('expert_info').pkid,
  63. memberId: uni.getStorageSync('expert_info').memberId,
  64. avatar: this.fileList1[0].url,
  65. realName: this.infoForm.realName,
  66. orgName: this.infoForm.orgName,
  67. idCard: this.infoForm.idCard,
  68. province: this.infoForm.province,
  69. city: this.infoForm.city,
  70. district: this.infoForm.district,
  71. phone: this.infoForm.phone,
  72. professorLevel: this.infoForm.professorLevel,
  73. adept: this.infoForm.adept,
  74. intro: this.infoForm.intro,
  75. smsCode: this.infoForm.smsCode,
  76. annex: this.fileList2,
  77. }
  78. expertUpdate(obj).then(res => {
  79. console.log(res, '修改成功了吗');
  80. if (res.code == '0') {
  81. uni.$u.toast('修改成功', 5000)
  82. uni.navigateBack()
  83. // this.getExpertInfo()
  84. } else {
  85. uni.$u.toast(res.msg, 5000)
  86. }
  87. })
  88. console.log(res);
  89. }).catch(error => {
  90. console.log(error);
  91. uni.$u.toast('请先按要求填写', 5000)
  92. })
  93. }
  94. },
  95. // 人脸识别
  96. verifyUser(verify_result) {
  97. let obj = {
  98. uuid: this.infoForm.idCard,
  99. verify_result: verify_result
  100. }
  101. verifyUser(obj).then(res => {
  102. if (res.code == '0') {
  103. this.$refs.uForm.validate().then(res => {
  104. let obj = {
  105. pkid: uni.getStorageSync('expert_info').pkid,
  106. memberId: uni.getStorageSync('expert_info').memberId,
  107. avatar: this.fileList1[0].url,
  108. realName: this.infoForm.realName,
  109. orgName: this.infoForm.orgName,
  110. idCard: this.infoForm.idCard,
  111. province: this.infoForm.province,
  112. city: this.infoForm.city,
  113. district: this.infoForm.district,
  114. phone: this.infoForm.phone,
  115. professorLevel: this.infoForm.professorLevel,
  116. adept: this.infoForm.adept,
  117. intro: this.infoForm.intro,
  118. smsCode: this.infoForm.smsCode,
  119. annex: this.fileList2,
  120. }
  121. expertUpdate(obj).then(res => {
  122. console.log(res, '修改成功了吗');
  123. if (res.code == '0') {
  124. uni.$u.toast('修改成功', 5000)
  125. uni.navigateBack()
  126. // this.getExpertInfo()
  127. } else {
  128. uni.$u.toast(res.msg, 5000)
  129. }
  130. })
  131. console.log(res);
  132. }).catch(error => {
  133. console.log(error);
  134. uni.$u.toast('请先按要求填写', 5000)
  135. })
  136. }
  137. })
  138. },

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/374230
推荐阅读
相关标签
  

闽ICP备14008679号