This commit is contained in:
2020-07-07 17:47:00 +08:00
parent 77083c0404
commit 5ede514a02
8 changed files with 230 additions and 90 deletions

View File

@@ -9,5 +9,36 @@ const common = {
temp = "" + temp;
return temp.replace(/&lt;/g, "<").replace(/&gt;/g, ">").replace(/&amp;/g, "&").replace(/&quot;/g, '"').replace(/&apos;/g, "'");
},
/**
* 上传文件(只能单文件上传)
* @param { String } url 服务器 url
* @param { String } filePath 要上传文件资源的路径(单文件上传)
* @param { String } type 上传文件验证类型 默认: img 可选: video
* @return { object } promise 对象 resolve 为文件服务器地址 reject 为错误信息
*/
uploadFile({ url, filePath, type = 'img' } = {}) {
const token = uni.getStorageSync('token');
const promise = new Promise((resolve, reject) => {
uni.uploadFile({
url: url,
header: { "authorization": 'Bearer' + " " + token },
filePath: filePath,
name: 'file',
formData: { type: type },
success: uploadFileRes => {
const data = JSON.parse(uploadFileRes.data);
if(data.errCode.code == 0) {
resolve(data.errCode.url);
} else {
reject(data.errCode.msg);
}
},
fail(){
reject('上传失败');
}
})
});
return promise;
},
}
export default common