my-image-upload.vue 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. import {ACCESS_TOKEN} from '@/common/util/constants.js'
  28. export default {
  29. name: 'MyImageUpoad',
  30. props: {
  31. value: {type:String,default:''},
  32. label:{type:String,default:'图片上传'},
  33. maxImg: {
  34. type: Number,
  35. default: 3
  36. },
  37. },
  38. mounted:function(){
  39. if (this.value.split(',')!=''){
  40. this.value.split(',').forEach(res=>{
  41. this.imgList.push(baseurl+res)
  42. })
  43. }
  44. },
  45. data() {
  46. return {
  47. imgList: [],
  48. pathlist:[],
  49. }
  50. },
  51. methods: {
  52. ChooseImage() {
  53. uni.chooseImage({
  54. count: this.maxImg, //默认9
  55. sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
  56. sourceType: ['album','camera'], //从相册选择
  57. success: (res) => {
  58. uni.uploadFile({
  59. url: "http://localhost:8080/jeecg-boot/sys/common/upload",
  60. filePath: res.tempFilePaths[0],
  61. name: 'file',
  62. header:{
  63. 'X-Access-Token':uni.getStorageSync(ACCESS_TOKEN)
  64. },
  65. success: (uploadFileRes) => {
  66. let path = JSON.parse(uploadFileRes.data).obj
  67. this.pathlist.push(path);
  68. this.$emit('input',this.pathlist.join(','))
  69. if (this.imgList.length != 0) {
  70. this.imgList = this.imgList.concat(res.tempFilePaths)
  71. } else {
  72. this.imgList = res.tempFilePaths
  73. }
  74. }
  75. })
  76. }
  77. });
  78. },
  79. ViewImage(e) {
  80. uni.previewImage({
  81. urls: this.imgList,
  82. current: e.currentTarget.dataset.url
  83. });
  84. },
  85. DelImg(e) {
  86. uni.showModal({
  87. title: '提示',
  88. content: '确认要删除吗',
  89. cancelText: '取消',
  90. confirmText: '确认',
  91. success: res => {
  92. if (res.confirm) {
  93. this.pathlist.splice(e.currentTarget.dataset.index,1)
  94. this.imgList.splice(e.currentTarget.dataset.index, 1)
  95. this.$emit('input',this.pathlist.join(','))
  96. }
  97. }
  98. })
  99. },
  100. }
  101. }
  102. </script>
  103. <style>
  104. </style>