g-upload.vue 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <template>
  2. <view class="imglistbx">
  3. <view :class="['imglistItem',columnNum==3?'column3':'column4']" v-for="(item,index) in showList" :key='index'>
  4. <image :src="item" class="itemImg" @click="previewImage(index)" mode="aspectFill"></image>
  5. <icon size="18" type="cancel" class="cancelBtn" @click="deleteImg(index)" v-if="deleteBtn"></icon>
  6. </view>
  7. <!-- 上传控件 -->
  8. <view :class="['imglistItem',columnNum==3?'column3':'column4']" @click="uploadImg" v-if="control&&showControl">
  9. <view class="itemImg uploadControl">+</view>
  10. </view>
  11. <view class="clear"></view>
  12. </view>
  13. </template>
  14. <script>
  15. export default {
  16. props: {
  17. //是否显示上传控件
  18. control: {
  19. type: Boolean,
  20. default: true
  21. },
  22. //是否显示上删除按钮
  23. deleteBtn: {
  24. type: Boolean,
  25. default: true
  26. },
  27. //行数量
  28. columnNum: {
  29. type: [Number, String],
  30. default: 4
  31. },
  32. //上传最大数量
  33. maxCount: {
  34. type: Number,
  35. default: 4
  36. },
  37. //服务返回回调的图片数组--回填
  38. mode: {
  39. type: Array,
  40. default: function() {
  41. return []
  42. }
  43. }
  44. },
  45. data() {
  46. return {
  47. imgList: [],
  48. showList: [],
  49. showControl: true
  50. }
  51. },
  52. watch: {
  53. mode(v) {
  54. this.init(v)
  55. },
  56. showList(){
  57. if (this.showList.length >= this.maxCount) {
  58. this.showControl = false
  59. return
  60. };
  61. this.showControl = true;
  62. }
  63. },
  64. created() {
  65. this.init(this.mode)
  66. },
  67. methods: {
  68. init(v) {
  69. if (this.mode.length != 0) {
  70. this.showList = v;
  71. return
  72. };
  73. this.showList = this.imgList;
  74. },
  75. // 上传图片
  76. uploadImg() {
  77. uni.chooseImage({
  78. sizeType: ['compressed '],
  79. count: this.maxCount,
  80. success: (chooseImageRes) => {
  81. console.info('chooseImageRes:',chooseImageRes);
  82. let tempFilePaths = chooseImageRes.tempFilePaths;
  83. let tempFiles=chooseImageRes.tempFiles;
  84. console.info("文件大小",tempFiles[0].size/(1024)+'KB');
  85. console.info("文件类型",tempFiles[0].name);
  86. tempFilePaths=tempFilePaths.slice(0,this.maxCount-this.showList.length);
  87. tempFilePaths.forEach((item) => {
  88. this.imgList.push(item);
  89. })
  90. this.$emit("chooseFile", this.imgList, tempFilePaths)
  91. }
  92. });
  93. },
  94. //删除图片
  95. deleteImg(eq) {
  96. let deleteImg = this.imgList;
  97. deleteImg.splice(eq, 1); //删除临时路径
  98. if (this.mode.length > 0) {
  99. let deleteImg = this.showList;
  100. deleteImg.splice(eq, 1); //删除服务那边的路径
  101. }
  102. this.$emit("imgDelete", this.handleImg(), eq)
  103. },
  104. // 预览图片
  105. previewImage(eq) {
  106. let getUrl = this.handleImg();
  107. uni.previewImage({
  108. current: getUrl[eq],
  109. urls: getUrl
  110. })
  111. },
  112. //返回需要操作的图片数组
  113. //如果是回调了则操作回填后的数组 否则操作临时路径的图片数组
  114. handleImg() {
  115. return this.mode.length > 0 ? this.showList : this.imgList
  116. },
  117. }
  118. }
  119. </script>
  120. <style scoped>
  121. /* 上传 str */
  122. .imglistbx {
  123. width: 100%;
  124. height: 100%;
  125. }
  126. .imglistItem {
  127. position: relative;
  128. float: left;
  129. margin-bottom: 20rpx;
  130. border-radius: 10rpx;
  131. }
  132. .column3 {
  133. width: 33.3333%;
  134. height: 160rpx;
  135. }
  136. .column4 {
  137. width: 25%;
  138. height: 130rpx;
  139. }
  140. .itemImg {
  141. width: 70%;
  142. height: 100%;
  143. margin: 0 auto;
  144. display: block;
  145. border-radius: 10rpx;
  146. }
  147. .cancelBtn {
  148. position: absolute;
  149. top: -10rpx;
  150. right: 10rpx;
  151. }
  152. /* 上传控件 */
  153. .uploadControl {
  154. font-size: 50rpx;
  155. color: #888;
  156. background-color: #EEEEEE;
  157. display: flex;
  158. justify-content: center;
  159. align-items: center;
  160. }
  161. /* 上传 str end*/
  162. .clear {
  163. clear: both;
  164. }
  165. </style>