Uploader.vue 4.3 KB

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