uploader.vue 2.1 KB

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