1.获取七牛云请求地址
//先下载包
npm install qiniu-js
import * as qiniu from 'qiniu-js'
//通过七牛云内置方法获取七牛云请求地址
const url = await qiniu.getUploadUrl(config, token)
console.log(url, "getUploadUrl");
2.七牛云上传方法
//上传七牛云
const customerUpload = async (request) => {
const {
file
} = request;
console.log(request, "request");
let token = tokens.value;
let key = '5uFEh7xzdQCuhLaYu0O8WS162Zieqm0' + Date.now();
return new Promise((resolve, reject) => {
uni.uploadFile({
url: '七牛云请求地址', //可以引入
filePath: request.url,
name: 'file',
formData: {
'key': key, // 存到七牛云后的文件名字,访问图片会用到
'token': token, // token,需要动态获取,调用服务端接口获取
},
// 存成功后的回调
success: (uploadFileRes) => {
console.log(uploadFileRes, "uploadFileRes");
let key = JSON.parse(uploadFileRes.data).key;
console.log(key, "key")
resolve('七牛云链接地址' + key)
},
fail: (err) => {
console.log('上传失败了', err);
reject(error)
}
});
})
}