graceNvueSelectImg.vue 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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"></image>
  5. <text class="grace-add-list-remove grace-icons" @tap="removeImg" :id="'grace-items-img-'+index" :style="{color:closeBtnColor}">&#xe632;</text>
  6. </view>
  7. <view class="grace-add-list-items grace-add-list-btn" @tap="addImg" v-if="imgLists.length < maxFileNumber">
  8. <text class="grace-add-list-btn-icon">+</text>
  9. <text class="grace-add-list-btn-text">{{btnName}}</text>
  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{flex-wrap:wrap; flex-direction:row;}
  84. .grace-add-list-btn{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;}
  86. .grace-add-list-btn-icon{font-size:70rpx; height:80rpx; line-height:80rpx; color:#999999;}
  87. .grace-add-list-items{width:222rpx; height:222rpx; overflow:hidden; margin-bottom:12rpx; margin-right:11rpx; background-color:#F6F7F8; font-size:0; border-radius:10rpx;}
  88. .grace-add-list-image{width:222rpx; height:222rpx;}
  89. .grace-add-list-remove{width:50rpx; height:50rpx; line-height:50rpx; text-align:center; font-size:40rpx; position:absolute; right:0; bottom:0; color:#888888;}
  90. </style>