index.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. "use strict";
  2. var __assign = (this && this.__assign) || function () {
  3. __assign = Object.assign || function(t) {
  4. for (var s, i = 1, n = arguments.length; i < n; i++) {
  5. s = arguments[i];
  6. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
  7. t[p] = s[p];
  8. }
  9. return t;
  10. };
  11. return __assign.apply(this, arguments);
  12. };
  13. Object.defineProperty(exports, "__esModule", { value: true });
  14. var component_1 = require("../common/component");
  15. var utils_1 = require("./utils");
  16. var shared_1 = require("./shared");
  17. var validator_1 = require("../common/validator");
  18. (0, component_1.VantComponent)({
  19. props: __assign(__assign(__assign(__assign({ disabled: Boolean, multiple: Boolean, uploadText: String, useBeforeRead: Boolean, afterRead: null, beforeRead: null, previewSize: {
  20. type: null,
  21. value: 80,
  22. }, name: {
  23. type: null,
  24. value: '',
  25. }, accept: {
  26. type: String,
  27. value: 'image',
  28. }, fileList: {
  29. type: Array,
  30. value: [],
  31. observer: 'formatFileList',
  32. }, maxSize: {
  33. type: Number,
  34. value: Number.MAX_VALUE,
  35. }, maxCount: {
  36. type: Number,
  37. value: 100,
  38. }, deletable: {
  39. type: Boolean,
  40. value: true,
  41. }, showUpload: {
  42. type: Boolean,
  43. value: true,
  44. }, previewImage: {
  45. type: Boolean,
  46. value: true,
  47. }, previewFullImage: {
  48. type: Boolean,
  49. value: true,
  50. }, videoFit: {
  51. type: String,
  52. value: 'contain',
  53. }, imageFit: {
  54. type: String,
  55. value: 'scaleToFill',
  56. }, uploadIcon: {
  57. type: String,
  58. value: 'photograph',
  59. } }, shared_1.imageProps), shared_1.videoProps), shared_1.mediaProps), shared_1.messageFileProps),
  60. data: {
  61. lists: [],
  62. isInCount: true,
  63. },
  64. methods: {
  65. formatFileList: function () {
  66. var _a = this.data, _b = _a.fileList, fileList = _b === void 0 ? [] : _b, maxCount = _a.maxCount;
  67. var lists = fileList.map(function (item) { return (__assign(__assign({}, item), { isImage: (0, utils_1.isImageFile)(item), isVideo: (0, utils_1.isVideoFile)(item), deletable: (0, validator_1.isBoolean)(item.deletable) ? item.deletable : true })); });
  68. this.setData({ lists: lists, isInCount: lists.length < maxCount });
  69. },
  70. getDetail: function (index) {
  71. return {
  72. name: this.data.name,
  73. index: index == null ? this.data.fileList.length : index,
  74. };
  75. },
  76. startUpload: function () {
  77. var _this = this;
  78. var _a = this.data, maxCount = _a.maxCount, multiple = _a.multiple, lists = _a.lists, disabled = _a.disabled;
  79. if (disabled)
  80. return;
  81. (0, utils_1.chooseFile)(__assign(__assign({}, this.data), { maxCount: maxCount - lists.length }))
  82. .then(function (res) {
  83. _this.onBeforeRead(multiple ? res : res[0]);
  84. })
  85. .catch(function (error) {
  86. _this.$emit('error', error);
  87. });
  88. },
  89. onBeforeRead: function (file) {
  90. var _this = this;
  91. var _a = this.data, beforeRead = _a.beforeRead, useBeforeRead = _a.useBeforeRead;
  92. var res = true;
  93. if (typeof beforeRead === 'function') {
  94. res = beforeRead(file, this.getDetail());
  95. }
  96. if (useBeforeRead) {
  97. res = new Promise(function (resolve, reject) {
  98. _this.$emit('before-read', __assign(__assign({ file: file }, _this.getDetail()), { callback: function (ok) {
  99. ok ? resolve() : reject();
  100. } }));
  101. });
  102. }
  103. if (!res) {
  104. return;
  105. }
  106. if ((0, validator_1.isPromise)(res)) {
  107. res.then(function (data) { return _this.onAfterRead(data || file); });
  108. }
  109. else {
  110. this.onAfterRead(file);
  111. }
  112. },
  113. onAfterRead: function (file) {
  114. var _a = this.data, maxSize = _a.maxSize, afterRead = _a.afterRead;
  115. var oversize = Array.isArray(file)
  116. ? file.some(function (item) { return item.size > maxSize; })
  117. : file.size > maxSize;
  118. if (oversize) {
  119. this.$emit('oversize', __assign({ file: file }, this.getDetail()));
  120. return;
  121. }
  122. if (typeof afterRead === 'function') {
  123. afterRead(file, this.getDetail());
  124. }
  125. this.$emit('after-read', __assign({ file: file }, this.getDetail()));
  126. },
  127. deleteItem: function (event) {
  128. var index = event.currentTarget.dataset.index;
  129. this.$emit('delete', __assign(__assign({}, this.getDetail(index)), { file: this.data.fileList[index] }));
  130. },
  131. onPreviewImage: function (event) {
  132. if (!this.data.previewFullImage)
  133. return;
  134. var index = event.currentTarget.dataset.index;
  135. var _a = this.data, lists = _a.lists, showmenu = _a.showmenu;
  136. var item = lists[index];
  137. wx.previewImage({
  138. urls: lists.filter(function (item) { return (0, utils_1.isImageFile)(item); }).map(function (item) { return item.url; }),
  139. current: item.url,
  140. showmenu: showmenu,
  141. fail: function () {
  142. wx.showToast({ title: '预览图片失败', icon: 'none' });
  143. },
  144. });
  145. },
  146. onPreviewVideo: function (event) {
  147. if (!this.data.previewFullImage)
  148. return;
  149. var index = event.currentTarget.dataset.index;
  150. var lists = this.data.lists;
  151. var sources = [];
  152. var current = lists.reduce(function (sum, cur, curIndex) {
  153. if (!(0, utils_1.isVideoFile)(cur)) {
  154. return sum;
  155. }
  156. sources.push(__assign(__assign({}, cur), { type: 'video' }));
  157. if (curIndex < index) {
  158. sum++;
  159. }
  160. return sum;
  161. }, 0);
  162. wx.previewMedia({
  163. sources: sources,
  164. current: current,
  165. fail: function () {
  166. wx.showToast({ title: '预览视频失败', icon: 'none' });
  167. },
  168. });
  169. },
  170. onPreviewFile: function (event) {
  171. var index = event.currentTarget.dataset.index;
  172. wx.openDocument({
  173. filePath: this.data.lists[index].url,
  174. showMenu: true,
  175. });
  176. },
  177. onClickPreview: function (event) {
  178. var index = event.currentTarget.dataset.index;
  179. var item = this.data.lists[index];
  180. this.$emit('click-preview', __assign(__assign({}, item), this.getDetail(index)));
  181. },
  182. },
  183. });