importUpload.vue 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. export default {
  20. name: 'importUpload',
  21. props: {
  22. fileList: Array
  23. },
  24. data() {
  25. return {
  26. oss_url: '',
  27. dataObj: {
  28. policy: '',
  29. signature: '',
  30. key: '',
  31. ossaccessKeyId: '',
  32. },
  33. aliosstoken: '',
  34. };
  35. },
  36. created() {
  37. request({
  38. url: '/qviKHUYsyN.php/index/getAliOssToken',
  39. method: 'get',
  40. params: {}
  41. }).then(res => {
  42. this.aliosstoken = res.data.aliosstoken;
  43. this.oss_url = res.data.upload_host;
  44. });
  45. },
  46. computed: {
  47. showFileList: {
  48. get: function() {
  49. if (this.fileList.length > 0 && this.fileList[0].url) {
  50. return true;
  51. } else {
  52. return false;
  53. }
  54. },
  55. set: function(newValue) {}
  56. }
  57. },
  58. methods: {
  59. getUUID() {
  60. return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, c => {
  61. return (c === 'x' ? (Math.random() * 16) | 0 : 'r&0x3' | '0x8').toString(16);
  62. });
  63. },
  64. createName(name) {
  65. const date = Date.now();
  66. const uuid = this.getUUID();
  67. const fileSuffix = name.substring(name.lastIndexOf(".") + 1);
  68. return `${date}${uuid}.${fileSuffix}`;
  69. },
  70. handleRemove(file, fileList) {
  71. this.fileList = [{ name: '', url: '' }];
  72. },
  73. beforeUpload(file) {
  74. const that = this;
  75. const fileSuffix = file.name.substring(file.name.lastIndexOf(".") + 1);
  76. const whiteList = ['xls', 'xlsx', 'xlsm'];
  77. if (whiteList.indexOf(fileSuffix) === -1) {
  78. return this.$errorMsg('只支持上传excel表格文件!');
  79. }
  80. const fileName = this.createName(file.name);
  81. const loading = this.$loading({
  82. lock: true,
  83. text: 'Loading',
  84. spinner: 'el-icon-loading',
  85. background: 'rgba(0, 0, 0, 0.7)'
  86. });
  87. return new Promise((resolve, reject) => {
  88. request({
  89. url: '/addons/alioss/index/params',
  90. method: 'post',
  91. data: {
  92. method: 'POST',
  93. md5: fileName.substring(0, fileName.indexOf('.')),
  94. name: fileName,
  95. type: file.type,
  96. size: file.size,
  97. aliosstoken: this.aliosstoken
  98. }
  99. }).then(res => {
  100. that.dataObj = res.data;
  101. resolve(true);
  102. }).catch(err => {
  103. reject(false);
  104. });
  105. });
  106. },
  107. handleUploadSuccess(res, file, fileList) {
  108. const loading = this.$loading({
  109. lock: true,
  110. text: 'Loading',
  111. spinner: 'el-icon-loading',
  112. background: 'rgba(0, 0, 0, 0.7)'
  113. });
  114. this.fileList.pop();
  115. this.fileList.push({
  116. name: file.name,
  117. url: this.dataObj.key
  118. });
  119. this.showFileList = true;
  120. loading.close();
  121. }
  122. }
  123. };
  124. </script>