uploadImage.vue 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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">
  8. <image class="uni-uploader__img" :src="image" :data-src="image" @tap="previewImage"></image>
  9. </view>
  10. </block>
  11. <view v-if="imageList.length != 3" class="uni-uploader__input-box deepHeight">
  12. <view class="uni-uploader__input" @tap="chooseImage"></view>
  13. </view>
  14. </view>
  15. </view>
  16. </view>
  17. </view>
  18. </template>
  19. <script>
  20. export default {
  21. data(){
  22. return{
  23. imageList:[],
  24. sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
  25. sourceType: ['album'], //从相册选择
  26. countIndex: 1,
  27. count: [1, 2, 3]
  28. }
  29. },
  30. onUnload() {
  31. this.imageList = [],
  32. this.sourceTypeIndex = 2,
  33. this.sourceType = ['album'],
  34. this.sizeType = ['original', 'compressed'],
  35. this.countIndex = 1;
  36. },
  37. methods: {
  38. chooseImage() {
  39. if (this.imageList.length === 3) {
  40. // let isContinue = await this.isFullImg();
  41. // console.log("是否继续?", isContinue);
  42. // if (!isContinue) {
  43. // return;
  44. // }
  45. return;
  46. }
  47. uni.chooseImage({
  48. sourceType: this.sourceType,
  49. sizeType: this.sizeType,
  50. count: this.imageList.length + this.count[this.countIndex] > 3 ? 3 - this.imageList.length : this.count[this.countIndex],
  51. success: (res) => {
  52. this.imageList = this.imageList.concat(res.tempFilePaths);
  53. uni.uploadFile({
  54. url: '',
  55. filePath: tempFilePaths[0],
  56. name: 'file',
  57. formData: {
  58. 'user': 'test'
  59. },
  60. success: (uploadFileRes) => {
  61. console.log(uploadFileRes.data);
  62. }
  63. });
  64. }
  65. })
  66. },
  67. }
  68. }
  69. </script>
  70. <style>
  71. .deepHeight {
  72. height: 108upx;
  73. width: 108upx;
  74. }
  75. .uni-uploader__file {
  76. width: 30%!important;
  77. }
  78. </style>