微信小程序人工智能之刷脸登录

首先是小程序用户端的页面



  

接下来就是当点击刷脸登录时,可以看到绑定了一个事件  takePhone  此时在此事件中调用已经写好的接口在  .js中实现登录

js代码

takePhoto() {
    const ctx = wx.createCameraContext()
    ctx.takePhoto({
      quality: 'high',
      success: (res) => {
        var tempImagePath = res.tempImagePath;
        // console.log(tempImagePath);

        wx.showLoading({
          title: '正在核验身份....',
        })
        this.setData({logindisabled:true})
        wx.uploadFile({
          url: app.globalData.url.login, //仅为示例,非真实的接口地址
          filePath: tempImagePath,
          header:{
            Cookie: wx.getStorageSync('session_id')
          },
          name: 'file',
          success: (res)=> {
            wx.hideLoading();
            this.setData({logindisabled:false});

            var data = res.data;
            console.log(data);
            wx.showModal({
              title: '提示',
              content: data,
              showCancel:false
            })
            //do something
          }
        })
      }
    })
  },

调用的接口

//刷脸登录
    public function login(){
        $dir = "./Uploads/temp/";
        if(!file_exists($dir)){
            mkdir($dir, 0777, true);
        }
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize = 3145728 ;// 设置附件上传大小
        $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath = $dir; // 设置附件上传根目录
        $upload->savePath = ''; // 设置附件上传(子)目录
        $upload->autoSub = false;
        // 上传文件
        $info = $upload->uploadOne($_FILES['file']);
        if(!$info) {// 上传错误提示错误信息
            // return $this->ajaxReturn(array('error'=>true,'msg'=>$upload->getError()));
            return json_encode(array('error'=>true,'msg'=>$upload->getError()),JSON_UNESCAPED_UNICODE);
        }else{// 上传成功 获取上传文件信息
            // return $this->ajaxReturn(array('error'=>false,'msg'=>'ok'));
            $file = $dir . $info['savepath'] . $info['savename'];
            $image = base64_encode(file_get_contents($file));
            // 调用人脸检测
            $client = $this->init_face();
            $options['liveness_control']='NORMAL';
            $options["max_face_num"] = 1;
            
            $ret = $client->search($image,'BASE64','pingjiao',$options);
            // echo json_encode($ret,JSON_UNESCAPED_UNICODE);
            // exit;

             if($ret['error_code']==0){
                $user=$ret['result']['user_list'][0];
                $no=$user['user_id'];
                $score=$user['score'];

                if(!empty('no')){
                    $data=M('student')->field('no,name,sex')->where("no='{$no}'")->find();

                    if($data){
                        $data['score']=$score;
                        if($score>95){
                            echo '姓名:'.$data['name'];
                        }else{
                            echo json_encode($data,JSON_UNESCAPED_UNICODE);
                        }
                    }else{
                        echo "本地数据库没有该学生,百度云信息:个人信息:{$no}, 分值:{$score}";
                    }
                }


            }else{
                echo '活体检验失败,'.json_encode($ret,JSON_UNESCAPED_UNICODE);
            }

        }
    }

 

你可能感兴趣的:(微信小程序人工智能之刷脸登录)