uploaders.vue 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <template>
  2. <view class="container">
  3. <view class="list-group">
  4. <view class="list-item" @longpress="bindDelImage" :data-idx="index"
  5. v-for="(item, index) in imgList" :key="item.unique">
  6. <!-- <image class="img-item-demo" :src="item.url"></image> -->
  7. <image class="img-item" :src="item.url" mode="aspectFill"></image>
  8. <!-- <view class="img_text">{{ item.title? item.title : '车辆照片'}}</view> -->
  9. </view>
  10. <view v-if="imgList.length < max" class="list-item" @tap="bindUpImg">
  11. <view class="img_text">车辆照片</view>
  12. <view class="empity-item">+</view>
  13. </view>
  14. </view>
  15. </view>
  16. </template>
  17. <script>
  18. import upload from './upload.js';
  19. export default {
  20. props: {
  21. car_info: {
  22. type: Array,
  23. required: true //必填项
  24. },
  25. max:{
  26. type: Number,
  27. default:1
  28. }
  29. },
  30. data() {
  31. return {
  32. imgList:[]
  33. }
  34. }
  35. /**
  36. * 生命周期函数--监听页面加载
  37. */
  38. ,
  39. mounted() {
  40. this.imgList = this.car_info
  41. },
  42. /**
  43. * 生命周期函数--监听页面显示
  44. */
  45. onShow: function() {
  46. },
  47. methods: {
  48. // 上传
  49. async bindUpImg(e) {
  50. let res = await uni.chooseImage({
  51. count: this.max - this.imgList.length,
  52. sourceType: ['camera'],
  53. })
  54. if(res[1]){
  55. const imgList = res[1].tempFilePaths
  56. let data = await upload.uploadFile(imgList)
  57. this.imgList = this.imgList.concat(data)
  58. this.updateImages(data)
  59. }
  60. },
  61. // 删除图片
  62. async bindDelImage(e) {
  63. const index = e.currentTarget.dataset.idx
  64. // 样例图不删除
  65. if (this.imgList[index].url == '') return false
  66. let res = await uni.showModal({
  67. title: '提示',
  68. content: '你确定要删除吗?',
  69. showCancel: true,
  70. })
  71. if (res[1].confirm) {
  72. this.imgList.splice(index,1)
  73. this.updateImages(this.imgList);
  74. }
  75. },
  76. updateImages(imgList) {
  77. this.$emit('update-car-images', imgList);
  78. }
  79. }
  80. };
  81. </script>
  82. <style>
  83. @import '../uploader/uploader.css';
  84. </style>