index.js 4.9 KB

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