graceNvueSelectImg.vue 3.0 KB

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