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
151
152
153
154
155
156
157
158
<template>
  <el-dialog
    :visible.sync="dialogVisible"
    append-to-body
    title="上传文件"
  >
    <Divider left="30px" text="导入提示"></Divider>
    <ul>
      <li>
        1.红色字体表示必输项
      </li>
      <li>
        2.每次仅能最多导入10000条数据
      </li>
    </ul>
    <Divider left="30px" text="excel文件,选择文件后会自动上传"></Divider>
    <el-upload
      :before-upload="beforeUpload"
      :headers="uploadHeaders"
      :on-change="uploadChange"
      :on-error="onError"
      :on-success="onSuccess"
      :show-file-list="false"
      accept=".xlsx, .xls"
      action="/api/ubcs-code/codeClassify/importClassify"
      class="upload-demo">
      <el-button size="small" style="margin: 15px 35px" type="primary">浏览文件</el-button>
    </el-upload>
    <template #footer>
      <el-button
        :loading="downloadLoading"
        size="small"
        type="primary"
        @click="downloadTemplateFun"
      >下载导入模板
      </el-button
      >
      <el-button size="small" @click="dialogVisible = false">关闭</el-button>
    </template>
  </el-dialog>
</template>
 
<script>
// import {downloadErrorFile,downloadBatchImportApplyTemplate} from '@/api/template/templateAttr'
import {getToken} from "@/util/auth";
import func from "@/util/func";
 
export default {
  name: "Upload-files",
  props: {
    visible: {
      type: Boolean,
      default: false,
    },
    codeClassifyOid: {
      type: String,
      default: "",
    },
  },
  data() {
    return {
      flga: true,
      pageLoading: null,
      downloadLoading: false,
    }
  },
  watch: {
    visible: {
      handler(newval, oldval) {
        // console.log('newval',newval)
      }
    }
  },
  computed: {
    uploadHeaders() {
      return {
        "Blade-Auth": "bearer " + getToken(),
      };
    },
    dialogVisible: {
      get() {
        return this.visible;
      },
      set(val) {
        this.$emit("update:visible", val);
      },
    },
  },
  methods: {
    //文件上传前
    async beforeUpload(file) {
      const fileType = file.name.split(".").pop();
      if (fileType !== "xlsx" && fileType !== "xls") {
        // 上传格式不符合要求,提示错误信息并取消上传
        this.$message.error("只允许上传xlsx、xls格式的文件");
        return Promise.reject(false);
      }
      this.pageLoading = this.$loading({
        lock: true,
        text: "文件上传中",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.7)",
      });
      return true;
    },
    // 文件上传成功
    onSuccess(resbonse) {
      if (Object.keys(resbonse.data).length === 0) {
        this.$message.success("上传成功!");
        this.dialogVisible = false;
        return;
      }
      if (resbonse.data.fileOid) {
        const fileName = resbonse.data.filePath.split("/").pop();
        this.$message.error("请下载错误信息文件进行查看!");
        downloadErrorFile({uuid: resbonse.data.fileOid}).then((res) => {
          func.downloadFileByBlobHandler(res);
        });
      }
    },
    //点击下载模板
    downloadTemplateFun() {
      this.downloadLoading = true;
      downloadBatchImportApplyTemplate({codeClassifyOid: this.codeClassifyOid}).then(res => {
        this.$utilFunc.downloadFileByBlob(res.data, "模板文件.xls");
        this.downloadLoading = false;
      }).catch((res) => {
        this.$message.warning(res)
        this.downloadLoading = false;
      })
    },
    //文件上传失败
    onError(res) {
      this.pageLoading.close();
    },
    //文件状态改变
    uploadChange(file) {
      if (file.status === "success" || file.status === "error") {
        this.pageLoading.close();
      }
    }
  }
}
</script>
 
<style lang="scss" scoped>
ul {
  color: rgb(188, 188, 188);
  margin: 20px 0 20px 0;
  padding: 0 0 0 30px;
  list-style: none;
 
  li {
    margin-bottom: 5px;
    font-size: 13px;
  }
}
</style>