图片上传到阿里云oss
1.调用后端接口获取上传密钥等数据
Network.common.get_oss_config()
2.将获取到的数据发送到阿里云oss
function Upload(file) {
return new Promise((reslove, reject) => {
Network.common.get_oss_config().then(res => {
let {
host,
OSSAccessKeyId,
policy,
signature
} = res.data;
const Name = file.split('.');
const number = Math.floor(Math.random() * 899999 + 100000);
const img = Name[name];
wx.uploadFile({
url: host,
filePath: file,
name: 'file',
formData: {
name: file,
key: 'mini/' + number + '.' + img,
success_action_status: '200',
OSSAccessKeyId,
policy,
signature
},
success: res => {
if (res.statusCode === 200) {
let url = `https://xxxxxxxx.com/mini/` + number + '.' + img;
reslove(url);
} else {
reslove(false);
}
},
fail: res => {
reslove(false);
console.log(res);
}
});
}).catch(err => {
reslove(err)
})
})
}