1.首先你要有一个可以就行人脸识别的服务器,然后就是上传到百度云,百度云人脸识别的API接口全面升级到V3版本,并进行开放测试
2.wxml代码
- <camera device-position="{{sxt}}" flash="off" binderror="error" style="width: 100%; height: 300px;"></camera>
 - <view class="weui-cells weui-cells_after-title">
 - <view class="weui-cell weui-cell_switch">
 - <view class="weui-cell__bd"></view>
 - <!-- <view class="weui-cell__ft"> -->
 - <input name='sex' value='{{sxt}}'>{{sxt}}</input>
 - <switch checked bindchange='switch1Change'/>
 - <!-- </view> -->
 - </view>
 - </view>
 - <button type="primary" bindtap="takePhoto">拍照</button>
 
3.js代码
- data: {
 - sxt: '前置'
 - },
 - switch1Change: function (e) {
 - console.log(e)
 - var front = '前置'
 - var back = '后置'
 - if (this.data.sxt == '前置') {
 - this.setData({ sxt: back })
 - } else if (this.data.sxt == '后置') {
 - this.setData({ sxt: front })
 - }
 - },
 - takePhoto() {
 - const ctx = wx.createCameraContext()
 - ctx.takePhoto({
 - quality: 'high',
 - success: (res) => {
 - // console.log(res);
 - this.setData({
 - src: res.tempImagePath
 - })
 - wx.showLoading({
 - title:'正在核验身份...',
 - })
 - this.setData({logindisabled:true});
 - wx.uploadFile({
 - url: 'http://www.swinder.top/server/index.php/home/index/login',
 - filePath:res.tempImagePath,
 - name:'file',
 - success:(res)=>{
 - wx.hideLoading();
 - this.setData({logindisabled:false});
 - var data = res.data
 - console.log(data);
 - wx.showModal({
 - title:'提示',
 - content:data,
 - showCancel:false
 - })
 - }
 - })
 - }
 - })
 - }
 
4.后台php代码
- private function init_face(){
 - $APP_ID=''; //在百度云上查看自己的信息
 - $API_KEY='';
 - $SECRET_KEY='';
 - $dir = APP_PATH . '/face-sdk/';
 - require_once $dir . 'AipFace.php';
 - return new \AipFace($APP_ID,$API_KEY,$SECRET_KEY);
 - }
 - public function login(){
 - //上传文件路径
 - $dir="./Uploads/temp/";
 - if(!file_exists($dir)){
 - mkdir($dir,0777,true);
 - }
 - $upload = new \Think\Upload();//实例化上传类
 - $upload->maxSize = 2048000;//设置附件上传大小2m
 - $upload->exts=array('jpg','gif','png','jpeg');//设置上传类型
 - $upload->rootPath=$dir;//设置附件上传根目录
 - $upload->savePath='';//设置附件上传(子)目录
 - $upload->autoSub=false;
 - //上传文件
 - $info = $upload->uploadOne($_FILES['file']);
 - if(!$info){
 - //上传错误提示信息
 - echo json_encode(array('error'=>true,'msg'=>$upload->getError()),JSON_UNESCAPED_UNICODE);
 - }else{
 - //上传成功
 - $file = $dir . $info['savepath'] . $info['savename'];
 - $image = base64_encode(file_get_contents($file));
 - $client = $this->init_face();
 - $options['liveness_control']='NORMAL';
 - $options['max_user_num']='1';
 - $ret = $client->search($image,'BASE64',$this->face_group(),$options);
 - 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;
 - echo json_encode($data,JSON_UNESCAPED_UNICODE);
 - }else{
 - //本地库不存在此学号
 - echo "本地数据库没有该学生,百度云库信息:个人信息:{$no},分值:{$score}";
 - }
 - }
 - }else{
 - echo "活体检测失败,".json_encode($ret,JSON_UNESCAPED_UNICODE);
 - }
 - }
 - }
 

















