3
0

u-upload.vue 18 KB

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