Uploader.vue 4.2 KB

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