UNPKG

@aplus-frontend/ui

Version:

238 lines (237 loc) 7.77 kB
import { defineComponent as Q, inject as a, ref as X, watch as Y, nextTick as Z, createElementBlock as U, openBlock as w, normalizeClass as ee, unref as s, createVNode as x, withCtx as k, createElementVNode as I, createTextVNode as oe, toDisplayString as L, withModifiers as te, createCommentVNode as ae, createBlock as se, resolveDynamicComponent as ne } from "vue"; import "../../config-provider/index.mjs"; import { getPath as re, getName as le, getReturnData as S } from "../utils/returnData.mjs"; import { useOss as ie, getOssInstance as z } from "../hooks/useOss.mjs"; import { message as c, Upload as ue, Button as ce } from "@aplus-frontend/antdv"; import { UploadOutlined as pe, VerticalAlignBottomOutlined as de } from "@ant-design/icons-vue"; import { fileMatchesAccept as me, getAcceptText as fe } from "../utils/accept.mjs"; import { useGlobalConfig as ge } from "../../config-provider/hooks/use-global-config.mjs"; import { useLocale as he } from "../../config-provider/hooks/use-locale.mjs"; import { useNamespace as ve } from "../../config-provider/hooks/use-namespace.mjs"; const we = { class: "multiple-file-context" }, xe = { key: 0 }, ye = { key: 1 }, Me = /* @__PURE__ */ Q({ __name: "MultipleFile", setup(Ce, { expose: B }) { const E = ge( "downloadCenterTriggerConfig" ), { t: r } = he(), { b: N } = ve("ap-upload-multiple-file"), { put: O } = ie(), P = a("theme"), D = a("dirName"), y = a("accept") || "*", _ = a("maxSize") ?? 500, V = a("title") || r("ap.apUpload.uploadFile"), m = a("subTitle"), l = a("maxCount") ?? 10, p = a("uploadingCount"), b = a("beforeUpload"), F = a("customRequest"), T = a("getOssAccess"), q = a("maxSizeErrorMsg"), j = a("maxCountErrorMsg"), $ = a("acceptErrorMsg"), A = a("onRemove"), C = a("showUploadErrorMsg"), n = a("value"), i = [], f = a("needName"), d = X(null); Y( () => n?.value, function(e) { !d?.value?.fileList?.length && e && R(e); }, { immediate: !0 } ); function R(e) { if (!e) { u = [], d.value.fileList = [], n.value = void 0, i.splice(0, i.length); return; } Z(() => { const o = e.map((t) => { const h = re(f, t), K = le(f, t), v = new File( [t], K, {} ); return v.response = h, v.uid = h, v.status = "done", v; }); if (l && o.length > l) { c.warning( r("ap.apUpload.fileInitializationException", { maxCount: l }) ); return; } o.forEach((t) => { u.push(t), i.push({ uid: t.uid, path: t.response }); }), d.value.fileList = o, n.value = e; }); } B({ setValue: R }); let u = [], M; function g(e) { setTimeout(() => { const o = d.value.fileList.findIndex( (t) => t.uid === e.uid ); o >= 0 && d.value.fileList.splice(o, 1); }); } async function G(e) { if (l && l > 1 && u.length >= l) return clearTimeout(M), M = setTimeout(() => { c.warning( j || r("ap.apUpload.maxUploadFiles", { maxCount: l }) ); }), g(e), !1; if (e.size > _ * 1024 * 1024) return c.warning( q || r("ap.apUpload.maxUploadFileSize", { maxSize: _ }) ), g(e), !1; if (!me(e, y)) return c.warning( $ || r("ap.apUpload.fileFormatNotSupported") ), g(e), !1; if (typeof b == "function" && await b(e) === !1) return g(e), !1; l && l === 1 && (u = [], n.value = void 0), u.push(e); } async function W(e) { if (p.value++, typeof F == "function") { F({ onProgress: (o) => { setTimeout(() => { e.onProgress({ percent: o }); }); }, onError: (o) => { C && c.warning(o || r("ap.apUpload.networkAnomaly")), setTimeout(() => { e.onError({ name: o, message: o }); }), p.value--; }, onSuccess: (o) => { setTimeout(() => { e.onSuccess(o); }), n.value = [ ...n.value || [], S(f, o, e.file.name) ], i.push({ uid: e.file.uid, path: o }), p.value--; }, file: e.file }); return; } try { const o = await z(T); e.file.oss = o, O({ file: e.file, dirName: D, oss: o, successCallBack(t) { e.onSuccess(t), n.value = [ ...n.value || [], S(f, t, e.file.name) ], i.push({ uid: e.file.uid, path: t }), p.value--; }, errorCallBack(t) { C && c.warning(t || r("ap.apUpload.networkAnomaly")), e.onError({ name: t, message: t }), p.value--; }, progressCallBack(t) { e.onProgress({ percent: t }); } }); } catch (o) { C && c.warning( typeof o?.message == "string" ? o.message : r("ap.apUpload.networkAnomaly") ), e.onError({ name: o?.message || r("ap.apUpload.networkAnomaly"), message: o?.message || r("ap.apUpload.networkAnomaly") }), p.value--; } } function H(e) { if (A && A?.(e), e.response) { if (Array.isArray(n.value)) { const t = i.findIndex((h) => h.uid === e.uid); t >= 0 && (n?.value.splice(t, 1), i.splice(t, 1)), n.value.length === 0 && (n.value = void 0); } } else e?.oss?.pauseUpload(); const o = u.findIndex( (t) => t.uid === e.uid ); return u.splice(o, 1), Promise.resolve(!0); } const J = async (e) => { if (E.value?.trigger) { let o = [e].map((t) => ({ objectName: t.response, fileName: t.name })); E.value?.trigger?.(o); } else (await z(T)).downloadFile([ { path: e.response, fileName: e.name } ]); }; return (e, o) => (w(), U("div", { class: ee({ [s(N)()]: !0, [s(N)("admin")]: s(P) === "admin" }) }, [ x(s(ue), { ref_key: "uploadRef", ref: d, accept: s(y), multiple: "", "max-count": s(l), progress: { strokeWidth: 3, showInfo: !1, strokeColor: "#0070FF" }, "before-upload": G, "custom-request": W, "show-upload-list": { showDownloadIcon: !0, showRemoveIcon: !0 }, onRemove: H, onDownload: J }, { downloadIcon: k(() => [ x(s(de)) ]), default: k(() => [ I("div", we, [ x(s(ce), { class: "file-btn" }, { default: k(() => [ x(s(pe)), oe(" " + L(s(V)), 1) ]), _: 1 }), I("div", { class: "sub-title", onClick: o[0] || (o[0] = te(() => { }, ["stop"])) }, [ typeof s(m) == "string" ? (w(), U("div", xe, L(s(m) || `${s(r)("ap.apUpload.supportExtension")}${s(fe)(s(y))}`), 1)) : s(m) ? (w(), U("div", ye, [ (w(), se(ne(s(m)))) ])) : ae("", !0) ]) ]) ]), _: 1 }, 8, ["accept", "max-count"]) ], 2)); } }); export { Me as default };