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">
  4. <view class="cu-avatar lg margin-left-sm" v-if="!disabled" @tap.stop="DelImg" :data-index="index" :style="`background-image:url('${item}')`" >
  5. <text class='cuIcon-close'></text>
  6. </view>
  7. </view>
  8. <view class="solids" v-if="!disabled" @tap="ChooseImage">
  9. <text class='cuIcon-cameraadd'></text>
  10. </view>
  11. </view>
  12. </template>
  13. <script>
  14. import fileService from "@/api/file/fileService"
  15. export default {
  16. data() {
  17. return {
  18. imgList: []
  19. }
  20. },
  21. watch:{
  22. value:{
  23. handler (val) {
  24. if(val){
  25. this.imgList = val.split(',')
  26. }
  27. },
  28. immediate: true,
  29. deep: false
  30. }
  31. },
  32. props: {
  33. value: {
  34. type: String,
  35. default: function () {
  36. return ''
  37. }
  38. },
  39. disabled: {
  40. type: Boolean,
  41. default: false
  42. }
  43. },
  44. methods: {
  45. ChooseImage() {
  46. uni.chooseImage({
  47. count: 4, //默认9
  48. sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
  49. sourceType: ['album'], //从相册选择
  50. success: (res) => {
  51. this.upload(res.tempFilePaths[0])
  52. }
  53. });
  54. },
  55. ViewImage(e) {
  56. let urls = this.imgList.map((img)=>{
  57. return img.url
  58. })
  59. uni.previewImage({
  60. urls: urls,
  61. current: e.currentTarget.dataset.url
  62. });
  63. },
  64. DelImg(e) {
  65. uni.showModal({
  66. title: '提示',
  67. content: '确定要删除图片吗?',
  68. cancelText: '取消',
  69. confirmText: '确定',
  70. success: res => {
  71. if (res.confirm) {
  72. this.imgList.splice(e.currentTarget.dataset.index, 1)
  73. this.$emit('input', this.imgList.join(','))
  74. }
  75. }
  76. })
  77. },
  78. upload(img) {
  79. fileService.upload(img).then((res)=>{
  80. this.imgList.push(res)
  81. this.$emit('input', this.imgList.join(','))
  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>