用的wpy框架,有this,原声小程序就按照你们的调方法就行了
//打开相机
openCream() {
const _this = this;
wx.showActionSheet({
itemList: ["拍照"],//['拍照','相册']
itemColor: "",
//成功时回调
success: function(res) {
if (!res.cancel) {
/* res.tapIndex返回用户点击的按钮序号,从上到下的顺序,从0开始比如用户点击本例中的拍照就返回0,相册就返回1我们res.tapIndex的值传给chooseImage()*/
_this.chooseImage(res.tapIndex);
}
},
//失败时回调
fail: function(res) {
console.log("调用失败");
},
complete: function(res) {},
});
},
chooseImage(){
let this_ = this;
wx.chooseImage({
//count表示一次可以选择多少照片
count: 1,
//sizeType所选的图片的尺寸,original原图,compressed压缩图
sizeType: ["original", "compressed"],
//如果sourceType为camera则调用摄像头,为album时调用相册
sourceType: ["camera"],
success(res) {
// tempFilePath可以作为img标签的src属性显示图片
const tempFilePaths = res.tempFilePaths;
this_.temSrc = tempFilePaths[0];//保存选择图片的本地地址,需要上传得到真正的url
wx.uploadFile({//上传
url: api.fileUpload,
filePath: tempFilePaths[0],
header: {
uapAccessToken: accessToken,
uapRefreshToken: refreshToken,
},
name: 'file',
success(re) {
const data = JSON.parse(re.data);
this_.src = data.url
this_.$apply()//wpy框架需要
},
fail() {
console.log()
}
})
},
});
}