index.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. import { VantComponent } from '../common/component';
  2. import { isImageFile } from './utils';
  3. import { addUnit } from '../common/utils';
  4. VantComponent({
  5. props: {
  6. disabled: Boolean,
  7. multiple: Boolean,
  8. uploadText: String,
  9. useSlot: Boolean,
  10. useBeforeRead: Boolean,
  11. previewSize: {
  12. type: null,
  13. value: 90,
  14. observer: 'setComputedPreviewSize'
  15. },
  16. name: {
  17. type: [Number, String],
  18. value: ''
  19. },
  20. accept: {
  21. type: String,
  22. value: 'image'
  23. },
  24. fileList: {
  25. type: Array,
  26. value: [],
  27. observer: 'formatFileList'
  28. },
  29. maxSize: {
  30. type: Number,
  31. value: Number.MAX_VALUE
  32. },
  33. maxCount: {
  34. type: Number,
  35. value: 100
  36. },
  37. deletable: {
  38. type: Boolean,
  39. value: true
  40. },
  41. previewImage: {
  42. type: Boolean,
  43. value: true
  44. },
  45. previewFullImage: {
  46. type: Boolean,
  47. value: true
  48. },
  49. imageFit: {
  50. type: String,
  51. value: 'scaleToFill'
  52. }
  53. },
  54. data: {
  55. lists: [],
  56. computedPreviewSize: '',
  57. isInCount: true
  58. },
  59. methods: {
  60. formatFileList() {
  61. const { fileList = [], maxCount } = this.data;
  62. const lists = fileList.map(item => (Object.assign(Object.assign({}, item), { isImage: typeof item.isImage === 'undefined' ? isImageFile(item) : item.isImage })));
  63. this.setData({ lists, isInCount: lists.length < maxCount });
  64. },
  65. setComputedPreviewSize(val) {
  66. this.setData({
  67. computedPreviewSize: addUnit(val)
  68. });
  69. },
  70. startUpload() {
  71. if (this.data.disabled)
  72. return;
  73. const { name = '', capture = ['album', 'camera'], maxCount = 100, multiple = false, maxSize, accept, lists, useBeforeRead = false // 是否定义了 beforeRead
  74. } = this.data;
  75. let chooseFile = null;
  76. const newMaxCount = maxCount - lists.length;
  77. // 设置为只选择图片的时候使用 chooseImage 来实现
  78. if (accept === 'image') {
  79. chooseFile = new Promise((resolve, reject) => {
  80. wx.chooseImage({
  81. count: multiple ? (newMaxCount > 9 ? 9 : newMaxCount) : 1,
  82. sourceType: capture,
  83. success: resolve,
  84. fail: reject
  85. });
  86. });
  87. }
  88. else {
  89. chooseFile = new Promise((resolve, reject) => {
  90. wx.chooseMessageFile({
  91. count: multiple ? newMaxCount : 1,
  92. type: 'file',
  93. success: resolve,
  94. fail: reject
  95. });
  96. });
  97. }
  98. chooseFile.then((res) => {
  99. const file = multiple ? res.tempFiles : res.tempFiles[0];
  100. // 检查文件大小
  101. if (file instanceof Array) {
  102. const sizeEnable = file.every(item => item.size <= maxSize);
  103. if (!sizeEnable) {
  104. this.$emit('oversize', { name });
  105. return;
  106. }
  107. }
  108. else if (file.size > maxSize) {
  109. this.$emit('oversize', { name });
  110. return;
  111. }
  112. // 触发上传之前的钩子函数
  113. if (useBeforeRead) {
  114. this.$emit('before-read', {
  115. file,
  116. name,
  117. callback: (result) => {
  118. if (result) {
  119. // 开始上传
  120. this.$emit('after-read', { file, name });
  121. }
  122. }
  123. });
  124. }
  125. else {
  126. this.$emit('after-read', { file, name });
  127. }
  128. });
  129. },
  130. deleteItem(event) {
  131. const { index } = event.currentTarget.dataset;
  132. this.$emit('delete', { index, name: this.data.name });
  133. },
  134. doPreviewImage(event) {
  135. if (!this.data.previewFullImage)
  136. return;
  137. const curUrl = event.currentTarget.dataset.url;
  138. const images = this.data.lists
  139. .filter(item => item.isImage)
  140. .map(item => item.url || item.path);
  141. this.$emit('click-preview', { url: curUrl, name: this.data.name });
  142. wx.previewImage({
  143. urls: images,
  144. current: curUrl,
  145. fail() {
  146. wx.showToast({ title: '预览图片失败', icon: 'none' });
  147. }
  148. });
  149. }
  150. }
  151. });