gui-choose-images.vue 3.7 KB

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