file-upload.vue 3.9 KB

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