index.vue 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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="#007AFF" @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="#007AFF"></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. },
  32. methods: {
  33. uplSuc() {
  34. const that = this;
  35. let serverFile = that.$config.serverFile;
  36. uni.chooseImage({
  37. count: 1,
  38. sizeType: ['original', 'compressed'],
  39. sourceType: ['album', 'camera'],
  40. success: async function(res) {
  41. let tempFile = JSON.parse(JSON.stringify(res.tempFilePaths));
  42. const arr = await that.$apifile(`/applet/upload`, 'file', tempFile[0],
  43. 'file');
  44. if (arr.errcode == '0') {
  45. that.$emit('uplSuc', {
  46. data: {
  47. id: arr.data.name,
  48. name: arr.data.name,
  49. uri: arr.data.uri,
  50. url: serverFile + arr.data.uri
  51. },
  52. name: that.name
  53. })
  54. } else {
  55. uni.showToast({
  56. title: arr.errmsg,
  57. icon: 'none'
  58. });
  59. }
  60. }
  61. });
  62. },
  63. // 删除图片
  64. uplDel(index, e) {
  65. const that = this;
  66. that.$emit('uplDel', {
  67. data: {
  68. file: e,
  69. index: index
  70. },
  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-wrap: wrap;
  91. .list {
  92. position: relative;
  93. width: 25vw;
  94. height: 25vw;
  95. text-align: center;
  96. margin: 1vw;
  97. border: 1px solid var(--f99Color);
  98. .image {
  99. width: 100%;
  100. height: 100%;
  101. }
  102. .add {
  103. position: relative;
  104. top: 20px;
  105. }
  106. .del {
  107. position: absolute;
  108. right: 0;
  109. top: 0;
  110. }
  111. }
  112. }
  113. }
  114. </style>