file-upload.vue 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <template>
  2. <div>
  3. <el-upload
  4. :action="oss_url"
  5. :data="dataObj"
  6. :multiple="multiple"
  7. :show-file-list="isShowFileList"
  8. :file-list="fileList"
  9. :limit="limit"
  10. :before-upload="beforeUpload"
  11. :on-remove="handleRemove"
  12. :on-success="handleUploadSuccess"
  13. >
  14. <el-button :size="size" type="primary">{{
  15. multiple ? '点击上传' : fileList.length == 0 ? '点击上传' : '重新上传'
  16. }}</el-button>
  17. </el-upload>
  18. </div>
  19. </template>
  20. <script>
  21. import { getOssConfig } from '@/api/common'
  22. export default {
  23. name: 'FileUpload',
  24. props: {
  25. fileList: Array,
  26. multiple: {
  27. type: Boolean,
  28. default: false
  29. },
  30. fileType: {
  31. type: Array,
  32. default: () => ['image', 'video', 'word', 'excel', 'ppt', 'pdf']
  33. },
  34. size: {
  35. type: String,
  36. default: 'small'
  37. },
  38. limit: {
  39. type: Number,
  40. default: null
  41. }
  42. },
  43. data() {
  44. return {
  45. oss_url: '',
  46. dataObj: {},
  47. aliosstoken: '',
  48. flag: false
  49. }
  50. },
  51. computed: {
  52. isShowFileList: {
  53. get: function() {
  54. if (!this.multiple) {
  55. if (this.fileList.length > 0 && this.fileList[0].url) {
  56. return true
  57. } else {
  58. return false
  59. }
  60. }
  61. },
  62. set: function(newValue) {}
  63. }
  64. },
  65. created() {
  66. getOssConfig().then(res => {
  67. this.oss_url = res.data.host
  68. this.dataObj = res.data
  69. })
  70. },
  71. methods: {
  72. getUUID() {
  73. return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, c => {
  74. return (c === 'x' ? (Math.random() * 16) | 0 : 'r&0x3' | '0x8').toString(16)
  75. })
  76. },
  77. createName(name) {
  78. console.log(444)
  79. const date = Date.now()
  80. const uuid = this.getUUID()
  81. const fileSuffix = name.substring(name.lastIndexOf('.') + 1)
  82. return `${date}${uuid}.${fileSuffix}`
  83. },
  84. handleRemove(file, fileList) {
  85. if (!this.flag) {
  86. fileList.pop()
  87. this.fileList.pop()
  88. }
  89. this.flag = false
  90. // this.fileList = [{ name: '', url: '' }];
  91. },
  92. beforeUpload(file) {
  93. console.log(file, 999)
  94. const fileSuffix = file.name.substring(file.name.lastIndexOf('.') + 1)
  95. const imgList = ['jpg', 'jpeg', 'png']
  96. const videoList = ['mp4']
  97. const wordList = ['doc', 'docx', 'dot', 'wps', 'wpt']
  98. const excelList = ['xls', 'xlsx', 'xlt', 'et', 'ett']
  99. const pptList = ['ppt', 'pptx', 'dps', 'dpt', 'pot', 'pps']
  100. const pdfList = ['pdf']
  101. let whiteList = []
  102. if (this.fileType.includes('image')) {
  103. whiteList = whiteList.concat(imgList)
  104. }
  105. if (this.fileType.includes('video')) {
  106. whiteList = whiteList.concat(videoList)
  107. }
  108. if (this.fileType.includes('word')) {
  109. whiteList = whiteList.concat(wordList)
  110. }
  111. if (this.fileType.includes('excel')) {
  112. whiteList = whiteList.concat(excelList)
  113. }
  114. if (this.fileType.includes('ppt')) {
  115. whiteList = whiteList.concat(pptList)
  116. }
  117. if (this.fileType.includes('pdf')) {
  118. whiteList = whiteList.concat(pdfList)
  119. }
  120. console.log(whiteList.indexOf(fileSuffix))
  121. if (whiteList.indexOf(fileSuffix) === -1) {
  122. this.$errorMsg('只支持上传' + this.fileType.join(',') + '文件!')
  123. this.flag = true
  124. return false
  125. } else {
  126. const fileName = this.createName(file.name)
  127. const loading = this.$loading({
  128. lock: true,
  129. text: 'Loading',
  130. spinner: 'el-icon-loading',
  131. background: 'rgba(0, 0, 0, 0.7)'
  132. })
  133. this.dataObj.key = this.dataObj.dir + fileName
  134. }
  135. },
  136. handleUploadSuccess(res, file, fileList) {
  137. const loading = this.$loading({
  138. lock: true,
  139. text: 'Loading',
  140. spinner: 'el-icon-loading',
  141. background: 'rgba(0, 0, 0, 0.7)'
  142. })
  143. if (!this.multiple) {
  144. this.fileList.pop()
  145. }
  146. console.log(4747)
  147. this.fileList.push({
  148. name: file.name,
  149. status: 'success',
  150. url: this.dataObj.key
  151. })
  152. this.showFileList = true
  153. loading.close()
  154. }
  155. }
  156. }
  157. </script>