uploadImage.vue 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 mode="aspectFit" 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','camera'], //从相册选择
  28. countIndex: 1,
  29. count: [0,1,2]
  30. }
  31. },
  32. onUnload() {
  33. this.imageList = [],
  34. this.sourceTypeIndex = 2,
  35. this.sourceType = ['album','camera'],
  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. const token = uni.getStorageSync('sessionId');
  56. this.imageList = this.imageList.concat(res.tempFilePaths);
  57. console.log(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. console.log(uploadFileRes)
  70. let data = uploadFileRes.data;
  71. let newdata = JSON.parse(data);
  72. let url = newdata.data.file_name;
  73. let array = [];
  74. array.push({})
  75. array[0] = url;
  76. this.filelist = this.filelist.concat(array);
  77. this.$emit("getImg", this.filelist);
  78. }
  79. });
  80. }
  81. })
  82. },
  83. }
  84. }
  85. </script>
  86. <style>
  87. .deepHeight {
  88. height: 108upx;
  89. width: 108upx;
  90. }
  91. .uni-uploader__file {
  92. width: 30%!important;
  93. }
  94. </style>