SCopperUpload.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. var K = (r, N, 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, N)).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, al as Ie, am as ae, an as oe, ao as ne, ap as ie, aq as E, ar as L, as as h, at as Be, au as j, av as M, aw as se, ax as q, ay as A, az as Ee, aA 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. }, Ae = { class: "file-thumb-ext" }, Ne = { class: "tool-wrap" }, _e = { 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. // 业务主键,随上传一起提交用于关联业务数据
  163. businessKey: {
  164. type: String,
  165. default: void 0
  166. },
  167. // 兼容保留(PictureUpload 无上传列表概念,仅声明避免外部传参告警)
  168. showUploadList: {
  169. type: Boolean,
  170. default: !0
  171. },
  172. // 已废弃:由 showRemove 取代,仅保留声明避免外部传参告警
  173. showRemoveIcon: {
  174. type: Boolean,
  175. default: !1
  176. }
  177. },
  178. emits: ["change", "handChange", "dragChange", "download"],
  179. setup(r, { emit: N }) {
  180. const g = N, 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 = {
  181. pdf: Ee,
  182. doc: A,
  183. docx: A,
  184. wps: A,
  185. rtf: A,
  186. xls: q,
  187. xlsx: q,
  188. csv: q,
  189. ppt: se,
  190. pptx: se,
  191. zip: M,
  192. rar: M,
  193. "7z": M,
  194. gz: M,
  195. tar: M,
  196. txt: j,
  197. log: j,
  198. json: j,
  199. xml: j,
  200. md: Be,
  201. png: h,
  202. jpg: h,
  203. jpeg: h,
  204. gif: h,
  205. webp: h,
  206. bmp: h,
  207. svg: h,
  208. ico: h,
  209. mp4: L,
  210. mov: L,
  211. avi: L,
  212. mkv: L,
  213. webm: L,
  214. mp3: E,
  215. wav: E,
  216. flac: E,
  217. aac: E,
  218. m4a: E
  219. }, S = (e = {}) => e.imageUrl || e.url || e.uid || "", G = (e = "") => {
  220. const t = String(e).split("?")[0].split("#")[0], l = t.substring(t.lastIndexOf("/") + 1);
  221. try {
  222. return decodeURIComponent(l);
  223. } catch (a) {
  224. return l;
  225. }
  226. }, U = (e = {}) => e.name || G(S(e)) || window.$l("未命名文件"), z = (e = {}) => {
  227. const t = U(e), l = t.lastIndexOf(".");
  228. return l > -1 ? t.slice(l + 1).toLowerCase() : "";
  229. }, H = (e = {}) => {
  230. const t = e.type || e.mimeType || "";
  231. return t ? String(t).indexOf("image/") === 0 : re.includes(z(e));
  232. }, W = (e = {}) => ue[z(e)] || Le, s = x([]), _ = x(!1), X = x(0);
  233. be(
  234. () => n.list,
  235. (e) => {
  236. s.value = e || [];
  237. },
  238. { immediate: !0, deep: !0 }
  239. );
  240. const P = () => {
  241. g("change", s.value), g("handChange", s.value);
  242. }, ce = (e) => {
  243. }, de = (e) => {
  244. s.value && s.value.length > 1 && s.value.forEach((t, l) => {
  245. t.sort = l;
  246. }), g("dragChange", s.value), P();
  247. }, pe = () => {
  248. F.value && F.value.click();
  249. }, F = x(null), Z = x(null);
  250. let $ = null;
  251. const fe = (e, t, l) => {
  252. const a = URL.createObjectURL(e);
  253. return new Promise((o) => {
  254. $ = (p) => {
  255. $ = null, URL.revokeObjectURL(a), o(p);
  256. }, Z.value.edit({
  257. img: a,
  258. aspectRatio: n.aspectRatio,
  259. title: l > 1 ? `${window.$l("图片裁剪")}(${t}/${l})` : window.$l("图片裁剪"),
  260. // png 保留 png(可能有透明背景),其余走 jpeg 控制体积
  261. outputType: e.type === "image/png" ? "png" : "jpeg",
  262. file: e
  263. });
  264. });
  265. }, ve = (e) => {
  266. $ && $(e && e.blob || null);
  267. }, me = () => {
  268. $ && $(null);
  269. }, ge = (e, t) => {
  270. const l = e.type || t.type || "image/png", a = l === "image/png" ? "png" : "jpg", o = (t.name || "image").replace(/\.[^.]+$/, "");
  271. return new File([e], `${o}.${a}`, { type: l, lastModified: Date.now() });
  272. }, we = (e) => K(this, null, function* () {
  273. const t = Array.from(e && e.target && e.target.files || []);
  274. if (F.value && (F.value.value = ""), !t.length) return;
  275. if (s.value.length + t.length > n.maxFileCount) {
  276. window.$message.warning(
  277. window.$l("上传图片,最多可上传") + n.maxFileCount + window.$l("张")
  278. );
  279. return;
  280. }
  281. let l = t;
  282. if (n.crop) {
  283. l = [];
  284. for (let a = 0; a < t.length; a++) {
  285. const o = yield fe(t[a], a + 1, t.length);
  286. if (!o) continue;
  287. const p = ge(o, t[a]);
  288. if (p.size > n.fileSize * 1024) {
  289. window.$message.warning(`${window.$l("上传图片大小不能大于")}${n.fileSize}KB`);
  290. continue;
  291. }
  292. l.push(p);
  293. }
  294. if (!l.length) return;
  295. } else
  296. for (let a = 0; a < t.length; a++)
  297. if (t[a].size > n.fileSize * 1024) {
  298. window.$message.warning(`${window.$l("上传图片大小不能大于")}${n.fileSize}KB`);
  299. return;
  300. }
  301. Fe(C(), l).then((a) => {
  302. if (a && a.length)
  303. for (let o = 0; o < a.length; o++)
  304. s.value.push({
  305. sort: o,
  306. name: l[o] && l[o].name || G(a[o].imageUrl),
  307. imageUrl: a[o].imageUrl,
  308. uid: a[o].imageUrl,
  309. url: a[o].imageUrl
  310. });
  311. P();
  312. });
  313. }), ye = (e) => {
  314. c.value && (e.showTool = !0);
  315. }, he = (e) => {
  316. c.value && (e.showTool = !1);
  317. }, J = (e, t) => {
  318. s.value.splice(e, 1), Re(C(), t.imageUrl || t.uid), P();
  319. }, Q = (e = {}) => {
  320. const t = S(e);
  321. if (!t) return;
  322. if (!H(e)) {
  323. window.open(t, "_blank", "noopener");
  324. return;
  325. }
  326. let l = s.value.findIndex((a) => a === e);
  327. l < 0 && (l = s.value.findIndex((a) => S(a) === t)), X.value = l > -1 ? l : 0, _.value = !0;
  328. }, Y = (...t) => K(this, [...t], function* (e = {}) {
  329. const l = S(e);
  330. if (!l) return;
  331. g("download", e);
  332. const a = U(e), o = (p, V) => {
  333. const f = document.createElement("a");
  334. f.href = p, f.download = a, V || (f.target = "_blank", f.rel = "noopener"), document.body.appendChild(f), f.click(), document.body.removeChild(f);
  335. };
  336. try {
  337. const p = yield fetch(l);
  338. if (!p.ok) throw new Error("fetch failed");
  339. const V = yield p.blob(), f = URL.createObjectURL(V);
  340. o(f, !0), setTimeout(() => URL.revokeObjectURL(f), 1e3);
  341. } catch (p) {
  342. o(l, !1);
  343. }
  344. });
  345. return (e, t) => (i(), w("div", {
  346. class: I(["s-file-upload-common", [`is-${r.listType}`, { "is-readonly": k.value }]])
  347. }, [
  348. r.label ? (i(), w("div", Me, [
  349. y("div", Te, B(r.label), 1)
  350. ])) : u("", !0),
  351. D(v(Oe), {
  352. modelValue: s.value,
  353. "onUpdate:modelValue": t[0] || (t[0] = (l) => s.value = l),
  354. onStart: ce,
  355. onEnd: de,
  356. handle: ".move",
  357. class: I(c.value ? "img-list-wrap" : "file-list-wrap"),
  358. style: ke(r.picStyle),
  359. disabled: !b.value,
  360. "item-key": "uid"
  361. }, {
  362. item: ee(({ element: l, index: a }) => [
  363. (i(), w("div", {
  364. key: l.uid || a,
  365. class: I(c.value ? "img-item-wrap" : "file-row"),
  366. onMouseover: (o) => ye(l),
  367. onMouseleave: (o) => he(l)
  368. }, [
  369. c.value ? (i(), w(le, { key: 0 }, [
  370. H(l) ? (i(), w("img", {
  371. key: 0,
  372. class: "upload-img-wrap",
  373. src: S(l),
  374. alt: U(l)
  375. }, null, 8, De)) : (i(), w("div", je, [
  376. (i(), m(te(W(l)), { class: "file-thumb-icon" })),
  377. y("span", Ae, B(z(l) || "FILE"), 1)
  378. ])),
  379. Ce(y("div", Ne, [
  380. y("div", _e, [
  381. r.showPreview ? (i(), m(v(ae), {
  382. key: 0,
  383. class: "tool-icon-wrap",
  384. title: e.$l("预览"),
  385. onClick: O((o) => Q(l), ["stop"])
  386. }, null, 8, ["title", "onClick"])) : u("", !0),
  387. r.showDownload ? (i(), m(v(oe), {
  388. key: 1,
  389. class: "tool-icon-wrap",
  390. title: e.$l("下载"),
  391. onClick: O((o) => Y(l), ["stop"])
  392. }, null, 8, ["title", "onClick"])) : u("", !0),
  393. d.value ? (i(), m(v(ne), {
  394. key: 2,
  395. class: "tool-icon-wrap",
  396. title: e.$l("删除"),
  397. onClick: O((o) => J(a, l), ["stop"])
  398. }, null, 8, ["title", "onClick"])) : u("", !0)
  399. ]),
  400. y("div", Pe, [
  401. b.value ? (i(), m(v(ie), { key: 0 })) : u("", !0)
  402. ])
  403. ], 512), [
  404. [$e, l.showTool]
  405. ])
  406. ], 64)) : (i(), w(le, { key: 1 }, [
  407. (i(), m(te(W(l)), { class: "file-row-icon" })),
  408. y("span", Ve, B(z(l) || "FILE"), 1),
  409. y("span", {
  410. class: "file-row-name",
  411. title: U(l)
  412. }, B(U(l)), 9, Ke),
  413. y("span", qe, [
  414. r.showPreview ? (i(), m(v(ae), {
  415. key: 0,
  416. class: "file-action",
  417. title: e.$l("预览"),
  418. onClick: O((o) => Q(l), ["stop"])
  419. }, null, 8, ["title", "onClick"])) : u("", !0),
  420. r.showDownload ? (i(), m(v(oe), {
  421. key: 1,
  422. class: "file-action",
  423. title: e.$l("下载"),
  424. onClick: O((o) => Y(l), ["stop"])
  425. }, null, 8, ["title", "onClick"])) : u("", !0),
  426. d.value ? (i(), m(v(ne), {
  427. key: 2,
  428. class: "file-action",
  429. title: e.$l("删除"),
  430. onClick: O((o) => J(a, l), ["stop"])
  431. }, null, 8, ["title", "onClick"])) : u("", !0),
  432. b.value ? (i(), m(v(ie), {
  433. key: 3,
  434. class: "file-action move",
  435. title: e.$l("拖动排序")
  436. }, null, 8, ["title"])) : u("", !0)
  437. ])
  438. ], 64))
  439. ], 42, ze))
  440. ]),
  441. footer: ee(() => [
  442. s.value.length < r.maxFileCount && r.isAdd && !k.value ? (i(), w("div", {
  443. key: 0,
  444. class: I(c.value ? "img-add-wrap" : "file-add-wrap"),
  445. onClick: pe
  446. }, [
  447. D(v(Ie), {
  448. class: I(c.value ? "plus-icon" : "plus-icon plus-icon-small")
  449. }, null, 8, ["class"]),
  450. c.value ? u("", !0) : (i(), w("span", Ge, B(e.$l("上传文件")), 1)),
  451. y("input", {
  452. accept: r.accept || "image/*",
  453. multiple: !0,
  454. type: "file",
  455. disabled: T.value,
  456. ref_key: "fileInput",
  457. ref: F,
  458. style: { display: "none" },
  459. onChange: we
  460. }, null, 40, He)
  461. ], 2)) : u("", !0)
  462. ]),
  463. _: 1
  464. }, 8, ["modelValue", "class", "style", "disabled"]),
  465. D(Se, {
  466. value: _.value,
  467. "onUpdate:value": t[1] || (t[1] = (l) => _.value = l),
  468. list: s.value,
  469. active: X.value
  470. }, null, 8, ["value", "list", "active"]),
  471. D(Ue, {
  472. ref_key: "cropperRef",
  473. ref: Z,
  474. onCropperOk: ve,
  475. onCropperNo: me
  476. }, null, 512)
  477. ], 2));
  478. }
  479. }, tl = /* @__PURE__ */ xe(We, [["__scopeId", "data-v-4482a9a8"]]);
  480. export {
  481. tl as default
  482. };