use-upload.vue 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. export default {
  16. props: {
  17. limit: {
  18. type: Number,
  19. default: 6
  20. }
  21. },
  22. data() {
  23. return {
  24. imgs: []
  25. };
  26. },
  27. methods: {
  28. chooseImage() {
  29. let _this = this;
  30. // uni.chooseImage({
  31. // count: _this.limit, //count: 6, //默认9
  32. // sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
  33. // sourceType: ['album'], //从相册选择
  34. // success: function(res) {
  35. // console.log(JSON.stringify(res.tempFilePaths));
  36. // this.imgs=JSON.stringify(res.tempFilePaths)
  37. // }
  38. // });
  39. uni.chooseImage({
  40. count: _this.limit,
  41. // 可以指定是原图|压缩图,默认二者都有
  42. sizeType: ['original', 'compressed'],
  43. success: (res) => {
  44. console.log(JSON.stringify(res.tempFilePaths));
  45. // _this.imgs.push((res.tempFilePaths)[0])
  46. // console.log(_this.imgs)
  47. const file = res.tempFilePaths[0];
  48. // 文件上传
  49. uni.uploadFile({
  50. url:getApp().globalData.baseurl+ '/file/open/', //自己的后端接口(默认发送post请求)
  51. filePath:file,
  52. name:"file", //这里应为自己后端文件形参的名字
  53. data:{
  54. //
  55. },
  56. header:{
  57. 'Content-type' : 'multipart/form-data'
  58. },
  59. success(res) {
  60. res = res.data
  61. console.log(JSON.parse(res).success)
  62. if(JSON.parse(res).success){
  63. _this.imgs.push(JSON.parse(res).data)
  64. _this.sendData()
  65. }
  66. }
  67. })
  68. // uni.showLoading({
  69. // title: '上传中',
  70. // mask: true
  71. // });
  72. for (var i = 0; i < res.tempFilePaths.length; i++) {
  73. console.log(i,'41')
  74. //解决跨越问题,让通过微信上传的图片你能够展示在浏览器前端
  75. // uni.getImageInfo({
  76. // src: res.tempFilePaths[0],
  77. // success: (path) => {
  78. // pathToBase64(path.path).then(base64 => {
  79. // console.log("base64="+base64); // 这就是转为base64格式的图片
  80. // _this.imgs=base64
  81. // })
  82. // .catch(error => {
  83. // console.error(error)
  84. // })
  85. // }
  86. // })
  87. // const result = uni.uploadFile({
  88. // filePath: res.tempFilePaths[i],
  89. // cloudPath: _this.$api.getFileName(res.tempFilePaths[i]),
  90. // onUploadProgress: function(progressEvent) {
  91. // console.log(progressEvent,'0p1')
  92. // var percentCompleted = Math.round((progressEvent.loaded *
  93. // 100) / progressEvent.total);
  94. // }
  95. // }).then(uRes => {
  96. // console.log(uRes, res.tempFiles[i],'0p')
  97. // uRes.url = uRes.fileID;
  98. // uRes.type = res.tempFiles[i].type || '';
  99. // uRes.size = res.tempFiles[i].size;
  100. // _this.imgs.push(uRes);
  101. // _this.sendData()
  102. // }).catch(err => {
  103. // console.log('use-upload', err);
  104. // });
  105. }
  106. uni.hideLoading();
  107. }
  108. });
  109. },
  110. preImage(item, urls) {
  111. if (item.type.indexOf('video/') !== -1) {
  112. return;
  113. }
  114. let _urls = urls.filter(x => x.type.indexOf('image/') !== -1).map(ele => {
  115. return ele.url;
  116. });
  117. console.log(item, urls);
  118. uni.previewImage({
  119. current: item.url,
  120. urls: _urls,
  121. longPressActions: {
  122. itemList: ['发送给朋友', '保存图片', '收藏'],
  123. success: function(data) {
  124. console.log('选中了第' + (data.tapIndex + 1) + '个按钮,第' + (data.index + 1) + '张图片');
  125. },
  126. fail: function(err) {
  127. console.log(err.errMsg);
  128. }
  129. }
  130. });
  131. },
  132. delImage(idx) {
  133. this.imgs.splice(idx, 1);
  134. this.sendData();
  135. },
  136. sendData() {
  137. this.$emit('upload', this.imgs);
  138. }
  139. }
  140. };
  141. </script>
  142. <style lang="scss">
  143. .use-upload {
  144. .item {
  145. width: 23%;
  146. margin-right: 2%;
  147. height: 150rpx;
  148. border: 1px solid #f0f0f0;
  149. image, video {
  150. width: 100%;
  151. height: 100%;
  152. }
  153. }
  154. .del {
  155. top: -30rpx;
  156. right: -10rpx;
  157. width: 50rpx;
  158. height: 50rpx;
  159. z-index: 99;
  160. box-shadow: 0px 3px 5px #f0f0f0;
  161. }
  162. }
  163. </style>