This commit is contained in:
icssoa 2025-09-02 10:44:06 +08:00
parent e66d64d83e
commit f670008cb4

View File

@ -1,428 +1,642 @@
<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
<div class="cl-upload__wrap" :class="[customClass]">
<div
class="cl-upload"
:class="[
`cl-upload--${type}`,
{
'is-disabled': disabled,
'is-multiple': multiple,
'is-small': small
}
]"
>
<template v-if="!drag">
<div v-if="type == 'file'" class="cl-upload__file-btn">
<el-upload
:ref="setRefs('upload')"
drag
:limit-size="limitSize"
:drag="drag"
action=""
:accept="accept"
:show-file-list="false"
:before-upload="onBeforeUpload"
:http-request="httpRequest"
:headers="headers"
:multiple="multiple"
:disabled="disabled"
:auto-upload="false"
:before-upload="onUpload"
:size="[220, '100%']"
/>
</div>
>
<slot>
<el-button type="success">{{ text }}</el-button>
</slot>
</el-upload>
</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()"
<!-- 列表 -->
<vue-draggable
v-if="showList"
v-model="list"
class="cl-upload__list"
tag="div"
ghost-class="Ghost"
drag-class="Drag"
item-key="uid"
:disabled="!draggable"
@end="update"
>
{{ $t('批量删除') }}
</el-button>
<!-- 触发器 -->
<template #footer>
<div v-if="(type == 'image' || drag) && isAdd" class="cl-upload__footer">
<el-upload
:ref="setRefs('upload')"
action=""
:drag="drag"
:accept="accept"
:show-file-list="false"
:before-upload="onBeforeUpload"
:http-request="httpRequest"
:headers="headers"
:multiple="multiple"
:disabled="disabled"
>
<slot>
<!-- 拖拽方式 -->
<div v-if="drag" class="cl-upload__demo is-dragger">
<el-icon :size="46">
<upload-filled />
</el-icon>
<div>
{{
t('点击上传或将文件拖动到此处,文件大小限制{n}M', {
n: limitSize
})
}}
</div>
</div>
<div class="cl-table">
<el-table
border
:data="list"
max-height="600px"
@selection-change="table.onSelectionChange"
@row-click="
row => {
row._edit = true;
<!-- 点击方式 -->
<div v-else class="cl-upload__demo">
<el-icon :size="36">
<component :is="icon" v-if="icon" />
<picture-filled v-else />
</el-icon>
<span v-if="text" class="text">{{ text }}</span>
</div>
</slot>
</el-upload>
</div>
</template>
<!-- 列表 -->
<template #item="{ element: item, index }">
<el-upload
action=""
:accept="accept"
:show-file-list="false"
:http-request="
req => {
return httpRequest(req, item);
}
"
:before-upload="
file => {
onBeforeUpload(file, item);
}
"
:headers="headers"
:disabled="disabled"
>
<el-table-column
type="selection"
width="60px"
align="center"
fixed="left"
<slot name="item" :item="item" :index="index">
<div class="cl-upload__item">
<upload-item
:show-tag="showTag"
:item="item"
:list="list"
:disabled="disabled"
:deletable="deletable"
@remove="remove(index)"
/>
<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"
<!-- 小图模式 -->
<el-icon
v-if="small"
class="cl-upload__item-remove"
@click.stop="remove(index)"
>
<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>
<circle-close-filled />
</el-icon>
</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"
/>
</slot>
</el-upload>
</template>
</vue-draggable>
</div>
</div>
</template>
</cl-form>
</template>
<script lang="ts" setup>
defineOptions({
name: 'cl-import-btn'
name: 'cl-upload'
});
import { useForm } from '@cool-vue/crud';
import { computed, ref, watch, type PropType, nextTick } from 'vue';
import { assign, isArray, isEmpty, isNumber } from 'lodash-es';
import VueDraggable from 'vuedraggable';
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';
import { PictureFilled, UploadFilled, CircleCloseFilled } from '@element-plus/icons-vue';
import { useForm } from '@cool-vue/crud';
import { useCool } from '/@/cool';
import { useBase } from '/$/base';
import { uuid, isPromise } from '/@/cool/utils';
import { getUrls, getType } from '../utils';
import { useUpload } from '../hooks';
import UploadItem from './upload-item/index.vue';
import { CrudProps } from '/#/crud';
import { useI18n } from 'vue-i18n';
const props = defineProps({
onConfig: Function,
onSubmit: Function,
template: {
type: String,
default: ''
},
tips: String,
limitSize: {
type: Number,
default: 10
...CrudProps,
//
modelValue: {
type: [String, Array],
default: () => []
},
//
type: {
type: String as PropType<
'default' | 'success' | 'warning' | 'info' | 'text' | 'primary' | 'danger'
>,
default: 'success'
type: String as PropType<'image' | 'file'>,
default: 'image'
},
icon: String,
//
accept: String,
//
multiple: Boolean,
//
limit: Number,
//
limitSize: Number,
//
autoUpload: {
type: Boolean,
default: true
},
//
size: [String, Number, Array],
//
small: Boolean,
//
icon: null,
//
text: String,
//
showTag: {
type: Boolean,
default: true
},
//
showFileList: {
type: Boolean,
default: true
},
//
draggable: Boolean,
//
drag: Boolean,
//
disabled: Boolean,
accept: {
type: String,
default:
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel,text/csv'
}
//
deletable: Boolean,
//
customClass: String,
//
beforeUpload: Function,
//
prefixPath: String
});
const emit = defineEmits(['change']);
const emit = defineEmits(['update:modelValue', 'change', 'upload', 'success', 'error', 'progress']);
const Form = useForm();
const { t } = useI18n();
const { refs, setRefs } = useCool();
const { user } = useBase();
const Form = useForm();
const { options, toUpload } = useUpload();
const { t } = useI18n();
//
const tips = computed(() => {
return props.tips || t('请按照模版填写信息');
//
const size = computed(() => {
const d = props.size || options.size;
return (isArray(d) ? d : [d, d]).map((e: string | number) => (isNumber(e) ? e + 'px' : e));
});
//
const upload = reactive({
filename: '',
file: null as File | null,
list: [] as any[]
//
const disabled = computed(() => {
return props.isDisabled || props.disabled;
});
//
const pagination = reactive({
size: 20,
page: 1,
onCurrentChange(page: number) {
pagination.page = page;
}
});
//
const limit = props.limit || options.limit.upload;
//
const table = reactive({
//
header: [] as string[],
//
const limitSize = props.limitSize || options.limit.size;
//
selection: [] as any[],
//
del(index?: number) {
if (index !== undefined) {
upload.list.splice(index, 1);
//
const text = computed(() => {
if (props.text !== undefined) {
return props.text;
} else {
upload.list = upload.list.filter(a => {
return !table.selection.includes(a._index);
});
switch (props.type) {
case 'file':
return t('选择文件');
case 'image':
return t('选择图片');
default:
return '';
}
},
//
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 = [];
refs.upload?.clear();
}
//
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'
},
hidden() {
return upload.filename;
}
},
{
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) => {
try {
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) => {
//
const headers = computed(() => {
return {
...e,
_index: i
Authorization: user.token
};
});
upload.filename = raw.name;
upload.file = raw;
const sheet = workbook.Sheets[Object.keys(workbook.Sheets)[0]];
//
const list = ref<Upload.Item[]>([]);
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);
//
const showList = computed(() => {
if (props.type == 'file') {
return props.showFileList ? !isEmpty(list.value) : false;
} else {
return true;
}
});
//
const accept = computed(() => {
return props.accept || (props.type == 'file' ? '' : 'image/*');
});
//
const isAdd = computed(() => {
const len = list.value.length;
if (props.multiple && !disabled.value) {
return limit - len > 0;
}
emit('change', json);
} catch (err) {
ElMessage.error(t('文件异常,请检查内容是否正确'));
clear();
}
return len == 0;
});
//
async function onBeforeUpload(file: any, item?: Upload.Item) {
function next() {
const d = {
uid: file.uid,
size: file.size,
name: file.name,
type: getType(file.name),
progress: props.autoUpload ? 0 : 100, // 100%
url: '',
preload: '',
error: ''
};
if (ext == 'csv') {
reader.readAsArrayBuffer(raw);
} else {
reader.readAsBinaryString(raw);
//
if (d.type == 'image') {
if (file instanceof File) {
d.preload = window.webkitURL.createObjectURL(file);
}
}
next();
//
emit('upload', d, file);
//
if (item) {
assign(item, d);
} else {
if (props.multiple) {
if (!isAdd.value) {
ElMessage.warning(t('最多只能上传{n}个文件', { n: limit }));
return false;
} else {
list.value.push(d);
}
} else {
list.value = [d];
}
}
return true;
}
//
if (file.size / 1024 / 1024 >= limitSize) {
ElMessage.error(t('上传文件大小不能超过 {n}MB!', { n: limitSize }));
return false;
}
//
function download() {
const link = document.createElement('a');
link.setAttribute('href', props.template);
link.setAttribute('download', '');
link.click();
//
if (props.beforeUpload) {
let r = props.beforeUpload(file, item, { next });
if (isPromise(r)) {
r.then(next).catch(() => null);
} else {
if (r) {
r = next();
}
}
return r;
} else {
return next();
}
}
//
function remove(index: number) {
list.value.splice(index, 1);
update();
}
//
function clear() {
list.value = [];
}
//
async function httpRequest(req: any, item?: Upload.Item) {
if (!item) {
item = list.value.find(e => e.uid == req.file.uid);
}
if (!item) {
return false;
}
//
toUpload(req.file, {
prefixPath: props.prefixPath,
onProgress(progress) {
item!.progress = progress;
emit('progress', item);
}
})
.then(res => {
assign(item!, res);
emit('success', item);
update();
})
.catch(err => {
item!.error = err.message;
emit('error', item);
});
}
//
function check() {
return list.value.find(e => !e.url);
}
//
function update() {
if (!check()) {
const urls = getUrls(list.value);
const val = props.multiple ? getUrls(list.value) : urls[0] || '';
//
emit('update:modelValue', val);
emit('change', val);
nextTick(() => {
if (props.prop) {
Form.value?.validateField(props.prop);
}
//
refs.upload?.clearFiles();
});
}
}
//
function upload(file: File) {
clear();
refs.upload?.clearFiles();
nextTick(() => {
refs.upload?.handleStart(file);
refs.upload?.submit();
});
}
//
watch(
() => props.modelValue,
(val: any[] | string) => {
if (check()) {
return false;
}
const urls = (isArray(val) ? val : [val]).filter(Boolean);
list.value = urls
.map((url, index) => {
const old = list.value[index] || {};
return assign(
{
progress: 100,
uid: uuid()
},
old,
{
type: getType(url),
url,
preload: old.url == url ? old.preload : url //
}
);
})
.filter((_, i) => {
return props.multiple ? true : i == 0;
});
},
{
immediate: true
}
);
//
defineExpose({
open,
isAdd,
list,
check,
clear,
Form
remove,
upload
});
</script>
<style lang="scss" scoped>
.upload {
display: flex;
flex-direction: column;
.cl-upload {
line-height: normal;
.inner {
.Ghost {
.cl-upload__item {
border: 1px dashed var(--el-color-primary) !important;
}
}
&__file {
width: 100%;
:deep(.cl-upload) {
.cl-upload__footer,
.cl-upload__list,
.el-upload,
.is-drag {
width: 100% !important;
}
}
&-btn {
width: fit-content;
}
}
.tips {
&__list {
display: flex;
justify-content: space-between;
flex-wrap: wrap;
}
&__item,
&__demo {
display: flex;
flex-direction: column;
align-items: center;
margin-bottom: 20px;
justify-content: center;
height: v-bind('size[0]');
width: v-bind('size[1]');
background-color: var(--el-fill-color-light);
color: var(--el-text-color-regular);
border-radius: 8px;
cursor: pointer;
box-sizing: border-box;
position: relative;
user-select: none;
}
& > span {
color: var(--el-color-warning);
&__demo {
font-size: 13px;
.el-icon {
font-size: 46px;
}
.text {
margin-top: 5px;
}
&.is-dragger {
padding: 20px;
}
}
.data-table {
.head {
margin-bottom: 10px;
}
.pagination {
display: flex;
justify-content: flex-end;
&__file-btn {
& + .cl-upload__list {
margin-top: 10px;
}
}
:deep(.el-upload) {
display: block;
.el-upload-dragger {
padding: 0;
border: 0;
background-color: transparent !important;
position: relative;
&.is-dragover {
&::after {
display: block;
content: '';
position: absolute;
left: 0;
top: 0;
height: 100%;
width: 100%;
pointer-events: none;
border-radius: 8px;
box-sizing: border-box;
border: 1px dashed var(--el-color-primary);
}
}
}
}
&.is-disabled {
.cl-upload__demo {
color: var(--el-text-color-placeholder);
}
:deep(.cl-upload__item) {
cursor: not-allowed;
background-color: var(--el-disabled-bg-color);
}
}
&.is-multiple {
.cl-upload__list {
margin-bottom: -5px;
}
.cl-upload__item {
margin: 0 5px 5px 0;
}
.cl-upload__footer {
margin-bottom: 5px;
}
}
&.is-small {
.cl-upload__demo {
.el-icon {
font-size: 20px !important;
}
.text {
display: none;
}
}
.cl-upload__item-remove {
position: absolute;
right: 0px;
top: 0px;
color: var(--el-color-danger);
background-color: #fff;
border-radius: 100%;
}
:deep(.cl-upload-item) {
.cl-upload-item__progress-bar,
.cl-upload-item__actions,
.cl-upload-item__tag {
display: none;
}
.cl-upload-item__progress-value {
font-size: 12px;
}
}
}
&:not(.is-disabled) {
.cl-upload__demo {
&:hover {
color: var(--el-color-primary);
}
}
}
}
</style>