UNPKG

vue-cloud-upload

Version:

基于vue+elementui的通用云上传组件,无缝衔接腾讯云,华为云,阿里云等对象存储平台,开箱即用

206 lines (205 loc) 6.26 kB
var T = Object.defineProperty; var I = Object.getOwnPropertySymbols; var x = Object.prototype.hasOwnProperty, k = Object.prototype.propertyIsEnumerable; var y = (l, t, e) => t in l ? T(l, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : l[t] = e, f = (l, t) => { for (var e in t || (t = {})) x.call(t, e) && y(l, e, t[e]); if (I) for (var e of I(t)) k.call(t, e) && y(l, e, t[e]); return l; }; var S = (l, t, e) => y(l, typeof t != "symbol" ? t + "" : t, e); var u = (l, t, e) => new Promise((a, s) => { var o = (c) => { try { n(e.next(c)); } catch (i) { s(i); } }, r = (c) => { try { n(e.throw(c)); } catch (i) { s(i); } }, n = (c) => c.done ? a(c.value) : Promise.resolve(c.value).then(o, r); n((e = e.apply(l, t)).next()); }); const m = class m { constructor(t) { // 外部传入的OSS对象 S(this, "ossClient", null); S(this, "initPromise", null); this.initPromise = this.initClient(t); } // 添加初始化Promise // 设置外部OSS对象的静态方法 static setExternalOSS(t) { this.externalOSS = t; } static getInstance(t) { return this.instance || t && t.getTempCredential && typeof t.getTempCredential == "function" && (this.instance = new m(t)), this.instance; } static destroyInstance() { this.instance = null, this.ossClient = null; try { const t = localStorage.getItem("ossCptDatas"); if (t) { let e = JSON.parse(t); const a = Date.now() - 14400 * 60 * 1e3; e = e.filter((s) => s.createTime ? s.createTime > a : !1), localStorage.setItem("ossCptDatas", JSON.stringify(e)); } } catch (t) { console.error("清理过期断点记录失败:", t); } } // 添加等待初始化完成的方法 static waitForInitialization() { return u(this, null, function* () { this.instance && this.instance.initPromise && (yield this.instance.initPromise); }); } // 添加确保初始化完成的方法(兼容之前的代码) static ensureInitialized() { return u(this, null, function* () { yield this.waitForInitialization(); }); } initClient(t) { return u(this, null, function* () { const e = yield t.getTempCredential(), a = ["accessKeyId", "accessKeySecret", "stsToken"]; let s = !0; if (a.forEach((r) => { e.hasOwnProperty(r) || (console.error(`getTempCredential函数未返回字段${r}`), s = !1); }), !s) { this.instance = null; return; } let o = m.externalOSS || window.OSS; this.ossClient = new o({ secure: !0, authorizationV4: !0, region: t.region, accessKeyId: e.accessKeyId, accessKeySecret: e.accessKeySecret, stsToken: e.stsToken, bucket: t.bucket, refreshSTSToken: t.getTempCredential, refreshSTSTokenInterval: t.refreshSTSTokenInterval || 85e3 }); }); } // 单文件上传 uploadFile(c) { return u(this, arguments, function* ({ bucket: t, region: e, key: a, file: s, sliceSize: o, chunkSize: r, onProgress: n }) { try { if (s.size < o) { const i = yield this.ossClient.put(a, s, { progress: (p) => { n && typeof n == "function" && n(p); } }); return f({ url: i.url, key: a, name: s.name }, i); } else { let i; const p = `${s.name}-${s.size}-${s.lastModified}`, d = this.getCptDataByKey(p); let D = d ? d.cpt : null; D ? i = yield this.ossClient.multipartUpload(d.name, s, { checkpoint: D, progress: (h, C) => { this.setCptData(p, a, C), n && typeof n == "function" && n(h); }, parallel: 4, // 并行上传的分片数 partSize: r // 分片大小 }) : i = yield this.ossClient.multipartUpload(a, s, { progress: (h, C) => { this.setCptData(p, a, C), n && typeof n == "function" && n(h); }, parallel: 4, // 并行上传的分片数 partSize: r // 分片大小 }), i.res.status == 200 && this.delCptData(p); const O = { "content-disposition": `attachment; filename=${encodeURIComponent( s.name )}` }, w = `https://${t}.${e}.aliyuncs.com/${i.name}`; return f({ url: w, key: i.name, name: s.name }, i); } } catch (i) { throw new Error(`阿里云OSS上传失败: ${i.message}`); } }); } getCptDataByKey(t) { let e = []; const a = localStorage.getItem("ossCptDatas"); a && (e = JSON.parse(a)); const s = e.findIndex((o) => o.key == t); return s >= 0 ? e[s] : null; } setCptData(t, e, a) { let s = []; const o = localStorage.getItem("ossCptDatas"); o && (s = JSON.parse(o)); const r = s.findIndex((n) => n.key == t); r >= 0 ? s[r].cpt = a : s.push({ key: t, name: e, cpt: a, createTime: Date.now() }), localStorage.setItem("ossCptDatas", JSON.stringify(s)); } delCptData(t) { let e = []; const a = localStorage.getItem("ossCptDatas"); a && (e = JSON.parse(a)); const s = e.findIndex((o) => o.key == t); s >= 0 && (e.splice(s, 1), localStorage.setItem("ossCptDatas", JSON.stringify(e))); } // 图片加水印 addWatermark(o) { return u(this, arguments, function* ({ bucket: t, region: e, key: a, watermarkText: s }) { try { return { processedUrl: this.ossClient.signatureUrl(a, { expires: 3600, // 1小时有效期 process: `image/watermark,text_${encodeURIComponent( s )},fill_ I0ZGRkZGRg,size_20,g_se,x_20,y_20` // 水印参数 }), originalKey: a }; } catch (r) { throw new Error(`添加水印失败: ${r.message}`); } }); } }; S(m, "instance", null), S(m, "externalOSS", null); let g = m; export { g as default };