index.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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({ 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. }, imageFit: {
  51. type: String,
  52. value: 'scaleToFill',
  53. }, uploadIcon: {
  54. type: String,
  55. value: 'photograph',
  56. } }, shared_1.chooseImageProps), shared_1.chooseVideoProps),
  57. data: {
  58. lists: [],
  59. isInCount: true,
  60. },
  61. methods: {
  62. formatFileList: function () {
  63. var _a = this.data, _b = _a.fileList, fileList = _b === void 0 ? [] : _b, maxCount = _a.maxCount;
  64. 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 })); });
  65. this.setData({ lists: lists, isInCount: lists.length < maxCount });
  66. },
  67. getDetail: function (index) {
  68. return {
  69. name: this.data.name,
  70. index: index == null ? this.data.fileList.length : index,
  71. };
  72. },
  73. startUpload: function () {
  74. var _this = this;
  75. var _a = this.data, maxCount = _a.maxCount, multiple = _a.multiple, lists = _a.lists, disabled = _a.disabled;
  76. if (disabled)
  77. return;
  78. (0, utils_1.chooseFile)(__assign(__assign({}, this.data), { maxCount: maxCount - lists.length }))
  79. .then(function (res) {
  80. _this.onBeforeRead(multiple ? res : res[0]);
  81. })
  82. .catch(function (error) {
  83. _this.$emit('error', error);
  84. });
  85. },
  86. onBeforeRead: function (file) {
  87. var _this = this;
  88. var _a = this.data, beforeRead = _a.beforeRead, useBeforeRead = _a.useBeforeRead;
  89. var res = true;
  90. if (typeof beforeRead === 'function') {
  91. res = beforeRead(file, this.getDetail());
  92. }
  93. if (useBeforeRead) {
  94. res = new Promise(function (resolve, reject) {
  95. _this.$emit('before-read', __assign(__assign({ file: file }, _this.getDetail()), { callback: function (ok) {
  96. ok ? resolve() : reject();
  97. } }));
  98. });
  99. }
  100. if (!res) {
  101. return;
  102. }
  103. if ((0, validator_1.isPromise)(res)) {
  104. res.then(function (data) { return _this.onAfterRead(data || file); });
  105. }
  106. else {
  107. this.onAfterRead(file);
  108. }
  109. },
  110. onAfterRead: function (file) {
  111. var _a = this.data, maxSize = _a.maxSize, afterRead = _a.afterRead;
  112. var oversize = Array.isArray(file)
  113. ? file.some(function (item) { return item.size > maxSize; })
  114. : file.size > maxSize;
  115. if (oversize) {
  116. this.$emit('oversize', __assign({ file: file }, this.getDetail()));
  117. return;
  118. }
  119. if (typeof afterRead === 'function') {
  120. afterRead(file, this.getDetail());
  121. }
  122. this.$emit('after-read', __assign({ file: file }, this.getDetail()));
  123. },
  124. deleteItem: function (event) {
  125. var index = event.currentTarget.dataset.index;
  126. this.$emit('delete', __assign(__assign({}, this.getDetail(index)), { file: this.data.fileList[index] }));
  127. },
  128. onPreviewImage: function (event) {
  129. if (!this.data.previewFullImage)
  130. return;
  131. var index = event.currentTarget.dataset.index;
  132. var lists = this.data.lists;
  133. var item = lists[index];
  134. wx.previewImage({
  135. urls: lists.filter(function (item) { return (0, utils_1.isImageFile)(item); }).map(function (item) { return item.url; }),
  136. current: item.url,
  137. fail: function () {
  138. wx.showToast({ title: '预览图片失败', icon: 'none' });
  139. },
  140. });
  141. },
  142. onPreviewVideo: function (event) {
  143. if (!this.data.previewFullImage)
  144. return;
  145. var index = event.currentTarget.dataset.index;
  146. var lists = this.data.lists;
  147. wx.previewMedia({
  148. sources: lists
  149. .filter(function (item) { return (0, utils_1.isVideoFile)(item); })
  150. .map(function (item) { return (__assign(__assign({}, item), { type: 'video' })); }),
  151. current: index,
  152. fail: function () {
  153. wx.showToast({ title: '预览视频失败', icon: 'none' });
  154. },
  155. });
  156. },
  157. onPreviewFile: function (event) {
  158. var index = event.currentTarget.dataset.index;
  159. wx.openDocument({
  160. filePath: this.data.lists[index].url,
  161. showMenu: true,
  162. });
  163. },
  164. onClickPreview: function (event) {
  165. var index = event.currentTarget.dataset.index;
  166. var item = this.data.lists[index];
  167. this.$emit('click-preview', __assign(__assign({}, item), this.getDetail(index)));
  168. },
  169. },
  170. });