index.vue 2.5 KB

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