田源
2024-07-05 5700aa436cf4e058cef10560c324e2ee762eebcc
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
<template>
  <el-dialog
    v-dialogDrag
    :close-on-click-modal="false"
    :destroy-on-close="true"
    :title="title"
    :visible.sync="visible"
    width="40%"
    append-to-body
    class="avue-dialog"
    style="margin-top: -20vh !important;"
  >
    <Divider left="30px" text="导入提示"></Divider>
    <ul>
      <li>
        请点击浏览文件进行上传
      </li>
    </ul>
    <Divider left="30px" text="选择文件后会自动上传"></Divider>
    <el-upload
      :action="fileUrl"
      :before-upload="beforeUpload"
      :data="fileData"
      :headers="uploadHeaders"
      :on-change="uploadChange"
      :on-error="onError"
      :on-success="onSuccess"
      :show-file-list="false"
      class="upload-demo">
      <el-button size="small" style="margin: 15px 35px" type="primary">浏览文件</el-button>
    </el-upload>
    <template #footer>
      <el-button size="small" @click="visible = false">关闭</el-button>
    </template>
  </el-dialog>
</template>
 
<script>
import {getToken} from "@/util/auth";
import func from "@/util/func";
import {validatenull} from "@/util/validate";
 
export default {
  name: "upload-file",
  props: {
    fileType: {
      type: Array,
      default: () => []
    },
    title: {
      type: String,
      default: '上传文件'
    },
    fileUrl: {
      type: String,
      default: ''
    },
    fileData: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      flga: true,
      pageLoading: null,
      downloadLoading: false,
      visible: false,
    }
  },
  created() {
  },
  watch: {
    visible: {
      handler(newval, oldval) {
        // console.log('newval',newval)
      }
    }
  },
  computed: {
    uploadHeaders() {
      return {
        "Authorizationtoken": getToken(),
      };
    },
  },
  methods: {
    //文件上传前
    async beforeUpload(file) {
      // 获取文件扩展名
      const fileExtension = file.name.split(".").pop().toLowerCase(); // 转换为小写以避免大小写不匹配的问题
      if (this.fileType) {
        if (!this.fileType.includes(fileExtension)) {
          // 上传格式不符合要求,提示错误信息并取消上传
          this.$message.error(`只允许上传${this.fileType.toString()}格式的文件`);
          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) {
      console.log(resbonse);
      if (resbonse.code === 200) {
        this.$message.success("导入成功!");
        this.visible = false;
        this.$emit('updata');
      } else {
        this.$message.error(resbonse.msg);
      }
    },
    //文件上传失败
    onError(res) {
      this.pageLoading.close();
      this.$message.error(res);
    },
    //文件状态改变
    uploadChange(file) {
      console.log( this.pageLoading);
      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>