uploaders.vue 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <template>
  2. <view >
  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. })
  53. if(res[1]){
  54. const imgList = res[1].tempFilePaths
  55. let data = await upload.uploadFile(imgList)
  56. this.imgList = this.imgList.concat(data)
  57. this.updateImages(this.imgList)
  58. }
  59. },
  60. // 删除图片
  61. async bindDelImage(e) {
  62. const index = e.currentTarget.dataset.idx
  63. // 样例图不删除
  64. if (this.imgList[index].url == '') return false
  65. let res = await uni.showModal({
  66. title: '提示',
  67. content: '你确定要删除吗?',
  68. showCancel: true,
  69. })
  70. if (res[1].confirm) {
  71. this.imgList.splice(index,1)
  72. this.updateImages(this.imgList);
  73. }
  74. },
  75. updateImages(imgList) {
  76. this.$emit('update-car-images', imgList);
  77. }
  78. }
  79. };
  80. </script>
  81. <style>
  82. @import '../uploader/uploader.css';
  83. </style>