var qiniuUploader = require('../../libs/qiniuUploader.js');
//获取七牛云token
getTokenFn = () =>{
this.props.getQiniuyunTokenFn({}).then(res => {
console.log(res);
qiniuData=res;
}).catch((err) => {
})
}
//选择头像图片
chooseAvat = () => {
let that=this;
Taro.chooseImage({
count:1,
sizeType: ['original','compressed'],
sourceType: ['album','camera'],
success(res) {
// tempFilePath可以作为img标签的src属性显示图片
const tempFilePaths = res.tempFilePaths;
that.uploadToQiniu(tempFilePaths[0]);
},
fail(err){
}
})
}
//图片直接上传到七牛云,获取七牛云链接
uploadToQiniu=(filePath)=>{
let that=this,photoResultUrl='';
let qiniu_key ='userphoto/'+ Date.parse(new Date()) /1000 +".jpg"; //七牛云上图片文件夹和图片名字的命名规则
wx.uploadFile({
url:'https://up-z0.qiniup.com',//分华北区,华东区之类的,大家自己注意下
name:'file',
filePath: filePath,
formData: {
token:qiniuData.uptoken,
key:qiniu_key
},
success:function(res) {
let data = res.data;
if(typeof data==='string'){
data = JSON.parse(data.trim());//解压缩
photoResultUrl=qiniuData.domain+''+data.key +'?imageView2/0/w/110'; //七牛云图片的高级图片处理,此处是图片瘦身,支持jpg和png
that.changePhoto(photoResultUrl);//调用服务器接口,修改用户头像。此处不赘述了。。。
}
},
fail:function(err) {
console.log(err)
}
});
}
//如果是一次性上传多张图片
七牛云上传会报错,error: "file exists" 因为设置了key以后,上传后的图片同名了。可以按如下修改
let qiniu_key ='usersuggest/'+ filePath.substr(20,30)+'.jpg';