gui-choose-images.vue 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <template>
  2. <view class="gui-flex gui-row gui-wrap">
  3. <view
  4. class="gui-add-list-items"
  5. :style="{borderRadius:borderRadius}"
  6. v-for="(item, index) in imgLists"
  7. :key="index">
  8. <image
  9. :src="item.url"
  10. :data-imgurl="item.url"
  11. :style="{borderRadius:borderRadius}"
  12. @tap="showImgs"
  13. class="gui-add-list-img"
  14. :mode="imgMode"></image>
  15. <text
  16. class="gui-add-list-remove gui-icons"
  17. :style="{color:removeBtnColor}"
  18. @tap="removeImg"
  19. :id="'gui-items-img-'+index">&#xe632;</text>
  20. </view>
  21. <view
  22. class="gui-add-list-items gui-flex gui-columns gui-justify-content-center gui-align-items-center gui-bg-gray gui-dark-bg-level-5"
  23. @tap="addImg"
  24. v-if="imgLists.length < maxFileNumber"
  25. :style="{borderRadius:borderRadius}">
  26. <text class="gui-add-list-btn-icon gui-block-text gui-color-gray">+</text>
  27. <text class="gui-add-list-btn-text gui-block-text gui-color-gray">{{btnName}}</text>
  28. </view>
  29. </view>
  30. </template>
  31. <script>
  32. export default {
  33. name : "gui-choose-images",
  34. props : {
  35. maxFileNumber : { type : Number, default : 9 },
  36. btnName : { type : String, default : "添加照片" },
  37. items : { type : Array, default : function () {return []; }},
  38. removeBtnColor : { type : String, default : "rgba(0, 0, 0, 0.8)" },
  39. imgMode : { type : String, default : 'widthFix' },
  40. sourceType : { type : Array, default : function () {return ['album', 'camera'];}},
  41. borderRadius : { type : String, default : "10rpx" }
  42. },
  43. data() {
  44. return {
  45. imgLists : []
  46. }
  47. },
  48. created:function () {
  49. this.initImgs();
  50. },
  51. watch:{
  52. items:function(){ this.initImgs(); }
  53. },
  54. methods:{
  55. initImgs : function(){
  56. var imgs = [];
  57. for(let i = 0; i < this.items.length; i++){
  58. imgs.push({url:this.items[i], progress : 100});
  59. }
  60. this.imgLists = imgs;
  61. },
  62. addImg : function(){
  63. var num = this.maxFileNumber - this.imgLists.length;
  64. if(num < 1){return false;}
  65. uni.chooseImage({
  66. count : num,
  67. sizeType : ['compressed'],
  68. sourceType : this.sourceType,
  69. success : (res) => {
  70. if(this.imgLists.length >= this.maxFileNumber){return ;}
  71. for(let i = 0; i < res.tempFilePaths.length; i++){
  72. if(this.imgLists.length >= this.maxFileNumber){break;}
  73. this.imgLists.push({url:res.tempFilePaths[i], progress:0});
  74. }
  75. this.$emit('change', this.imgLists);
  76. },
  77. complete : function(){}
  78. });
  79. },
  80. removeImg : function(e){
  81. var index = e.currentTarget.id.replace('gui-items-img-', '');
  82. var removeImg = this.imgLists.splice(index, 1);
  83. this.$emit('removeImg', removeImg[0]);
  84. this.$emit('change' , this.imgLists);
  85. },
  86. showImgs : function(e){
  87. var currentImg = e.currentTarget.dataset.imgurl;
  88. var imgs = [];
  89. for(let i = 0; i < this.imgLists.length; i++){
  90. imgs.push(this.imgLists[i].url);
  91. }
  92. uni.previewImage({
  93. urls: imgs,
  94. current : currentImg
  95. })
  96. },
  97. setItems : function(items){
  98. this.imgLists = [];
  99. for(let i = 0; i < items.length; i++){
  100. this.imgLists.push({url : items[i], progress : 100});
  101. }
  102. this.$emit('change', this.imgLists);
  103. }
  104. },
  105. emits : ['change', 'removeImg']
  106. }
  107. </script>
  108. <style scoped>
  109. .gui-add-list-btn-text{font-size:26rpx; line-height:36rpx; text-align:center;}
  110. .gui-add-list-btn-icon{font-size:80rpx; height:80rpx; line-height:80rpx; margin-bottom:20rpx;}
  111. .gui-add-list-items{width:210rpx; height:210rpx; overflow:hidden; margin:10rpx; font-size:0; position:relative;}
  112. .gui-add-list-remove{width:60rpx; height:60rpx; line-height:60rpx; text-align:center; font-size:44rpx; position:absolute; z-index:1; right:0; bottom:0;}
  113. .gui-add-list-img{width:210rpx;}
  114. </style>