index.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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: 80,
  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. deletable:
  82. typeof item.deletable === 'undefined' ? true : item.deletable,
  83. })
  84. );
  85. this.setData({ lists, isInCount: lists.length < maxCount });
  86. },
  87. getDetail(index) {
  88. return {
  89. name: this.data.name,
  90. index: index == null ? this.data.fileList.length : index,
  91. };
  92. },
  93. startUpload() {
  94. const { maxCount, multiple, accept, lists, disabled } = this.data;
  95. if (disabled) return;
  96. chooseFile(
  97. Object.assign(Object.assign({}, this.data), {
  98. maxCount: maxCount - lists.length,
  99. })
  100. )
  101. .then((res) => {
  102. let file = null;
  103. if (isVideo(res, accept)) {
  104. file = Object.assign({ path: res.tempFilePath }, res);
  105. } else {
  106. file = multiple ? res.tempFiles : res.tempFiles[0];
  107. }
  108. this.onBeforeRead(file);
  109. })
  110. .catch((error) => {
  111. this.$emit('error', error);
  112. });
  113. },
  114. onBeforeRead(file) {
  115. const { beforeRead, useBeforeRead } = this.data;
  116. let res = true;
  117. if (typeof beforeRead === 'function') {
  118. res = beforeRead(file, this.getDetail());
  119. }
  120. if (useBeforeRead) {
  121. res = new Promise((resolve, reject) => {
  122. this.$emit(
  123. 'before-read',
  124. Object.assign(Object.assign({ file }, this.getDetail()), {
  125. callback: (ok) => {
  126. ok ? resolve() : reject();
  127. },
  128. })
  129. );
  130. });
  131. }
  132. if (!res) {
  133. return;
  134. }
  135. if (isPromise(res)) {
  136. res.then((data) => this.onAfterRead(data || file));
  137. } else {
  138. this.onAfterRead(file);
  139. }
  140. },
  141. onAfterRead(file) {
  142. const { maxSize } = this.data;
  143. const oversize = Array.isArray(file)
  144. ? file.some((item) => item.size > maxSize)
  145. : file.size > maxSize;
  146. if (oversize) {
  147. this.$emit('oversize', Object.assign({ file }, this.getDetail()));
  148. return;
  149. }
  150. if (typeof this.data.afterRead === 'function') {
  151. this.data.afterRead(file, this.getDetail());
  152. }
  153. this.$emit('after-read', Object.assign({ file }, this.getDetail()));
  154. },
  155. deleteItem(event) {
  156. const { index } = event.currentTarget.dataset;
  157. this.$emit(
  158. 'delete',
  159. Object.assign(Object.assign({}, this.getDetail(index)), {
  160. file: this.data.fileList[index],
  161. })
  162. );
  163. },
  164. onPreviewImage(event) {
  165. if (!this.data.previewFullImage) return;
  166. const { index } = event.currentTarget.dataset;
  167. const { lists } = this.data;
  168. const item = lists[index];
  169. wx.previewImage({
  170. urls: lists
  171. .filter((item) => item.isImage)
  172. .map((item) => item.url || item.path),
  173. current: item.url || item.path,
  174. fail() {
  175. wx.showToast({ title: '预览图片失败', icon: 'none' });
  176. },
  177. });
  178. },
  179. onClickPreview(event) {
  180. const { index } = event.currentTarget.dataset;
  181. const item = this.data.lists[index];
  182. this.$emit(
  183. 'click-preview',
  184. Object.assign(Object.assign({}, item), this.getDetail(index))
  185. );
  186. },
  187. },
  188. });