index.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. var validator_1 = require('../common/validator');
  22. component_1.VantComponent({
  23. props: __assign(
  24. __assign(
  25. {
  26. disabled: Boolean,
  27. multiple: Boolean,
  28. uploadText: String,
  29. useBeforeRead: Boolean,
  30. afterRead: null,
  31. beforeRead: null,
  32. previewSize: {
  33. type: null,
  34. value: 80,
  35. },
  36. name: {
  37. type: null,
  38. value: '',
  39. },
  40. accept: {
  41. type: String,
  42. value: 'image',
  43. },
  44. fileList: {
  45. type: Array,
  46. value: [],
  47. observer: 'formatFileList',
  48. },
  49. maxSize: {
  50. type: Number,
  51. value: Number.MAX_VALUE,
  52. },
  53. maxCount: {
  54. type: Number,
  55. value: 100,
  56. },
  57. deletable: {
  58. type: Boolean,
  59. value: true,
  60. },
  61. showUpload: {
  62. type: Boolean,
  63. value: true,
  64. },
  65. previewImage: {
  66. type: Boolean,
  67. value: true,
  68. },
  69. previewFullImage: {
  70. type: Boolean,
  71. value: true,
  72. },
  73. imageFit: {
  74. type: String,
  75. value: 'scaleToFill',
  76. },
  77. uploadIcon: {
  78. type: String,
  79. value: 'photograph',
  80. },
  81. },
  82. shared_1.chooseImageProps
  83. ),
  84. shared_1.chooseVideoProps
  85. ),
  86. data: {
  87. lists: [],
  88. isInCount: true,
  89. },
  90. methods: {
  91. formatFileList: function () {
  92. var _a = this.data,
  93. _b = _a.fileList,
  94. fileList = _b === void 0 ? [] : _b,
  95. maxCount = _a.maxCount;
  96. var lists = fileList.map(function (item) {
  97. return __assign(__assign({}, item), {
  98. isImage: utils_1.isImageFile(item),
  99. isVideo: utils_1.isVideoFile(item),
  100. deletable: validator_1.isBoolean(item.deletable)
  101. ? item.deletable
  102. : true,
  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. lists = _a.lists,
  119. disabled = _a.disabled;
  120. if (disabled) return;
  121. utils_1
  122. .chooseFile(
  123. __assign(__assign({}, this.data), {
  124. maxCount: maxCount - lists.length,
  125. })
  126. )
  127. .then(function (res) {
  128. _this.onBeforeRead(multiple ? res : res[0]);
  129. })
  130. .catch(function (error) {
  131. _this.$emit('error', error);
  132. });
  133. },
  134. onBeforeRead: function (file) {
  135. var _this = this;
  136. var _a = this.data,
  137. beforeRead = _a.beforeRead,
  138. useBeforeRead = _a.useBeforeRead;
  139. var res = true;
  140. if (typeof beforeRead === 'function') {
  141. res = beforeRead(file, this.getDetail());
  142. }
  143. if (useBeforeRead) {
  144. res = new Promise(function (resolve, reject) {
  145. _this.$emit(
  146. 'before-read',
  147. __assign(__assign({ file: file }, _this.getDetail()), {
  148. callback: function (ok) {
  149. ok ? resolve() : reject();
  150. },
  151. })
  152. );
  153. });
  154. }
  155. if (!res) {
  156. return;
  157. }
  158. if (validator_1.isPromise(res)) {
  159. res.then(function (data) {
  160. return _this.onAfterRead(data || file);
  161. });
  162. } else {
  163. this.onAfterRead(file);
  164. }
  165. },
  166. onAfterRead: function (file) {
  167. var _a = this.data,
  168. maxSize = _a.maxSize,
  169. afterRead = _a.afterRead;
  170. var oversize = Array.isArray(file)
  171. ? file.some(function (item) {
  172. return item.size > maxSize;
  173. })
  174. : file.size > maxSize;
  175. if (oversize) {
  176. this.$emit('oversize', __assign({ file: file }, this.getDetail()));
  177. return;
  178. }
  179. if (typeof afterRead === 'function') {
  180. afterRead(file, this.getDetail());
  181. }
  182. this.$emit('after-read', __assign({ file: file }, this.getDetail()));
  183. },
  184. deleteItem: function (event) {
  185. var index = event.currentTarget.dataset.index;
  186. this.$emit(
  187. 'delete',
  188. __assign(__assign({}, this.getDetail(index)), {
  189. file: this.data.fileList[index],
  190. })
  191. );
  192. },
  193. onPreviewImage: function (event) {
  194. if (!this.data.previewFullImage) return;
  195. var index = event.currentTarget.dataset.index;
  196. var lists = this.data.lists;
  197. var item = lists[index];
  198. wx.previewImage({
  199. urls: lists
  200. .filter(function (item) {
  201. return utils_1.isImageFile(item);
  202. })
  203. .map(function (item) {
  204. return item.url;
  205. }),
  206. current: item.url,
  207. fail: function () {
  208. wx.showToast({ title: '预览图片失败', icon: 'none' });
  209. },
  210. });
  211. },
  212. onPreviewVideo: function (event) {
  213. if (!this.data.previewFullImage) return;
  214. var index = event.currentTarget.dataset.index;
  215. var lists = this.data.lists;
  216. wx.previewMedia({
  217. sources: lists
  218. .filter(function (item) {
  219. return utils_1.isVideoFile(item);
  220. })
  221. .map(function (item) {
  222. return __assign(__assign({}, item), { type: 'video' });
  223. }),
  224. current: index,
  225. fail: function () {
  226. wx.showToast({ title: '预览视频失败', icon: 'none' });
  227. },
  228. });
  229. },
  230. onPreviewFile: function (event) {
  231. var index = event.currentTarget.dataset.index;
  232. wx.openDocument({
  233. filePath: this.data.lists[index].url,
  234. showMenu: true,
  235. });
  236. },
  237. onClickPreview: function (event) {
  238. var index = event.currentTarget.dataset.index;
  239. var item = this.data.lists[index];
  240. this.$emit(
  241. 'click-preview',
  242. __assign(__assign({}, item), this.getDetail(index))
  243. );
  244. },
  245. },
  246. });