index.js 6.0 KB

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