SCopperUpload.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. var K = (r, _, g) => new Promise((n, T) => {
  2. var k = (d) => {
  3. try {
  4. b(g.next(d));
  5. } catch (C) {
  6. T(C);
  7. }
  8. }, c = (d) => {
  9. try {
  10. b(g.throw(d));
  11. } catch (C) {
  12. T(C);
  13. }
  14. }, b = (d) => d.done ? n(d.value) : Promise.resolve(d.value).then(k, c);
  15. b((g = g.apply(r, _)).next());
  16. });
  17. import { computed as R, ref as x, watch as be, openBlock as i, createElementBlock as w, normalizeClass as I, createElementVNode as y, toDisplayString as B, createCommentVNode as u, createVNode as D, unref as v, normalizeStyle as ke, withCtx as ee, Fragment as le, createBlock as m, resolveDynamicComponent as te, withDirectives as Ce, withModifiers as O, vShow as $e } from "vue";
  18. import { d as Oe } from "./vuedraggable.js";
  19. import Se from "./SFilePreview.js";
  20. import Ue from "./CropperModal.js";
  21. import { b as Fe, d as Re } from "./upload.js";
  22. import { n as xe, ak as Ie, al as ae, am as oe, an as ne, ao as ie, ap as E, aq as L, ar as h, as as Be, at as j, au as M, av as se, aw as q, ax as N, ay as Ee, az as Le } from "./index2.js";
  23. const Me = {
  24. key: 0,
  25. class: "head"
  26. }, Te = { class: "label" }, ze = ["onMouseover", "onMouseleave"], De = ["src", "alt"], je = {
  27. key: 1,
  28. class: "file-thumb"
  29. }, Ne = { class: "file-thumb-ext" }, _e = { class: "tool-wrap" }, Ae = { class: "left" }, Pe = { class: "right move" }, Ve = { class: "file-row-ext" }, Ke = ["title"], qe = { class: "file-row-actions" }, Ge = {
  30. key: 0,
  31. class: "file-add-text"
  32. }, He = ["accept", "disabled"], We = {
  33. __name: "SCopperUpload",
  34. props: {
  35. /**
  36. * 文件列表(同 PictureUpload 的 data)
  37. * @list
  38. */
  39. list: {
  40. type: Array,
  41. default: () => []
  42. },
  43. /**
  44. * 标题
  45. * @label
  46. */
  47. label: {
  48. type: String,
  49. default: ""
  50. },
  51. /**
  52. * 只读(同 PictureUpload 的 disabled)
  53. * @readonly
  54. */
  55. readonly: {
  56. type: Boolean,
  57. default: !1
  58. },
  59. /**
  60. * 详情模式:只读展示,不显示新增入口、不可删除拖拽,仅保留预览与下载
  61. * @detailMode
  62. */
  63. detailMode: {
  64. type: Boolean,
  65. default: !1
  66. },
  67. /**
  68. * 是否显示新增(isAdd=false 等价 PictureUpload 的 ishiddenAdd=true:隐藏添加框且不可删除/拖拽)
  69. * @isAdd
  70. */
  71. isAdd: {
  72. type: Boolean,
  73. default: !0
  74. },
  75. /**
  76. * 文件类型
  77. * @accept
  78. */
  79. accept: {
  80. type: String,
  81. default: "image/*"
  82. },
  83. /**
  84. * 裁剪比例:1 = 1:1(头像);'16:9' 或 16/9 = 封面;0 = 自由裁剪
  85. * @aspectRatio
  86. */
  87. aspectRatio: {
  88. type: [Number, String],
  89. default: 1
  90. },
  91. /**
  92. * 选图后是否先裁剪(false 时原图直传,等同改造前的行为)
  93. * @crop
  94. */
  95. crop: {
  96. type: Boolean,
  97. default: !0
  98. },
  99. /**
  100. * 展示形态:card 卡片(图片显示原图,其他类型显示文件类型);list 列表(文件类型 + 文件名 + 操作按钮)
  101. * @listType
  102. */
  103. listType: {
  104. type: String,
  105. default: "card"
  106. },
  107. /**
  108. * 是否显示预览按钮
  109. * @showPreview
  110. */
  111. showPreview: {
  112. type: Boolean,
  113. default: !0
  114. },
  115. /**
  116. * 是否显示下载按钮
  117. * @showDownload
  118. */
  119. showDownload: {
  120. type: Boolean,
  121. default: !0
  122. },
  123. /**
  124. * 是否显示删除按钮(仍需 isAdd 且非只读)
  125. * @showRemove
  126. */
  127. showRemove: {
  128. type: Boolean,
  129. default: !0
  130. },
  131. // 最多可上传数量(同 PictureUpload 的 maxSize)
  132. maxFileCount: {
  133. type: Number,
  134. default: 5
  135. },
  136. // 单文件大小上限,单位 KB(同 PictureUpload 的 fileSize)
  137. fileSize: {
  138. type: Number,
  139. default: 200
  140. },
  141. // FTP 上传目录(同 PictureUpload 的 path),不传时回退 businessTableName
  142. path: {
  143. type: String,
  144. default: void 0
  145. },
  146. // 是否隐藏拖拽排序图标(同 PictureUpload 的 noDrag)
  147. noDrag: {
  148. type: Boolean,
  149. default: !1
  150. },
  151. // 图片列表容器样式(同 PictureUpload 的 picStyle)
  152. picStyle: {
  153. type: Object,
  154. default: () => {
  155. }
  156. },
  157. // 兼容保留:不传 path 时作为 FTP 目录名
  158. businessTableName: {
  159. type: String,
  160. default: "sapit-ui-plus-components-upload"
  161. },
  162. businessKey: {
  163. type: String,
  164. default: void 0
  165. },
  166. // 兼容保留(PictureUpload 无上传列表概念,仅声明避免外部传参告警)
  167. showUploadList: {
  168. type: Boolean,
  169. default: !0
  170. },
  171. // 已废弃:由 showRemove 取代,仅保留声明避免外部传参告警
  172. showRemoveIcon: {
  173. type: Boolean,
  174. default: !1
  175. }
  176. },
  177. emits: ["change", "handChange", "dragChange", "download"],
  178. setup(r, { emit: _ }) {
  179. const g = _, n = r, T = R(() => n.readonly || n.detailMode), k = R(() => n.readonly || n.detailMode), c = R(() => n.listType !== "list"), b = R(() => n.isAdd && !k.value && !n.noDrag), d = R(() => n.isAdd && !k.value && n.showRemove), C = () => n.path || n.businessTableName, re = ["png", "jpg", "jpeg", "gif", "webp", "bmp", "svg", "ico", "avif", "tif", "tiff"], ue = {
  180. pdf: Ee,
  181. doc: N,
  182. docx: N,
  183. wps: N,
  184. rtf: N,
  185. xls: q,
  186. xlsx: q,
  187. csv: q,
  188. ppt: se,
  189. pptx: se,
  190. zip: M,
  191. rar: M,
  192. "7z": M,
  193. gz: M,
  194. tar: M,
  195. txt: j,
  196. log: j,
  197. json: j,
  198. xml: j,
  199. md: Be,
  200. png: h,
  201. jpg: h,
  202. jpeg: h,
  203. gif: h,
  204. webp: h,
  205. bmp: h,
  206. svg: h,
  207. ico: h,
  208. mp4: L,
  209. mov: L,
  210. avi: L,
  211. mkv: L,
  212. webm: L,
  213. mp3: E,
  214. wav: E,
  215. flac: E,
  216. aac: E,
  217. m4a: E
  218. }, S = (e = {}) => e.imageUrl || e.url || e.uid || "", G = (e = "") => {
  219. const t = String(e).split("?")[0].split("#")[0], l = t.substring(t.lastIndexOf("/") + 1);
  220. try {
  221. return decodeURIComponent(l);
  222. } catch (a) {
  223. return l;
  224. }
  225. }, U = (e = {}) => e.name || G(S(e)) || window.$l("未命名文件"), z = (e = {}) => {
  226. const t = U(e), l = t.lastIndexOf(".");
  227. return l > -1 ? t.slice(l + 1).toLowerCase() : "";
  228. }, H = (e = {}) => {
  229. const t = e.type || e.mimeType || "";
  230. return t ? String(t).indexOf("image/") === 0 : re.includes(z(e));
  231. }, W = (e = {}) => ue[z(e)] || Le, s = x([]), A = x(!1), X = x(0);
  232. be(
  233. () => n.list,
  234. (e) => {
  235. s.value = e || [];
  236. },
  237. { immediate: !0, deep: !0 }
  238. );
  239. const P = () => {
  240. g("change", s.value), g("handChange", s.value);
  241. }, ce = (e) => {
  242. }, de = (e) => {
  243. s.value && s.value.length > 1 && s.value.forEach((t, l) => {
  244. t.sort = l;
  245. }), g("dragChange", s.value), P();
  246. }, pe = () => {
  247. F.value && F.value.click();
  248. }, F = x(null), Z = x(null);
  249. let $ = null;
  250. const fe = (e, t, l) => {
  251. const a = URL.createObjectURL(e);
  252. return new Promise((o) => {
  253. $ = (p) => {
  254. $ = null, URL.revokeObjectURL(a), o(p);
  255. }, Z.value.edit({
  256. img: a,
  257. aspectRatio: n.aspectRatio,
  258. title: l > 1 ? `${window.$l("图片裁剪")}(${t}/${l})` : window.$l("图片裁剪"),
  259. // png 保留 png(可能有透明背景),其余走 jpeg 控制体积
  260. outputType: e.type === "image/png" ? "png" : "jpeg",
  261. file: e
  262. });
  263. });
  264. }, ve = (e) => {
  265. $ && $(e && e.blob || null);
  266. }, me = () => {
  267. $ && $(null);
  268. }, ge = (e, t) => {
  269. const l = e.type || t.type || "image/png", a = l === "image/png" ? "png" : "jpg", o = (t.name || "image").replace(/\.[^.]+$/, "");
  270. return new File([e], `${o}.${a}`, { type: l, lastModified: Date.now() });
  271. }, we = (e) => K(this, null, function* () {
  272. const t = Array.from(e && e.target && e.target.files || []);
  273. if (F.value && (F.value.value = ""), !t.length) return;
  274. if (s.value.length + t.length > n.maxFileCount) {
  275. window.$message.warning(
  276. window.$l("上传图片,最多可上传") + n.maxFileCount + window.$l("张")
  277. );
  278. return;
  279. }
  280. let l = t;
  281. if (n.crop) {
  282. l = [];
  283. for (let a = 0; a < t.length; a++) {
  284. const o = yield fe(t[a], a + 1, t.length);
  285. if (!o) continue;
  286. const p = ge(o, t[a]);
  287. if (p.size > n.fileSize * 1024) {
  288. window.$message.warning(`${window.$l("上传图片大小不能大于")}${n.fileSize}KB`);
  289. continue;
  290. }
  291. l.push(p);
  292. }
  293. if (!l.length) return;
  294. } else
  295. for (let a = 0; a < t.length; a++)
  296. if (t[a].size > n.fileSize * 1024) {
  297. window.$message.warning(`${window.$l("上传图片大小不能大于")}${n.fileSize}KB`);
  298. return;
  299. }
  300. Fe(C(), l).then((a) => {
  301. if (a && a.length)
  302. for (let o = 0; o < a.length; o++)
  303. s.value.push({
  304. sort: o,
  305. name: l[o] && l[o].name || G(a[o].imageUrl),
  306. imageUrl: a[o].imageUrl,
  307. uid: a[o].imageUrl,
  308. url: a[o].imageUrl
  309. });
  310. P();
  311. });
  312. }), ye = (e) => {
  313. c.value && (e.showTool = !0);
  314. }, he = (e) => {
  315. c.value && (e.showTool = !1);
  316. }, J = (e, t) => {
  317. s.value.splice(e, 1), Re(C(), t.imageUrl || t.uid), P();
  318. }, Q = (e = {}) => {
  319. const t = S(e);
  320. if (!t) return;
  321. if (!H(e)) {
  322. window.open(t, "_blank", "noopener");
  323. return;
  324. }
  325. let l = s.value.findIndex((a) => a === e);
  326. l < 0 && (l = s.value.findIndex((a) => S(a) === t)), X.value = l > -1 ? l : 0, A.value = !0;
  327. }, Y = (...t) => K(this, [...t], function* (e = {}) {
  328. const l = S(e);
  329. if (!l) return;
  330. g("download", e);
  331. const a = U(e), o = (p, V) => {
  332. const f = document.createElement("a");
  333. f.href = p, f.download = a, V || (f.target = "_blank", f.rel = "noopener"), document.body.appendChild(f), f.click(), document.body.removeChild(f);
  334. };
  335. try {
  336. const p = yield fetch(l);
  337. if (!p.ok) throw new Error("fetch failed");
  338. const V = yield p.blob(), f = URL.createObjectURL(V);
  339. o(f, !0), setTimeout(() => URL.revokeObjectURL(f), 1e3);
  340. } catch (p) {
  341. o(l, !1);
  342. }
  343. });
  344. return (e, t) => (i(), w("div", {
  345. class: I(["s-file-upload-common", [`is-${r.listType}`, { "is-readonly": k.value }]])
  346. }, [
  347. r.label ? (i(), w("div", Me, [
  348. y("div", Te, B(r.label), 1)
  349. ])) : u("", !0),
  350. D(v(Oe), {
  351. modelValue: s.value,
  352. "onUpdate:modelValue": t[0] || (t[0] = (l) => s.value = l),
  353. onStart: ce,
  354. onEnd: de,
  355. handle: ".move",
  356. class: I(c.value ? "img-list-wrap" : "file-list-wrap"),
  357. style: ke(r.picStyle),
  358. disabled: !b.value,
  359. "item-key": "uid"
  360. }, {
  361. item: ee(({ element: l, index: a }) => [
  362. (i(), w("div", {
  363. key: l.uid || a,
  364. class: I(c.value ? "img-item-wrap" : "file-row"),
  365. onMouseover: (o) => ye(l),
  366. onMouseleave: (o) => he(l)
  367. }, [
  368. c.value ? (i(), w(le, { key: 0 }, [
  369. H(l) ? (i(), w("img", {
  370. key: 0,
  371. class: "upload-img-wrap",
  372. src: S(l),
  373. alt: U(l)
  374. }, null, 8, De)) : (i(), w("div", je, [
  375. (i(), m(te(W(l)), { class: "file-thumb-icon" })),
  376. y("span", Ne, B(z(l) || "FILE"), 1)
  377. ])),
  378. Ce(y("div", _e, [
  379. y("div", Ae, [
  380. r.showPreview ? (i(), m(v(ae), {
  381. key: 0,
  382. class: "tool-icon-wrap",
  383. title: e.$l("预览"),
  384. onClick: O((o) => Q(l), ["stop"])
  385. }, null, 8, ["title", "onClick"])) : u("", !0),
  386. r.showDownload ? (i(), m(v(oe), {
  387. key: 1,
  388. class: "tool-icon-wrap",
  389. title: e.$l("下载"),
  390. onClick: O((o) => Y(l), ["stop"])
  391. }, null, 8, ["title", "onClick"])) : u("", !0),
  392. d.value ? (i(), m(v(ne), {
  393. key: 2,
  394. class: "tool-icon-wrap",
  395. title: e.$l("删除"),
  396. onClick: O((o) => J(a, l), ["stop"])
  397. }, null, 8, ["title", "onClick"])) : u("", !0)
  398. ]),
  399. y("div", Pe, [
  400. b.value ? (i(), m(v(ie), { key: 0 })) : u("", !0)
  401. ])
  402. ], 512), [
  403. [$e, l.showTool]
  404. ])
  405. ], 64)) : (i(), w(le, { key: 1 }, [
  406. (i(), m(te(W(l)), { class: "file-row-icon" })),
  407. y("span", Ve, B(z(l) || "FILE"), 1),
  408. y("span", {
  409. class: "file-row-name",
  410. title: U(l)
  411. }, B(U(l)), 9, Ke),
  412. y("span", qe, [
  413. r.showPreview ? (i(), m(v(ae), {
  414. key: 0,
  415. class: "file-action",
  416. title: e.$l("预览"),
  417. onClick: O((o) => Q(l), ["stop"])
  418. }, null, 8, ["title", "onClick"])) : u("", !0),
  419. r.showDownload ? (i(), m(v(oe), {
  420. key: 1,
  421. class: "file-action",
  422. title: e.$l("下载"),
  423. onClick: O((o) => Y(l), ["stop"])
  424. }, null, 8, ["title", "onClick"])) : u("", !0),
  425. d.value ? (i(), m(v(ne), {
  426. key: 2,
  427. class: "file-action",
  428. title: e.$l("删除"),
  429. onClick: O((o) => J(a, l), ["stop"])
  430. }, null, 8, ["title", "onClick"])) : u("", !0),
  431. b.value ? (i(), m(v(ie), {
  432. key: 3,
  433. class: "file-action move",
  434. title: e.$l("拖动排序")
  435. }, null, 8, ["title"])) : u("", !0)
  436. ])
  437. ], 64))
  438. ], 42, ze))
  439. ]),
  440. footer: ee(() => [
  441. s.value.length < r.maxFileCount && r.isAdd && !k.value ? (i(), w("div", {
  442. key: 0,
  443. class: I(c.value ? "img-add-wrap" : "file-add-wrap"),
  444. onClick: pe
  445. }, [
  446. D(v(Ie), {
  447. class: I(c.value ? "plus-icon" : "plus-icon plus-icon-small")
  448. }, null, 8, ["class"]),
  449. c.value ? u("", !0) : (i(), w("span", Ge, B(e.$l("上传文件")), 1)),
  450. y("input", {
  451. accept: r.accept || "image/*",
  452. multiple: !0,
  453. type: "file",
  454. disabled: T.value,
  455. ref_key: "fileInput",
  456. ref: F,
  457. style: { display: "none" },
  458. onChange: we
  459. }, null, 40, He)
  460. ], 2)) : u("", !0)
  461. ]),
  462. _: 1
  463. }, 8, ["modelValue", "class", "style", "disabled"]),
  464. D(Se, {
  465. value: A.value,
  466. "onUpdate:value": t[1] || (t[1] = (l) => A.value = l),
  467. list: s.value,
  468. active: X.value
  469. }, null, 8, ["value", "list", "active"]),
  470. D(Ue, {
  471. ref_key: "cropperRef",
  472. ref: Z,
  473. onCropperOk: ve,
  474. onCropperNo: me
  475. }, null, 512)
  476. ], 2));
  477. }
  478. }, tl = /* @__PURE__ */ xe(We, [["__scopeId", "data-v-64e419f4"]]);
  479. export {
  480. tl as default
  481. };