uploadImage.vue 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <template>
  2. <view class="cell-pd">
  3. <view class="uni-uploader">
  4. <view class="uni-uploader-body">
  5. <view class="uni-uploader__files">
  6. <block v-for="(image,index) in imageList" :key="index">
  7. <view class="uni-uploader__file" style="position: relative;">
  8. <image class="uni-uploader__img" :src="image" :data-src="image" ></image>
  9. <view @click="deleteImg(index)" style="position: absolute;right:0;top:0;">X</view>
  10. </view>
  11. </block>
  12. <view v-if="imageList.length != 3" class="uni-uploader__input-box deepHeight">
  13. <view class="uni-uploader__input" @tap="chooseImage"></view>
  14. </view>
  15. </view>
  16. </view>
  17. </view>
  18. </view>
  19. </template>
  20. <script>
  21. export default {
  22. data(){
  23. return{
  24. imageList:[],
  25. filelist:[],
  26. sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
  27. sourceType: ['album'], //从相册选择
  28. countIndex: 1,
  29. count: [1, 2, 3]
  30. }
  31. },
  32. onUnload() {
  33. this.imageList = [],
  34. this.sourceTypeIndex = 2,
  35. this.sourceType = ['album'],
  36. this.sizeType = ['original', 'compressed'],
  37. this.countIndex = 1;
  38. },
  39. methods: {
  40. deleteImg(e) {
  41. let index = e;
  42. this.imageList.splice(index,1);
  43. this.filelist.splice(index,1);
  44. this.$emit("getImg", this.filelist);
  45. },
  46. chooseImage() {
  47. if (this.imageList.length === 3) {
  48. return;
  49. }
  50. uni.chooseImage({
  51. sourceType: this.sourceType,
  52. sizeType: this.sizeType,
  53. count: this.imageList.length + this.count[this.countIndex] > 3 ? 3 - this.imageList.length : this.count[this.countIndex],
  54. success: (res) => {
  55. console.log(res);
  56. const token = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwczpcL1wvd3d3LnF6YWl3YW5nLmNvbSIsImlhdCI6MTU4OTMzNTEyOCwiZXhwIjoxNTkwMTk5MTI4LCJuYmYiOjE1ODkzMzUxMjgsInN1YiI6MTYsInBydiI6ImU5YTM5YzhhZjYwMWYyNGMzZjA3ZjQ0NjMyMmE1NmM4MDVjNWNkZTYifQ.Uec3pKa_Btj0u_6qR95YK_MzyhL9gEcczRd3FpG9xUg';
  57. this.imageList = this.imageList.concat(res.tempFilePaths);
  58. uni.uploadFile({
  59. url: 'https://www.qzaiwang.com/v2/upload/tmp',
  60. filePath: res.tempFilePaths[0],
  61. name: 'file',
  62. header: {
  63. 'X-TOKEN' : token,
  64. },
  65. formData: {
  66. 'user': 'test'
  67. },
  68. success: (uploadFileRes) => {
  69. let data = uploadFileRes.data;
  70. let newdata = JSON.parse(data);
  71. let url = newdata.data.file_name;
  72. console.log(url)
  73. let array = [];
  74. array.push({})
  75. array[0] = url;
  76. console.log(array);
  77. this.filelist = this.filelist.concat(array);
  78. this.$emit("getImg", this.filelist);
  79. }
  80. });
  81. }
  82. })
  83. },
  84. }
  85. }
  86. </script>
  87. <style>
  88. .deepHeight {
  89. height: 108upx;
  90. width: 108upx;
  91. }
  92. .uni-uploader__file {
  93. width: 30%!important;
  94. }
  95. </style>