u-upload.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. "use strict";
  2. const common_vendor = require("../../../../common/vendor.js");
  3. const _sfc_main = {
  4. name: "u-upload",
  5. mixins: [common_vendor.mpMixin, common_vendor.mixin, common_vendor.mixinUp, common_vendor.props$9],
  6. data() {
  7. return {
  8. lists: [],
  9. isInCount: true
  10. };
  11. },
  12. watch: {
  13. // 监听文件列表的变化,重新整理内部数据
  14. fileList: {
  15. handler() {
  16. this.formatFileList();
  17. },
  18. immediate: true,
  19. deep: true
  20. }
  21. },
  22. emits: ["error", "beforeRead", "oversize", "afterRead", "delete", "clickPreview"],
  23. methods: {
  24. addUnit: common_vendor.addUnit,
  25. addStyle: common_vendor.addStyle,
  26. formatFileList() {
  27. const {
  28. fileList = [],
  29. maxCount
  30. } = this;
  31. const lists = fileList.map(
  32. (item) => Object.assign(Object.assign({}, item), {
  33. // 如果item.url为本地选择的blob文件的话,无法判断其为video还是image,此处优先通过accept做判断处理
  34. isImage: this.accept === "image" || common_vendor.test.image(item.url || item.thumb),
  35. isVideo: this.accept === "video" || common_vendor.test.video(item.url || item.thumb),
  36. deletable: typeof item.deletable === "boolean" ? item.deletable : this.deletable
  37. })
  38. );
  39. this.lists = lists;
  40. this.isInCount = lists.length < maxCount;
  41. },
  42. chooseFile() {
  43. const {
  44. maxCount,
  45. multiple,
  46. lists,
  47. disabled
  48. } = this;
  49. if (disabled)
  50. return;
  51. let capture;
  52. try {
  53. capture = common_vendor.test.array(this.capture) ? this.capture : this.capture.split(",");
  54. } catch (e) {
  55. capture = [];
  56. }
  57. common_vendor.chooseFile(
  58. Object.assign({
  59. accept: this.accept,
  60. multiple: this.multiple,
  61. capture,
  62. compressed: this.compressed,
  63. maxDuration: this.maxDuration,
  64. sizeType: this.sizeType,
  65. camera: this.camera
  66. }, {
  67. maxCount: maxCount - lists.length
  68. })
  69. ).then((res) => {
  70. this.onBeforeRead(multiple ? res : res[0]);
  71. }).catch((error) => {
  72. this.$emit("error", error);
  73. });
  74. },
  75. // 文件读取之前
  76. onBeforeRead(file) {
  77. const {
  78. beforeRead,
  79. useBeforeRead
  80. } = this;
  81. let res = true;
  82. if (common_vendor.test.func(beforeRead)) {
  83. res = beforeRead(file, this.getDetail());
  84. }
  85. if (useBeforeRead) {
  86. res = new Promise((resolve, reject) => {
  87. this.$emit(
  88. "beforeRead",
  89. Object.assign(Object.assign({
  90. file
  91. }, this.getDetail()), {
  92. callback: (ok) => {
  93. ok ? resolve() : reject();
  94. }
  95. })
  96. );
  97. });
  98. }
  99. if (!res) {
  100. return;
  101. }
  102. if (common_vendor.test.promise(res)) {
  103. res.then((data) => this.onAfterRead(data || file));
  104. } else {
  105. this.onAfterRead(file);
  106. }
  107. },
  108. getDetail(index) {
  109. return {
  110. name: this.name,
  111. index: index == null ? this.fileList.length : index
  112. };
  113. },
  114. onAfterRead(file) {
  115. const {
  116. maxSize,
  117. afterRead
  118. } = this;
  119. const oversize = Array.isArray(file) ? file.some((item) => item.size > maxSize) : file.size > maxSize;
  120. if (oversize) {
  121. this.$emit("oversize", Object.assign({
  122. file
  123. }, this.getDetail()));
  124. return;
  125. }
  126. if (typeof afterRead === "function") {
  127. afterRead(file, this.getDetail());
  128. }
  129. this.$emit("afterRead", Object.assign({
  130. file
  131. }, this.getDetail()));
  132. },
  133. deleteItem(index) {
  134. this.$emit(
  135. "delete",
  136. Object.assign(Object.assign({}, this.getDetail(index)), {
  137. file: this.fileList[index]
  138. })
  139. );
  140. },
  141. // 预览图片
  142. onPreviewImage(item) {
  143. if (!item.isImage || !this.previewFullImage)
  144. return;
  145. common_vendor.index.previewImage({
  146. // 先filter找出为图片的item,再返回filter结果中的图片url
  147. urls: this.lists.filter((item2) => this.accept === "image" || common_vendor.test.image(item2.url || item2.thumb)).map((item2) => item2.url || item2.thumb),
  148. current: item.url || item.thumb,
  149. fail() {
  150. common_vendor.toast("预览图片失败");
  151. }
  152. });
  153. },
  154. onPreviewVideo(event) {
  155. if (!this.data.previewFullImage)
  156. return;
  157. const {
  158. index
  159. } = event.currentTarget.dataset;
  160. const {
  161. lists
  162. } = this.data;
  163. common_vendor.wx$1.previewMedia({
  164. sources: lists.filter((item) => isVideoFile(item)).map(
  165. (item) => Object.assign(Object.assign({}, item), {
  166. type: "video"
  167. })
  168. ),
  169. current: index,
  170. fail() {
  171. common_vendor.toast("预览视频失败");
  172. }
  173. });
  174. },
  175. onClickPreview(event) {
  176. const {
  177. index
  178. } = event.currentTarget.dataset;
  179. const item = this.data.lists[index];
  180. if (!this.data.previewFullImage)
  181. return;
  182. switch (item.type) {
  183. case "video":
  184. this.onPreviewVideo(event);
  185. break;
  186. }
  187. this.$emit(
  188. "clickPreview",
  189. Object.assign(Object.assign({}, item), this.getDetail(index))
  190. );
  191. }
  192. }
  193. };
  194. if (!Array) {
  195. const _easycom_u_icon2 = common_vendor.resolveComponent("u-icon");
  196. const _easycom_u_loading_icon2 = common_vendor.resolveComponent("u-loading-icon");
  197. (_easycom_u_icon2 + _easycom_u_loading_icon2)();
  198. }
  199. const _easycom_u_icon = () => "../u-icon/u-icon.js";
  200. const _easycom_u_loading_icon = () => "../u-loading-icon/u-loading-icon.js";
  201. if (!Math) {
  202. (_easycom_u_icon + _easycom_u_loading_icon)();
  203. }
  204. function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
  205. return common_vendor.e({
  206. a: _ctx.previewImage
  207. }, _ctx.previewImage ? {
  208. b: common_vendor.f($data.lists, (item, index, i0) => {
  209. return common_vendor.e({
  210. a: item.isImage || item.type && item.type === "image"
  211. }, item.isImage || item.type && item.type === "image" ? {
  212. b: item.thumb || item.url,
  213. c: _ctx.imageMode,
  214. d: common_vendor.o(($event) => $options.onPreviewImage(item), index),
  215. e: common_vendor.s({
  216. width: $options.addUnit(_ctx.width),
  217. height: $options.addUnit(_ctx.height)
  218. })
  219. } : {
  220. f: "cafe0b2a-0-" + i0,
  221. g: common_vendor.p({
  222. color: "#80CBF9",
  223. size: "26",
  224. name: item.isVideo || item.type && item.type === "video" ? "movie" : "folder"
  225. }),
  226. h: common_vendor.t(item.isVideo || item.type && item.type === "video" ? "视频" : "文件"),
  227. i: common_vendor.o(($event) => $options.onClickPreview($event, item), index)
  228. }, {
  229. j: item.status === "uploading" || item.status === "failed"
  230. }, item.status === "uploading" || item.status === "failed" ? common_vendor.e({
  231. k: item.status === "failed"
  232. }, item.status === "failed" ? {
  233. l: "cafe0b2a-1-" + i0,
  234. m: common_vendor.p({
  235. name: "close-circle",
  236. color: "#ffffff",
  237. size: "25"
  238. })
  239. } : {
  240. n: "cafe0b2a-2-" + i0,
  241. o: common_vendor.p({
  242. size: "22",
  243. mode: "circle",
  244. color: "#ffffff"
  245. })
  246. }, {
  247. p: item.message
  248. }, item.message ? {
  249. q: common_vendor.t(item.message)
  250. } : {}) : {}, {
  251. r: item.status !== "uploading" && (_ctx.deletable || item.deletable)
  252. }, item.status !== "uploading" && (_ctx.deletable || item.deletable) ? {
  253. s: "cafe0b2a-3-" + i0,
  254. t: common_vendor.p({
  255. name: "close",
  256. color: "#ffffff",
  257. size: "10"
  258. }),
  259. v: common_vendor.o(($event) => $options.deleteItem(index), index)
  260. } : {}, {
  261. w: item.status === "success"
  262. }, item.status === "success" ? {
  263. x: "cafe0b2a-4-" + i0,
  264. y: common_vendor.p({
  265. name: "checkmark",
  266. color: "#ffffff",
  267. size: "12"
  268. })
  269. } : {}, {
  270. z: index
  271. });
  272. })
  273. } : {}, {
  274. c: $data.isInCount
  275. }, $data.isInCount ? common_vendor.e({
  276. d: _ctx.$slots.default || _ctx.$slots.$default
  277. }, _ctx.$slots.default || _ctx.$slots.$default ? {
  278. e: common_vendor.o((...args) => $options.chooseFile && $options.chooseFile(...args))
  279. } : common_vendor.e({
  280. f: common_vendor.p({
  281. name: _ctx.uploadIcon,
  282. size: "26",
  283. color: _ctx.uploadIconColor
  284. }),
  285. g: _ctx.uploadText
  286. }, _ctx.uploadText ? {
  287. h: common_vendor.t(_ctx.uploadText)
  288. } : {}, {
  289. i: !_ctx.disabled ? "u-upload__button--hover" : "",
  290. j: common_vendor.o((...args) => $options.chooseFile && $options.chooseFile(...args)),
  291. k: common_vendor.n(_ctx.disabled && "u-upload__button--disabled"),
  292. l: common_vendor.s({
  293. width: $options.addUnit(_ctx.width),
  294. height: $options.addUnit(_ctx.height)
  295. })
  296. })) : {}, {
  297. m: common_vendor.s($options.addStyle(_ctx.customStyle))
  298. });
  299. }
  300. const Component = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["render", _sfc_render], ["__scopeId", "data-v-cafe0b2a"], ["__file", "D:/project/学吧/learn_applet/node_modules/uview-plus/components/u-upload/u-upload.vue"]]);
  301. wx.createComponent(Component);