UNPKG

@aplus-frontend/ui

Version:

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