ludc
2025-01-15 c659560c7ee8d8f8278b938421de13bf65d1e1b1
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
159
160
161
162
163
164
<template>
  <el-dialog
    v-dialogDrag
    :close-on-click-modal="false"
    :title="title"
    :visible.sync="visible"
    width="40%"
    append-to-body
    class="avue-dialog"
    style="margin-top: -20vh !important;"
  >
    <Divider left="30px" text="导入提示">
    </Divider>
    <ul v-if="tipList.length === 0" style="color: #f33939">
      <li>
        请点击浏览文件进行上传
      </li>
    </ul>
 
    <ul style="color: #f33939" v-if="tipList.length >=1 ">
      <li v-for="(item, index) in tipList" :key="index">{{ index + 1 }}: {{ item }} 。</li>
    </ul>
    <Divider left="30px" text="选择文件后会自动上传"></Divider>
    <el-upload
      :name="fileName"
      :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: {
    // 控制上传类型 格式为: ['xls', 'xlsx'],
    fileType: {
      type: Array,
      default: () => []
    },
    fileName: {
      type: String,
      default: () => "file"
    },
    // 对话框头部名称
    title: {
      type: String,
      default: '上传文件'
    },
    // 必传 上传的地址
    fileUrl: {
      type: String,
      default: ''
    },
    // 请求地址 携带参数
    fileData: {
      type: Object,
      default: () => {}
    },
    // 导入提示文字  tipList:["导入模板中标明红色字体的为必输项","部门列上下级关系必须按照反斜杠隔开(/)"]
    tipList:{
      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(response) {
      if (response.code === 200) {
        this.$message.success("导入成功!");
        this.visible = false;
        this.$emit('updata',response);
      } else {
        this.$emit('upfaildata',response);
        this.$message.error(response.msg);
      }
    },
    //文件上传失败
    onError(res) {
      this.pageLoading.close();
      this.$message.error(res);
    },
    //文件状态改变
    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>