jp-image-upload.vue 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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.url">
  4. <image :src="item.url" 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. this.imgList = val
  26. },
  27. immediate: true,
  28. deep: false
  29. }
  30. },
  31. props: {
  32. value: {
  33. type: Array,
  34. default: function () {
  35. return []
  36. }
  37. },
  38. disabled: {
  39. type: Boolean,
  40. default: false
  41. }
  42. },
  43. methods: {
  44. ChooseImage() {
  45. uni.chooseImage({
  46. count: 4, //默认9
  47. sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
  48. sourceType: ['album'], //从相册选择
  49. success: (res) => {
  50. this.upload(res.tempFilePaths[0])
  51. }
  52. });
  53. },
  54. ViewImage(e) {
  55. let urls = this.imgList.map((img)=>{
  56. return img.url
  57. })
  58. uni.previewImage({
  59. urls: urls,
  60. current: e.currentTarget.dataset.url
  61. });
  62. },
  63. DelImg(e) {
  64. uni.showModal({
  65. title: '提示',
  66. content: '确定要删除图片吗?',
  67. cancelText: '取消',
  68. confirmText: '确定',
  69. success: res => {
  70. if (res.confirm) {
  71. this.imgList.splice(e.currentTarget.dataset.index, 1)
  72. console.log(this.imgList)
  73. this.$emit('input', this.imgList)
  74. }
  75. }
  76. })
  77. },
  78. upload(img) {
  79. fileService.upload(img).then(({data})=>{
  80. this.imgList.push(data)
  81. this.$emit('input', this.imgList)
  82. })
  83. }
  84. }
  85. }
  86. </script>
  87. <style>
  88. .btn-logout {
  89. margin-top: 100upx;
  90. width: 80%;
  91. border-radius: 50upx;
  92. font-size: 16px;
  93. color: #fff;
  94. background: linear-gradient(to right, #365fff, #36bbff);
  95. }
  96. .btn-logout-hover {
  97. background: linear-gradient(to right, #365fdd, #36bbfa);
  98. }
  99. uni-image>div, uni-image>img {
  100. width: 100upx !important;
  101. height: 100upx !important;
  102. }
  103. </style>