use-upload.vue 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <template>
  2. <view class="use-upload dflex dflex-wrap-w">
  3. <view class="item pos-r" v-for="(item, index) in imgs" @click="preImage(item, imgs)" :key="index">
  4. <video v-if="item.type.indexOf('video/') !== -1" :src="item.url"></video>
  5. <image v-else :src="item.url" mode="aspectFill"></image>
  6. <view class="del pos-a bg-main dflex-c border-radius-c iconfont iconlajitong-01 ft-dark" @tap.stop="delImage(index)"></view>
  7. </view>
  8. <view class="item dflex-c" v-if="imgs.length < limit" @tap="chooseImage">
  9. <view class="iconfont iconxiangji-01 fs-big ft-dark"></view>
  10. </view>
  11. </view>
  12. </template>
  13. <script>
  14. export default {
  15. props: {
  16. limit: {
  17. type: Number,
  18. default: 3
  19. }
  20. },
  21. data() {
  22. return {
  23. imgs: []
  24. };
  25. },
  26. methods: {
  27. chooseImage() {
  28. let _this = this;
  29. uni.chooseImage({
  30. count: _this.limit,
  31. // 可以指定是原图|压缩图,默认二者都有
  32. sizeType: ['original', 'compressed'],
  33. success: async (res) => {
  34. uni.showLoading({
  35. title: '上传中',
  36. mask: true
  37. });
  38. for (var i = 0; i < res.tempFilePaths.length; i++) {
  39. const result = await uniCloud.uploadFile({
  40. filePath: res.tempFilePaths[i],
  41. cloudPath: _this.$api.getFileName(res.tempFilePaths[i]),
  42. onUploadProgress: function(progressEvent) {
  43. var percentCompleted = Math.round((progressEvent.loaded *
  44. 100) / progressEvent.total);
  45. }
  46. }).then(uRes => {
  47. console.log(uRes, res.tempFiles[i])
  48. uRes.url = uRes.fileID;
  49. uRes.type = res.tempFiles[i].type || '';
  50. uRes.size = res.tempFiles[i].size;
  51. _this.imgs.push(uRes);
  52. _this.sendData()
  53. }).catch(err => {
  54. console.log('use-upload', err);
  55. });
  56. }
  57. uni.hideLoading();
  58. }
  59. });
  60. },
  61. preImage(item, urls) {
  62. if (item.type.indexOf('video/') !== -1) {
  63. return;
  64. }
  65. let _urls = urls.filter(x => x.type.indexOf('image/') !== -1).map(ele => {
  66. return ele.url;
  67. });
  68. console.log(item, urls);
  69. uni.previewImage({
  70. current: item.url,
  71. urls: _urls,
  72. longPressActions: {
  73. itemList: ['发送给朋友', '保存图片', '收藏'],
  74. success: function(data) {
  75. console.log('选中了第' + (data.tapIndex + 1) + '个按钮,第' + (data.index + 1) + '张图片');
  76. },
  77. fail: function(err) {
  78. console.log(err.errMsg);
  79. }
  80. }
  81. });
  82. },
  83. delImage(idx) {
  84. this.imgs.splice(idx, 1);
  85. this.sendData();
  86. },
  87. sendData() {
  88. this.$emit('upload', this.imgs);
  89. }
  90. }
  91. };
  92. </script>
  93. <style lang="scss">
  94. .use-upload {
  95. .item {
  96. width: 23%;
  97. margin-right: 2%;
  98. height: 150rpx;
  99. border: 1px solid #f0f0f0;
  100. image, video {
  101. width: 100%;
  102. height: 100%;
  103. }
  104. }
  105. .del {
  106. top: -30rpx;
  107. right: -10rpx;
  108. width: 50rpx;
  109. height: 50rpx;
  110. z-index: 99;
  111. box-shadow: 0px 3px 5px #f0f0f0;
  112. }
  113. }
  114. </style>