graceSelectImg.vue 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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" :data-imgurl="item" @tap="showImgs" class="grace-add-list-img" mode="widthFix"></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. },
  35. data() {
  36. return {
  37. imgLists : []
  38. }
  39. },
  40. created:function () {
  41. this.imgLists = this.items;
  42. },
  43. watch:{
  44. imgLists : function(newVal, oldVal){
  45. this.$emit('change', newVal);
  46. }
  47. },
  48. methods:{
  49. addImg : function(){
  50. var num = this.maxFileNumber - this.imgLists.length;
  51. if(num < 1){return false;}
  52. //uni.showLoading({title:""});
  53. uni.chooseImage({
  54. count: num,
  55. sizeType: ['compressed'],
  56. success:(res) => {
  57. this.imgLists = this.imgLists.concat(res.tempFilePaths);
  58. //uni.hideLoading();
  59. },
  60. fail:function(){
  61. //uni.hideLoading();
  62. }
  63. });
  64. },
  65. removeImg : function(e){
  66. var index = e.currentTarget.id.replace('grace-items-img-', '');
  67. this.imgLists.splice(index, 1);
  68. },
  69. showImgs : function(e){
  70. var currentImg = e.currentTarget.dataset.imgurl;
  71. uni.previewImage({
  72. urls: this.imgLists,
  73. current : currentImg
  74. })
  75. },
  76. setItems : function(items){
  77. this.imgLists = items;
  78. }
  79. }
  80. }
  81. </script>
  82. <style scoped>
  83. .grace-add-list{display:flex; flex-wrap:wrap;}
  84. .grace-add-list-btn{display:flex; flex-direction:column; align-items:center; justify-content:center;}
  85. .grace-add-list-btn-text{font-size:26rpx; line-height:36rpx; text-align:center; color:#999999; width:100%;}
  86. .grace-add-list-btn-icon{font-size:80rpx; height:80rpx; line-height:80rpx; margin-bottom:20rpx; color:#999999;}
  87. .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;}
  88. .grace-add-list-image{width:222rpx;}
  89. .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;}
  90. .grace-add-list-img{width:222rpx;}
  91. </style>