graceSelectImg.vue 3.5 KB

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