import-button.vue 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <template>
  2. <div>
  3. <el-upload
  4. style="display: inline-block; margin: 0 10px;"
  5. action="import"
  6. :http-request="handleImport"
  7. :file-list="importFileList"
  8. >
  9. <el-button size="small" type="primary" :icon="isIcon ? 'el-icon-download':''" :loading="importLoading">{{ importLoading ? '导入中...' : imText }}</el-button>
  10. </el-upload>
  11. </div>
  12. </template>
  13. <script>
  14. import { handleImport } from '@/utils/util'
  15. export default {
  16. name: 'ImportButton',
  17. props: {
  18. imText: { // 按钮文字
  19. type: String,
  20. default: '导入数据'
  21. },
  22. isIcon: { // 是否显示icon图标
  23. type: Boolean,
  24. default: true
  25. },
  26. imUrl: { // 导入地址
  27. type: String,
  28. default: '',
  29. },
  30. imParams: { // 导入参数
  31. type: Object,
  32. default: null
  33. }
  34. },
  35. data() {
  36. return {
  37. importLoading: false,
  38. importFileList: [],
  39. }
  40. },
  41. methods: {
  42. async handleImport(param) {
  43. this.importLoading = true;
  44. const file = param.file;
  45. const formData = new FormData();
  46. formData.append("file", file);
  47. if(this.imParams) {
  48. for (let key in this.imParams) {
  49. if (this.imParams.hasOwnProperty(key)) {
  50. formData.append(key, this.imParams[key]);
  51. }
  52. }
  53. }
  54. let result = await handleImport(this.imUrl, formData);
  55. this.importLoading = false;
  56. this.importFileList = [];
  57. if(result.code == 200) {
  58. this.$alert(result.message, '导入成功', {
  59. confirmButtonText: '确定'
  60. });
  61. this.$emit('importSuccess');
  62. }else {
  63. this.$alert(result.message, '导入失败', {
  64. confirmButtonText: '确定'
  65. });
  66. }
  67. },
  68. }
  69. }
  70. </script>
  71. <style>
  72. </style>