wangzhibo
2026-07-16 b57020623cf0c946706573bf102e548c3a544423
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
<template>
    <el-button :icon="icon" :disabled="disabled" :type="type" @click="open">
        {{ $t('导入') }}
    </el-button>
 
    <cl-form ref="Form">
        <template #slot-upload>
            <div v-if="!upload.filename" class="upload">
                <div class="tips" v-if="template">
                    <span>{{ tips }}</span>
                    <el-button type="primary" text bg @click="download">{{
                        $t('下载模版')
                    }}</el-button>
                </div>
 
                <div class="inner">
                    <cl-upload
                        drag
                        :limit-size="limitSize"
                        :accept="accept"
                        :disabled="disabled"
                        :auto-upload="false"
                        :before-upload="onUpload"
                        :size="[220, '100%']"
                    />
                </div>
            </div>
        </template>
 
        <template #slot-list>
            <div v-if="list.length" class="data-table">
                <div class="head">
                    <el-button type="success" @click="clear">{{ $t('重新上传') }}</el-button>
                    <el-button
                        type="danger"
                        :disabled="table.selection.length == 0"
                        @click="table.del()"
                    >
                        {{ $t('批量删除') }}
                    </el-button>
                </div>
 
                <div class="cl-table">
                    <el-table
                        border
                        :data="list"
                        max-height="600px"
                        @selection-change="table.onSelectionChange"
                        @row-click="
                            row => {
                                row._edit = true;
                            }
                        "
                    >
                        <el-table-column
                            type="selection"
                            width="60px"
                            align="center"
                            fixed="left"
                        />
 
                        <el-table-column
                            :label="$t('序号')"
                            type="index"
                            width="80px"
                            align="center"
                            fixed="left"
                            :index="table.onIndex"
                        />
 
                        <el-table-column
                            v-for="item in table.header"
                            :key="item"
                            :prop="item"
                            :label="item"
                            min-width="160px"
                            align="center"
                        >
                            <template #default="scope">
                                <span v-if="!scope.row._edit">{{ scope.row[item] }}</span>
 
                                <template v-else>
                                    <el-input
                                        v-model="scope.row[item]"
                                        type="textarea"
                                        clearable
                                        :placeholder="item"
                                    />
                                </template>
                            </template>
                        </el-table-column>
 
                        <el-table-column
                            :label="$t('操作')"
                            width="100px"
                            align="center"
                            fixed="right"
                        >
                            <template #default="scope">
                                <el-button
                                    text
                                    bg
                                    type="danger"
                                    @click.stop="table.del(scope.$index)"
                                >
                                    {{ $t('删除') }}
                                </el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                </div>
 
                <div class="pagination">
                    <el-pagination
                        v-model:current-page="pagination.page"
                        background
                        layout="total, prev, pager, next"
                        :total="upload.list.length"
                        :page-size="pagination.size"
                        @current-change="pagination.onCurrentChange"
                    />
                </div>
            </div>
        </template>
    </cl-form>
</template>
 
<script lang="ts" setup>
defineOptions({
    name: 'cl-import-btn'
});
 
import { useForm } from '@cool-vue/crud';
import { ElMessage } from 'element-plus';
import { reactive, type PropType, computed } from 'vue';
import * as XLSX from 'xlsx';
import chardet from 'chardet';
import { extname } from '/@/cool/utils';
import { has } from 'lodash-es';
import { useI18n } from 'vue-i18n';
 
const props = defineProps({
    onConfig: Function,
    onSubmit: Function,
    template: {
        type: String,
        default: ''
    },
    tips: String,
    limitSize: {
        type: Number,
        default: 10
    },
    type: {
        type: String as PropType<
            'default' | 'success' | 'warning' | 'info' | 'text' | 'primary' | 'danger'
        >,
        default: 'success'
    },
    icon: String,
    disabled: Boolean,
    accept: {
        type: String,
        default:
            'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel,text/csv'
    }
});
 
const emit = defineEmits(['change']);
 
const Form = useForm();
const { t } = useI18n();
 
// 提示信息
const tips = computed(() => {
    return props.tips || t('请按照模版填写信息');
});
 
// 上传信息
const upload = reactive({
    filename: '',
    file: null as File | null,
    list: [] as any[]
});
 
// 分页信息
const pagination = reactive({
    size: 20,
    page: 1,
    onCurrentChange(page: number) {
        pagination.page = page;
    }
});
 
// 数据表格
const table = reactive({
    // 表头
    header: [] as string[],
 
    // 选中列表
    selection: [] as any[],
 
    // 删除行
    del(index?: number) {
        if (index !== undefined) {
            upload.list.splice(index, 1);
        } else {
            upload.list = upload.list.filter(a => {
                return !table.selection.includes(a._index);
            });
        }
    },
 
    // 序号
    onIndex(index: number) {
        return index + 1 + (pagination.page - 1) * pagination.size;
    },
 
    // 选中
    onSelectionChange(arr: any[]) {
        table.selection = arr.map(e => e._index);
    }
});
 
// 数据列表
const list = computed(() => {
    return upload.list.filter((_, i) => {
        return (
            i >= (pagination.page - 1) * pagination.size && i < pagination.page * pagination.size
        );
    });
});
 
// 清空
function clear() {
    upload.filename = '';
    upload.file = null;
    upload.list = [];
    table.header = [];
    table.selection = [];
}
 
// 打开
function open() {
    clear();
 
    Form.value?.open({
        title: t('导入'),
        width: computed(() => (upload.filename ? '80%' : '800px')),
        dialog: {
            'close-on-press-escape': false
        },
        items: [
            ...(props.onConfig ? props.onConfig(Form) : []),
            {
                prop: 'file',
                component: {
                    name: 'slot-upload'
                }
            },
            {
                component: {
                    name: 'slot-list'
                }
            }
        ],
        op: {
            saveButtonText: t('提交')
        },
        on: {
            submit(_, { done, close }) {
                if (!upload.filename) {
                    done();
                    return ElMessage.error(t('请选择文件'));
                }
 
                if (props.onSubmit) {
                    props.onSubmit({
                        ...upload,
                        ..._
                    }, { done, close });
                } else {
                    ElMessage.error(t('[cl-import-btn] onSubmit is required'));
                    done();
                }
            }
        }
    });
}
 
// 上传
function onUpload(raw: File, _: any, { next }: any) {
    const reader = new FileReader();
    const ext = extname(raw.name);
 
    reader.onload = (event: any) => {
        let data = '';
 
        if (ext == 'csv') {
            const detected: any = chardet.detect(new Uint8Array(event.target.result));
            const decoder = new TextDecoder(detected);
            data = decoder.decode(event.target.result);
        } else {
            data = event.target.result;
        }
 
        const workbook = XLSX.read(data, { type: 'binary', raw: ext == 'csv' });
 
        let json: any[] = [];
        for (const sheet in workbook.Sheets) {
            if (has(workbook.Sheets, sheet)) {
                json = json.concat(
                    XLSX.utils.sheet_to_json(workbook.Sheets[sheet], {
                        raw: false,
                        dateNF: 'yyyy-mm-dd',
                        defval: ''
                    })
                );
            }
        }
 
        upload.list = json.map((e, i) => {
            return {
                ...e,
                _index: i
            };
        });
        upload.filename = raw.name;
        upload.file = raw;
 
        const sheet = workbook.Sheets[Object.keys(workbook.Sheets)[0]];
 
        for (const i in sheet) {
            if (i[0] === '!') continue;
 
            const row = i.match(/[0-9]+/)?.[0];
 
            if (row == '1') {
                table.header.push(sheet[i].v);
            }
        }
 
        emit('change', json);
    };
 
    if (ext == 'csv') {
        reader.readAsArrayBuffer(raw);
    } else {
        reader.readAsBinaryString(raw);
    }
 
    next();
 
    return false;
}
 
// 下载模版
function download() {
    const link = document.createElement('a');
    link.setAttribute('href', props.template);
    link.setAttribute('download', '');
    link.click();
}
 
defineExpose({
    open,
    clear,
    Form
});
</script>
 
<style lang="scss" scoped>
.upload {
    display: flex;
    flex-direction: column;
 
    .inner {
        width: 100%;
 
        :deep(.cl-upload) {
            .cl-upload__footer,
            .cl-upload__list,
            .el-upload,
            .is-drag {
                width: 100% !important;
            }
        }
    }
}
 
.tips {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 20px;
 
    & > span {
        color: var(--el-color-warning);
    }
}
 
.data-table {
    .head {
        margin-bottom: 10px;
    }
 
    .pagination {
        display: flex;
        justify-content: flex-end;
        margin-top: 10px;
    }
}
</style>