index.vue 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <template>
  2. <view class="upload">
  3. <view class="upload_1">
  4. <view class="list" v-for="(item, index) in list" :key="index">
  5. <image class="image" :src="item.url" @click="toView(index, item)"></image>
  6. <uni-icons class="del" type="close" size="30" color="#ff0000" @click="uplDel(index, item)"></uni-icons>
  7. </view>
  8. <view class="list" v-if="list && list.length < count" @tap="uplSuc()">
  9. <uni-icons class="add" type="plusempty" size="55" color="#ff0000"></uni-icons>
  10. </view>
  11. </view>
  12. </view>
  13. </template>
  14. <script>
  15. export default {
  16. props: {
  17. list: {
  18. type: Array,
  19. default: []
  20. },
  21. name: {
  22. type: String,
  23. },
  24. count: {
  25. type: Number,
  26. default: 1,
  27. },
  28. },
  29. data() {
  30. return {};
  31. },
  32. methods: {
  33. uplSuc() {
  34. const that = this;
  35. let serverUrl = that.$config.fileserverUrl;
  36. uni.chooseImage({
  37. count: 1,
  38. sizeType: ["original", "compressed"],
  39. sourceType: ["album", "camera"],
  40. async success(chooseRes) {
  41. let file = JSON.parse(JSON.stringify(chooseRes.tempFilePaths));
  42. let res = await that.$apifile("files/projectadmin/upload", null, file[0]);
  43. res = JSON.parse(res);
  44. if (res.errcode == "0") {
  45. let data = {
  46. id: res.id,
  47. name: res.name,
  48. uri: res.uri,
  49. url: serverUrl + res.uri
  50. }
  51. that.$emit("uplSuc", {
  52. data: data,
  53. name: that.name,
  54. });
  55. } else {
  56. uni.showToast({
  57. title: '请求接口失败',
  58. icon: 'fail',
  59. });
  60. }
  61. },
  62. });
  63. },
  64. // 删除图片
  65. uplDel(index, e) {
  66. const that = this;
  67. let data = {
  68. ...e,
  69. index: index
  70. }
  71. that.$emit("uplDel", {
  72. data: data,
  73. name: that.name,
  74. });
  75. },
  76. // 图片预览
  77. toView(index, e) {
  78. const that = this;
  79. uni.previewImage({
  80. current: index,
  81. urls: [e.url],
  82. });
  83. },
  84. },
  85. };
  86. </script>
  87. <style lang="scss">
  88. .upload {
  89. padding: 0 2vw;
  90. .upload_1 {
  91. display: flex;
  92. flex-direction: row;
  93. flex-wrap: wrap;
  94. .list {
  95. position: relative;
  96. width: 30vw;
  97. height: 28vw;
  98. text-align: center;
  99. margin: 0 3vw 2vw 0;
  100. border-radius: 5px;
  101. box-shadow: 0 0 2px #858585;
  102. .image {
  103. width: 100%;
  104. height: 100%;
  105. border-radius: 5px;
  106. }
  107. .add {
  108. position: relative;
  109. top: 20px;
  110. }
  111. .del {
  112. position: absolute;
  113. right: 0;
  114. top: 0;
  115. }
  116. }
  117. .list:nth-child(3n) {
  118. margin: 0 0 2vw 0;
  119. }
  120. }
  121. }
  122. </style>