const common = { /** * 转义富文本标签 * @param { String } temp 后台返回需要处理的富文本 * @return { String } 处理好的富文本 */ unescapeHTML(temp){ if(!temp) return ''; temp = "" + temp; return temp.replace(/</g, "<").replace(/>/g, ">").replace(/&/g, "&").replace(/"/g, '"').replace(/'/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 == 0) { resolve(data.data.url); } else { reject(data.message); } }, fail(){ reject('上传失败'); } }) }); return promise; }, } export default common