mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-25 06:29:20 +08:00
feat: 新增导入功能
This commit is contained in:
@@ -7,12 +7,16 @@ export namespace ImportMappingDTO {
|
||||
export interface CreateImportMappingDTO {
|
||||
/** 导入类型(product-商品 order-订单) */
|
||||
type: string;
|
||||
/** 映射名称 */
|
||||
name: string;
|
||||
/** 字段映射(JSON格式) */
|
||||
mapping: string;
|
||||
}
|
||||
|
||||
/** 更新导入映射参数 */
|
||||
export interface UpdateImportMappingDTO {
|
||||
/** 映射名称 */
|
||||
name?: string;
|
||||
/** 字段映射(JSON格式) */
|
||||
mapping: string;
|
||||
}
|
||||
@@ -21,5 +25,7 @@ export namespace ImportMappingDTO {
|
||||
export interface ImportMappingFilterDTO {
|
||||
/** 导入类型(product-商品 order-订单) */
|
||||
type?: string;
|
||||
/** 映射名称(模糊查询) */
|
||||
name?: string;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,6 +11,8 @@ export namespace ImportMappingVO {
|
||||
companyId?: number;
|
||||
/** 导入类型(product-商品 order-订单) */
|
||||
type: string;
|
||||
/** 映射名称 */
|
||||
name: string;
|
||||
/** 字段映射(JSON格式) */
|
||||
mapping: string;
|
||||
/** 创建时间 */
|
||||
|
||||
@@ -2,8 +2,13 @@ import type { ImportTaskDTO, ImportTaskVO } from './types';
|
||||
|
||||
import type { PageQueryDTO, PageResultVO } from '#/types/common';
|
||||
|
||||
import { useAppConfig } from '@vben/hooks';
|
||||
import { useAccessStore } from '@vben/stores';
|
||||
|
||||
import { requestClient } from '#/api/request';
|
||||
|
||||
const { apiURL } = useAppConfig(import.meta.env, import.meta.env.PROD);
|
||||
|
||||
/**
|
||||
* 导入任务管理API
|
||||
*/
|
||||
@@ -31,3 +36,191 @@ export async function getImportTaskPageApi(
|
||||
params,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* SSE连接管理器(使用fetch API支持自定义请求头)
|
||||
*/
|
||||
class SseConnection {
|
||||
private abortController: AbortController | null = null;
|
||||
private accessStore: ReturnType<typeof useAccessStore>;
|
||||
private listeners: Map<string, Set<(event: MessageEvent) => void>> =
|
||||
new Map();
|
||||
private readyState: number = EventSource.CLOSED;
|
||||
|
||||
constructor() {
|
||||
this.accessStore = useAccessStore();
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加事件监听器
|
||||
*/
|
||||
addEventListener(eventType: string, handler: (event: MessageEvent) => void) {
|
||||
if (!this.listeners.has(eventType)) {
|
||||
this.listeners.set(eventType, new Set());
|
||||
}
|
||||
this.listeners.get(eventType)!.add(handler);
|
||||
}
|
||||
|
||||
/**
|
||||
* 建立SSE连接(使用fetch API)
|
||||
*/
|
||||
connect(): void {
|
||||
if (this.abortController) {
|
||||
this.disconnect();
|
||||
}
|
||||
|
||||
const token = this.accessStore.accessToken;
|
||||
if (!token) {
|
||||
console.error('无法建立SSE连接:token不存在');
|
||||
return;
|
||||
}
|
||||
|
||||
const url = `${apiURL}/ec/import-task/sse/connect`;
|
||||
this.abortController = new AbortController();
|
||||
this.readyState = EventSource.CONNECTING;
|
||||
|
||||
fetch(url, {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
Authorization: `Bearer ${token}`,
|
||||
Accept: 'text/event-stream',
|
||||
},
|
||||
signal: this.abortController.signal,
|
||||
})
|
||||
.then((response) => {
|
||||
if (!response.ok) {
|
||||
throw new Error(`SSE连接失败: ${response.status}`);
|
||||
}
|
||||
|
||||
this.readyState = EventSource.OPEN;
|
||||
|
||||
const reader = response.body?.getReader();
|
||||
const decoder = new TextDecoder();
|
||||
|
||||
if (!reader) {
|
||||
throw new Error('无法获取响应流');
|
||||
}
|
||||
|
||||
// 读取流数据
|
||||
const readStream = () => {
|
||||
reader
|
||||
.read()
|
||||
.then(({ done, value }) => {
|
||||
if (done) {
|
||||
this.readyState = EventSource.CLOSED;
|
||||
return;
|
||||
}
|
||||
|
||||
const chunk = decoder.decode(value, { stream: true });
|
||||
this.parseSSE(chunk);
|
||||
readStream();
|
||||
})
|
||||
.catch((error) => {
|
||||
if (error.name !== 'AbortError') {
|
||||
console.error('读取SSE流失败:', error);
|
||||
this.readyState = EventSource.CLOSED;
|
||||
this.dispatchEvent(
|
||||
'error',
|
||||
new MessageEvent('error', { data: error }),
|
||||
);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
readStream();
|
||||
})
|
||||
.catch((error) => {
|
||||
if (error.name !== 'AbortError') {
|
||||
console.error('建立SSE连接失败:', error);
|
||||
this.readyState = EventSource.CLOSED;
|
||||
this.dispatchEvent(
|
||||
'error',
|
||||
new MessageEvent('error', { data: error }),
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 断开连接
|
||||
*/
|
||||
disconnect() {
|
||||
if (this.abortController) {
|
||||
this.abortController.abort();
|
||||
this.abortController = null;
|
||||
this.readyState = EventSource.CLOSED;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取连接状态
|
||||
*/
|
||||
getReadyState(): number {
|
||||
return this.readyState;
|
||||
}
|
||||
|
||||
/**
|
||||
* 移除事件监听器
|
||||
*/
|
||||
removeEventListener(
|
||||
eventType: string,
|
||||
handler: (event: MessageEvent) => void,
|
||||
) {
|
||||
const handlers = this.listeners.get(eventType);
|
||||
if (handlers) {
|
||||
handlers.delete(handler);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 分发事件
|
||||
*/
|
||||
private dispatchEvent(eventType: string, event: MessageEvent) {
|
||||
const handlers = this.listeners.get(eventType);
|
||||
if (handlers) {
|
||||
handlers.forEach((handler) => {
|
||||
try {
|
||||
handler(event);
|
||||
} catch (error) {
|
||||
console.error('事件处理器执行失败:', error);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析SSE数据
|
||||
*/
|
||||
private parseSSE(chunk: string) {
|
||||
const lines = chunk.split('\n');
|
||||
let eventType = 'message';
|
||||
let data = '';
|
||||
|
||||
for (const line of lines) {
|
||||
if (line.startsWith('event:')) {
|
||||
eventType = line.slice(6).trim();
|
||||
} else if (line.startsWith('data:')) {
|
||||
data += `${line.slice(5).trim()}\n`;
|
||||
} else if (
|
||||
line === '' && // 空行表示一个完整的消息
|
||||
data
|
||||
) {
|
||||
const event = new MessageEvent(eventType, {
|
||||
data: data.trim(),
|
||||
});
|
||||
this.dispatchEvent(eventType, event);
|
||||
this.dispatchEvent('message', event);
|
||||
data = '';
|
||||
eventType = 'message';
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 建立SSE连接
|
||||
* @returns SseConnection实例
|
||||
*/
|
||||
export function createSseConnection(): SseConnection {
|
||||
return new SseConnection();
|
||||
}
|
||||
|
||||
@@ -7,6 +7,8 @@ export namespace ProductDTO {
|
||||
export interface Create {
|
||||
/** 商品名称 */
|
||||
name: string;
|
||||
/** 商品编码 */
|
||||
productCode?: string;
|
||||
/** 主图图片ID */
|
||||
mainImageId?: number;
|
||||
/** 商品规格列表(可选) */
|
||||
@@ -18,6 +20,8 @@ export namespace ProductDTO {
|
||||
/** 商品名称 */
|
||||
name: string;
|
||||
/** 商品编码 */
|
||||
productCode?: string;
|
||||
/** 商品编码 */
|
||||
specCode?: string;
|
||||
/** 商品SKU */
|
||||
skuCode?: string;
|
||||
@@ -64,6 +68,8 @@ export namespace ProductDTO {
|
||||
/** 商品名称(模糊查询) */
|
||||
name?: string;
|
||||
/** 商品编码(模糊查询) */
|
||||
productCode?: string;
|
||||
/** 商品编码(模糊查询) */
|
||||
specCode?: string;
|
||||
/** 商品SKU(模糊查询) */
|
||||
skuCode?: string;
|
||||
|
||||
@@ -11,6 +11,8 @@ export namespace ProductVO {
|
||||
companyId: number;
|
||||
/** 商品名称 */
|
||||
name: string;
|
||||
/** 商品编码 */
|
||||
productCode?: string;
|
||||
/** 主图URL(冗余字段) */
|
||||
mainImage?: string;
|
||||
/** 主图图片ID */
|
||||
|
||||
@@ -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>
|
||||
@@ -1,7 +1,7 @@
|
||||
<script lang="ts" setup>
|
||||
import type { NotificationItem } from '@vben/layouts';
|
||||
|
||||
import { computed, ref, watch } from 'vue';
|
||||
import { computed, onMounted, ref, watch } from 'vue';
|
||||
|
||||
import { AuthenticationLoginExpiredModal } from '@vben/common-ui';
|
||||
import { VBEN_DOC_URL, VBEN_GITHUB_URL } from '@vben/constants';
|
||||
@@ -17,9 +17,10 @@ import { preferences } from '@vben/preferences';
|
||||
import { useAccessStore, useUserStore } from '@vben/stores';
|
||||
import { openWindow } from '@vben/utils';
|
||||
|
||||
import ImportTaskButton from '#/components/ImportTaskButton.vue';
|
||||
import SwitchAccountModal from '#/components/SwitchAccountModal.vue';
|
||||
import { $t } from '#/locales';
|
||||
import { useAuthStore } from '#/store';
|
||||
import { useAuthStore, useImportTaskStore } from '#/store';
|
||||
import LoginForm from '#/views/_core/authentication/login.vue';
|
||||
|
||||
const notifications = ref<NotificationItem[]>([
|
||||
@@ -56,11 +57,17 @@ const notifications = ref<NotificationItem[]>([
|
||||
const userStore = useUserStore();
|
||||
const authStore = useAuthStore();
|
||||
const accessStore = useAccessStore();
|
||||
const importTaskStore = useImportTaskStore();
|
||||
const { destroyWatermark, updateWatermark } = useWatermark();
|
||||
const showDot = computed(() =>
|
||||
notifications.value.some((item) => !item.isRead),
|
||||
);
|
||||
|
||||
// 初始化导入任务store
|
||||
onMounted(() => {
|
||||
importTaskStore.init();
|
||||
});
|
||||
|
||||
// 切换账户模态框引用
|
||||
const switchAccountModalRef = ref();
|
||||
|
||||
@@ -155,6 +162,9 @@ watch(
|
||||
@logout="handleLogout"
|
||||
/>
|
||||
</template>
|
||||
<template #header-right-90>
|
||||
<ImportTaskButton />
|
||||
</template>
|
||||
<template #notification>
|
||||
<Notification
|
||||
:dot="showDot"
|
||||
|
||||
@@ -0,0 +1,293 @@
|
||||
import type { ImportTaskVO } from '#/api/ecommerce/import-task';
|
||||
import type { PageQueryDTO } from '#/types/common';
|
||||
|
||||
import { computed, ref } from 'vue';
|
||||
|
||||
import { defineStore } from 'pinia';
|
||||
|
||||
import {
|
||||
createSseConnection,
|
||||
getImportTaskPageApi,
|
||||
} from '#/api/ecommerce/import-task';
|
||||
|
||||
/**
|
||||
* 导入任务管理 Store
|
||||
*/
|
||||
export const useImportTaskStore = defineStore('import-task', () => {
|
||||
// 任务列表
|
||||
const tasks = ref<ImportTaskVO.ImportTaskInfoVO[]>([]);
|
||||
|
||||
// 分页信息
|
||||
const pagination = ref({
|
||||
current: 1,
|
||||
pageSize: 10,
|
||||
total: 0,
|
||||
});
|
||||
|
||||
// SSE连接
|
||||
const sseConnection = ref<null | ReturnType<typeof createSseConnection>>(
|
||||
null,
|
||||
);
|
||||
const sseConnected = ref(false);
|
||||
|
||||
// 加载状态
|
||||
const loading = ref(false);
|
||||
|
||||
// 控制下拉菜单打开状态
|
||||
const shouldOpenDropdown = ref(false);
|
||||
|
||||
// 计算属性:是否有进行中的任务
|
||||
const hasActiveTasks = computed(() => {
|
||||
return tasks.value.some(
|
||||
(task) => task.status === 'pending' || task.status === 'processing',
|
||||
);
|
||||
});
|
||||
|
||||
// 计算属性:进行中的任务数量
|
||||
const activeTaskCount = computed(() => {
|
||||
return tasks.value.filter(
|
||||
(task) => task.status === 'pending' || task.status === 'processing',
|
||||
).length;
|
||||
});
|
||||
|
||||
/**
|
||||
* 获取任务列表
|
||||
*/
|
||||
async function fetchTasks(reset = false) {
|
||||
if (loading.value) return;
|
||||
|
||||
try {
|
||||
loading.value = true;
|
||||
|
||||
if (reset) {
|
||||
pagination.value.current = 1;
|
||||
}
|
||||
|
||||
const params: PageQueryDTO = {
|
||||
page: pagination.value.current,
|
||||
pageSize: pagination.value.pageSize,
|
||||
filters: {},
|
||||
};
|
||||
|
||||
const result = await getImportTaskPageApi(params);
|
||||
|
||||
if (reset) {
|
||||
tasks.value = result.records || [];
|
||||
} else {
|
||||
tasks.value.push(...(result.records || []));
|
||||
}
|
||||
|
||||
pagination.value.total = result.total || 0;
|
||||
} catch (error) {
|
||||
console.error('获取导入任务列表失败:', error);
|
||||
throw error;
|
||||
} finally {
|
||||
loading.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新任务状态(通过SSE接收到的更新)
|
||||
*/
|
||||
function updateTask(task: ImportTaskVO.ImportTaskInfoVO) {
|
||||
const index = tasks.value.findIndex((t) => t.id === task.id);
|
||||
if (index === -1) {
|
||||
// 如果是新任务,添加到列表开头
|
||||
tasks.value.unshift(task);
|
||||
} else {
|
||||
// 更新现有任务
|
||||
tasks.value[index] = task;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 建立SSE连接
|
||||
* @returns Promise,连接建立成功后resolve
|
||||
*/
|
||||
function connectSse(): Promise<void> {
|
||||
return new Promise((resolve, reject) => {
|
||||
// 如果已经连接且已连接成功,直接resolve
|
||||
if (sseConnection.value && sseConnected.value) {
|
||||
resolve();
|
||||
return;
|
||||
}
|
||||
|
||||
// 如果已经连接但未成功,先关闭
|
||||
if (sseConnection.value) {
|
||||
disconnectSse();
|
||||
}
|
||||
|
||||
try {
|
||||
const connection = createSseConnection();
|
||||
sseConnection.value = connection;
|
||||
sseConnected.value = false;
|
||||
|
||||
let timeoutId: null | ReturnType<typeof setTimeout> = null;
|
||||
|
||||
// 监听连接成功事件
|
||||
const connectedHandler = (event: MessageEvent) => {
|
||||
try {
|
||||
const data = JSON.parse(event.data);
|
||||
if (data.type === 'connected') {
|
||||
sseConnected.value = true;
|
||||
connection.removeEventListener('message', connectedHandler);
|
||||
if (timeoutId) {
|
||||
clearTimeout(timeoutId);
|
||||
}
|
||||
resolve();
|
||||
}
|
||||
} catch {
|
||||
// 忽略解析错误,继续等待
|
||||
}
|
||||
};
|
||||
|
||||
// 监听连接错误
|
||||
const errorHandler = (_event: MessageEvent) => {
|
||||
connection.removeEventListener('message', connectedHandler);
|
||||
connection.removeEventListener('error', errorHandler);
|
||||
if (timeoutId) {
|
||||
clearTimeout(timeoutId);
|
||||
}
|
||||
reject(new Error('SSE连接失败'));
|
||||
};
|
||||
|
||||
connection.addEventListener('message', connectedHandler);
|
||||
connection.addEventListener('error', errorHandler);
|
||||
|
||||
// 设置超时,避免无限等待
|
||||
timeoutId = setTimeout(() => {
|
||||
if (!sseConnected.value) {
|
||||
connection.removeEventListener('message', connectedHandler);
|
||||
connection.removeEventListener('error', errorHandler);
|
||||
reject(new Error('SSE连接超时'));
|
||||
}
|
||||
}, 10_000); // 10秒超时
|
||||
|
||||
connection.connect();
|
||||
|
||||
// 连接成功(如果不在等待连接,则正常处理)
|
||||
connection.addEventListener('message', (event) => {
|
||||
try {
|
||||
const data = JSON.parse(event.data);
|
||||
if (data.type === 'connected') {
|
||||
sseConnected.value = true;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('解析SSE消息失败:', error);
|
||||
}
|
||||
});
|
||||
|
||||
// 接收任务进度更新
|
||||
connection.addEventListener('task_progress', (event) => {
|
||||
try {
|
||||
const data = JSON.parse(event.data);
|
||||
if (data.type === 'task_progress' && data.task) {
|
||||
updateTask(data.task);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('解析任务进度消息失败:', error);
|
||||
}
|
||||
});
|
||||
|
||||
// 所有任务完成
|
||||
connection.addEventListener('all_tasks_completed', () => {
|
||||
try {
|
||||
// 刷新任务列表
|
||||
fetchTasks(true);
|
||||
} catch (error) {
|
||||
console.error('刷新任务列表失败:', error);
|
||||
}
|
||||
});
|
||||
|
||||
// 连接错误
|
||||
connection.addEventListener('error', (event) => {
|
||||
console.error('SSE连接错误:', event);
|
||||
sseConnected.value = false;
|
||||
// 如果连接关闭,尝试重连
|
||||
if (connection.getReadyState() === EventSource.CLOSED) {
|
||||
// 延迟重连,避免频繁重连
|
||||
setTimeout(() => {
|
||||
if (hasActiveTasks.value) {
|
||||
connectSse();
|
||||
}
|
||||
}, 5000);
|
||||
}
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('建立SSE连接失败:', error);
|
||||
sseConnected.value = false;
|
||||
reject(error);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 断开SSE连接
|
||||
*/
|
||||
function disconnectSse() {
|
||||
if (sseConnection.value) {
|
||||
sseConnection.value.disconnect();
|
||||
sseConnection.value = null;
|
||||
sseConnected.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化:获取第一页任务
|
||||
*/
|
||||
async function init() {
|
||||
await fetchTasks(true);
|
||||
|
||||
// 如果有进行中的任务,建立SSE连接
|
||||
if (hasActiveTasks.value && !sseConnection.value) {
|
||||
connectSse();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 重置store
|
||||
*/
|
||||
function $reset() {
|
||||
tasks.value = [];
|
||||
pagination.value = {
|
||||
current: 1,
|
||||
pageSize: 10,
|
||||
total: 0,
|
||||
};
|
||||
disconnectSse();
|
||||
loading.value = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 打开下拉菜单
|
||||
*/
|
||||
function openDropdown() {
|
||||
shouldOpenDropdown.value = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 关闭下拉菜单
|
||||
*/
|
||||
function closeDropdown() {
|
||||
shouldOpenDropdown.value = false;
|
||||
}
|
||||
|
||||
return {
|
||||
tasks,
|
||||
pagination,
|
||||
sseConnection,
|
||||
sseConnected,
|
||||
loading,
|
||||
shouldOpenDropdown,
|
||||
hasActiveTasks,
|
||||
activeTaskCount,
|
||||
fetchTasks,
|
||||
updateTask,
|
||||
connectSse,
|
||||
disconnectSse,
|
||||
init,
|
||||
openDropdown,
|
||||
closeDropdown,
|
||||
$reset,
|
||||
};
|
||||
});
|
||||
@@ -1 +1,2 @@
|
||||
export * from './auth';
|
||||
export * from './import-task';
|
||||
|
||||
+293
-12
@@ -1,5 +1,9 @@
|
||||
<script setup lang="ts">
|
||||
import type { ImportFieldDefVO } from '#/api/ecommerce/import-field-def';
|
||||
import type {
|
||||
ImportMappingDTO,
|
||||
ImportMappingVO,
|
||||
} from '#/api/ecommerce/import-mapping';
|
||||
import type { ImportTaskDTO } from '#/api/ecommerce/import-task';
|
||||
import type { ShopVO } from '#/api/shop/shop/types';
|
||||
|
||||
@@ -7,12 +11,37 @@ import { computed, ref } from 'vue';
|
||||
|
||||
import { useVbenModal } from '@vben/common-ui';
|
||||
|
||||
import { Button, Form, message, Select, Spin, Steps } from 'ant-design-vue';
|
||||
import {
|
||||
Button,
|
||||
Form,
|
||||
Input,
|
||||
message,
|
||||
Select,
|
||||
Spin,
|
||||
Steps,
|
||||
} from 'ant-design-vue';
|
||||
|
||||
import { getFieldDefsByTypeApi } from '#/api/ecommerce/import-field-def';
|
||||
import {
|
||||
createImportMappingApi,
|
||||
getImportMappingPageApi,
|
||||
} from '#/api/ecommerce/import-mapping';
|
||||
import { createImportTaskApi } from '#/api/ecommerce/import-task';
|
||||
import { uploadFileApi } from '#/api/media/file';
|
||||
import { getShopPageApi } from '#/api/shop/shop';
|
||||
import { useImportTaskStore } from '#/store';
|
||||
|
||||
// 定义映射项数据结构
|
||||
interface FieldMappingItem {
|
||||
/** 数据库字段名 */
|
||||
fieldName: string;
|
||||
/** 中文字段名 */
|
||||
fieldLabel: string;
|
||||
/** 文件字段名(CSV列名) */
|
||||
fileFieldName?: string;
|
||||
/** 是否必填 */
|
||||
required: boolean;
|
||||
}
|
||||
|
||||
defineOptions({ name: 'CreateImportTaskModal' });
|
||||
|
||||
@@ -54,9 +83,35 @@ const fieldMappings = ref<
|
||||
>
|
||||
>({});
|
||||
|
||||
// 映射模板相关
|
||||
const mappingTemplates = ref<ImportMappingVO.ImportMappingInfoVO[]>([]);
|
||||
const selectedTemplateId = ref<number | undefined>();
|
||||
const templateLoading = ref(false);
|
||||
const savingTemplate = ref(false);
|
||||
const templateName = ref('');
|
||||
|
||||
// 创建保存模板的 vben modal
|
||||
const [SaveTemplateModal, saveTemplateModalApi] = useVbenModal({
|
||||
draggable: true,
|
||||
class: 'w-[500px]',
|
||||
title: '保存映射模板',
|
||||
showCancelButton: true,
|
||||
showConfirmButton: true,
|
||||
confirmText: '保存',
|
||||
cancelText: '取消',
|
||||
onConfirm: handleConfirmSaveTemplate,
|
||||
onCancel() {
|
||||
templateName.value = '';
|
||||
saveTemplateModalApi.close();
|
||||
},
|
||||
});
|
||||
|
||||
// 表单引用
|
||||
const formRef = ref();
|
||||
|
||||
// 导入任务store
|
||||
const importTaskStore = useImportTaskStore();
|
||||
|
||||
// 计算属性:必填字段是否都已绑定
|
||||
const canGoToNextStep = computed(() => {
|
||||
if (currentStep.value === 0) {
|
||||
@@ -167,6 +222,153 @@ const fetchFieldDefs = async () => {
|
||||
}
|
||||
};
|
||||
|
||||
// 获取映射模板列表
|
||||
const fetchMappingTemplates = async () => {
|
||||
try {
|
||||
templateLoading.value = true;
|
||||
const result = await getImportMappingPageApi({
|
||||
page: 1,
|
||||
pageSize: 100,
|
||||
filters: {
|
||||
type: 'order',
|
||||
},
|
||||
});
|
||||
mappingTemplates.value = result.records || [];
|
||||
} catch (error) {
|
||||
console.error('获取映射模板列表失败:', error);
|
||||
message.error('获取映射模板列表失败');
|
||||
} finally {
|
||||
templateLoading.value = false;
|
||||
}
|
||||
};
|
||||
|
||||
// 选择映射模板
|
||||
const handleSelectTemplate = async (value: any) => {
|
||||
const templateId = typeof value === 'number' ? value : undefined;
|
||||
if (!templateId) {
|
||||
return;
|
||||
}
|
||||
|
||||
const template = mappingTemplates.value.find((t) => t.id === templateId);
|
||||
if (!template) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
// 解析模板的 mapping JSON
|
||||
const mappingItems: FieldMappingItem[] = JSON.parse(template.mapping);
|
||||
|
||||
// 根据模板填充字段映射
|
||||
const mappings: typeof fieldMappings.value = {};
|
||||
fieldDefs.value.forEach((fieldDef) => {
|
||||
const templateItem = mappingItems.find(
|
||||
(item) => item.fieldName === fieldDef.fieldName,
|
||||
);
|
||||
let mappedHeader: string | undefined;
|
||||
|
||||
if (templateItem?.fileFieldName) {
|
||||
// 检查 CSV 表头中是否存在该字段名
|
||||
const matched = csvHeaders.value.find(
|
||||
(header) => header === templateItem.fileFieldName,
|
||||
);
|
||||
if (matched) {
|
||||
mappedHeader = matched;
|
||||
}
|
||||
}
|
||||
|
||||
mappings[fieldDef.id] = {
|
||||
fieldDef,
|
||||
mappedHeader,
|
||||
};
|
||||
});
|
||||
|
||||
fieldMappings.value = mappings;
|
||||
message.success('已应用映射模板');
|
||||
} catch (error) {
|
||||
console.error('应用映射模板失败:', error);
|
||||
message.error('应用映射模板失败,模板格式可能不正确');
|
||||
}
|
||||
};
|
||||
|
||||
// 打开保存模板模态框
|
||||
const handleSaveTemplate = () => {
|
||||
// 验证是否有映射数据
|
||||
const mappingItems: FieldMappingItem[] = Object.values(fieldMappings.value)
|
||||
.filter((m) => m.mappedHeader)
|
||||
.map((m) => ({
|
||||
fieldName: m.fieldDef.fieldName,
|
||||
fieldLabel: m.fieldDef.fieldLabel,
|
||||
fileFieldName: m.mappedHeader,
|
||||
required: m.fieldDef.required,
|
||||
}));
|
||||
|
||||
if (mappingItems.length === 0) {
|
||||
message.warning('请先完成字段映射');
|
||||
return;
|
||||
}
|
||||
|
||||
templateName.value = '';
|
||||
saveTemplateModalApi.open();
|
||||
};
|
||||
|
||||
// 确认保存映射模板
|
||||
async function handleConfirmSaveTemplate() {
|
||||
if (!templateName.value.trim()) {
|
||||
message.warning('请输入模板名称');
|
||||
return;
|
||||
}
|
||||
|
||||
// 验证必填字段是否都已绑定
|
||||
const requiredFields = fieldDefs.value.filter((f) => f.required);
|
||||
const unMappedFields = requiredFields.filter((field) => {
|
||||
const mapping = fieldMappings.value[field.id];
|
||||
return !mapping || !mapping.mappedHeader;
|
||||
});
|
||||
|
||||
if (unMappedFields.length > 0) {
|
||||
message.warning(
|
||||
`请先绑定所有必填字段:${unMappedFields.map((f) => f.fieldLabel).join('、')}`,
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
savingTemplate.value = true;
|
||||
saveTemplateModalApi.lock();
|
||||
|
||||
// 构建映射数据
|
||||
const mappingItems: FieldMappingItem[] = Object.values(fieldMappings.value)
|
||||
.filter((m) => m.mappedHeader)
|
||||
.map((m) => ({
|
||||
fieldName: m.fieldDef.fieldName,
|
||||
fieldLabel: m.fieldDef.fieldLabel,
|
||||
fileFieldName: m.mappedHeader,
|
||||
required: m.fieldDef.required,
|
||||
}));
|
||||
|
||||
const createData: ImportMappingDTO.CreateImportMappingDTO = {
|
||||
type: 'order',
|
||||
name: templateName.value.trim(),
|
||||
mapping: JSON.stringify(mappingItems),
|
||||
};
|
||||
|
||||
await createImportMappingApi(createData);
|
||||
message.success('保存映射模板成功');
|
||||
templateName.value = '';
|
||||
saveTemplateModalApi.close();
|
||||
// 刷新模板列表
|
||||
await fetchMappingTemplates();
|
||||
// 清空选中的模板
|
||||
selectedTemplateId.value = undefined;
|
||||
} catch (error: any) {
|
||||
console.error('保存映射模板失败:', error);
|
||||
message.error(error?.message || '保存映射模板失败');
|
||||
} finally {
|
||||
savingTemplate.value = false;
|
||||
saveTemplateModalApi.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
// 下一步
|
||||
const handleNext = () => {
|
||||
if (currentStep.value === 0) {
|
||||
@@ -178,6 +380,10 @@ const handleNext = () => {
|
||||
if (fieldDefs.value.length === 0) {
|
||||
fetchFieldDefs();
|
||||
}
|
||||
// 加载映射模板列表
|
||||
if (mappingTemplates.value.length === 0) {
|
||||
fetchMappingTemplates();
|
||||
}
|
||||
} else if (currentStep.value === 1) {
|
||||
// 验证必填字段
|
||||
const requiredFields = fieldDefs.value.filter((f) => f.required);
|
||||
@@ -218,29 +424,56 @@ const handleCreateTask = async () => {
|
||||
try {
|
||||
loading.value = true;
|
||||
|
||||
// 确保SSE连接已建立(如果还没有连接,先建立连接)
|
||||
if (!importTaskStore.sseConnection || !importTaskStore.sseConnected) {
|
||||
message.loading('正在建立连接...', 0);
|
||||
try {
|
||||
await importTaskStore.connectSse();
|
||||
message.destroy();
|
||||
} catch (error: any) {
|
||||
message.destroy();
|
||||
message.error(
|
||||
error?.message || '建立SSE连接失败,无法实时接收任务进度',
|
||||
);
|
||||
// 即使连接失败,也允许继续创建任务
|
||||
}
|
||||
}
|
||||
|
||||
// 先上传文件
|
||||
message.loading('正在上传文件...', 0);
|
||||
const fileInfo = await uploadFileApi(selectedFile.value);
|
||||
message.destroy();
|
||||
|
||||
// 构建字段映射 JSON
|
||||
const mapping: Record<string, string> = {};
|
||||
Object.values(fieldMappings.value).forEach((m) => {
|
||||
if (m.mappedHeader) {
|
||||
mapping[m.fieldDef.fieldName] = m.mappedHeader;
|
||||
}
|
||||
});
|
||||
// 构建字段映射 JSON(基于 ImportFieldDefInfoVO 结构)
|
||||
const mappingItems: FieldMappingItem[] = Object.values(fieldMappings.value)
|
||||
.filter((m) => m.mappedHeader)
|
||||
.map((m) => ({
|
||||
fieldName: m.fieldDef.fieldName,
|
||||
fieldLabel: m.fieldDef.fieldLabel,
|
||||
fileFieldName: m.mappedHeader,
|
||||
required: m.fieldDef.required,
|
||||
}));
|
||||
|
||||
const createData: ImportTaskDTO.CreateImportTaskDTO = {
|
||||
shopId: selectedShopId.value!,
|
||||
type: 'order',
|
||||
fileName: selectedFile.value.name,
|
||||
filePath: fileInfo.url,
|
||||
mapping: JSON.stringify(mapping),
|
||||
mapping: JSON.stringify(mappingItems),
|
||||
};
|
||||
|
||||
await createImportTaskApi(createData);
|
||||
const task = await createImportTaskApi(createData);
|
||||
message.success('创建导入任务成功');
|
||||
|
||||
// 更新store中的任务列表
|
||||
importTaskStore.updateTask(task);
|
||||
|
||||
// 刷新任务列表
|
||||
await importTaskStore.fetchTasks(true);
|
||||
|
||||
// 创建成功后,自动展开任务下拉菜单
|
||||
importTaskStore.openDropdown();
|
||||
|
||||
// 创建成功后,重置状态并关闭
|
||||
currentStep.value = 0;
|
||||
selectedShopId.value = undefined;
|
||||
@@ -354,8 +587,41 @@ const shopOptions = computed(() => {
|
||||
|
||||
<!-- 第二步:字段映射 -->
|
||||
<div v-if="currentStep === 1" class="py-4">
|
||||
<div class="mb-4 text-sm text-gray-600">
|
||||
请为每个订单字段选择对应的CSV列,必填字段必须绑定
|
||||
<div class="mb-4 flex items-center justify-between">
|
||||
<div class="text-sm text-gray-600">
|
||||
请为每个订单字段选择对应的CSV列,必填字段必须绑定
|
||||
</div>
|
||||
<div class="flex items-center space-x-2">
|
||||
<Select
|
||||
v-model:value="selectedTemplateId"
|
||||
placeholder="选择已有模板"
|
||||
class="w-[200px]"
|
||||
:loading="templateLoading"
|
||||
allow-clear
|
||||
@change="handleSelectTemplate"
|
||||
>
|
||||
<template #notFoundContent>
|
||||
<div v-if="templateLoading" class="py-4 text-center">
|
||||
<Spin size="small" />
|
||||
<div class="mt-2">加载中...</div>
|
||||
</div>
|
||||
<div v-else class="py-4 text-center text-gray-400">
|
||||
暂无模板
|
||||
</div>
|
||||
</template>
|
||||
<Select.Option
|
||||
v-for="template in mappingTemplates"
|
||||
:key="template.id"
|
||||
:value="template.id"
|
||||
>
|
||||
{{ template.name }}
|
||||
<span class="ml-2 text-xs text-gray-400">
|
||||
({{ new Date(template.createTime).toLocaleDateString() }})
|
||||
</span>
|
||||
</Select.Option>
|
||||
</Select>
|
||||
<Button @click="handleSaveTemplate">保存为模板</Button>
|
||||
</div>
|
||||
</div>
|
||||
<div class="max-h-[400px] space-y-4 overflow-y-auto">
|
||||
<div
|
||||
@@ -452,6 +718,21 @@ const shopOptions = computed(() => {
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 保存模板模态框 -->
|
||||
<SaveTemplateModal :confirm-loading="savingTemplate">
|
||||
<Form :label-col="{ span: 6 }" :wrapper-col="{ span: 18 }">
|
||||
<Form.Item label="模板名称" required>
|
||||
<Input
|
||||
v-model:value="templateName"
|
||||
placeholder="请输入模板名称"
|
||||
:maxlength="50"
|
||||
show-count
|
||||
@press-enter="handleConfirmSaveTemplate"
|
||||
/>
|
||||
</Form.Item>
|
||||
</Form>
|
||||
</SaveTemplateModal>
|
||||
</Modal>
|
||||
</template>
|
||||
|
||||
|
||||
@@ -89,6 +89,14 @@ const handleManageSpecs = (product: ProductVO.Info) => {
|
||||
{{ product.name }}
|
||||
</div>
|
||||
|
||||
<!-- 商品编码 -->
|
||||
<div
|
||||
v-if="product.productCode"
|
||||
class="mb-2 text-xs text-gray-500 dark:text-gray-400"
|
||||
>
|
||||
编码: {{ product.productCode }}
|
||||
</div>
|
||||
|
||||
<!-- 规格信息预览 -->
|
||||
<div
|
||||
v-if="product.specs && product.specs.length > 0"
|
||||
|
||||
+22
-6
@@ -66,6 +66,7 @@ const imageLoadError = ref(false); // 图片加载错误状态
|
||||
// 表单数据
|
||||
const formData = ref({
|
||||
name: '',
|
||||
productCode: '',
|
||||
specCode: '',
|
||||
skuCode: '',
|
||||
mainImageId: undefined as number | undefined,
|
||||
@@ -93,6 +94,7 @@ function initFormData() {
|
||||
const productData = currentProductData.value as any;
|
||||
formData.value = {
|
||||
name: currentProductData.value.name || '',
|
||||
productCode: currentProductData.value.productCode || '',
|
||||
specCode: productData.specCode || '',
|
||||
skuCode: productData.skuCode || '',
|
||||
mainImageId: undefined,
|
||||
@@ -105,6 +107,7 @@ function initFormData() {
|
||||
// 创建模式 - 如果有传入的productData,则使用它来初始化表单
|
||||
formData.value = {
|
||||
name: currentProductData.value?.name || '',
|
||||
productCode: currentProductData.value?.productCode || '',
|
||||
specCode: '',
|
||||
skuCode: '',
|
||||
mainImageId: undefined,
|
||||
@@ -174,15 +177,21 @@ async function handleSubmit() {
|
||||
'id' in currentProductData.value &&
|
||||
currentProductData.value.id
|
||||
) {
|
||||
await updateProductApi(
|
||||
currentProductData.value.id,
|
||||
submitData as ProductDTO.Update,
|
||||
);
|
||||
const updateData: ProductDTO.Update = {
|
||||
name: submitData.name,
|
||||
productCode: submitData.productCode || undefined,
|
||||
specCode: submitData.specCode || undefined,
|
||||
skuCode: submitData.skuCode || undefined,
|
||||
mainImageId: submitData.mainImageId,
|
||||
costPrice: submitData.costPrice,
|
||||
};
|
||||
await updateProductApi(currentProductData.value.id, updateData);
|
||||
message.success('更新成功');
|
||||
} else {
|
||||
// 创建模式:统一使用创建接口,规格信息作为可选字段
|
||||
const createData: ProductDTO.Create = {
|
||||
name: submitData.name,
|
||||
productCode: submitData.productCode || undefined,
|
||||
mainImageId: submitData.mainImageId,
|
||||
...(specs.value.length > 0 && { specs: specs.value }),
|
||||
};
|
||||
@@ -512,8 +521,15 @@ onUnmounted(() => {
|
||||
<Input v-model:value="formData.name" placeholder="请输入商品名称" />
|
||||
</Form.Item>
|
||||
|
||||
<Form.Item v-if="isEdit" label="商品编码" name="specCode">
|
||||
<Input v-model:value="formData.specCode" placeholder="请输入商品编码" />
|
||||
<Form.Item label="商品编码" name="productCode">
|
||||
<Input
|
||||
v-model:value="formData.productCode"
|
||||
placeholder="请输入商品编码"
|
||||
/>
|
||||
</Form.Item>
|
||||
|
||||
<Form.Item v-if="isEdit" label="规格编码" name="specCode">
|
||||
<Input v-model:value="formData.specCode" placeholder="请输入规格编码" />
|
||||
</Form.Item>
|
||||
|
||||
<Form.Item v-if="isEdit" label="商品SKU" name="skuCode">
|
||||
|
||||
@@ -31,6 +31,7 @@ const productGridViewRef = ref<null | { refreshTable: () => void }>(null);
|
||||
// 搜索表单
|
||||
const searchForm = ref({
|
||||
name: '',
|
||||
productCode: '',
|
||||
specCode: '',
|
||||
skuCode: '',
|
||||
minCostPrice: undefined as number | undefined,
|
||||
|
||||
+17
-1
@@ -30,6 +30,7 @@ const props = defineProps<{
|
||||
maxCostPrice?: number;
|
||||
minCostPrice?: number;
|
||||
name: string;
|
||||
productCode: string;
|
||||
skuCode: string;
|
||||
specCode: string;
|
||||
};
|
||||
@@ -54,6 +55,7 @@ const scrollContainerRef = ref<HTMLElement | null>(null);
|
||||
// 本地搜索表单副本(用于 v-model)
|
||||
const localSearchForm = ref({
|
||||
name: props.searchForm.name,
|
||||
productCode: props.searchForm.productCode,
|
||||
specCode: props.searchForm.specCode,
|
||||
skuCode: props.searchForm.skuCode,
|
||||
minCostPrice: props.searchForm.minCostPrice,
|
||||
@@ -66,12 +68,14 @@ watch(
|
||||
(newForm) => {
|
||||
if (
|
||||
localSearchForm.value.name !== newForm.name ||
|
||||
localSearchForm.value.productCode !== newForm.productCode ||
|
||||
localSearchForm.value.specCode !== newForm.specCode ||
|
||||
localSearchForm.value.skuCode !== newForm.skuCode ||
|
||||
localSearchForm.value.minCostPrice !== newForm.minCostPrice ||
|
||||
localSearchForm.value.maxCostPrice !== newForm.maxCostPrice
|
||||
) {
|
||||
localSearchForm.value.name = newForm.name;
|
||||
localSearchForm.value.productCode = newForm.productCode;
|
||||
localSearchForm.value.specCode = newForm.specCode;
|
||||
localSearchForm.value.skuCode = newForm.skuCode;
|
||||
localSearchForm.value.minCostPrice = newForm.minCostPrice;
|
||||
@@ -92,6 +96,7 @@ watch(
|
||||
(newForm) => {
|
||||
if (
|
||||
props.searchForm.name !== newForm.name ||
|
||||
props.searchForm.productCode !== newForm.productCode ||
|
||||
props.searchForm.specCode !== newForm.specCode ||
|
||||
props.searchForm.skuCode !== newForm.skuCode ||
|
||||
props.searchForm.minCostPrice !== newForm.minCostPrice ||
|
||||
@@ -114,6 +119,7 @@ const loadProductList = async (reset = false) => {
|
||||
pageSize: pageSize.value,
|
||||
filters: {
|
||||
name: localSearchForm.value.name || undefined,
|
||||
productCode: localSearchForm.value.productCode || undefined,
|
||||
specCode: localSearchForm.value.specCode || undefined,
|
||||
skuCode: localSearchForm.value.skuCode || undefined,
|
||||
minCostPrice: localSearchForm.value.minCostPrice,
|
||||
@@ -157,6 +163,7 @@ const handleSearch = () => {
|
||||
const handleReset = () => {
|
||||
localSearchForm.value = {
|
||||
name: '',
|
||||
productCode: '',
|
||||
specCode: '',
|
||||
skuCode: '',
|
||||
minCostPrice: undefined,
|
||||
@@ -235,13 +242,22 @@ onUnmounted(() => {
|
||||
</Form.Item>
|
||||
<Form.Item label="商品编码" class="mb-0">
|
||||
<Input
|
||||
v-model:value="localSearchForm.specCode"
|
||||
v-model:value="localSearchForm.productCode"
|
||||
placeholder="请输入商品编码"
|
||||
style="width: 150px"
|
||||
allow-clear
|
||||
@press-enter="handleSearch"
|
||||
/>
|
||||
</Form.Item>
|
||||
<Form.Item label="规格编码" class="mb-0">
|
||||
<Input
|
||||
v-model:value="localSearchForm.specCode"
|
||||
placeholder="请输入规格编码"
|
||||
style="width: 150px"
|
||||
allow-clear
|
||||
@press-enter="handleSearch"
|
||||
/>
|
||||
</Form.Item>
|
||||
<Form.Item label="商品SKU" class="mb-0">
|
||||
<Input
|
||||
v-model:value="localSearchForm.skuCode"
|
||||
|
||||
+21
-1
@@ -17,6 +17,7 @@ const props = defineProps<{
|
||||
maxCostPrice?: number;
|
||||
minCostPrice?: number;
|
||||
name: string;
|
||||
productCode: string;
|
||||
skuCode: string;
|
||||
specCode: string;
|
||||
};
|
||||
@@ -35,6 +36,7 @@ const loading = ref(false);
|
||||
// 本地搜索表单副本(用于 v-model)
|
||||
const localSearchForm = ref({
|
||||
name: props.searchForm.name,
|
||||
productCode: props.searchForm.productCode,
|
||||
specCode: props.searchForm.specCode,
|
||||
skuCode: props.searchForm.skuCode,
|
||||
minCostPrice: props.searchForm.minCostPrice,
|
||||
@@ -47,12 +49,14 @@ watch(
|
||||
(newForm) => {
|
||||
if (
|
||||
localSearchForm.value.name !== newForm.name ||
|
||||
localSearchForm.value.productCode !== newForm.productCode ||
|
||||
localSearchForm.value.specCode !== newForm.specCode ||
|
||||
localSearchForm.value.skuCode !== newForm.skuCode ||
|
||||
localSearchForm.value.minCostPrice !== newForm.minCostPrice ||
|
||||
localSearchForm.value.maxCostPrice !== newForm.maxCostPrice
|
||||
) {
|
||||
localSearchForm.value.name = newForm.name;
|
||||
localSearchForm.value.productCode = newForm.productCode;
|
||||
localSearchForm.value.specCode = newForm.specCode;
|
||||
localSearchForm.value.skuCode = newForm.skuCode;
|
||||
localSearchForm.value.minCostPrice = newForm.minCostPrice;
|
||||
@@ -69,6 +73,7 @@ watch(
|
||||
(newForm) => {
|
||||
if (
|
||||
props.searchForm.name !== newForm.name ||
|
||||
props.searchForm.productCode !== newForm.productCode ||
|
||||
props.searchForm.specCode !== newForm.specCode ||
|
||||
props.searchForm.skuCode !== newForm.skuCode ||
|
||||
props.searchForm.minCostPrice !== newForm.minCostPrice ||
|
||||
@@ -99,6 +104,12 @@ const gridOptions: VxeGridProps<any> = {
|
||||
minWidth: 200,
|
||||
showOverflow: 'tooltip' as const,
|
||||
},
|
||||
{
|
||||
field: 'productCode',
|
||||
title: '商品编码',
|
||||
minWidth: 150,
|
||||
showOverflow: 'tooltip' as const,
|
||||
},
|
||||
{
|
||||
title: '规格数量',
|
||||
minWidth: 100,
|
||||
@@ -166,6 +177,7 @@ const gridOptions: VxeGridProps<any> = {
|
||||
pageSize: page.pageSize,
|
||||
filters: {
|
||||
name: localSearchForm.value.name || undefined,
|
||||
productCode: localSearchForm.value.productCode || undefined,
|
||||
specCode: localSearchForm.value.specCode || undefined,
|
||||
skuCode: localSearchForm.value.skuCode || undefined,
|
||||
minCostPrice: localSearchForm.value.minCostPrice,
|
||||
@@ -208,9 +220,17 @@ const formOptions = computed(() => ({
|
||||
componentProps: {
|
||||
placeholder: '请输入商品编码',
|
||||
},
|
||||
fieldName: 'specCode',
|
||||
fieldName: 'productCode',
|
||||
label: '商品编码',
|
||||
},
|
||||
{
|
||||
component: 'Input',
|
||||
componentProps: {
|
||||
placeholder: '请输入规格编码',
|
||||
},
|
||||
fieldName: 'specCode',
|
||||
label: '规格编码',
|
||||
},
|
||||
{
|
||||
component: 'Input',
|
||||
componentProps: {
|
||||
|
||||
Reference in New Issue
Block a user