u-upload.vue 17 KB

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