index.vue 2.4 KB

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