Uploader.vue 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <template>
  2. <div class="leo-uploader" v-loading="uploaderLoading">
  3. <el-upload
  4. class="uploader-box"
  5. v-show="uploaderShow"
  6. :action="uploaderRequestUrl"
  7. name="file"
  8. :headers="uploaderHeaders"
  9. :data="uploaderFormData"
  10. :show-file-list="false"
  11. :before-upload="uploaderHandleBefore"
  12. :on-success="uploaderHandleSuccess"
  13. :disabled="uploaderDisabled"
  14. :style="`width:${width};height:${height};`"
  15. >
  16. <img v-if="uploaderImageUrl" :src="uploaderImageUrl" class="image-preview"
  17. :style="`width:${width};height:${height};display: block;`">
  18. <i v-else class="el-icon-plus uploader-icon"
  19. :style="`width:${width};height:${height};line-height:${height};`"></i>
  20. </el-upload>
  21. <div class="image-show" v-show="!uploaderShow">
  22. <img :src="uploaderImageUrl" alt="" :width="width" :height="height">
  23. </div>
  24. </div>
  25. </template>
  26. <script>
  27. import {REQUEST_URL, ACCESS_TOKEN_PREFIX} from '@/utils/config'
  28. import userInfo from '@/utils/userInfo'
  29. import network from '../utils/network'
  30. export default {
  31. name: 'LeoUploader',
  32. props: {
  33. requestRoute: {
  34. type: String,
  35. required: true,
  36. },
  37. defaultImageUrl: {
  38. type: String,
  39. default: null,
  40. },
  41. uploaderSuccessCanChange: {
  42. type: Boolean,
  43. default: true,
  44. },
  45. width: {
  46. type: String,
  47. default: '720px',
  48. },
  49. height: {
  50. type: String,
  51. default: '480px',
  52. }
  53. },
  54. data() {
  55. return {
  56. uploaderShow: true,
  57. uploaderLoading: false,
  58. uploaderFormData: {
  59. 'uploadToken': '',
  60. },
  61. uploaderRequestUrl: `${REQUEST_URL}${this.requestRoute}`,
  62. uploaderHeaders: {
  63. 'Device-Type': 'pc',
  64. 'Suppress-Response-Code': '1',
  65. 'Authorization': '',
  66. },
  67. uploaderDisabled: false,
  68. successImageUrl: null,
  69. }
  70. },
  71. computed: {
  72. uploaderImageUrl() {
  73. if (this.successImageUrl !== null) {
  74. return this.successImageUrl
  75. }
  76. else if (this.defaultImageUrl !== null) {
  77. this.uploaderShow = false
  78. this.uploaderDisabled = true
  79. this.uploaderLoading = false
  80. return this.defaultImageUrl
  81. }
  82. }
  83. },
  84. watch: {
  85. requestRoute(newVal) {
  86. Object.assign(this.$data, this.$options.data())
  87. this.uploaderRequestUrl = `${REQUEST_URL}${this.requestRoute}`
  88. },
  89. },
  90. methods: {
  91. uploaderHandleBefore() {
  92. this.$message({
  93. message: '正在上传,请稍后。请勿关闭窗口!',
  94. type: 'warning',
  95. duration: 0,
  96. })
  97. this.uploaderLoading = true
  98. return network.updateToken().then(accessToken => {
  99. this.uploaderHeaders.Authorization = ACCESS_TOKEN_PREFIX + accessToken
  100. return network.getData(`file/token`)
  101. }).then(response => {
  102. this.uploaderFormData.uploadToken = response
  103. }).catch(response => {
  104. })
  105. },
  106. uploaderHandleSuccess(response, file) {
  107. this.$message.closeAll()
  108. if (response.success) {
  109. this.$message({
  110. message: '上传成功',
  111. type: 'success'
  112. })
  113. this.successImageUrl = URL.createObjectURL(file.raw)
  114. if (!this.uploaderSuccessCanChange) {
  115. this.uploaderShow = false
  116. this.uploaderDisabled = true
  117. this.uploaderLoading = false
  118. }
  119. this.uploaderLoading = false
  120. this.$emit('on-success', response.data)
  121. } else {
  122. this.$message({
  123. message: response.data.message,
  124. type: 'warning'
  125. })
  126. this.uploaderLoading = false
  127. }
  128. },
  129. }
  130. }
  131. </script>
  132. <style scoped>
  133. .uploader-box {
  134. border: 1px dashed #d9d9d9;
  135. border-radius: 6px;
  136. cursor: pointer;
  137. position: relative;
  138. overflow: hidden;
  139. }
  140. .uploader-box:hover {
  141. border-color: #409EFF;
  142. }
  143. .uploader-icon {
  144. font-size: 28px;
  145. color: #8c939d;
  146. text-align: center;
  147. }
  148. </style>