// 阿里云oss上传 import OSS from 'ali-oss' import OssConfig from './config' import Util from '@/util' let client = null let Oss // 初始化oss const initOss = async () => { Oss = await OssConfig() if (!client) client = new OSS(Oss.config) } initOss() export default { // 上传文件 upload (file) { initOss() return new Promise(async (resolve, reject) => { try { // 上传到阿里云oss const res = await client.multipartUpload(Date.now() + '.' + Util.getFileExt(file.name), file); resolve({ format: Util.getFileExt(file.name), name: file.name, url: Oss.preUrl + res.name, size: file.size, }) } catch (error) { reject() } }) }, // 删除文件(传完整url,不是没有https的name,因为很多接口没有存name,只存url,所以统一使用url) async del (url) { initOss() await client.delete(url.replace(Oss.preUrl, '')); } }