uv-upload.vue 18 KB

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