my-image-upload.vue 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <template>
  2. <view class="margin-top">
  3. <view class="cu-bar bg-white ">
  4. <view class="action">
  5. {{label}}
  6. </view>
  7. <view class="action">
  8. {{imgList.length}}/{{maxImg}}
  9. </view>
  10. </view>
  11. <view class="cu-form-group">
  12. <view class="grid col-4 grid-square flex-sub">
  13. <view class="bg-img" v-for="(item,index) in imgList" :key="index" @tap="ViewImage" :data-url="imgList[index]">
  14. <image :src="imgList[index]" mode="aspectFill"></image>
  15. <view class="cu-tag bg-red" @tap.stop="DelImg" :data-index="index">
  16. <text class='cuIcon-close'></text>
  17. </view>
  18. </view>
  19. <view class="solids" @tap="ChooseImage" v-if="imgList.length<maxImg">
  20. <text class='cuIcon-cameraadd'></text>
  21. </view>
  22. </view>
  23. </view>
  24. </view>
  25. </template>
  26. <script>
  27. export default {
  28. name: 'MyImageUpoad',
  29. props: {
  30. value: {type:String,default:''},
  31. label:{type:String,default:'图片上传'},
  32. maxImg: {
  33. type: Number,
  34. default: 3
  35. },
  36. },
  37. mounted:function(){
  38. if (this.value.split(',')!=''){
  39. this.value.split(',').forEach(res=>{
  40. this.imgList.push(baseurl+res)
  41. })
  42. }
  43. },
  44. data() {
  45. return {
  46. imgList: [],
  47. pathlist:[],
  48. }
  49. },
  50. methods: {
  51. ChooseImage() {
  52. uni.chooseImage({
  53. count: this.maxImg, //默认9
  54. sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
  55. sourceType: ['album','camera'], //从相册选择
  56. success: (res) => {
  57. uni.uploadFile({
  58. url: `${baseurl}systemController/filedeal.do?isup=1`,
  59. filePath: res.tempFilePaths[0],
  60. name: 'file',
  61. success: (uploadFileRes) => {
  62. let path = JSON.parse(uploadFileRes.data).obj
  63. this.pathlist.push(path);
  64. this.$emit('input',this.pathlist.join(','))
  65. if (this.imgList.length != 0) {
  66. this.imgList = this.imgList.concat(res.tempFilePaths)
  67. } else {
  68. this.imgList = res.tempFilePaths
  69. }
  70. }
  71. })
  72. }
  73. });
  74. },
  75. ViewImage(e) {
  76. uni.previewImage({
  77. urls: this.imgList,
  78. current: e.currentTarget.dataset.url
  79. });
  80. },
  81. DelImg(e) {
  82. uni.showModal({
  83. title: '提示',
  84. content: '确认要删除吗',
  85. cancelText: '取消',
  86. confirmText: '确认',
  87. success: res => {
  88. if (res.confirm) {
  89. this.pathlist.splice(e.currentTarget.dataset.index,1)
  90. this.imgList.splice(e.currentTarget.dataset.index, 1)
  91. this.$emit('input',this.pathlist.join(','))
  92. }
  93. }
  94. })
  95. },
  96. }
  97. }
  98. </script>
  99. <style>
  100. </style>