deming/static/js/common.js

44 lines
1.3 KiB
JavaScript
Raw Normal View History

2020-07-01 17:32:36 +08:00
const common = {
/**
* 转义富文本标签
* @param { String } temp 后台返回需要处理的富文本
* @return { String } 处理好的富文本
*/
unescapeHTML(temp){
2020-07-03 09:08:53 +08:00
if(!temp) return '';
2020-07-01 17:32:36 +08:00
temp = "" + temp;
return temp.replace(/&lt;/g, "<").replace(/&gt;/g, ">").replace(/&amp;/g, "&").replace(/&quot;/g, '"').replace(/&apos;/g, "'");
},
2020-07-07 17:47:00 +08:00
/**
* 上传文件(只能单文件上传)
* @param { String } url 服务器 url
2020-07-07 17:48:20 +08:00
* @param { String } filePath 要上传文件资源的路径
2020-07-07 17:47:00 +08:00
* @param { String } type 上传文件验证类型 默认: img 可选: video
2020-07-08 18:14:41 +08:00
* @return { object } promise 对象 resolve 返回文件服务器地址 reject 返回错误信息
2020-07-07 17:47:00 +08:00
*/
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);
2020-07-08 18:14:41 +08:00
if(data.errCode == 0) {
resolve(data.data.url);
2020-07-07 17:47:00 +08:00
} else {
2020-07-08 18:14:41 +08:00
reject(data.message);
2020-07-07 17:47:00 +08:00
}
},
fail(){
reject('上传失败');
}
})
});
return promise;
},
2020-07-01 17:32:36 +08:00
}
export default common