本文出自:https://www.cnblogs.com/2186009311CFF/p/14248337.html
//oss参数请求接口获得 uploadImage: function(imageSrc_local, OssPara, userId) { return new Promise((resolve, reject) => { //当前时间精确到毫秒字符串 var date = new Date(); //获取时间 //console.log(date); var year = date.getFullYear(); var month = date.getMonth() + 1; var day = date.getDate(); var hour = date.getHours(); var minute = date.getMinutes(); var second = date.getSeconds(); month = isSubTen(month); day = isSubTen(day); //这样写显示时间在1~9会挤占空间;所以要在1~9的数字前补零; if (hour < 10) { hour = '0' + hour; } if (minute < 10) { minute = '0' + minute; } if (second < 10) { second = '0' + second; } //var x=date.getDay();//获取星期 var time = year + month + day + hour + minute + second; //图片命名规则:接口返回的地址 +接口返回的前缀 + 用户id +'_'+当前时间精确到毫秒字符串 +'.png'; let nameStr = OssPara.dir + userId + '_' + date + '.png'; console.log('nameStr' + nameStr) uni.uploadFile({ url: OssPara.host, //输入你的bucketname.endpoint filePath: imageSrc_local, fileType: 'image', name: 'file', formData: { name: nameStr, key: nameStr, policy: OssPara.policy, // 输入你获取的的policy OSSAccessKeyId: OssPara.accessKeyId, // 输入你的AccessKeyId success_action_status: '200', // 让服务端返回200,不然,默认会返回204 signature: OssPara.signature, // 输入你获取的的signature }, success: res => { //console.log(res) if (res.statusCode == '200') { resolve(OssPara.host + '/' + nameStr); } else { console.log.log(res); reject('接口返回错误'); } }, fail() { reject('网络链接错误'); } }); }); },