use-upload.vue 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 :src="item" 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. import common from '../../../static/comon.js'
  15. import { cos } from '@/util/cos.js'
  16. export default {
  17. props: {
  18. limit: {
  19. type: Number,
  20. default: 6
  21. }
  22. },
  23. data() {
  24. return {
  25. imgs: []
  26. };
  27. },
  28. methods: {
  29. chooseImage() {
  30. let _this = this;
  31. // uni.chooseImage({
  32. // count: _this.limit, //count: 6, //默认9
  33. // sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
  34. // sourceType: ['album'], //从相册选择
  35. // success: function(res) {
  36. // console.log(JSON.stringify(res.tempFilePaths));
  37. // this.imgs=JSON.stringify(res.tempFilePaths)
  38. // }
  39. // });
  40. uni.chooseImage({
  41. count: _this.limit,
  42. // 可以指定是原图|压缩图,默认二者都有
  43. sizeType: ['original', 'compressed'],
  44. success: (res) => {
  45. // _this.imgs.push((res.tempFilePaths)[0])
  46. // console.log(_this.imgs)
  47. const file = res.tempFilePaths[0];
  48. let Key = file.substr(file.lastIndexOf('/') + 1)
  49. cos.postObject(
  50. {
  51. Bucket: 'jinganminsu-1320402385',
  52. Region: 'ap-nanjing',
  53. Key: Key,
  54. FilePath: file,
  55. onProgress: (info) => {
  56. // console.log(info)
  57. }
  58. },
  59. (err, data) => {
  60. if (err) {
  61. console.log('上传失败', err)
  62. } else {
  63. console.log('上传成功', data)
  64. uni.hideLoading()
  65. let imgUrl = 'https://' + data.Location
  66. // this.form.img =imgUrl
  67. _this.imgs.push(imgUrl)
  68. _this.sendData()
  69. }
  70. }
  71. )
  72. for (var i = 0; i < res.tempFilePaths.length; i++) {
  73. }
  74. uni.hideLoading();
  75. }
  76. });
  77. },
  78. preImage(item, urls) {
  79. if (item.type.indexOf('video/') !== -1) {
  80. return;
  81. }
  82. let _urls = urls.filter(x => x.type.indexOf('image/') !== -1).map(ele => {
  83. return ele.url;
  84. });
  85. console.log(item, urls);
  86. uni.previewImage({
  87. current: item.url,
  88. urls: _urls,
  89. longPressActions: {
  90. itemList: ['发送给朋友', '保存图片', '收藏'],
  91. success: function(data) {
  92. console.log('选中了第' + (data.tapIndex + 1) + '个按钮,第' + (data.index + 1) + '张图片');
  93. },
  94. fail: function(err) {
  95. console.log(err.errMsg);
  96. }
  97. }
  98. });
  99. },
  100. delImage(idx) {
  101. this.imgs.splice(idx, 1);
  102. this.sendData();
  103. },
  104. sendData() {
  105. this.$emit('upload', this.imgs);
  106. }
  107. }
  108. };
  109. </script>
  110. <style lang="scss">
  111. .use-upload {
  112. .item {
  113. width: 23%;
  114. margin-right: 2%;
  115. height: 150rpx;
  116. border: 1px solid #f0f0f0;
  117. image, video {
  118. width: 100%;
  119. height: 100%;
  120. }
  121. }
  122. .del {
  123. top: -30rpx;
  124. right: -10rpx;
  125. width: 50rpx;
  126. height: 50rpx;
  127. z-index: 99;
  128. box-shadow: 0px 3px 5px #f0f0f0;
  129. }
  130. }
  131. </style>