当用户选好照片后点击确定上传图片进入图库并和人像库进行对比
前台代码wxml
<!--pages/headimg/headimg.wxml--> <form bindsubmit='formSubmit'> <view class="page"> <view class="page__hd"> <view class="page__title">采集照片</view> <view class="page__desc">{{student.name}}{{student.no}}</view> </view> <view class="page__bd"> <view class="weui-cells"> <view class="weui-cell"> <view class="weui-cell__bd"> <view class="weui-uploader"> <view class="weui-uploader__hd"> <view class="weui-uploader__title">个人照片</view> <view class="weui-uploader__info">{{imageList.length}}/1</view> </view> <view class="weui-uploader__bd"> <view class="weui-uploader__files"> <block wx:for="{{imageList}}" wx:for-item="image" wx:key="imageList"> <view class="weui-uploader__file"> <image class="weui-uploader__img" src="{{image}}" data-src="{{image}}" bindtap="previewImage"></image> </view> </block> </view> <view class="weui-uploader__input-box"> <view class="weui-uploader__input" bindtap="chooseImage"></view> </view> </view> </view> </view> </view> </view> </view> </view> <view class="weui-btn-area"> <button class="weui-btn" type="primary" form-type='submit'>确定</button> <button class="weui-btn" type="default" form-type='reset'>取消</button> </view> </form>
前台是使用weui框架所以直接引用样式就可以了
js代码调用接口实现具体功能
// pages/headimg/headimg.js //获取应用实例 const app = getApp() //上传 function upload(that, id) { if (that.data.imageList.length == 0) { return; } wx.uploadFile({ url: app.globalData.url.upload, //仅为示例,非真实的接口地址 filePath: that.data.imageList[0], header: { Cookie: wx.getStorageSync('session_id') }, name: 'file', formData: { 'id': id }, success: function (res) { var data = res.data console.log(data); // var json = JSON.parse(res.data); wx.showToast({ title: data, icon: 'none', duration: 3000 }) setTimeout(function () { wx.navigateTo({ url: '../index/index', }) }, 2000) } }) } Page({ data: { imageList: [], id: null, options: null }, onLoad: function (options) { var id = options.id; console.log(options); this.setData({ options: options }) this.setData({ id: options.id }) }, formSubmit: function (e) { upload(this, this.data.id); wx.navigateTo({ url: '../list/list' }) }, chooseImage: function () { var that = this wx.chooseImage({ sourceType: ['album', 'camera'], sizeType: ['original', 'compressed'], count: 1, success: function (res) { console.log(res) that.setData({ imageList: res.tempFilePaths }) } }) }, previewImage: function (e) { // var current = e.target.dataset.src wx.previewImage({ current: e.currentTarget.id, urls: this.data.imageList }) }, })
具体调用的接口
首先是上传图片的接口
//上传图片 public function upload($id=''){ if(empty($id)){ return false; } $no = M("student")->where("id={$id}")->getField('no'); $dir = "./Uploads/studentface/"; 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->saveName = $no; $upload->replace = true; $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'=>$info['savepath'].$info['savename'],'id'=>$id)); $file = $dir . $info['savepath'] . $info['savename']; $image = base64_encode(file_get_contents($file)); $this->facevalid($no,$image); $m = M('head'); $data = $m->where("no='{$no}'")->find(); if($data){ //有数据,则更新 $m->where("no='{$no}'")->save(array('base64'=>$image, 'path'=>$file)); }else{ $m->add(array('no'=>$no,'base64'=>$image,'path'=>$file)); } return "采集照片成功"; } }
private function face_group(){ $groupname = 'pingjiao'; $client = $this->init_face(); $ret = $client->getGroupList(); if($ret['error_code'] == 0){ $grouplist = $ret['result']['group_id_list']; if(in_array($groupname, $grouplist)){ return $groupname; }else{ $ret = $client->groupAdd($groupname); if($ret['error_code'] == 0){ return $groupname; }else{ return false; } } } }