index.vue 9.7 KB

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