u-upload.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. <template>
  2. <view class="u-upload" :style="[$u.addStyle(customStyle)]">
  3. <view class="u-upload__wrap">
  4. <template v-if="previewImage">
  5. <view class="u-upload__wrap__preview" v-for="(item, index) in lists" :key="index">
  6. <image v-if="item.isImage || (item.type && item.type === 'image')" :src="item.thumb || item.url"
  7. :mode="imageMode" class="u-upload__wrap__preview__image" @tap="onPreviewImage(item)" :style="[{
  8. width: $u.addUnit(width),
  9. height: $u.addUnit(height)
  10. }]" />
  11. <view @tap="toast(item)" v-else class="u-upload__wrap__preview__other">
  12. <u-icon color="#80CBF9" size="26"
  13. :name="item.isVideo || (item.type && item.type === 'video') ? 'movie' : 'folder'"></u-icon>
  14. <text
  15. class="u-upload__wrap__preview__other__text">{{item.isVideo || (item.type && item.type === 'video') ? '视频' : '文件'}}</text>
  16. </view>
  17. <view class="u-upload__status" v-if="item.status === 'uploading' || item.status === 'failed'">
  18. <view class="u-upload__status__icon">
  19. <u-icon v-if="item.status === 'failed'" name="close-circle" color="#ffffff" size="25" />
  20. <u-loading-icon size="22" mode="circle" color="#ffffff" v-else />
  21. </view>
  22. <text v-if="item.message" class="u-upload__status__message">{{ item.message }}</text>
  23. </view>
  24. <view class="u-upload__deletable"
  25. v-if="item.status !== 'uploading' && (deletable || item.deletable)"
  26. @tap.stop="deleteItem(index)">
  27. <view class="u-upload__deletable__icon">
  28. <u-icon name="close" color="#ffffff" size="10"></u-icon>
  29. </view>
  30. </view>
  31. <view class="u-upload__success" v-if="item.status === 'success'">
  32. <!-- #ifdef APP-NVUE -->
  33. <image :src="successIcon" class="u-upload__success__icon"></image>
  34. <!-- #endif -->
  35. <!-- #ifndef APP-NVUE -->
  36. <view class="u-upload__success__icon">
  37. <u-icon name="checkmark" color="#ffffff" size="12"></u-icon>
  38. </view>
  39. <!-- #endif -->
  40. </view>
  41. </view>
  42. </template>
  43. <template v-if="isInCount">
  44. <view v-if="$slots.default || $slots.$default" @tap="chooseFile">
  45. <slot />
  46. </view>
  47. <view v-else class="u-upload__button" :hover-class="!disabled ? 'u-upload__button--hover' : ''"
  48. hover-stay-time="150" @tap="chooseFile" :class="[disabled && 'u-upload__button--disabled']" :style="[{
  49. width: $u.addUnit(width),
  50. height: $u.addUnit(height)
  51. }]">
  52. <u-icon :name="uploadIcon" size="26" :color="uploadIconColor"></u-icon>
  53. <text v-if="uploadText" class="u-upload__button__text">{{ uploadText }}</text>
  54. </view>
  55. </template>
  56. </view>
  57. </view>
  58. </template>
  59. <script>
  60. import {
  61. chooseFile
  62. } from './utils';
  63. import mixin from './mixin.js';
  64. import props from './props.js';
  65. /**
  66. * upload 上传
  67. * @description 该组件用于上传图片场景
  68. * @tutorial https://uviewui.com/components/upload.html
  69. * @property {String} accept 接受的文件类型, 可选值为all media image file video (默认 'image' )
  70. * @property {String | Array} capture 图片或视频拾取模式,当accept为image类型时设置capture可选额外camera可以直接调起摄像头(默认 ['album', 'camera'] )
  71. * @property {Boolean} compressed 当accept为video时生效,是否压缩视频,默认为true(默认 true )
  72. * @property {String} camera 当accept为video时生效,可选值为back或front(默认 'back' )
  73. * @property {Number} maxDuration 当accept为video时生效,拍摄视频最长拍摄时间,单位秒(默认 60 )
  74. * @property {String} uploadIcon 上传区域的图标,只能内置图标(默认 'camera-fill' )
  75. * @property {String} uploadIconColor 上传区域的图标的字体颜色,只能内置图标(默认 #D3D4D6 )
  76. * @property {Boolean} useBeforeRead 是否开启文件读取前事件(默认 false )
  77. * @property {Boolean} previewFullImage 是否显示组件自带的图片预览功能(默认 true )
  78. * @property {String | Number} maxCount 最大上传数量(默认 52 )
  79. * @property {Boolean} disabled 是否启用(默认 false )
  80. * @property {String} imageMode 预览上传的图片时的裁剪模式,和image组件mode属性一致(默认 'aspectFill' )
  81. * @property {String} name 标识符,可以在回调函数的第二项参数中获取
  82. * @property {Array} sizeType 所选的图片的尺寸, 可选值为original compressed(默认 ['original', 'compressed'] )
  83. * @property {Boolean} multiple 是否开启图片多选,部分安卓机型不支持 (默认 false )
  84. * @property {Boolean} deletable 是否展示删除按钮(默认 true )
  85. * @property {String | Number} maxSize 文件大小限制,单位为byte (默认 Number.MAX_VALUE )
  86. * @property {Array} fileList 显示已上传的文件列表
  87. * @property {String} uploadText 上传区域的提示文字
  88. * @property {String | Number} width 内部预览图片区域和选择图片按钮的区域宽度(默认 80 )
  89. * @property {String | Number} height 内部预览图片区域和选择图片按钮的区域高度(默认 80 )
  90. * @property {Object} customStyle 组件的样式,对象形式
  91. * @event {Function} afterRead 读取后的处理函数
  92. * @event {Function} beforeRead 读取前的处理函数
  93. * @event {Function} oversize 文件超出大小限制
  94. * @event {Function} clickPreview 点击预览图片
  95. * @event {Function} delete 删除图片
  96. * @example <u-upload :action="action" :fileList="fileList" ></u-upload>
  97. */
  98. export default {
  99. name: "u-upload",
  100. mixins: [uni.$u.mpMixin, uni.$u.mixin, mixin, props],
  101. data() {
  102. return {
  103. // #ifdef APP-NVUE
  104. successIcon: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACgAAAAoCAYAAACM/rhtAAAAAXNSR0IArs4c6QAAAERlWElmTU0AKgAAAAgAAYdpAAQAAAABAAAAGgAAAAAAA6ABAAMAAAABAAEAAKACAAQAAAABAAAAKKADAAQAAAABAAAAKAAAAAB65masAAACP0lEQVRYCc3YXygsURwH8K/dpcWyG3LF5u/6/+dKVylSypuUl6uUPMifKMWL8oKEB1EUT1KeUPdR3uTNUsSLxb2udG/cbvInNuvf2rVnazZ/ZndmZ87snjM1Z+Z3zpzfp9+Z5mEAhlvjRtZgCKs+gnPAOcAkkMOR4jEHfItjDvgRxxSQD8cM0BuOCaAvXNCBQrigAsXgggYUiwsK0B9cwIH+4gIKlIILGFAqLiBAOTjFgXJxigJp4BQD0sIpAqSJow6kjSNAFTnRaHJwLenD6Mud52VQAcrBfTd2oyq+HtGaGGWAcnAVcXWoM3bCZrdi+ncPfaAcXE5UKVpdW/vitGPqqAtn98d0gXJwX7Qp6MmegUYVhvmTIezdmHlxJCjpHRTCFerLkRRu4k0aqdajN3sWOo0BK//msHa+xDuPC/oNFMKRhTtM4xjIX0SCNpXL4+7VIaHuyiWEp2L7ahWLf8fejfPdqPmC3mJicORZUp1CQzm+GiphvljGk+PBvWRbxii+xVTj5M6CiZ/tsDufvaXyxEUDxeLIyvu3m0iOyEFWVAkydcVYdyFrE9tQk9iMq6f/GNlvwt3LjQfh60LUrw9/cFyyMJUW/XkLSNMV4Mi6C5ML+ui4x5ClAX9sB9w0wV6wglJwJCv5fOxcr6EstgbGiEw4XcfUry4cWrcEUW8n+ARKxXEJHhw2WG43UKSvwI/TSZgvl7kh0b3XLZaLEy0QmMgLZAVH7J+ALOE+AVnDvQOyiPMAWcW5gSzjCPAV+78S5WE0GrQAAAAASUVORK5CYII=',
  105. // #endif
  106. lists: [],
  107. isInCount: true,
  108. }
  109. },
  110. watch: {
  111. // 监听文件列表的变化,重新整理内部数据
  112. fileList: {
  113. immediate: true,
  114. handler() {
  115. this.formatFileList()
  116. }
  117. },
  118. },
  119. methods: {
  120. toast(item) {
  121. uni.$u.toast('视频不支持预览')
  122. },
  123. formatFileList() {
  124. const {
  125. fileList = [], maxCount
  126. } = this;
  127. const lists = fileList.map((item) =>
  128. Object.assign(Object.assign({}, item), {
  129. // 如果item.url为本地选择的blob文件的话,无法判断其为video还是image,此处优先通过accept做判断处理
  130. isImage: this.accept === 'image' || uni.$u.test.image(item.url || item.thumb),
  131. isVideo: this.accept === 'video' || uni.$u.test.video(item.url || item.thumb),
  132. deletable: typeof(item.deletable) === 'boolean' ? item.deletable : this.deletable,
  133. })
  134. );
  135. this.lists = lists
  136. this.isInCount = lists.length < maxCount
  137. },
  138. chooseFile() {
  139. const {
  140. maxCount,
  141. multiple,
  142. lists,
  143. disabled
  144. } = this;
  145. if (disabled) return;
  146. // 如果用户传入的是字符串,需要格式化成数组
  147. let capture;
  148. try {
  149. capture = uni.$u.test.array(this.capture) ? this.capture : this.capture.split(',');
  150. } catch (e) {
  151. capture = [];
  152. }
  153. chooseFile(
  154. Object.assign({
  155. accept: this.accept,
  156. multiple: this.multiple,
  157. capture: capture,
  158. compressed: this.compressed,
  159. maxDuration: this.maxDuration,
  160. sizeType: this.sizeType,
  161. camera: this.camera,
  162. }, {
  163. maxCount: maxCount - lists.length,
  164. })
  165. )
  166. .then((res) => {
  167. this.onBeforeRead(multiple ? res : res[0]);
  168. })
  169. .catch((error) => {
  170. this.$emit('error', error);
  171. });
  172. },
  173. // 文件读取之前
  174. onBeforeRead(file) {
  175. const {
  176. beforeRead,
  177. useBeforeRead,
  178. } = this;
  179. let res = true
  180. // beforeRead是否为一个方法
  181. if (uni.$u.test.func(beforeRead)) {
  182. // 如果用户定义了此方法,则去执行此方法,并传入读取的文件回调
  183. res = beforeRead(file, this.getDetail());
  184. }
  185. if (useBeforeRead) {
  186. res = new Promise((resolve, reject) => {
  187. this.$emit(
  188. 'beforeRead',
  189. Object.assign(Object.assign({
  190. file
  191. }, this.getDetail()), {
  192. callback: (ok) => {
  193. ok ? resolve() : reject();
  194. },
  195. })
  196. );
  197. });
  198. }
  199. if (!res) {
  200. return;
  201. }
  202. if (uni.$u.test.promise(res)) {
  203. res.then((data) => this.onAfterRead(data || file));
  204. } else {
  205. this.onAfterRead(file);
  206. }
  207. },
  208. getDetail(index) {
  209. return {
  210. name: this.name,
  211. index: index == null ? this.fileList.length : index,
  212. };
  213. },
  214. onAfterRead(file) {
  215. const {
  216. maxSize,
  217. afterRead
  218. } = this;
  219. const oversize = Array.isArray(file) ?
  220. file.some((item) => item.size > maxSize) :
  221. file.size > maxSize;
  222. if (oversize) {
  223. this.$emit('oversize', Object.assign({
  224. file
  225. }, this.getDetail()));
  226. return;
  227. }
  228. if (typeof afterRead === 'function') {
  229. afterRead(file, this.getDetail());
  230. }
  231. this.$emit('afterRead', Object.assign({
  232. file
  233. }, this.getDetail()));
  234. },
  235. deleteItem(index) {
  236. this.$emit(
  237. 'delete',
  238. Object.assign(Object.assign({}, this.getDetail(index)), {
  239. file: this.fileList[index],
  240. })
  241. );
  242. },
  243. // 预览图片
  244. onPreviewImage(item) {
  245. if (!item.isImage || !this.previewFullImage) return
  246. uni.previewImage({
  247. // 先filter找出为图片的item,再返回filter结果中的图片url
  248. urls: this.lists.filter((item) => this.accept === 'image' || uni.$u.test.image(item.url || item
  249. .thumb)).map((item) => item.url || item.thumb),
  250. current: item.url || item.thumb,
  251. fail() {
  252. uni.$u.toast('预览图片失败')
  253. },
  254. });
  255. },
  256. onPreviewVideo(event) {
  257. if (!this.data.previewFullImage) return;
  258. const {
  259. index
  260. } = event.currentTarget.dataset;
  261. const {
  262. lists
  263. } = this.data;
  264. wx.previewMedia({
  265. sources: lists
  266. .filter((item) => isVideoFile(item))
  267. .map((item) =>
  268. Object.assign(Object.assign({}, item), {
  269. type: 'video'
  270. })
  271. ),
  272. current: index,
  273. fail() {
  274. uni.$u.toast('预览视频失败')
  275. },
  276. });
  277. },
  278. onClickPreview(event) {
  279. const {
  280. index
  281. } = event.currentTarget.dataset;
  282. const item = this.data.lists[index];
  283. this.$emit(
  284. 'clickPreview',
  285. Object.assign(Object.assign({}, item), this.getDetail(index))
  286. );
  287. }
  288. }
  289. }
  290. </script>
  291. <style lang="scss" scoped>
  292. @import '../../libs/css/components.scss';
  293. $u-upload-preview-border-radius: 2px !default;
  294. $u-upload-preview-margin: 0 8px 8px 0 !default;
  295. $u-upload-image-width: 80px !default;
  296. $u-upload-image-height: $u-upload-image-width;
  297. $u-upload-other-bgColor: rgb(242, 242, 242) !default;
  298. $u-upload-other-flex: 1 !default;
  299. $u-upload-text-font-size: 11px !default;
  300. $u-upload-text-color: $u-tips-color !default;
  301. $u-upload-text-margin-top: 2px !default;
  302. $u-upload-deletable-right: 0 !default;
  303. $u-upload-deletable-top: 0 !default;
  304. $u-upload-deletable-bgColor: rgb(55, 55, 55) !default;
  305. $u-upload-deletable-height: 14px !default;
  306. $u-upload-deletable-width: $u-upload-deletable-height;
  307. $u-upload-deletable-boder-bottom-left-radius: 100px !default;
  308. $u-upload-deletable-zIndex: 3 !default;
  309. $u-upload-success-bottom: 0 !default;
  310. $u-upload-success-right: 0 !default;
  311. $u-upload-success-border-style: solid !default;
  312. $u-upload-success-border-top-color: transparent !default;
  313. $u-upload-success-border-left-color: transparent !default;
  314. $u-upload-success-border-bottom-color: $u-success !default;
  315. $u-upload-success-border-right-color: $u-upload-success-border-bottom-color;
  316. $u-upload-success-border-width: 9px !default;
  317. $u-upload-icon-top: 0px !default;
  318. $u-upload-icon-right: 0px !default;
  319. $u-upload-icon-h5-top: 1px !default;
  320. $u-upload-icon-h5-right: 0 !default;
  321. $u-upload-icon-width: 16px !default;
  322. $u-upload-icon-height: $u-upload-icon-width;
  323. $u-upload-success-icon-bottom: -10px !default;
  324. $u-upload-success-icon-right: -10px !default;
  325. $u-upload-status-right: 0 !default;
  326. $u-upload-status-left: 0 !default;
  327. $u-upload-status-bottom: 0 !default;
  328. $u-upload-status-top: 0 !default;
  329. $u-upload-status-bgColor: rgba(0, 0, 0, 0.5) !default;
  330. $u-upload-status-icon-Zindex: 1 !default;
  331. $u-upload-message-font-size: 12px !default;
  332. $u-upload-message-color: #FFFFFF !default;
  333. $u-upload-message-margin-top: 5px !default;
  334. $u-upload-button-width: 80px !default;
  335. $u-upload-button-height: $u-upload-button-width;
  336. $u-upload-button-bgColor: rgb(244, 245, 247) !default;
  337. $u-upload-button-border-radius: 2px !default;
  338. $u-upload-botton-margin: 0 8px 8px 0 !default;
  339. $u-upload-text-font-size: 11px !default;
  340. $u-upload-text-color: $u-tips-color !default;
  341. $u-upload-text-margin-top: 2px !default;
  342. $u-upload-hover-bgColor: rgb(230, 231, 233) !default;
  343. $u-upload-disabled-opacity: .5 !default;
  344. .u-upload {
  345. @include flex(column);
  346. flex: 1;
  347. &__wrap {
  348. @include flex;
  349. flex-wrap: wrap;
  350. flex: 1;
  351. &__preview {
  352. border-radius: $u-upload-preview-border-radius;
  353. margin: $u-upload-preview-margin;
  354. position: relative;
  355. overflow: hidden;
  356. @include flex;
  357. &__image {
  358. width: $u-upload-image-width;
  359. height: $u-upload-image-height;
  360. }
  361. &__other {
  362. width: $u-upload-image-width;
  363. height: $u-upload-image-height;
  364. background-color: $u-upload-other-bgColor;
  365. flex: $u-upload-other-flex;
  366. @include flex(column);
  367. justify-content: center;
  368. align-items: center;
  369. &__text {
  370. font-size: $u-upload-text-font-size;
  371. color: $u-upload-text-color;
  372. margin-top: $u-upload-text-margin-top;
  373. }
  374. }
  375. }
  376. }
  377. &__deletable {
  378. position: absolute;
  379. top: $u-upload-deletable-top;
  380. right: $u-upload-deletable-right;
  381. background-color: $u-upload-deletable-bgColor;
  382. height: $u-upload-deletable-height;
  383. width: $u-upload-deletable-width;
  384. @include flex;
  385. border-bottom-left-radius: $u-upload-deletable-boder-bottom-left-radius;
  386. align-items: center;
  387. justify-content: center;
  388. z-index: $u-upload-deletable-zIndex;
  389. &__icon {
  390. position: absolute;
  391. transform: scale(0.7);
  392. top: $u-upload-icon-top;
  393. right: $u-upload-icon-right;
  394. /* #ifdef H5 */
  395. top: $u-upload-icon-h5-top;
  396. right: $u-upload-icon-h5-right;
  397. /* #endif */
  398. }
  399. }
  400. &__success {
  401. position: absolute;
  402. bottom: $u-upload-success-bottom;
  403. right: $u-upload-success-right;
  404. @include flex;
  405. // 由于weex(nvue)为阿里巴巴的KPI(部门业绩考核)的laji产物,不支持css绘制三角形
  406. // 所以在nvue下使用图片,非nvue下使用css实现
  407. /* #ifndef APP-NVUE */
  408. border-style: $u-upload-success-border-style;
  409. border-top-color: $u-upload-success-border-top-color;
  410. border-left-color: $u-upload-success-border-left-color;
  411. border-bottom-color: $u-upload-success-border-bottom-color;
  412. border-right-color: $u-upload-success-border-right-color;
  413. border-width: $u-upload-success-border-width;
  414. align-items: center;
  415. justify-content: center;
  416. /* #endif */
  417. &__icon {
  418. /* #ifndef APP-NVUE */
  419. position: absolute;
  420. transform: scale(0.7);
  421. bottom: $u-upload-success-icon-bottom;
  422. right: $u-upload-success-icon-right;
  423. /* #endif */
  424. /* #ifdef APP-NVUE */
  425. width: $u-upload-icon-width;
  426. height: $u-upload-icon-height;
  427. /* #endif */
  428. }
  429. }
  430. &__status {
  431. position: absolute;
  432. top: $u-upload-status-top;
  433. bottom: $u-upload-status-bottom;
  434. left: $u-upload-status-left;
  435. right: $u-upload-status-right;
  436. background-color: $u-upload-status-bgColor;
  437. @include flex(column);
  438. align-items: center;
  439. justify-content: center;
  440. &__icon {
  441. position: relative;
  442. z-index: $u-upload-status-icon-Zindex;
  443. }
  444. &__message {
  445. font-size: $u-upload-message-font-size;
  446. color: $u-upload-message-color;
  447. margin-top: $u-upload-message-margin-top;
  448. }
  449. }
  450. &__button {
  451. @include flex(column);
  452. align-items: center;
  453. justify-content: center;
  454. width: $u-upload-button-width;
  455. height: $u-upload-button-height;
  456. background-color: $u-upload-button-bgColor;
  457. border-radius: $u-upload-button-border-radius;
  458. margin: $u-upload-botton-margin;
  459. /* #ifndef APP-NVUE */
  460. box-sizing: border-box;
  461. /* #endif */
  462. &__text {
  463. font-size: $u-upload-text-font-size;
  464. color: $u-upload-text-color;
  465. margin-top: $u-upload-text-margin-top;
  466. }
  467. &--hover {
  468. background-color: $u-upload-hover-bgColor;
  469. }
  470. &--disabled {
  471. opacity: $u-upload-disabled-opacity;
  472. }
  473. }
  474. }
  475. </style>