index.vue 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <template>
  2. <el-upload
  3. action="/api-web/uploadFile"
  4. :headers="headers"
  5. :on-success="success"
  6. :on-remove="remove"
  7. :on-progress="progress"
  8. :on-error="error"
  9. :file-list="filelist"
  10. :accept="accept">
  11. <el-button size="small" type="primary" plain >点击上传</el-button>
  12. <div slot="tip" v-if="tips" class="el-upload__tip">{{tips}}</div>
  13. <div slot="file" slot-scope="{file}">
  14. <div style="display: flex; align-items: center;flex: 1 auto;justify-content: space-between;">
  15. <div style="display: flex; align-items: center;overflow: hidden;">
  16. <i v-if="!!file.url" @click.stop="copyText(file.url)" title="复制" style="padding-right: 5px" class="el-icon-document-copy"></i>
  17. <span class="upload-text" :title="file.url">{{ file.url }}</span>
  18. </div>
  19. <i v-if="!!file.url" class="el-icon-delete" @click="remove"></i>
  20. </div>
  21. </div>
  22. </el-upload>
  23. </template>
  24. <script>
  25. import copy from 'copy-to-clipboard'
  26. import { getToken } from '@/utils/auth'
  27. import load from '@/utils/loading'
  28. export default {
  29. name: 'singe-file-upload',
  30. props: {
  31. tips: {
  32. type: String,
  33. default: ''
  34. },
  35. value: {
  36. type: String,
  37. default: ''
  38. },
  39. accept: {
  40. type: String,
  41. default: ''
  42. }
  43. },
  44. watch: {
  45. value: {
  46. handler() {
  47. if (this.value) {
  48. this.filelist = [{
  49. name: this.value,
  50. url: this.value,
  51. }]
  52. } else {
  53. this.remove()
  54. }
  55. },
  56. immediate: true
  57. }
  58. },
  59. data() {
  60. return {
  61. filelist: [],
  62. headers: {
  63. Authorization: getToken()
  64. },
  65. }
  66. },
  67. methods: {
  68. remove() {
  69. this.filelist = []
  70. this.$emit('update:value', '')
  71. this.$emit('input', '')
  72. },
  73. error() {
  74. this.remove()
  75. load.endLoading()
  76. },
  77. progress() {
  78. load.startLoading()
  79. },
  80. success(res) {
  81. load.endLoading()
  82. if (res.code == 200) {
  83. this.filelist = [{
  84. name: res.data.url,
  85. url: res.data.url,
  86. }]
  87. this.$emit('update:value', res.data.url)
  88. this.$emit('input', res.data.url)
  89. } else {
  90. this.remove()
  91. this.$message.error(res.msg || '上传失败')
  92. }
  93. },
  94. copyText(text) {
  95. if (text) {
  96. copy(text)
  97. this.$message.success('复制成功')
  98. }
  99. }
  100. }
  101. }
  102. </script>
  103. <style lang="less" scoped>
  104. .upload-text {
  105. display: inline-block;
  106. width: 100%;
  107. overflow: hidden;
  108. white-space: nowrap;
  109. text-overflow: ellipsis;
  110. }
  111. </style>