jp-image-upload2.vue 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <template>
  2. <view class="margin-top grid col-4 grid-square flex-sub">
  3. <view class="bg-img" v-for="(item,index) in imgList" :key="index" @tap="ViewImage" :data-url="item">
  4. <image :src="item" mode="aspectFill"></image>
  5. <view class="cu-tag bg-red" v-if="!disabled" @tap.stop="DelImg" :data-index="index">
  6. <text class='cuIcon-close'></text>
  7. </view>
  8. </view>
  9. <view class="solids" v-if="!disabled" @tap="ChooseImage">
  10. <text class='cuIcon-cameraadd'></text>
  11. </view>
  12. </view>
  13. </template>
  14. <script>
  15. import fileService from "@/api/file/fileService"
  16. export default {
  17. data() {
  18. return {
  19. imgList: []
  20. }
  21. },
  22. watch:{
  23. value:{
  24. handler (val) {
  25. if(val){
  26. this.imgList = val.split(',')
  27. }
  28. },
  29. immediate: true,
  30. deep: false
  31. }
  32. },
  33. props: {
  34. value: {
  35. type: String,
  36. default: function () {
  37. return ''
  38. }
  39. },
  40. disabled: {
  41. type: Boolean,
  42. default: false
  43. }
  44. },
  45. methods: {
  46. ChooseImage() {
  47. uni.chooseImage({
  48. count: 4, //默认9
  49. sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
  50. sourceType: ['album'], //从相册选择
  51. success: (res) => {
  52. this.upload(res.tempFilePaths[0])
  53. }
  54. });
  55. },
  56. ViewImage(e) {
  57. let urls = this.imgList.map((img)=>{
  58. return img.url
  59. })
  60. uni.previewImage({
  61. urls: urls,
  62. current: e.currentTarget.dataset.url
  63. });
  64. },
  65. DelImg(e) {
  66. uni.showModal({
  67. title: '提示',
  68. content: '确定要删除图片吗?',
  69. cancelText: '取消',
  70. confirmText: '确定',
  71. success: res => {
  72. if (res.confirm) {
  73. this.imgList.splice(e.currentTarget.dataset.index, 1)
  74. this.$emit('input', this.imgList.join(','))
  75. }
  76. }
  77. })
  78. },
  79. upload(img) {
  80. fileService.upload(img).then(({data})=>{
  81. this.imgList.push(data)
  82. this.$emit('input', this.imgList.join(','))
  83. })
  84. }
  85. }
  86. }
  87. </script>
  88. <style>
  89. .btn-logout {
  90. margin-top: 100upx;
  91. width: 80%;
  92. border-radius: 50upx;
  93. font-size: 16px;
  94. color: #fff;
  95. background: linear-gradient(to right, #365fff, #36bbff);
  96. }
  97. .btn-logout-hover {
  98. background: linear-gradient(to right, #365fdd, #36bbfa);
  99. }
  100. uni-image>div, uni-image>img {
  101. width: 100upx !important;
  102. height: 100upx !important;
  103. }
  104. </style>