uv-upload.vue 18 KB

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