index.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. <template>
  2. <uni-shadow-root class="weapp-lib-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'] = 'weapp/lib/uploader/index'
  39. "use strict";
  40. var __assign = (this && this.__assign) || function () {
  41. __assign = Object.assign || function(t) {
  42. for (var s, i = 1, n = arguments.length; i < n; i++) {
  43. s = arguments[i];
  44. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
  45. t[p] = s[p];
  46. }
  47. return t;
  48. };
  49. return __assign.apply(this, arguments);
  50. };
  51. Object.defineProperty(exports, "__esModule", { value: true });
  52. var component_1 = require("../common/component");
  53. var utils_1 = require("./utils");
  54. var shared_1 = require("./shared");
  55. var validator_1 = require("../common/validator");
  56. (0, component_1.VantComponent)({
  57. props: __assign(__assign(__assign(__assign({ disabled: Boolean, multiple: Boolean, uploadText: String, useBeforeRead: Boolean, afterRead: null, beforeRead: null, previewSize: {
  58. type: null,
  59. value: 80,
  60. }, name: {
  61. type: null,
  62. value: '',
  63. }, accept: {
  64. type: String,
  65. value: 'image',
  66. }, fileList: {
  67. type: Array,
  68. value: [],
  69. observer: 'formatFileList',
  70. }, maxSize: {
  71. type: Number,
  72. value: Number.MAX_VALUE,
  73. }, maxCount: {
  74. type: Number,
  75. value: 100,
  76. }, deletable: {
  77. type: Boolean,
  78. value: true,
  79. }, showUpload: {
  80. type: Boolean,
  81. value: true,
  82. }, previewImage: {
  83. type: Boolean,
  84. value: true,
  85. }, previewFullImage: {
  86. type: Boolean,
  87. value: true,
  88. }, imageFit: {
  89. type: String,
  90. value: 'scaleToFill',
  91. }, uploadIcon: {
  92. type: String,
  93. value: 'photograph',
  94. } }, shared_1.imageProps), shared_1.videoProps), shared_1.mediaProps), shared_1.messageFileProps),
  95. data: {
  96. lists: [],
  97. isInCount: true,
  98. },
  99. methods: {
  100. formatFileList: function () {
  101. var _a = this.data, _b = _a.fileList, fileList = _b === void 0 ? [] : _b, maxCount = _a.maxCount;
  102. var lists = fileList.map(function (item) { return (__assign(__assign({}, item), { isImage: (0, utils_1.isImageFile)(item), isVideo: (0, utils_1.isVideoFile)(item), deletable: (0, validator_1.isBoolean)(item.deletable) ? item.deletable : true })); });
  103. this.setData({ lists: lists, isInCount: lists.length < maxCount });
  104. },
  105. getDetail: function (index) {
  106. return {
  107. name: this.data.name,
  108. index: index == null ? this.data.fileList.length : index,
  109. };
  110. },
  111. startUpload: function () {
  112. var _this = this;
  113. var _a = this.data, maxCount = _a.maxCount, multiple = _a.multiple, lists = _a.lists, disabled = _a.disabled;
  114. if (disabled)
  115. return;
  116. (0, utils_1.chooseFile)(__assign(__assign({}, this.data), { maxCount: maxCount - lists.length }))
  117. .then(function (res) {
  118. _this.onBeforeRead(multiple ? res : res[0]);
  119. })
  120. .catch(function (error) {
  121. _this.$emit('error', error);
  122. });
  123. },
  124. onBeforeRead: function (file) {
  125. var _this = this;
  126. var _a = this.data, beforeRead = _a.beforeRead, useBeforeRead = _a.useBeforeRead;
  127. var res = true;
  128. if (typeof beforeRead === 'function') {
  129. res = beforeRead(file, this.getDetail());
  130. }
  131. if (useBeforeRead) {
  132. res = new Promise(function (resolve, reject) {
  133. _this.$emit('before-read', __assign(__assign({ file: file }, _this.getDetail()), { callback: function (ok) {
  134. ok ? resolve() : reject();
  135. } }));
  136. });
  137. }
  138. if (!res) {
  139. return;
  140. }
  141. if ((0, validator_1.isPromise)(res)) {
  142. res.then(function (data) { return _this.onAfterRead(data || file); });
  143. }
  144. else {
  145. this.onAfterRead(file);
  146. }
  147. },
  148. onAfterRead: function (file) {
  149. var _a = this.data, maxSize = _a.maxSize, afterRead = _a.afterRead;
  150. var oversize = Array.isArray(file)
  151. ? file.some(function (item) { return item.size > maxSize; })
  152. : file.size > maxSize;
  153. if (oversize) {
  154. this.$emit('oversize', __assign({ file: file }, this.getDetail()));
  155. return;
  156. }
  157. if (typeof afterRead === 'function') {
  158. afterRead(file, this.getDetail());
  159. }
  160. this.$emit('after-read', __assign({ file: file }, this.getDetail()));
  161. },
  162. deleteItem: function (event) {
  163. var index = event.currentTarget.dataset.index;
  164. this.$emit('delete', __assign(__assign({}, this.getDetail(index)), { file: this.data.fileList[index] }));
  165. },
  166. onPreviewImage: function (event) {
  167. if (!this.data.previewFullImage)
  168. return;
  169. var index = event.currentTarget.dataset.index;
  170. var _a = this.data, lists = _a.lists, showmenu = _a.showmenu;
  171. var item = lists[index];
  172. wx.previewImage({
  173. urls: lists.filter(function (item) { return (0, utils_1.isImageFile)(item); }).map(function (item) { return item.url; }),
  174. current: item.url,
  175. showmenu: showmenu,
  176. fail: function () {
  177. wx.showToast({ title: '预览图片失败', icon: 'none' });
  178. },
  179. });
  180. },
  181. onPreviewVideo: function (event) {
  182. if (!this.data.previewFullImage)
  183. return;
  184. var index = event.currentTarget.dataset.index;
  185. var lists = this.data.lists;
  186. var sources = [];
  187. var current = lists.reduce(function (sum, cur, curIndex) {
  188. if (!(0, utils_1.isVideoFile)(cur)) {
  189. return sum;
  190. }
  191. sources.push(__assign(__assign({}, cur), { type: 'video' }));
  192. if (curIndex < index) {
  193. sum++;
  194. }
  195. return sum;
  196. }, 0);
  197. wx.previewMedia({
  198. sources: sources,
  199. current: current,
  200. fail: function () {
  201. wx.showToast({ title: '预览视频失败', icon: 'none' });
  202. },
  203. });
  204. },
  205. onPreviewFile: function (event) {
  206. var index = event.currentTarget.dataset.index;
  207. wx.openDocument({
  208. filePath: this.data.lists[index].url,
  209. showMenu: true,
  210. });
  211. },
  212. onClickPreview: function (event) {
  213. var index = event.currentTarget.dataset.index;
  214. var item = this.data.lists[index];
  215. this.$emit('click-preview', __assign(__assign({}, item), this.getDetail(index)));
  216. },
  217. },
  218. });
  219. export default global['__wxComponents']['weapp/lib/uploader/index']
  220. </script>
  221. <style platform="mp-weixin">
  222. @import '../common/index.css';.van-uploader{display:inline-block;position:relative}.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{align-items:center;background-color:var(--uploader-upload-background-color,#f7f8fa);box-sizing:border-box;display:flex;flex-direction:column;height:var(--uploader-size,80px);justify-content:center;margin:0 8px 8px 0;position:relative;width:var(--uploader-size,80px)}.van-uploader__upload:active{background-color:var(--uploader-upload-active-color,#f2f3f5)}.van-uploader__upload-icon{color:var(--uploader-icon-color,#dcdee0);font-size:var(--uploader-icon-size,24px)}.van-uploader__upload-text{color:var(--uploader-text-color,#969799);font-size:var(--uploader-text-font-size,12px);margin-top:var(--padding-xs,8px)}.van-uploader__upload--disabled{opacity:var(--uploader-disabled-opacity,.5)}.van-uploader__preview{cursor:pointer;margin:0 8px 8px 0;position:relative}.van-uploader__preview-image{display:block;height:var(--uploader-size,80px);overflow:hidden;width:var(--uploader-size,80px)}.van-uploader__preview-delete,.van-uploader__preview-delete:after{height:var(--uploader-delete-icon-size,14px);position:absolute;right:0;top:0;width:var(--uploader-delete-icon-size,14px)}.van-uploader__preview-delete:after{background-color:var(--uploader-delete-background-color,rgba(0,0,0,.7));border-radius:0 0 0 12px;content:""}.van-uploader__preview-delete-icon{color:var(--uploader-delete-color,#fff);font-size:var(--uploader-delete-icon-size,14px);position:absolute;right:0;top:0;transform:scale(.7) translate(10%,-10%);z-index:1}.van-uploader__file{align-items:center;background-color:var(--uploader-file-background-color,#f7f8fa);display:flex;flex-direction:column;height:var(--uploader-size,80px);justify-content:center;width:var(--uploader-size,80px)}.van-uploader__file-icon{color:var(--uploader-file-icon-color,#646566);font-size:var(--uploader-file-icon-size,20px)}.van-uploader__file-name{box-sizing:border-box;color:var(--uploader-file-name-text-color,#646566);font-size:var(--uploader-file-name-font-size,12px);margin-top:var(--uploader-file-name-margin-top,8px);padding:var(--uploader-file-name-padding,0 4px);text-align:center;width:100%}.van-uploader__mask{align-items:center;background-color:var(--uploader-mask-background-color,rgba(50,50,51,.88));bottom:0;color:#fff;display:flex;flex-direction:column;justify-content:center;left:0;position:absolute;right:0;top:0}.van-uploader__mask-icon{font-size:var(--uploader-mask-icon-size,22px)}.van-uploader__mask-message{font-size:var(--uploader-mask-message-font-size,12px);line-height:var(--uploader-mask-message-line-height,14px);margin-top:6px;padding:0 var(--padding-base,4px)}.van-uploader__loading{color:var(--uploader-loading-icon-color,#fff)!important;height:var(--uploader-loading-icon-size,22px);width:var(--uploader-loading-icon-size,22px)}
  223. </style>