分支自 SuZhouGuanHong/TaiYuanTaiZhong

dengjunjie
2024-01-16 5884c9023393061afbe6d3d6e709e53e672ddde8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<template>
  <div class="upload-container">
    <a :href="template.url" ref="template"></a>
    <div class="button-group">
      <Upload ref="uploadFile" :max-size="maxSize" :before-upload="beforeUpload" :action="url">
        <Button icon="ios-cloud-upload-outline">选择文件</Button>
      </Upload>
      <Button
        v-if="template.url"
        type="info"
        icon="md-arrow-round-down"
        @click="dowloadTemplate"
        :loading="loadingStatus"
      >下载模板</Button>
      <Button type="error" icon="md-arrow-round-up" @click="upload" :loading="loadingStatus">上传文件</Button>
    </div>
    <div class="alert">
      <Alert show-icon>只能上传excel文件,文件大小不超过5M</Alert>
    </div>
    <Divider>文件列表</Divider>
    <div class="file-info" v-if="file!= null">
      <span>文件名:{{file.name}}</span>
      <span>大小{{(file.size / 1024).toFixed(2)}}KB</span>
    </div>
    <slot></slot>
  </div>
</template>
<script>
import { error } from "util";
//目前只支持单个Excel上传,其他功能开发中...
export default {
  components: {},
  props: {
    url: {
      type: String,
      default: ""
    },
    template: {
      //下载模板配置
      type: Object,
      default: () => {
        return {
          url: "", //模板下载路径,如果没有模板路径,则不显示下载模板功能
          fileName: "未定义文件名" //下载模板的文件名
        };
      }
    }
  },
  data() {
    return {
      maxSize: 102 * 5,
      model: true,
      file: null,
      loadingStatus: false
    };
  },
  methods: {
    getFileType() {
      let fileName =
        this.file.name
          .split(".")
          .pop()
          .toLocaleLowerCase() || "";
      if (["numbers", "csv", "xls", "xlsx"].indexOf(fileName) == -1) {
        this.$message.error("只能选择excel文件");
        return false;
      }
      return true;
    },
    beforeUpload(file) {
      this.file = file;
      if (!this.getFileType()) {
        return false;
      }
      return false;
    },
    upload() {
      if (!this.url) {
        return this.$message.error("没有配置好Url");
      }
      if (!this.file) {
        return this.$message.error("请选择文件");
      }
      var forms = new FormData();
      forms.append("fileInput", this.file);
      this.loadingStatus = true;
      this.http.post(this.url, forms).then(
        x => {
          // this.$refs.uploadFile.clearFiles();
          this.loadingStatus = false;
          this.file = null;
          this.$emit('importExcelAfter',x);
          this.$Message[x.status?'success':'error'](x.message);
          //刷新表格数据
        },
        error => {
          this.loadingStatus = false;
        }
      );
    },
    dowloadTemplate() {
      let url = this.template.url;
      let xmlResquest = new XMLHttpRequest();
      xmlResquest.open("GET", url, true);
      xmlResquest.setRequestHeader("Content-type", "application/json");
      xmlResquest.setRequestHeader(
        "Authorization",
        this.$store.getters.getToken()
      );
      let fileName = this.template.fileName + ".xlsx";
      let elink = this.$refs.template;
      xmlResquest.responseType = "blob";
      let $_vue = this;
      this.loadingStatus = true;
      xmlResquest.onload = function(oEvent) {
        $_vue.loadingStatus = false;
        if (xmlResquest.response.type == "application/json") {
          return $_vue.message.error("未找到下载文件");
        }
        let content = xmlResquest.response;
        elink.download = fileName;
        let blob = new Blob([content]);
        elink.href = URL.createObjectURL(blob);
        elink.click();
      };
      xmlResquest.send();
    }
  }
};
</script>
<style lang="less" scoped>
.upload-container {
  display: inline-block;
  width: 100%;
  padding: 10px;
  border: 1px dashed #2d8cf0;
  min-height: 250px;
  border-radius: 5px;
  .alert {
    margin-top: 43px;
  }
  .button-group > * {
    float: left;
    margin-right: 10px;
  }
  .file-info > span {
    margin-right: 20px;
  }
}
</style>