index.vue 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <template>
  2. <div class="upimgList">
  3. <div class="upimg" v-for="(i,s) in imgs" :key="s">
  4. <img :src="i" class="logo" alt="" srcset="" />
  5. <div class="del iconfont" @click="del">&#xe609;</div>
  6. </div>
  7. <div v-if="imgs.length < count" class="upimg flex_c flex_ac flex_jc flex_wrap" @click="upimg">
  8. <div class="upico iconfont">&#xe674;</div>
  9. <div class="upmsg" v-if="!desc">最多{{ count }}张</div>
  10. <div class="upmsg" v-else>{{ desc }}</div>
  11. </div>
  12. <center-popup ref="centerPopup" :title="Popuptitle" @confirmClick="delimg" />
  13. </div>
  14. </template>
  15. <script>
  16. import centerPopup from "@/pagesC/components/centerPopup/centerPopup"
  17. export default {
  18. name: "upimgList",
  19. props: {
  20. count: {
  21. type: Number,
  22. default: 1
  23. },
  24. value: {
  25. type: String,
  26. default: ""
  27. },
  28. desc: "",
  29. },
  30. components: { centerPopup },
  31. data() {
  32. return {
  33. imgs: [],
  34. Popuptitle: "",
  35. Popuptitle: "",
  36. delinx: "", // 要删除的图片索引
  37. };
  38. },
  39. methods: {
  40. upimg() {
  41. let that = this
  42. uni.chooseMedia({
  43. count: that.count,
  44. mediaType: ["image"],
  45. sizeType: ["compressed"],
  46. sourceType: ["album", "camera"],
  47. success: async (res) => {
  48. let arr = res.tempFiles.slice(0,this.count - this.imgs.length)
  49. for (const it of arr) {
  50. if (it.size/1024/1024 <= 1) {
  51. let url = await that.$up(it.tempFilePath)
  52. this.imgs.push(url)
  53. }else{
  54. return uni.showToast({
  55. title: "图片大小超出1M",
  56. icon: "none",
  57. });
  58. }
  59. }
  60. that.emitI(this.imgs);
  61. },
  62. });
  63. },
  64. del(inx) {
  65. this.delinx = inx
  66. this.Popuptitle = "确认要删除这张图片吗?"
  67. this.$refs.centerPopup.open()
  68. },
  69. delimg() {
  70. this.imgs.splice(this.delinx, 1)
  71. this.emitI(this.imgs)
  72. this.$refs.centerPopup.close()
  73. },
  74. emitI(arr){
  75. let va = ""
  76. if(this.count == 1) va = arr[0]
  77. else va = JSON.stringify(arr)
  78. this.$emit("input", va);
  79. },
  80. },
  81. watch: {
  82. value: {
  83. immediate: true,
  84. deep: true,
  85. handler(str) {
  86. if (str){
  87. if(this.count == 1) this.imgs = [str]
  88. else this.imgs = JSON.parse(str)
  89. }
  90. }
  91. }
  92. },
  93. onLoad(da) {},
  94. onShow() {},
  95. mounted() {},
  96. };
  97. </script>
  98. <style scoped lang='scss'>
  99. .upimgList {
  100. display: grid;
  101. justify-content: space-between;
  102. grid-template-columns: repeat(auto-fill, 170rpx);
  103. grid-gap: 0 1px;
  104. }
  105. .upimg {
  106. width: 170rpx;
  107. height: 130rpx;
  108. font-size: 23rpx;
  109. border-radius: 15rpx;
  110. border: 2rpx dashed #999;
  111. color: #999;
  112. margin-bottom: 6rpx;
  113. position: relative;
  114. .upico {
  115. color: #999;
  116. font-size: 70rpx;
  117. }
  118. .logo {
  119. width: 100%;
  120. height: 100%;
  121. border-radius: 15rpx;
  122. }
  123. .del {
  124. position: absolute;
  125. top: 6rpx;
  126. right: 6px;
  127. color: #fff;
  128. padding: 6rpx 12rpx;
  129. font-weight: 600;
  130. border-radius: 12rpx;
  131. background-color: rgba($color: #f20, $alpha: 0.6);
  132. }
  133. }
  134. </style>