You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

581 lines
18 KiB

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