feat: 新增导入功能

This commit is contained in:
ah--desktop
2025-12-04 23:22:02 +08:00
parent e2caac2965
commit d3d9fd47d8
33 changed files with 1585 additions and 176 deletions
@@ -0,0 +1,39 @@
<script setup lang="ts">
import { computed } from 'vue';
import { createIconifyIcon } from '@vben/icons';
import { Button } from 'ant-design-vue';
import { useImportTaskStore } from '#/store';
import ImportTaskDropdown from './ImportTaskDropdown.vue';
defineOptions({ name: 'ImportTaskButton' });
// 创建下载图标组件
const DownloadIcon = createIconifyIcon('lucide:download');
const importTaskStore = useImportTaskStore();
// 计算属性:是否有进行中的任务
const hasActiveTasks = computed(() => importTaskStore.hasActiveTasks);
</script>
<template>
<ImportTaskDropdown>
<template #trigger>
<div class="flex-center mr-2 h-full">
<Button type="text" shape="circle" class="relative">
<template #icon>
<DownloadIcon class="size-4" />
</template>
<span
v-if="hasActiveTasks"
class="bg-primary absolute right-0.5 top-0.5 h-2 w-2 rounded"
></span>
</Button>
</div>
</template>
</ImportTaskDropdown>
</template>
@@ -0,0 +1,234 @@
<script setup lang="ts">
import type { ImportTaskVO } from '#/api/ecommerce/import-task';
import { computed, onMounted, onUnmounted, ref, watch } from 'vue';
import { useVbenModal } from '@vben/common-ui';
import { useToggle } from '@vueuse/core';
import { Button, Empty, List, Popover, Progress, Tag } from 'ant-design-vue';
import { useImportTaskStore } from '#/store';
defineOptions({ name: 'ImportTaskDropdown' });
const importTaskStore = useImportTaskStore();
const [open] = useToggle();
// 监听 store 中的 shouldOpenDropdown 状态,自动打开下拉菜单
watch(
() => importTaskStore.shouldOpenDropdown,
(shouldOpen) => {
if (shouldOpen) {
open.value = true;
// 重置状态,避免重复触发
importTaskStore.closeDropdown();
}
},
);
// 创建消息详情模态框
const [MessageModal, messageModalApi] = useVbenModal({
draggable: true,
class: 'w-[700px]',
title: '任务消息详情',
showCancelButton: false,
showConfirmButton: false,
footer: false,
});
// 当前要显示的消息
const currentMessage = ref<string>('');
// 计算属性:任务列表(按创建时间倒序)
const taskList = computed(() => {
return [...importTaskStore.tasks].sort(
(a, b) =>
new Date(b.createTime).getTime() - new Date(a.createTime).getTime(),
);
});
// 获取任务状态标签
function getStatusTag(status: string) {
const statusMap: Record<string, { color: string; text: string }> = {
pending: { color: 'default', text: '等待中' },
processing: { color: 'processing', text: '处理中' },
success: { color: 'success', text: '成功' },
failed: { color: 'error', text: '失败' },
};
return statusMap[status] || { color: 'default', text: status };
}
// 获取任务进度百分比
function getProgress(task: ImportTaskVO.ImportTaskInfoVO): number {
if (!task.totalCount || task.totalCount === 0) {
return 0;
}
const processed = (task.successCount || 0) + (task.failCount || 0);
return Math.round((processed / task.totalCount) * 100);
}
// 获取任务进度文本
function getProgressText(task: ImportTaskVO.ImportTaskInfoVO): string {
if (!task.totalCount || task.totalCount === 0) {
return '等待开始';
}
const processed = (task.successCount || 0) + (task.failCount || 0);
return `${processed}/${task.totalCount}`;
}
// 打开消息详情模态框
function openMessageModal(message: string) {
currentMessage.value = message;
messageModalApi.open();
}
// 格式化时间
function formatTime(time: string): string {
return new Date(time).toLocaleString();
}
// 组件挂载时初始化
onMounted(() => {
importTaskStore.init();
});
// 组件卸载时清理
onUnmounted(() => {
// 如果没有进行中的任务,断开SSE连接
if (!importTaskStore.hasActiveTasks) {
importTaskStore.disconnectSse();
}
});
</script>
<template>
<Popover
v-model:open="open"
trigger="click"
:overlay-style="{ width: '400px', padding: 0 }"
placement="bottomRight"
>
<template #content>
<div class="relative">
<div class="flex items-center justify-between border-b p-4 py-3">
<div class="font-medium">导入任务管理</div>
<Button
v-if="taskList.length > 0"
size="small"
type="text"
@click="importTaskStore.fetchTasks(true)"
>
刷新
</Button>
</div>
<div v-if="taskList.length > 0" class="max-h-[400px] overflow-y-auto">
<List :data-source="taskList" item-layout="vertical">
<template #renderItem="{ item }">
<List.Item class="border-b px-4 py-3">
<div class="flex w-full flex-col gap-2">
<!-- 任务标题和状态 -->
<div class="flex items-center justify-between">
<div class="flex items-center gap-2">
<span class="text-sm font-medium">{{
item.fileName
}}</span>
<Tag
:color="getStatusTag(item.status).color"
size="small"
>
{{ getStatusTag(item.status).text }}
</Tag>
</div>
<span class="text-xs text-gray-500">
{{ formatTime(item.createTime) }}
</span>
</div>
<!-- 进度条 -->
<div v-if="item.status === 'processing'" class="space-y-1">
<Progress
:percent="getProgress(item)"
:show-info="true"
size="small"
:status="
item.status === 'failed' ? 'exception' : 'active'
"
/>
<div class="flex justify-between text-xs text-gray-500">
<span>已处理: {{ getProgressText(item) }}</span>
<span>
成功: {{ item.successCount || 0 }} / 失败:
{{ item.failCount || 0 }}
</span>
</div>
</div>
<!-- 完成信息 -->
<div
v-if="item.status === 'success' || item.status === 'failed'"
class="flex justify-between text-xs text-gray-500"
>
<span>总行数: {{ item.totalCount || 0 }}</span>
<span>
成功: {{ item.successCount || 0 }} / 失败:
{{ item.failCount || 0 }}
</span>
</div>
<!-- 错误信息 -->
<div
v-if="item.status === 'failed' && item.message"
class="flex items-center gap-2 rounded bg-red-50 p-2 dark:bg-red-950"
>
<div
class="flex-1 overflow-hidden text-ellipsis whitespace-nowrap text-xs text-red-600 dark:text-red-400"
>
{{ item.message }}
</div>
<Button
type="link"
size="small"
class="h-auto p-0 text-xs"
@click="openMessageModal(item.message)"
>
查看
</Button>
</div>
<!-- 任务类型 -->
<div class="text-xs text-gray-400">
类型: {{ item.type === 'order' ? '订单导入' : '商品导入' }}
</div>
</div>
</List.Item>
</template>
</List>
</div>
<Empty v-else description="暂无导入任务" class="min-h-[150px]" />
</div>
</template>
<template #default>
<slot name="trigger"></slot>
</template>
</Popover>
<!-- 消息详情模态框 -->
<MessageModal>
<div class="max-h-[500px] overflow-y-auto">
<div
class="whitespace-pre-wrap break-words rounded bg-gray-50 p-4 text-sm dark:bg-gray-800"
>
{{ currentMessage }}
</div>
</div>
<!-- 底部操作栏 -->
<div class="mt-4 flex justify-end border-t pt-4">
<Button @click="messageModalApi.close()">关闭</Button>
</div>
</MessageModal>
</template>