index.vue 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <template>
  2. <uni-shadow-root class="vant-dist-uploader-index"><view class="van-uploader">
  3. <view class="van-uploader__wrapper">
  4. <view v-for="(item,index) in (lists)" :key="item.index" v-if="previewImage" class="van-uploader__preview" :data-index="index" @click="onClickPreview">
  5. <image v-if="item.isImage" :mode="imageFit" :src="item.thumb || item.url" :alt="item.name || ('图片' + index)" class="van-uploader__preview-image" :style="computed.sizeStyle({ previewSize })" :data-index="index" @click="onPreviewImage"></image>
  6. <video v-else-if="item.isVideo" :src="item.url" :title="item.name || ('视频' + index)" :poster="item.thumb" :autoplay="item.autoplay" class="van-uploader__preview-image" :style="computed.sizeStyle({ previewSize })" :data-index="index" @click="onPreviewVideo">
  7. </video>
  8. <view v-else class="van-uploader__file" :style="computed.sizeStyle({ previewSize })" :data-index="index" @click="onPreviewFile">
  9. <van-icon name="description" class="van-uploader__file-icon"></van-icon>
  10. <view class="van-uploader__file-name van-ellipsis">{{ item.name || item.url }}</view>
  11. </view>
  12. <view v-if="item.status === 'uploading' || item.status === 'failed'" class="van-uploader__mask">
  13. <van-icon v-if="item.status === 'failed'" name="close" class="van-uploader__mask-icon"></van-icon>
  14. <van-loading v-else custom-class="van-uploader__loading"></van-loading>
  15. <text v-if="item.message" class="van-uploader__mask-message">{{ item.message }}</text>
  16. </view>
  17. <view v-if="deletable && item.deletable" :data-index="index" class="van-uploader__preview-delete" @click.stop.prevent="deleteItem">
  18. <van-icon name="cross" class="van-uploader__preview-delete-icon"></van-icon>
  19. </view>
  20. </view>
  21. <block v-if="isInCount">
  22. <view class="van-uploader__slot" @click="startUpload">
  23. <slot></slot>
  24. </view>
  25. <view v-if="showUpload" :class="'van-uploader__upload '+(disabled ? 'van-uploader__upload--disabled': '')" :style="computed.sizeStyle({ previewSize })" @click="startUpload">
  26. <van-icon :name="uploadIcon" class="van-uploader__upload-icon"></van-icon>
  27. <text v-if="uploadText" class="van-uploader__upload-text">{{ uploadText }}</text>
  28. </view>
  29. </block>
  30. </view>
  31. </view></uni-shadow-root>
  32. </template>
  33. <wxs src="../wxs/utils.wxs" module="utils"></wxs><wxs src="./index.wxs" module="computed"></wxs>
  34. <script>
  35. import VanIcon from '../icon/index.vue'
  36. import VanLoading from '../loading/index.vue'
  37. global['__wxVueOptions'] = {components:{'van-icon': VanIcon,'van-loading': VanLoading}}
  38. global['__wxRoute'] = 'vant/dist/uploader/index'
  39. import { VantComponent } from '../common/component';
  40. import { isImageFile, chooseFile, isVideoFile } from './utils';
  41. import { chooseImageProps, chooseVideoProps } from './shared';
  42. import { isBoolean, isPromise } from '../common/validator';
  43. VantComponent({
  44. props: Object.assign(Object.assign({ disabled: Boolean, multiple: Boolean, uploadText: String, useBeforeRead: Boolean, afterRead: null, beforeRead: null, previewSize: {
  45. type: null,
  46. value: 80,
  47. }, name: {
  48. type: null,
  49. value: '',
  50. }, accept: {
  51. type: String,
  52. value: 'image',
  53. }, fileList: {
  54. type: Array,
  55. value: [],
  56. observer: 'formatFileList',
  57. }, maxSize: {
  58. type: Number,
  59. value: Number.MAX_VALUE,
  60. }, maxCount: {
  61. type: Number,
  62. value: 100,
  63. }, deletable: {
  64. type: Boolean,
  65. value: true,
  66. }, showUpload: {
  67. type: Boolean,
  68. value: true,
  69. }, previewImage: {
  70. type: Boolean,
  71. value: true,
  72. }, previewFullImage: {
  73. type: Boolean,
  74. value: true,
  75. }, imageFit: {
  76. type: String,
  77. value: 'scaleToFill',
  78. }, uploadIcon: {
  79. type: String,
  80. value: 'photograph',
  81. } }, chooseImageProps), chooseVideoProps),
  82. data: {
  83. lists: [],
  84. isInCount: true,
  85. },
  86. methods: {
  87. formatFileList() {
  88. const { fileList = [], maxCount } = this.data;
  89. const lists = fileList.map((item) => (Object.assign(Object.assign({}, item), { isImage: isImageFile(item), isVideo: isVideoFile(item), deletable: isBoolean(item.deletable) ? item.deletable : true })));
  90. this.setData({ lists, isInCount: lists.length < maxCount });
  91. },
  92. getDetail(index) {
  93. return {
  94. name: this.data.name,
  95. index: index == null ? this.data.fileList.length : index,
  96. };
  97. },
  98. startUpload() {
  99. const { maxCount, multiple, lists, disabled } = this.data;
  100. if (disabled)
  101. return;
  102. chooseFile(Object.assign(Object.assign({}, this.data), { maxCount: maxCount - lists.length }))
  103. .then((res) => {
  104. this.onBeforeRead(multiple ? res : res[0]);
  105. })
  106. .catch((error) => {
  107. this.$emit('error', error);
  108. });
  109. },
  110. onBeforeRead(file) {
  111. const { beforeRead, useBeforeRead } = this.data;
  112. let res = true;
  113. if (typeof beforeRead === 'function') {
  114. res = beforeRead(file, this.getDetail());
  115. }
  116. if (useBeforeRead) {
  117. res = new Promise((resolve, reject) => {
  118. this.$emit('before-read', Object.assign(Object.assign({ file }, this.getDetail()), { callback: (ok) => {
  119. ok ? resolve() : reject();
  120. } }));
  121. });
  122. }
  123. if (!res) {
  124. return;
  125. }
  126. if (isPromise(res)) {
  127. res.then((data) => this.onAfterRead(data || file));
  128. }
  129. else {
  130. this.onAfterRead(file);
  131. }
  132. },
  133. onAfterRead(file) {
  134. const { maxSize, afterRead } = this.data;
  135. const oversize = Array.isArray(file)
  136. ? file.some((item) => item.size > maxSize)
  137. : file.size > maxSize;
  138. if (oversize) {
  139. this.$emit('oversize', Object.assign({ file }, this.getDetail()));
  140. return;
  141. }
  142. if (typeof afterRead === 'function') {
  143. afterRead(file, this.getDetail());
  144. }
  145. this.$emit('after-read', Object.assign({ file }, this.getDetail()));
  146. },
  147. deleteItem(event) {
  148. const { index } = event.currentTarget.dataset;
  149. this.$emit('delete', Object.assign(Object.assign({}, this.getDetail(index)), { file: this.data.fileList[index] }));
  150. },
  151. onPreviewImage(event) {
  152. if (!this.data.previewFullImage)
  153. return;
  154. const { index } = event.currentTarget.dataset;
  155. const { lists } = this.data;
  156. const item = lists[index];
  157. wx.previewImage({
  158. urls: lists.filter((item) => isImageFile(item)).map((item) => item.url),
  159. current: item.url,
  160. fail() {
  161. wx.showToast({ title: '预览图片失败', icon: 'none' });
  162. },
  163. });
  164. },
  165. onPreviewVideo(event) {
  166. if (!this.data.previewFullImage)
  167. return;
  168. const { index } = event.currentTarget.dataset;
  169. const { lists } = this.data;
  170. wx.previewMedia({
  171. sources: lists
  172. .filter((item) => isVideoFile(item))
  173. .map((item) => (Object.assign(Object.assign({}, item), { type: 'video' }))),
  174. current: index,
  175. fail() {
  176. wx.showToast({ title: '预览视频失败', icon: 'none' });
  177. },
  178. });
  179. },
  180. onPreviewFile(event) {
  181. const { index } = event.currentTarget.dataset;
  182. wx.openDocument({
  183. filePath: this.data.lists[index].url,
  184. showMenu: true,
  185. });
  186. },
  187. onClickPreview(event) {
  188. const { index } = event.currentTarget.dataset;
  189. const item = this.data.lists[index];
  190. this.$emit('click-preview', Object.assign(Object.assign({}, item), this.getDetail(index)));
  191. },
  192. },
  193. });
  194. export default global['__wxComponents']['vant/dist/uploader/index']
  195. </script>
  196. <style platform="mp-weixin">
  197. @import '../common/index.css';.van-uploader{position:relative;display:inline-block}.van-uploader__wrapper{display:flex;flex-wrap:wrap}.van-uploader__slot:empty{display:none}.van-uploader__slot:not(:empty)+.van-uploader__upload{display:none!important}.van-uploader__upload{position:relative;display:flex;flex-direction:column;align-items:center;justify-content:center;box-sizing:border-box;width:80px;width:var(--uploader-size,80px);height:80px;height:var(--uploader-size,80px);margin:0 8px 8px 0;margin:0 var(--padding-xs,8px) var(--padding-xs,8px) 0;background-color:#f7f8fa;background-color:var(--uploader-upload-background-color,#f7f8fa)}.van-uploader__upload:active{background-color:#f2f3f5;background-color:var(--uploader-upload-active-color,#f2f3f5)}.van-uploader__upload-icon{color:#dcdee0;color:var(--uploader-icon-color,#dcdee0);font-size:24px;font-size:var(--uploader-icon-size,24px)}.van-uploader__upload-text{margin-top:8px;margin-top:var(--padding-xs,8px);color:#969799;color:var(--uploader-text-color,#969799);font-size:12px;font-size:var(--uploader-text-font-size,12px)}.van-uploader__upload--disabled{opacity:.5;opacity:var(--uploader-disabled-opacity,.5)}.van-uploader__preview{position:relative;cursor:pointer;margin:0 8px 8px 0;margin:0 var(--padding-xs,8px) var(--padding-xs,8px) 0}.van-uploader__preview-image{display:block;overflow:hidden;width:80px;width:var(--uploader-size,80px);height:80px;height:var(--uploader-size,80px)}.van-uploader__preview-delete{padding:0 0 8px 8px;padding:0 0 var(--padding-xs,8px) var(--padding-xs,8px)}.van-uploader__preview-delete,.van-uploader__preview-delete:after{position:absolute;top:0;right:0;width:14px;width:var(--uploader-delete-icon-size,14px);height:14px;height:var(--uploader-delete-icon-size,14px)}.van-uploader__preview-delete:after{content:"";background-color:rgba(0,0,0,.7);background-color:var(--uploader-delete-background-color,rgba(0,0,0,.7));border-radius:0 0 0 12px;border-radius:0 0 0 calc(var(--uploader-delete-icon-size, 14px) - 2px)}.van-uploader__preview-delete-icon{position:absolute;top:-2px;right:-2px;z-index:1;transform:scale(.5);font-size:16px;font-size:calc(var(--uploader-delete-icon-size, 14px) + 2px);color:#fff;color:var(--uploader-delete-color,#fff)}.van-uploader__file{display:flex;flex-direction:column;align-items:center;justify-content:center;width:80px;width:var(--uploader-size,80px);height:80px;height:var(--uploader-size,80px);background-color:#f7f8fa;background-color:var(--uploader-file-background-color,#f7f8fa)}.van-uploader__file-icon{color:#646566;color:var(--uploader-file-icon-color,#646566);font-size:20px;font-size:var(--uploader-file-icon-size,20px)}.van-uploader__file-name{box-sizing:border-box;width:100%;text-align:center;margin-top:8px;margin-top:var(--uploader-file-name-margin-top,8px);padding:0 4px;padding:var(--uploader-file-name-padding,0 4px);color:#646566;color:var(--uploader-file-name-text-color,#646566);font-size:12px;font-size:var(--uploader-file-name-font-size,12px)}.van-uploader__mask{position:absolute;top:0;right:0;bottom:0;left:0;display:flex;flex-direction:column;align-items:center;justify-content:center;color:#fff;color:var(--white,#fff);background-color:rgba(50,50,51,.88);background-color:var(--uploader-mask-background-color,rgba(50,50,51,.88))}.van-uploader__mask-icon{font-size:22px;font-size:var(--uploader-mask-icon-size,22px)}.van-uploader__mask-message{margin-top:6px;padding:0 4px;padding:0 var(--padding-base,4px);font-size:12px;font-size:var(--uploader-mask-message-font-size,12px);line-height:14px;line-height:var(--uploader-mask-message-line-height,14px)}.van-uploader__loading{width:22px;width:var(--uploader-loading-icon-size,22px);height:22px;height:var(--uploader-loading-icon-size,22px);color:#fff!important;color:var(--uploader-loading-icon-color,#fff)!important}
  198. </style>