// 调用摄像头
<button class='photo_left_text' bindtap='addphoto'>添加照片</button>
data:{
album: ["album","camera"],//相册或者摄像头
}
//选择照片
addphoto(e){
let that = this;
wx.showActionSheet({//选择相册或者拍照api
itemList: ['相册选择', '拍照'],
success: (res) => {
if (res.tapIndex == 0) {
// 相册
wx.showLoading({
title: '上传中',
});
wx.chooseImage({//获取图片
count: "1",//获取图片数量最多能从相册获取9张
sizeType: ["original"],
sourceType: [that.data.album[res.tapIndex]],
success: function (res) {
that.phonts(res.tempFilePaths[0]);
},
})
} else {
// 相机
wx.showLoading({
title: '上传中',
});
wx.chooseImage({
count: "1",
sizeType: ["original"],
sourceType: [that.data.album[res.tapIndex]],
success: function (res) {
that.phonts(res.tempFilePaths[0]);
},
})
}
}
})
},
//调用后台接口把图片上传到后台并返回路径
phonts(t){
let that=this;
wx.uploadFile({
url: 'https://test.com',//后台接口
filePath: t,
name: 'file',//后台接口的名字
success(r) {
console.log('成功', r)
wx.hideLoading();
that.setData({
photo: r.data
});
},
fail(e) {
console.log("失败", e)
}
})
},