add
zongzhibin
2024-11-24 d89732b82ff7fc047628ca3772311a0fbeea3553
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
<template>
    <div class="testStudent-container" >
        
        <el-dialog v-model="isShowDialog"  :width="800" draggable="" :close-on-click-modal="false"  :show-close="false">
            <template #header>
                <div style="color: #fff">
                    <el-icon size="16" style="margin-right: 3px; display: inline; vertical-align: middle"> <ele-UploadFilled /> </el-icon>
                    <span> {{title}} </span>
                </div>
            </template>
            <div>
                <el-upload ref="uploadRef" v-loading="state.loading" drag :auto-upload="false" :limit="1" :file-list="state.fileList" action="" :on-change="handleChange" accept=".xlsx">
                    <el-icon class="el-icon--upload">
                        <ele-UploadFilled />
                    </el-icon>
                    <div class="el-upload__text">将文件拖到此处,或<em>点击上传</em></div>
                    <template #tip>
                        <div class="el-upload__tip">请上传大小不超过 10MB 的文件</div>
                    </template>
                </el-upload>
                <span>【</span>
        <el-button style="width: 90px;text-align: left;" type="warning" @click="downFile" link>下载导入模板</el-button>,填写并上传
        <span>】</span>
            </div>
            <template #footer>
                <span class="dialog-footer">
                    <el-button @click="closeDialog" :disabled="disable_confiom">取消</el-button>
                    <el-button type="primary" @click="uploadFile" :disabled="disable_confiom">确定</el-button>
                </span>
            </template>
        </el-dialog>
    </div>
</template>
<style scoped>
:deep(.el-select),
:deep(.el-input-number) {
    width: 100%;
}
</style>
<script lang="ts" setup>
    import { ref,reactive   } from "vue";
    import { ElMessage } from "element-plus";
    import { downloadFile } from '/@/utils/download2';
 
    const state = reactive({
    loading: false,
    fileList: [] as any
});
 
    const disable_confiom=ref<bool>(false);
    const uploadRef = ref();
    const getEnumGenderData = ref<any>([]);
    const getEnumOtherGenderData = ref<any>([]);
    //父级传递来的参数
    var props = defineProps({
        title: {
        type: String,
        default: "",
    },
    });
    //父级传递来的函数,用于回调
    const emit = defineEmits(["parentUploadFun","parentDownFun"]);
    const isShowDialog = ref(false);
 
    // 打开弹窗
    const openDialog = async () => {
        isShowDialog.value = true;
    };
 
    // 关闭弹窗
    const closeDialog = () => {
        isShowDialog.value = false;
        clearData();
    };
 
    // 下载导入模板
    const downFile = async () => {
    //调用父级页面方法
    await emit("parentDownFun");
    };
 
    // 清空数据
    const clearData = () =>{
      state.fileList.value = [];
      uploadRef.value.clearFiles();//清空已上传的文件列表
    };
 
   // 通过onChanne方法获得文件列表
   const handleChange = (file: any, fileList: []) => {
     state.fileList = fileList;
    };
 
    // 上传
    const uploadFile = async () => {
    if (state.fileList.length < 1) {
        ElMessage.warning('请上传文件!');
        return;
    };
    disable_confiom.value=true;
    state.loading = true;
    //调用父级页面方法
    //debugger
    const formData = new FormData()
    formData.append('file',state.fileList[0].raw)
    await emit("parentUploadFun",formData);
   };
   //父级页面回调上传文件
   const callBackUploadFun=(result:any)=>{
    if(result.type=="success"){
      ElMessage.success('上传成功');
      isShowDialog.value = false;
      clearData();
     }else{
        //失败不用自己写提示,框架自动写
        //ElMessage.error('上传失败:'+result.message);
     }
    state.loading = false;
    disable_confiom.value=false;
   };
   //父级页面回调下载模板文件
   const callBackDownFun=(result:any)=>{
    downloadFile(result);
   }
 
    //将属性或者函数暴露给父组件
    defineExpose({ openDialog,callBackUploadFun,callBackDownFun });
</script>