master
2022-02-14 1684d66eaf39f77980b987359f413d2ff5714940
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
165
<template>
    <el-button
        :size="size"
        :type="type"
        :plain="plain"
        :round="round"
        :circle="circle"
        :icon="icon"
        :loading="loading"
        :disabled="disabled"
        @click="toExport"
    >
        <slot>导出</slot>
    </el-button>
</template>
 
<script lang="ts">
import { defineComponent, inject, ref } from "vue";
import { currentDate, export_json_to_excel } from "../utils";
 
export default defineComponent({
    name: "cl-export-btn",
 
    props: {
        filename: [Function, String],
        autoWidth: {
            type: Boolean,
            default: true
        },
        bookType: {
            type: String,
            default: "xlsx"
        },
        header: Array,
        columns: {
            type: Array,
            required: true
        },
        data: [Function, Array],
        maxExportLimit: Number, // 最大导出条数,不传或者小于等于0为不限制
        size: {
            type: String,
            default: "mini"
        },
        disabled: Boolean,
        type: String,
        plain: Boolean,
        round: Boolean,
        circle: Boolean,
        icon: String
    },
 
    setup(props) {
        const loading = ref<boolean>(false);
        const crud = inject<any>("crud");
 
        async function getHeader(columns: any[], fields: any[]) {
            return (
                props.header || columns.filter((e) => fields.includes(e.prop)).map((e) => e.label)
            );
        }
 
        function getData() {
            if (typeof props.data === "function") {
                return props.data();
            } else {
                if (props.data) {
                    return props.data;
                } else {
                    return crud.service
                        .page({
                            ...crud.paramsReplace(crud.params),
                            maxExportLimit: props.maxExportLimit,
                            isExport: true
                        })
                        .then((res: any) => {
                            return res.list.map((e: any) => {
                                for (const i in e) {
                                    const col: any = props.columns.find((c: any) => c.prop == i);
 
                                    if (col) {
                                        if (col.dict) {
                                            const d = col.dict.find((d: any) => d.value == e[i]);
 
                                            if (d) {
                                                e[i] = d.label;
                                            }
                                        }
                                    }
                                }
 
                                return e;
                            });
                        })
                        .catch((err: string) => {
                            console.error(err);
                            return null;
                        });
                }
            }
        }
 
        async function getFileName() {
            if (typeof props.filename === "function") {
                return await props?.filename();
            } else {
                const { year, month, day, hour, minu, sec } = currentDate();
                return props.filename || `报表(${year}-${month}-${day} ${hour}_${minu}_${sec})`;
            }
        }
 
        async function toExport() {
            if (!props.columns) {
                return console.error("cl-export-btn.columns 不能为空!");
            }
 
            // 加载
            loading.value = true;
 
            // 表格列
            const columns = props.columns.filter(
                (e: any) =>
                    !["selection", "expand", "index"].includes(e.type) &&
                    !(e.filterExport || e["filter-export"])
            );
 
            // 字段
            const fields = columns.map((e: any) => e.prop).filter(Boolean);
 
            // 表头
            let header = await getHeader(columns, fields);
 
            // 数据
            let data = await getData();
 
            if (!data) {
                loading.value = false;
                return console.error("导出数据异常");
            }
 
            // 文件名
            let filename = await getFileName();
 
            // 过滤
            data = data.map((d: any) => fields.map((f) => d[f]));
 
            // 导出 excel
            export_json_to_excel({
                header,
                data,
                filename,
                autoWidth: props.autoWidth,
                bookType: props.bookType
            });
 
            loading.value = false;
        }
 
        return {
            loading,
            toExport
        };
    }
});
</script>