index.vue 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <template>
  2. <div>
  3. <el-upload
  4. :class="{ 'el-upload-hidden': !chooseState }"
  5. :list-type="listType"
  6. :action="action"
  7. :headers="headers"
  8. :on-success="uploadImageSuccess"
  9. :on-remove="handleImageRemove"
  10. :before-upload="beforeUpload"
  11. :on-error="uploadError"
  12. :on-preview="handlePictureCardPreview"
  13. :limit="limit"
  14. :multiple="multiple"
  15. :accept="accept"
  16. :file-list="imageList"
  17. :disabled="disabled"
  18. >
  19. <div v-if="tip" slot="tip" class="el-upload__tip">{{ tip }}</div>
  20. <i slot="default" class="el-icon-plus" />
  21. </el-upload>
  22. <el-dialog :visible.sync="dialogVisible" v-if="showDetail">
  23. <img width="100%" :src="dialogImageUrl" />
  24. </el-dialog>
  25. </div>
  26. </template>
  27. <script>
  28. // import { mapGetters } from 'vuex'
  29. export default {
  30. name: 'SimpleUploadImage',
  31. props: {
  32. tip: {
  33. type: String,
  34. default: '',
  35. },
  36. multiple: {
  37. type: Boolean,
  38. default: false,
  39. },
  40. limit: {
  41. type: Number,
  42. default: 1,
  43. },
  44. accept: {
  45. type: String,
  46. default: '.jpg,.png,.gif',
  47. },
  48. listType: {
  49. type: String,
  50. default: 'picture-card',
  51. },
  52. imageList: {
  53. type: Array,
  54. default: () => [],
  55. },
  56. uuid: {
  57. type: Number,
  58. default: 0,
  59. },
  60. beforeUpload: {
  61. type: Function,
  62. default: () => true,
  63. },
  64. disabled: {
  65. type: Boolean,
  66. default: false,
  67. },
  68. showDetail: {
  69. type: Boolean,
  70. default: true,
  71. },
  72. },
  73. data() {
  74. return {
  75. dialogVisible: false,
  76. dialogImageUrl: '',
  77. }
  78. },
  79. computed: {
  80. // ...mapGetters(['token']),
  81. chooseState() {
  82. return this.imageList.length < this.limit
  83. },
  84. action() {
  85. return process.env.BASE_URL + '/wx/upload/image'
  86. },
  87. headers() {
  88. return {
  89. // 'X-Token': this.token,
  90. }
  91. },
  92. },
  93. methods: {
  94. // 上传成功
  95. uploadImageSuccess(response, file, fileList) {
  96. this.$emit('success', { response, file, fileList })
  97. },
  98. // 删除
  99. handleImageRemove(file, fileList) {
  100. this.$emit('remove', { file, fileList })
  101. },
  102. // 上传失败
  103. uploadError(err, file, fileList) {
  104. this.$emit('error', { err, file, fileList })
  105. },
  106. handlePictureCardPreview(file) {
  107. this.dialogImageUrl = file.url
  108. this.dialogVisible = true
  109. },
  110. },
  111. }
  112. </script>
  113. <style scoped lang="scss">
  114. ::v-deep {
  115. .el-upload-hidden {
  116. .el-upload {
  117. display: none;
  118. }
  119. }
  120. }
  121. </style>