wx.chooseImage
,这个API会自己去调用相机和相册,之后的工作完全交给底层去实现,简直是简单到没朋友:
// 拍照功能
getLocalImage:function(){
var that=this;
wx.chooseImage({
count:1,
success:function(res){
// 这里无论用户是从相册选择还是直接用相机拍摄,拍摄完成后的图片临时路径都会传递进来
app.startOperating("保存中")
var filePath=res.tempFilePaths[0];
var session_key=wx.getStorageSync('session_key');
// 这里顺道展示一下如何将上传上来的文件返回给后端,就是调用wx.uploadFile函数
wx.uploadFile({
url: app.globalData.url+'/home/upload/uploadFile/session_key/'+session_key,
filePath: filePath,
name: 'file',
success:function(res){
app.stopOperating();
// 下面的处理其实是跟我自己的业务逻辑有关
var data=JSON.parse(res.data);
if(parseInt(data.status)===1){
app.showSuccess('文件保存成功');
}else{
app.showError("文件保存失败");
}
}
})
},
fail:function(error){
console.error("调用本地相册文件时出错")
console.warn(error)
},
complete:function(){
}
})
},
|
wx.chooseVideo
,其中可以设置拍摄时间,用户拍摄结束以及用户拍摄时间超时等,都会调用success的回调函数,所以又是可以安心处理接下来的业务流程,而不用需要调用camera组件。
// 摄像功能
getLocalVideo:function(){
var that=this;
var session_key=wx.getStorageSync('session_key');
wx.chooseVideo({
maxDuration:10,
success:function(res1){
app.startOperating("上传中")
// 这个就是最终拍摄视频的临时路径了
var tempFilePath=res1.tempFilePath;
},
fail:function(){
console.error("获取本地视频时出错");
}
})
},
|