getPhoto() {
let that = this;
uni.chooseImage({
count: 1,
sizeType: ['compressed'],
sourceType: ['album', 'camera'],
success: function(res) {
// #ifdef APP-PLUS
that.compressImg(res)
// #endif
// #ifdef H5
that.getImageInfo(res.tempFilePaths[0])
// #endif
},
complete: function(res) {
uni.hideLoading()
}
});
},
getImageInfo(src) {
let _this = this
uni.getImageInfo({
src,
success(res) {
let canvasWidth = res.width //图片原始长宽
let canvasHeight = res.height
let ratio = canvasWidth / canvasHeight;
if (canvasWidth > 500) {
canvasWidth = 500;
canvasHeight = Math.floor(canvasWidth / ratio);
}
let img = new Image()
img.src = res.path
let canvas = document.createElement('canvas');
let ctx = canvas.getContext('2d')
canvas.width = canvasWidth
canvas.height = canvasHeight
ctx.drawImage(img, 0, 0, canvasWidth, canvasHeight)
canvas.toBlob(function(fileSrc) {
let imgSrc = window.URL.createObjectURL(fileSrc)
_this.upimg(imgSrc)
})
}
})
},
compressImg(res){
uni.compressImage({
src: res.tempFilePaths[0],
quality: 80,
success: info => {
this.upimg(info.tempFilePath)
}
})
},
upimg(info){
uni.showLoading({
title: '上传中...'
})
var that = this
uni.uploadFile({
url: '', //接口地址
filePath: info,
name: 'file',
success: (uploadFileRes) => {
uni.hideLoading()
console.log(uploadFileRes);
},
fail: function(res) {
uni.hideLoading()
uni.showModal({
title: '提示',
content: '上传失败,请重新上传',
showCancel: false,
})
}
});
},