import-upload.vue 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <template>
  2. <div>
  3. <el-upload
  4. :action="oss_url"
  5. :data="dataObj"
  6. :multiple="false"
  7. :show-file-list="showFileList"
  8. :file-list="fileList"
  9. :before-upload="beforeUpload"
  10. :on-remove="handleRemove"
  11. :on-success="handleUploadSuccess"
  12. >
  13. <el-button size="small" type="primary">{{fileList.length == 0 ? '点击上传':'重新上传'}}</el-button>
  14. </el-upload>
  15. </div>
  16. </template>
  17. <script>
  18. import request from '@/utils/request';
  19. import { getOssConfig } from '@/api/common'
  20. export default {
  21. name: 'importUpload',
  22. props: {
  23. fileList: Array
  24. },
  25. data() {
  26. return {
  27. oss_url: '',
  28. dataObj: {},
  29. };
  30. },
  31. created() {
  32. getOssConfig().then(res => {
  33. this.oss_url = res.data.host
  34. })
  35. },
  36. computed: {
  37. showFileList: {
  38. get: function() {
  39. if (this.fileList.length > 0 && this.fileList[0].url) {
  40. return true;
  41. } else {
  42. return false;
  43. }
  44. },
  45. set: function(newValue) {}
  46. }
  47. },
  48. methods: {
  49. // 获取oss配置
  50. async getOssConfig(fileName) {
  51. const result = await new Promise((resolve, reject) => {
  52. getOssConfig()
  53. .then(res => {
  54. const fileKey = this.createName(fileName)
  55. res.data.name = fileName
  56. res.data.key = res.data.dir + fileKey
  57. resolve(res.data)
  58. })
  59. .catch(res => {
  60. resolve({})
  61. })
  62. })
  63. return result
  64. },
  65. getUUID() {
  66. return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, c => {
  67. return (c === 'x' ? (Math.random() * 16) | 0 : 'r&0x3' | '0x8').toString(16);
  68. });
  69. },
  70. createName(name) {
  71. const date = Date.now();
  72. const uuid = this.getUUID();
  73. const fileSuffix = name.substring(name.lastIndexOf(".") + 1);
  74. return `${date}${uuid}.${fileSuffix}`;
  75. },
  76. handleRemove(file, fileList) {
  77. this.fileList = [{ name: '', url: '' }];
  78. },
  79. async beforeUpload(file) {
  80. const fileSuffix = file.name.substring(file.name.lastIndexOf(".") + 1);
  81. const whiteList = ['xls', 'xlsx', 'xlsm'];
  82. if (whiteList.indexOf(fileSuffix) === -1) {
  83. return this.$errorMsg('只支持上传excel表格文件!');
  84. }
  85. const fileName = this.createName(file.name);
  86. const loading = this.$loading({
  87. lock: true,
  88. text: 'Loading',
  89. spinner: 'el-icon-loading',
  90. background: 'rgba(0, 0, 0, 0.7)'
  91. });
  92. this.dataObj = await this.getOssConfig(fileName);
  93. },
  94. handleUploadSuccess(res, file, fileList) {
  95. const loading = this.$loading({
  96. lock: true,
  97. text: 'Loading',
  98. spinner: 'el-icon-loading',
  99. background: 'rgba(0, 0, 0, 0.7)'
  100. });
  101. this.fileList.pop();
  102. this.fileList.push({
  103. name: file.name,
  104. url: this.dataObj.key
  105. });
  106. this.showFileList = true;
  107. loading.close();
  108. }
  109. }
  110. };
  111. </script>