uv-upload.vue 18 KB

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