mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-25 03:49:19 +08:00
完成订单批量导入功能
This commit is contained in:
+350
@@ -0,0 +1,350 @@
|
||||
<template>
|
||||
<Modal
|
||||
:open="visible"
|
||||
:title="modalTitle"
|
||||
width="600px"
|
||||
:confirm-loading="loading"
|
||||
@ok="handleSubmit"
|
||||
@cancel="handleCancel"
|
||||
>
|
||||
<Form
|
||||
ref="formRef"
|
||||
:model="formData"
|
||||
:label-col="{ span: 6 }"
|
||||
:wrapper-col="{ span: 18 }"
|
||||
>
|
||||
<Form.Item label="公司Logo" name="logoImageId">
|
||||
<div style="display: flex; align-items: center; gap: 16px;">
|
||||
|
||||
<!-- Logo预览区域 -->
|
||||
<div style="position: relative; display: inline-block;">
|
||||
<!-- 有图片时显示图片 -->
|
||||
<img
|
||||
v-if="currentLogoUrl && !imageLoadError"
|
||||
:src="currentLogoUrl"
|
||||
alt="公司Logo"
|
||||
style="width: 80px; height: 80px; object-fit: cover; border-radius: 8px; border: 1px solid #d9d9d9; background-color: #f5f5f5;"
|
||||
@error="handleImageError"
|
||||
/>
|
||||
<!-- 无图片或加载失败时显示空状态 -->
|
||||
<div
|
||||
v-else
|
||||
style="width: 80px; height: 80px; border: 1px dashed #d9d9d9; border-radius: 8px; background-color: #fafafa; display: flex; align-items: center; justify-content: center; color: #999; font-size: 12px; text-align: center; line-height: 1.2;">
|
||||
<div style="display: flex; flex-direction: column; align-items: center; justify-content: center;">
|
||||
<CameraIcon style="font-size: 16px; margin-bottom: 4px;" />
|
||||
<div>上传Logo</div>
|
||||
</div>
|
||||
</div>
|
||||
<!-- 点击更换遮罩 -->
|
||||
<div
|
||||
style="position: absolute; top: 0; left: 0; right: 0; bottom: 0; background: rgba(0,0,0,0.6); border-radius: 8px; display: flex; align-items: center; justify-content: center; opacity: 0; transition: opacity 0.3s; cursor: pointer;"
|
||||
@mouseenter="($event.target as HTMLElement).style.opacity = '1'"
|
||||
@mouseleave="($event.target as HTMLElement).style.opacity = '0'"
|
||||
@click="selectLogoFile">
|
||||
<div style="color: white; text-align: center; display: flex; flex-direction: column; align-items: center; justify-content: center;">
|
||||
<CameraIcon style="font-size: 16px; margin-bottom: 4px;" />
|
||||
<div style="font-size: 12px;">{{ currentLogoUrl && !imageLoadError ? '更换Logo' : '上传Logo' }}</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 重置按钮 -->
|
||||
<div v-if="newLogoFile">
|
||||
<Button size="small" @click="removeNewLogo">重置</Button>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
</Form.Item>
|
||||
|
||||
<Form.Item label="公司名称" name="name" :rules="[{ required: true, message: '请输入公司名称' }]">
|
||||
<Input v-model:value="formData.name" placeholder="请输入公司名称" />
|
||||
</Form.Item>
|
||||
|
||||
<Form.Item label="公司地址" name="address">
|
||||
<Input v-model:value="formData.address" placeholder="请输入公司地址" />
|
||||
</Form.Item>
|
||||
|
||||
<Form.Item label="联系人" name="contactPerson">
|
||||
<Input v-model:value="formData.contactPerson" placeholder="请输入联系人" />
|
||||
</Form.Item>
|
||||
|
||||
<Form.Item label="联系电话" name="contactPhone" :rules="[{ pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号码' }]">
|
||||
<Input v-model:value="formData.contactPhone" placeholder="请输入联系电话" />
|
||||
</Form.Item>
|
||||
|
||||
<Form.Item label="邮箱" name="email" :rules="[{ type: 'email', message: '请输入正确的邮箱地址' }]">
|
||||
<Input v-model:value="formData.email" placeholder="请输入邮箱" />
|
||||
</Form.Item>
|
||||
|
||||
<Form.Item label="状态" name="status" :rules="[{ required: true, message: '请选择状态' }]">
|
||||
<Select
|
||||
v-model:value="formData.status"
|
||||
placeholder="请选择状态"
|
||||
:options="statusOptions"
|
||||
/>
|
||||
</Form.Item>
|
||||
|
||||
<Form.Item label="订阅到期时间" name="expireTime">
|
||||
<DatePicker
|
||||
v-model:value="formData.expireTime"
|
||||
placeholder="请选择订阅到期时间"
|
||||
style="width: 100%;"
|
||||
show-time
|
||||
format="YYYY-MM-DD HH:mm:ss"
|
||||
/>
|
||||
</Form.Item>
|
||||
</Form>
|
||||
</Modal>
|
||||
</template>
|
||||
|
||||
<script lang="ts" setup>
|
||||
import { computed, ref, onUnmounted } from 'vue';
|
||||
import { Form, Input, Modal, Select, DatePicker, Button, message } from 'ant-design-vue';
|
||||
import { createIconifyIcon } from '@vben/icons';
|
||||
import dayjs from 'dayjs';
|
||||
import {
|
||||
uploadCompanyLogoApi,
|
||||
createCompanyApi,
|
||||
updateCompanyApi,
|
||||
type CompanyVO,
|
||||
type CompanyDTO
|
||||
} from '#/api/org';
|
||||
|
||||
interface Emits {
|
||||
(e: 'success'): void;
|
||||
}
|
||||
|
||||
const emit = defineEmits<Emits>();
|
||||
|
||||
// 创建相机图标组件
|
||||
const CameraIcon = createIconifyIcon('mdi:camera');
|
||||
|
||||
// 内部状态管理
|
||||
const visible = ref(false);
|
||||
const isEdit = ref(false);
|
||||
const currentCompanyData = ref<CompanyVO.CompanyInfoVO | null>(null);
|
||||
|
||||
// 响应式数据
|
||||
const formRef = ref();
|
||||
const loading = ref(false);
|
||||
const newLogoFile = ref<File | null>(null); // 新选择的logo文件
|
||||
const originalLogoUrl = ref(''); // 原始logo URL
|
||||
const imageLoadError = ref(false); // 图片加载错误状态
|
||||
|
||||
// 表单数据
|
||||
const formData = ref({
|
||||
name: '',
|
||||
address: '',
|
||||
contactPerson: '',
|
||||
contactPhone: '',
|
||||
email: '',
|
||||
logoImageId: undefined as number | undefined,
|
||||
status: 1,
|
||||
expireTime: undefined as any,
|
||||
});
|
||||
|
||||
// 状态选项
|
||||
const statusOptions = [
|
||||
{ label: '正常', value: 1 },
|
||||
{ label: '禁用', value: 0 },
|
||||
];
|
||||
|
||||
// 模态框标题
|
||||
const modalTitle = computed(() => isEdit.value ? '编辑公司' : '添加公司');
|
||||
|
||||
// 当前显示的logo URL(优先显示新选择的)
|
||||
const currentLogoUrl = computed(() => {
|
||||
if (newLogoFile.value) {
|
||||
return URL.createObjectURL(newLogoFile.value);
|
||||
}
|
||||
return originalLogoUrl.value;
|
||||
});
|
||||
|
||||
// 初始化表单数据
|
||||
function initFormData() {
|
||||
if (isEdit.value && currentCompanyData.value) {
|
||||
// 编辑模式
|
||||
formData.value = {
|
||||
name: currentCompanyData.value.name,
|
||||
address: currentCompanyData.value.address || '',
|
||||
contactPerson: currentCompanyData.value.contactPerson || '',
|
||||
contactPhone: currentCompanyData.value.contactPhone || '',
|
||||
email: currentCompanyData.value.email || '',
|
||||
logoImageId: undefined,
|
||||
status: currentCompanyData.value.status,
|
||||
expireTime: currentCompanyData.value.expireTime ? dayjs(currentCompanyData.value.expireTime) : undefined,
|
||||
};
|
||||
|
||||
// 设置原始Logo URL
|
||||
originalLogoUrl.value = currentCompanyData.value.logo || '';
|
||||
} else {
|
||||
// 创建模式
|
||||
formData.value = {
|
||||
name: '',
|
||||
address: '',
|
||||
contactPerson: '',
|
||||
contactPhone: '',
|
||||
email: '',
|
||||
logoImageId: undefined,
|
||||
status: 1,
|
||||
expireTime: undefined,
|
||||
};
|
||||
originalLogoUrl.value = '';
|
||||
}
|
||||
|
||||
// 重置新选择的logo和图片加载状态
|
||||
newLogoFile.value = null;
|
||||
imageLoadError.value = false;
|
||||
}
|
||||
|
||||
// 重置表单(在open方法中调用)
|
||||
function resetForm() {
|
||||
formRef.value?.resetFields();
|
||||
newLogoFile.value = null;
|
||||
originalLogoUrl.value = '';
|
||||
imageLoadError.value = false;
|
||||
}
|
||||
|
||||
// 处理提交
|
||||
async function handleSubmit() {
|
||||
try {
|
||||
await formRef.value?.validate();
|
||||
loading.value = true;
|
||||
|
||||
// 准备提交数据
|
||||
const submitData = { ...formData.value };
|
||||
|
||||
// 处理日期格式
|
||||
if (submitData.expireTime) {
|
||||
submitData.expireTime = submitData.expireTime.format('YYYY-MM-DD HH:mm:ss');
|
||||
}
|
||||
|
||||
// 如果有新选择的logo,先上传获取ID
|
||||
if (newLogoFile.value) {
|
||||
const logoImageId = await uploadLogoAndGetId();
|
||||
if (logoImageId) {
|
||||
submitData.logoImageId = logoImageId;
|
||||
}
|
||||
} else if (!isEdit.value) {
|
||||
// 创建模式且没有选择Logo,移除logoImageId字段
|
||||
delete submitData.logoImageId;
|
||||
}
|
||||
|
||||
// 调用API
|
||||
if (isEdit.value && currentCompanyData.value) {
|
||||
await updateCompanyApi(currentCompanyData.value.id, submitData as CompanyDTO.UpdateCompanyDTO);
|
||||
message.success('更新成功');
|
||||
} else {
|
||||
await createCompanyApi(submitData as CompanyDTO.CreateCompanyDTO);
|
||||
message.success('创建成功');
|
||||
}
|
||||
|
||||
visible.value = false;
|
||||
emit('success');
|
||||
} catch (error) {
|
||||
console.error('保存公司失败:', error);
|
||||
message.error('保存公司失败');
|
||||
} finally {
|
||||
loading.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
// 处理取消
|
||||
function handleCancel() {
|
||||
visible.value = false;
|
||||
}
|
||||
|
||||
// 暴露给父组件的方法
|
||||
function open(editMode: boolean = false, companyData?: CompanyVO.CompanyInfoVO | null) {
|
||||
isEdit.value = editMode;
|
||||
currentCompanyData.value = companyData || null;
|
||||
|
||||
resetForm(); // 先重置表单
|
||||
initFormData(); // 再初始化数据
|
||||
|
||||
visible.value = true;
|
||||
}
|
||||
|
||||
// 暴露方法给父组件
|
||||
defineExpose({
|
||||
open,
|
||||
});
|
||||
|
||||
// 选择logo文件
|
||||
function selectLogoFile() {
|
||||
const input = document.createElement('input');
|
||||
input.type = 'file';
|
||||
input.accept = 'image/*';
|
||||
input.style.display = 'none';
|
||||
document.body.appendChild(input);
|
||||
|
||||
input.onchange = (e) => {
|
||||
const file = (e.target as HTMLInputElement).files?.[0];
|
||||
if (file) {
|
||||
// 验证文件
|
||||
const isImage = file.type.startsWith('image/');
|
||||
if (!isImage) {
|
||||
message.error('只能上传图片文件');
|
||||
document.body.removeChild(input);
|
||||
return;
|
||||
}
|
||||
|
||||
const isLt2M = file.size / 1024 / 1024 < 2;
|
||||
if (!isLt2M) {
|
||||
message.error('图片大小不能超过 2MB');
|
||||
document.body.removeChild(input);
|
||||
return;
|
||||
}
|
||||
|
||||
// 设置新选择的文件
|
||||
newLogoFile.value = file;
|
||||
imageLoadError.value = false; // 重置图片加载错误状态
|
||||
message.success('Logo选择成功');
|
||||
}
|
||||
|
||||
document.body.removeChild(input);
|
||||
};
|
||||
|
||||
input.click();
|
||||
}
|
||||
|
||||
// 重置新选择的logo
|
||||
function removeNewLogo() {
|
||||
newLogoFile.value = null;
|
||||
imageLoadError.value = false; // 重置图片加载错误状态
|
||||
message.success('已重置logo');
|
||||
}
|
||||
|
||||
// 上传Logo并获取图片ID
|
||||
async function uploadLogoAndGetId(): Promise<number | undefined> {
|
||||
if (!newLogoFile.value) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await uploadCompanyLogoApi(newLogoFile.value);
|
||||
console.log('Logo上传成功:', response);
|
||||
return response.id;
|
||||
} catch (error) {
|
||||
console.error('Logo上传失败:', error);
|
||||
message.error('Logo上传失败');
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
// 图片加载错误处理
|
||||
function handleImageError(event: Event) {
|
||||
console.error('图片加载失败:', event);
|
||||
imageLoadError.value = true;
|
||||
message.error('图片加载失败,请重新选择');
|
||||
}
|
||||
|
||||
|
||||
// 组件卸载时清理
|
||||
onUnmounted(() => {
|
||||
if (newLogoFile.value) {
|
||||
const url = URL.createObjectURL(newLogoFile.value);
|
||||
URL.revokeObjectURL(url);
|
||||
}
|
||||
});
|
||||
</script>
|
||||
+89
-146
@@ -1,19 +1,17 @@
|
||||
<script lang="ts" setup>
|
||||
import { h, ref, onUnmounted } from 'vue';
|
||||
import dayjs from 'dayjs';
|
||||
import { h, ref } from 'vue';
|
||||
|
||||
import { Page } from '@vben/common-ui';
|
||||
import { Button, DatePicker, Form, Input, Modal, Select, Table, message } from 'ant-design-vue';
|
||||
import { Button, Input, Select, Table, message, Tag } from 'ant-design-vue';
|
||||
|
||||
import {
|
||||
createCompanyApi,
|
||||
deleteCompanyApi,
|
||||
getCompanyPageApi,
|
||||
updateCompanyApi,
|
||||
getCompanyPageApi,
|
||||
type CompanyVO,
|
||||
type CompanyDTO
|
||||
} from '#/api/org';
|
||||
import type { PageQueryDTO } from '#/types/common';
|
||||
import CompanyFormModal from './components/CompanyFormModal.vue';
|
||||
|
||||
defineOptions({ name: 'CompanyManagement' });
|
||||
|
||||
@@ -23,30 +21,20 @@ const companyList = ref<CompanyVO.CompanyInfoVO[]>([]);
|
||||
const total = ref(0);
|
||||
const current = ref(1);
|
||||
const pageSize = ref(10);
|
||||
const modalVisible = ref(false);
|
||||
const modalTitle = ref('');
|
||||
const isEdit = ref(false);
|
||||
const currentCompanyId = ref<number | null>(null);
|
||||
const formRef = ref();
|
||||
const companyFormModalRef = ref();
|
||||
|
||||
// 搜索表单
|
||||
const searchForm = ref({
|
||||
name: '',
|
||||
status: undefined as number | undefined,
|
||||
});
|
||||
|
||||
// 表单数据
|
||||
const formData = ref({
|
||||
name: '',
|
||||
address: '',
|
||||
contactPerson: '',
|
||||
contactPhone: '',
|
||||
email: '',
|
||||
logo: '',
|
||||
status: 1,
|
||||
expireTime: null as any,
|
||||
status: undefined as number | undefined,
|
||||
expired: undefined as string | undefined,
|
||||
});
|
||||
|
||||
// 表单数据(已移至CompanyFormModal组件)
|
||||
|
||||
// 状态选项
|
||||
const statusOptions = [
|
||||
{ label: '禁用', value: 0 },
|
||||
@@ -107,9 +95,26 @@ const columns = [
|
||||
key: 'status',
|
||||
width: 100,
|
||||
customRender: ({ record }: { record: CompanyVO.CompanyInfoVO }) => {
|
||||
return h('span', {
|
||||
style: { color: record.status === 1 ? '#52c41a' : '#ff4d4f' },
|
||||
}, record.status === 1 ? '正常' : '禁用');
|
||||
const statusMap = {
|
||||
0: { text: '禁用', color: 'red' },
|
||||
1: { text: '正常', color: 'green' },
|
||||
};
|
||||
const status = statusMap[record.status as keyof typeof statusMap] || { text: '未知', color: 'default' };
|
||||
return h(Tag, { color: status.color }, () => status.text);
|
||||
},
|
||||
},
|
||||
{
|
||||
title: '过期状态',
|
||||
dataIndex: 'expired',
|
||||
key: 'expired',
|
||||
width: 100,
|
||||
customRender: ({ record }: { record: CompanyVO.CompanyInfoVO }) => {
|
||||
if (record.expired === true) {
|
||||
return h(Tag, { color: 'red' }, () => '已过期');
|
||||
} else if (record.expired === false) {
|
||||
return h(Tag, { color: 'green' }, () => '正常');
|
||||
}
|
||||
return h('span', { style: { color: '#999' } }, '未知');
|
||||
},
|
||||
},
|
||||
{
|
||||
@@ -165,7 +170,11 @@ function handleSearch() {
|
||||
function handleReset() {
|
||||
searchForm.value = {
|
||||
name: '',
|
||||
contactPerson: '',
|
||||
contactPhone: '',
|
||||
email: '',
|
||||
status: undefined,
|
||||
expired: undefined,
|
||||
};
|
||||
current.value = 1;
|
||||
fetchCompanyList();
|
||||
@@ -180,38 +189,12 @@ function handleTableChange(pagination: any) {
|
||||
|
||||
// 处理添加公司
|
||||
function handleAdd() {
|
||||
modalTitle.value = '添加公司';
|
||||
isEdit.value = false;
|
||||
currentCompanyId.value = null;
|
||||
formData.value = {
|
||||
name: '',
|
||||
address: '',
|
||||
contactPerson: '',
|
||||
contactPhone: '',
|
||||
email: '',
|
||||
logo: '',
|
||||
status: 1,
|
||||
expireTime: null,
|
||||
};
|
||||
modalVisible.value = true;
|
||||
companyFormModalRef.value?.open(false);
|
||||
}
|
||||
|
||||
// 处理编辑公司
|
||||
function handleEdit(record: CompanyVO.CompanyInfoVO) {
|
||||
modalTitle.value = '编辑公司';
|
||||
isEdit.value = true;
|
||||
currentCompanyId.value = record.id;
|
||||
formData.value = {
|
||||
name: record.name,
|
||||
address: record.address || '',
|
||||
contactPerson: record.contactPerson || '',
|
||||
contactPhone: record.contactPhone || '',
|
||||
email: record.email || '',
|
||||
logo: record.logo || '',
|
||||
status: record.status,
|
||||
expireTime: record.expireTime ? dayjs(record.expireTime) : null,
|
||||
};
|
||||
modalVisible.value = true;
|
||||
companyFormModalRef.value?.open(true, record);
|
||||
}
|
||||
|
||||
|
||||
@@ -219,11 +202,6 @@ function handleEdit(record: CompanyVO.CompanyInfoVO) {
|
||||
// 组件卸载标志
|
||||
let isUnmounted = false;
|
||||
|
||||
// 组件卸载时设置标志
|
||||
onUnmounted(() => {
|
||||
isUnmounted = true;
|
||||
});
|
||||
|
||||
// 获取公司列表
|
||||
async function fetchCompanyList() {
|
||||
if (isUnmounted) return;
|
||||
@@ -235,7 +213,11 @@ async function fetchCompanyList() {
|
||||
pageSize: pageSize.value,
|
||||
filters: {
|
||||
name: searchForm.value.name || undefined,
|
||||
contactPerson: searchForm.value.contactPerson || undefined,
|
||||
contactPhone: searchForm.value.contactPhone || undefined,
|
||||
email: searchForm.value.email || undefined,
|
||||
status: searchForm.value.status,
|
||||
expired: searchForm.value.expired === 'true' ? true : searchForm.value.expired === 'false' ? false : undefined,
|
||||
},
|
||||
};
|
||||
const data = await getCompanyPageApi(params);
|
||||
@@ -273,41 +255,13 @@ async function handleDelete(record: CompanyVO.CompanyInfoVO) {
|
||||
}
|
||||
}
|
||||
|
||||
// 处理表单提交
|
||||
async function handleSubmit() {
|
||||
try {
|
||||
await formRef.value?.validate();
|
||||
|
||||
// 准备提交数据,转换日期格式
|
||||
const submitData = {
|
||||
...formData.value,
|
||||
expireTime: formData.value.expireTime ? formData.value.expireTime.format('YYYY-MM-DD HH:mm:ss') : undefined,
|
||||
};
|
||||
|
||||
if (isEdit.value && currentCompanyId.value) {
|
||||
await updateCompanyApi(currentCompanyId.value, submitData);
|
||||
|
||||
if (isUnmounted) return;
|
||||
|
||||
message.success('更新成功');
|
||||
} else {
|
||||
await createCompanyApi(submitData);
|
||||
|
||||
if (isUnmounted) return;
|
||||
|
||||
message.success('创建成功');
|
||||
}
|
||||
|
||||
modalVisible.value = false;
|
||||
await fetchCompanyList();
|
||||
} catch (error) {
|
||||
if (isUnmounted) return;
|
||||
|
||||
console.error('保存公司失败:', error);
|
||||
message.error('保存公司失败');
|
||||
}
|
||||
// 处理模态框成功回调
|
||||
async function handleModalSuccess() {
|
||||
await fetchCompanyList();
|
||||
}
|
||||
|
||||
// Logo相关方法已移至CompanyFormModal组件
|
||||
|
||||
// 初始化
|
||||
fetchCompanyList();
|
||||
</script>
|
||||
@@ -338,6 +292,33 @@ fetchCompanyList();
|
||||
@press-enter="handleSearch"
|
||||
/>
|
||||
</div>
|
||||
<div>
|
||||
<label style="display: block; margin-bottom: 4px; font-weight: 500;">联系人</label>
|
||||
<Input
|
||||
v-model:value="searchForm.contactPerson"
|
||||
placeholder="请输入联系人"
|
||||
style="width: 150px;"
|
||||
@press-enter="handleSearch"
|
||||
/>
|
||||
</div>
|
||||
<div>
|
||||
<label style="display: block; margin-bottom: 4px; font-weight: 500;">联系电话</label>
|
||||
<Input
|
||||
v-model:value="searchForm.contactPhone"
|
||||
placeholder="请输入联系电话"
|
||||
style="width: 150px;"
|
||||
@press-enter="handleSearch"
|
||||
/>
|
||||
</div>
|
||||
<div>
|
||||
<label style="display: block; margin-bottom: 4px; font-weight: 500;">邮箱</label>
|
||||
<Input
|
||||
v-model:value="searchForm.email"
|
||||
placeholder="请输入邮箱"
|
||||
style="width: 200px;"
|
||||
@press-enter="handleSearch"
|
||||
/>
|
||||
</div>
|
||||
<div>
|
||||
<label style="display: block; margin-bottom: 4px; font-weight: 500;">状态</label>
|
||||
<Select
|
||||
@@ -348,6 +329,19 @@ fetchCompanyList();
|
||||
allow-clear
|
||||
/>
|
||||
</div>
|
||||
<div>
|
||||
<label style="display: block; margin-bottom: 4px; font-weight: 500;">过期状态</label>
|
||||
<Select
|
||||
v-model:value="searchForm.expired"
|
||||
placeholder="请选择过期状态"
|
||||
style="width: 120px;"
|
||||
:options="[
|
||||
{ label: '已过期', value: 'true' },
|
||||
{ label: '正常', value: 'false' }
|
||||
]"
|
||||
allow-clear
|
||||
/>
|
||||
</div>
|
||||
<div style="display: flex; gap: 8px; align-items: end;">
|
||||
<Button type="primary" @click="handleSearch">搜索</Button>
|
||||
<Button @click="handleReset">重置</Button>
|
||||
@@ -372,61 +366,10 @@ fetchCompanyList();
|
||||
@change="handleTableChange"
|
||||
/>
|
||||
|
||||
<!-- 添加/编辑模态框 -->
|
||||
<Modal
|
||||
v-model:open="modalVisible"
|
||||
:title="modalTitle"
|
||||
width="600px"
|
||||
@ok="handleSubmit"
|
||||
>
|
||||
<Form
|
||||
ref="formRef"
|
||||
:model="formData"
|
||||
:label-col="{ span: 6 }"
|
||||
:wrapper-col="{ span: 18 }"
|
||||
>
|
||||
<Form.Item label="公司名称" name="name" :rules="[{ required: true, message: '请输入公司名称' }]">
|
||||
<Input v-model:value="formData.name" placeholder="请输入公司名称" />
|
||||
</Form.Item>
|
||||
|
||||
<Form.Item label="公司地址" name="address">
|
||||
<Input v-model:value="formData.address" placeholder="请输入公司地址" />
|
||||
</Form.Item>
|
||||
|
||||
<Form.Item label="联系人" name="contactPerson">
|
||||
<Input v-model:value="formData.contactPerson" placeholder="请输入联系人" />
|
||||
</Form.Item>
|
||||
|
||||
<Form.Item label="联系电话" name="contactPhone" :rules="[{ pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号码' }]">
|
||||
<Input v-model:value="formData.contactPhone" placeholder="请输入联系电话" />
|
||||
</Form.Item>
|
||||
|
||||
<Form.Item label="邮箱" name="email" :rules="[{ type: 'email', message: '请输入正确的邮箱地址' }]">
|
||||
<Input v-model:value="formData.email" placeholder="请输入邮箱" />
|
||||
</Form.Item>
|
||||
|
||||
<Form.Item label="Logo" name="logo" :rules="[{ type: 'url', message: '请输入正确的URL地址' }]">
|
||||
<Input v-model:value="formData.logo" placeholder="请输入Logo地址" />
|
||||
</Form.Item>
|
||||
|
||||
<Form.Item label="状态" name="status" :rules="[{ required: true, message: '请选择状态' }]">
|
||||
<Select
|
||||
v-model:value="formData.status"
|
||||
placeholder="请选择状态"
|
||||
:options="statusOptions"
|
||||
/>
|
||||
</Form.Item>
|
||||
|
||||
<Form.Item label="订阅到期时间" name="expireTime">
|
||||
<DatePicker
|
||||
v-model:value="formData.expireTime"
|
||||
placeholder="请选择订阅到期时间"
|
||||
style="width: 100%;"
|
||||
show-time
|
||||
format="YYYY-MM-DD HH:mm:ss"
|
||||
/>
|
||||
</Form.Item>
|
||||
</Form>
|
||||
</Modal>
|
||||
<!-- 公司表单模态框 -->
|
||||
<CompanyFormModal
|
||||
ref="companyFormModalRef"
|
||||
@success="handleModalSuccess"
|
||||
/>
|
||||
</Page>
|
||||
</template>
|
||||
|
||||
+193
@@ -0,0 +1,193 @@
|
||||
<script lang="ts" setup>
|
||||
import { computed, ref } from 'vue';
|
||||
import { Form, Input, Modal, Select, message } from 'ant-design-vue';
|
||||
|
||||
import {
|
||||
createDepartmentApi,
|
||||
updateDepartmentApi,
|
||||
type DepartmentVO,
|
||||
type DepartmentDTO
|
||||
} from '#/api/org';
|
||||
|
||||
interface Props {
|
||||
departmentList: DepartmentVO.DepartmentInfoVO[];
|
||||
}
|
||||
|
||||
interface Emits {
|
||||
(e: 'success'): void;
|
||||
}
|
||||
|
||||
const props = defineProps<Props>();
|
||||
|
||||
const emit = defineEmits<Emits>();
|
||||
|
||||
// 响应式数据
|
||||
const formRef = ref();
|
||||
const loading = ref(false);
|
||||
const visible = ref(false);
|
||||
const isEdit = ref(false);
|
||||
const currentDepartmentData = ref<DepartmentVO.DepartmentInfoVO | null>(null);
|
||||
|
||||
// 表单数据
|
||||
const formData = ref<DepartmentDTO.CreateDepartmentDTO>({
|
||||
name: '',
|
||||
parentId: undefined,
|
||||
remark: '',
|
||||
status: 1,
|
||||
});
|
||||
|
||||
// 状态选项
|
||||
const statusOptions = [
|
||||
{ label: '禁用', value: 0 },
|
||||
{ label: '启用', value: 1 },
|
||||
];
|
||||
|
||||
// 父部门选项
|
||||
const parentDepartmentOptions = computed(() => {
|
||||
const options: Array<{ label: string; value: number | undefined }> = [{ label: '根部门', value: undefined }];
|
||||
|
||||
const addDepartmentOptions = (departments: DepartmentVO.DepartmentInfoVO[], level = 0) => {
|
||||
departments.forEach(dept => {
|
||||
// 编辑时排除自己和自己的子部门
|
||||
if (isEdit.value && currentDepartmentData.value &&
|
||||
(dept.id === currentDepartmentData.value.id || isChildDepartment(dept, currentDepartmentData.value.id))) {
|
||||
return;
|
||||
}
|
||||
|
||||
const prefix = ' '.repeat(level);
|
||||
options.push({
|
||||
label: `${prefix}${dept.name}`,
|
||||
value: dept.id,
|
||||
});
|
||||
if (dept.children && dept.children.length > 0) {
|
||||
addDepartmentOptions(dept.children, level + 1);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
addDepartmentOptions(props.departmentList);
|
||||
return options;
|
||||
});
|
||||
|
||||
// 检查是否为子部门
|
||||
function isChildDepartment(department: DepartmentVO.DepartmentInfoVO, parentId: number): boolean {
|
||||
if (department.children) {
|
||||
for (const child of department.children) {
|
||||
if (child.id === parentId || isChildDepartment(child, parentId)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// 模态框标题
|
||||
const modalTitle = computed(() => isEdit.value ? '编辑部门' : '添加部门');
|
||||
|
||||
// 重置表单
|
||||
function resetForm() {
|
||||
formData.value = {
|
||||
name: '',
|
||||
parentId: undefined,
|
||||
remark: '',
|
||||
status: 1,
|
||||
};
|
||||
formRef.value?.resetFields();
|
||||
}
|
||||
|
||||
// 处理模态框关闭
|
||||
function handleCancel() {
|
||||
visible.value = false;
|
||||
}
|
||||
|
||||
// 处理表单提交
|
||||
async function handleSubmit() {
|
||||
try {
|
||||
await formRef.value?.validate();
|
||||
loading.value = true;
|
||||
|
||||
if (isEdit.value && currentDepartmentData.value) {
|
||||
await updateDepartmentApi(currentDepartmentData.value.id, formData.value);
|
||||
message.success('更新成功');
|
||||
} else {
|
||||
await createDepartmentApi(formData.value);
|
||||
message.success('创建成功');
|
||||
}
|
||||
|
||||
visible.value = false;
|
||||
emit('success');
|
||||
} catch (error) {
|
||||
console.error('保存部门失败:', error);
|
||||
message.error('保存部门失败');
|
||||
} finally {
|
||||
loading.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
// 暴露给父组件的方法
|
||||
function open(editMode: boolean = false, departmentData?: DepartmentVO.DepartmentInfoVO | null) {
|
||||
isEdit.value = editMode;
|
||||
currentDepartmentData.value = departmentData || null;
|
||||
|
||||
resetForm();
|
||||
|
||||
if (editMode && departmentData) {
|
||||
formData.value = {
|
||||
name: departmentData.name,
|
||||
parentId: departmentData.parentId,
|
||||
remark: departmentData.remark || '',
|
||||
status: departmentData.status,
|
||||
};
|
||||
}
|
||||
|
||||
visible.value = true;
|
||||
}
|
||||
|
||||
// 暴露方法给父组件
|
||||
defineExpose({
|
||||
open,
|
||||
});
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<Modal
|
||||
:open="visible"
|
||||
:title="modalTitle"
|
||||
width="500px"
|
||||
:confirm-loading="loading"
|
||||
@ok="handleSubmit"
|
||||
@cancel="handleCancel"
|
||||
>
|
||||
<Form
|
||||
ref="formRef"
|
||||
:model="formData"
|
||||
:label-col="{ span: 6 }"
|
||||
:wrapper-col="{ span: 18 }"
|
||||
>
|
||||
<Form.Item label="父部门" name="parentId">
|
||||
<Select
|
||||
v-model:value="formData.parentId"
|
||||
placeholder="请选择父部门"
|
||||
:options="parentDepartmentOptions"
|
||||
allow-clear
|
||||
/>
|
||||
</Form.Item>
|
||||
|
||||
<Form.Item label="部门名称" name="name" :rules="[{ required: true, message: '请输入部门名称' }]">
|
||||
<Input v-model:value="formData.name" placeholder="请输入部门名称" />
|
||||
</Form.Item>
|
||||
|
||||
<Form.Item label="描述" name="remark">
|
||||
<Input.TextArea v-model:value="formData.remark" placeholder="请输入部门描述" :rows="3" />
|
||||
</Form.Item>
|
||||
|
||||
<Form.Item label="状态" name="status">
|
||||
<Select
|
||||
v-model:value="formData.status"
|
||||
placeholder="请选择状态"
|
||||
:options="statusOptions"
|
||||
/>
|
||||
</Form.Item>
|
||||
</Form>
|
||||
</Modal>
|
||||
</template>
|
||||
+28
-95
@@ -2,17 +2,16 @@
|
||||
import { computed, h, ref } from 'vue';
|
||||
|
||||
import { Page } from '@vben/common-ui';
|
||||
import { Button, Form, Input, Modal, Select, Table, message } from 'ant-design-vue';
|
||||
import { Button, Input, Select, Table, message } from 'ant-design-vue';
|
||||
|
||||
import {
|
||||
createDepartmentApi,
|
||||
deleteDepartmentApi,
|
||||
getDepartmentPageApi,
|
||||
updateDepartmentApi,
|
||||
getDepartmentPageApi,
|
||||
type DepartmentVO,
|
||||
type DepartmentDTO
|
||||
} from '#/api/org';
|
||||
import type { PageQueryDTO } from '#/types/common';
|
||||
import DepartmentModal from './components/DepartmentModal.vue';
|
||||
|
||||
defineOptions({ name: 'DepartmentManagement' });
|
||||
|
||||
@@ -22,26 +21,15 @@ const departmentList = ref<DepartmentVO.DepartmentInfoVO[]>([]);
|
||||
const total = ref(0);
|
||||
const current = ref(1);
|
||||
const pageSize = ref(10);
|
||||
const modalVisible = ref(false);
|
||||
const modalTitle = ref('');
|
||||
const isEdit = ref(false);
|
||||
const currentDepartmentId = ref<number | null>(null);
|
||||
const formRef = ref();
|
||||
const departmentModalRef = ref();
|
||||
|
||||
// 搜索表单
|
||||
const searchForm = ref({
|
||||
name: '',
|
||||
parentId: undefined as number | undefined,
|
||||
status: undefined as number | undefined,
|
||||
});
|
||||
|
||||
// 表单数据
|
||||
const formData = ref<DepartmentDTO.CreateDepartmentDTO>({
|
||||
name: '',
|
||||
parentId: undefined,
|
||||
remark: '',
|
||||
status: 1,
|
||||
});
|
||||
|
||||
// 状态选项
|
||||
const statusOptions = [
|
||||
{ label: '禁用', value: 0 },
|
||||
@@ -155,6 +143,7 @@ async function fetchDepartmentList() {
|
||||
pageSize: pageSize.value,
|
||||
filters: {
|
||||
name: searchForm.value.name || undefined,
|
||||
parentId: searchForm.value.parentId,
|
||||
status: searchForm.value.status,
|
||||
},
|
||||
};
|
||||
@@ -179,6 +168,7 @@ function handleSearch() {
|
||||
function handleReset() {
|
||||
searchForm.value = {
|
||||
name: '',
|
||||
parentId: undefined,
|
||||
status: undefined,
|
||||
};
|
||||
current.value = 1;
|
||||
@@ -194,30 +184,12 @@ function handleTableChange(pagination: any) {
|
||||
|
||||
// 处理添加部门
|
||||
function handleAdd() {
|
||||
modalTitle.value = '添加部门';
|
||||
isEdit.value = false;
|
||||
currentDepartmentId.value = null;
|
||||
formData.value = {
|
||||
name: '',
|
||||
parentId: undefined as number | undefined,
|
||||
remark: '',
|
||||
status: 1,
|
||||
};
|
||||
modalVisible.value = true;
|
||||
departmentModalRef.value?.open(false);
|
||||
}
|
||||
|
||||
// 处理编辑部门
|
||||
function handleEdit(record: DepartmentVO.DepartmentInfoVO) {
|
||||
modalTitle.value = '编辑部门';
|
||||
isEdit.value = true;
|
||||
currentDepartmentId.value = record.id;
|
||||
formData.value = {
|
||||
name: record.name,
|
||||
parentId: record.parentId,
|
||||
remark: record.remark || '',
|
||||
status: record.status,
|
||||
};
|
||||
modalVisible.value = true;
|
||||
departmentModalRef.value?.open(true, record);
|
||||
}
|
||||
|
||||
// 处理删除部门
|
||||
@@ -232,25 +204,9 @@ async function handleDelete(record: DepartmentVO.DepartmentInfoVO) {
|
||||
}
|
||||
}
|
||||
|
||||
// 处理表单提交
|
||||
async function handleSubmit() {
|
||||
try {
|
||||
await formRef.value?.validate();
|
||||
|
||||
if (isEdit.value && currentDepartmentId.value) {
|
||||
await updateDepartmentApi(currentDepartmentId.value, formData.value);
|
||||
message.success('更新成功');
|
||||
} else {
|
||||
await createDepartmentApi(formData.value);
|
||||
message.success('创建成功');
|
||||
}
|
||||
|
||||
modalVisible.value = false;
|
||||
await fetchDepartmentList();
|
||||
} catch (error) {
|
||||
console.error('保存部门失败:', error);
|
||||
message.error('保存部门失败');
|
||||
}
|
||||
// 处理模态框成功回调
|
||||
async function handleModalSuccess() {
|
||||
await fetchDepartmentList();
|
||||
}
|
||||
|
||||
// 初始化
|
||||
@@ -283,6 +239,16 @@ fetchDepartmentList();
|
||||
@press-enter="handleSearch"
|
||||
/>
|
||||
</div>
|
||||
<div>
|
||||
<label style="display: block; margin-bottom: 4px; font-weight: 500;">父部门</label>
|
||||
<Select
|
||||
v-model:value="searchForm.parentId"
|
||||
placeholder="请选择父部门"
|
||||
style="width: 200px;"
|
||||
:options="parentDepartmentOptions"
|
||||
allow-clear
|
||||
/>
|
||||
</div>
|
||||
<div>
|
||||
<label style="display: block; margin-bottom: 4px; font-weight: 500;">状态</label>
|
||||
<Select
|
||||
@@ -317,44 +283,11 @@ fetchDepartmentList();
|
||||
@change="handleTableChange"
|
||||
/>
|
||||
|
||||
<!-- 添加/编辑模态框 -->
|
||||
<Modal
|
||||
v-model:open="modalVisible"
|
||||
:title="modalTitle"
|
||||
width="500px"
|
||||
@ok="handleSubmit"
|
||||
>
|
||||
<Form
|
||||
ref="formRef"
|
||||
:model="formData"
|
||||
:label-col="{ span: 6 }"
|
||||
:wrapper-col="{ span: 18 }"
|
||||
>
|
||||
<Form.Item label="父部门" name="parentId">
|
||||
<Select
|
||||
v-model:value="formData.parentId"
|
||||
placeholder="请选择父部门"
|
||||
:options="parentDepartmentOptions"
|
||||
allow-clear
|
||||
/>
|
||||
</Form.Item>
|
||||
|
||||
<Form.Item label="部门名称" name="name" :rules="[{ required: true, message: '请输入部门名称' }]">
|
||||
<Input v-model:value="formData.name" placeholder="请输入部门名称" />
|
||||
</Form.Item>
|
||||
|
||||
<Form.Item label="描述" name="remark">
|
||||
<Input.TextArea v-model:value="formData.remark" placeholder="请输入部门描述" :rows="3" />
|
||||
</Form.Item>
|
||||
|
||||
<Form.Item label="状态" name="status">
|
||||
<Select
|
||||
v-model:value="formData.status"
|
||||
placeholder="请选择状态"
|
||||
:options="statusOptions"
|
||||
/>
|
||||
</Form.Item>
|
||||
</Form>
|
||||
</Modal>
|
||||
<!-- 部门模态框组件 -->
|
||||
<DepartmentModal
|
||||
ref="departmentModalRef"
|
||||
:department-list="departmentList"
|
||||
@success="handleModalSuccess"
|
||||
/>
|
||||
</Page>
|
||||
</template>
|
||||
|
||||
+161
@@ -0,0 +1,161 @@
|
||||
<script lang="ts" setup>
|
||||
import { computed, ref } from 'vue';
|
||||
import { Form, Input, Modal, Select, message } from 'ant-design-vue';
|
||||
|
||||
import {
|
||||
createPositionApi,
|
||||
updatePositionApi,
|
||||
type PositionVO,
|
||||
type PositionDTO
|
||||
} from '#/api/org';
|
||||
|
||||
interface Props {
|
||||
departmentOptions: Array<{ label: string; value: number }>;
|
||||
}
|
||||
|
||||
interface Emits {
|
||||
(e: 'success'): void;
|
||||
}
|
||||
|
||||
defineProps<Props>();
|
||||
|
||||
const emit = defineEmits<Emits>();
|
||||
|
||||
// 响应式数据
|
||||
const formRef = ref();
|
||||
const loading = ref(false);
|
||||
const visible = ref(false);
|
||||
const isEdit = ref(false);
|
||||
const currentPositionData = ref<PositionVO.PositionInfoVO | null>(null);
|
||||
|
||||
// 表单数据
|
||||
const formData = ref<PositionDTO.CreatePositionDTO>({
|
||||
name: '',
|
||||
code: '',
|
||||
departmentId: undefined,
|
||||
remark: '',
|
||||
status: 1,
|
||||
});
|
||||
|
||||
// 状态选项
|
||||
const statusOptions = [
|
||||
{ label: '禁用', value: 0 },
|
||||
{ label: '启用', value: 1 },
|
||||
];
|
||||
|
||||
// 模态框标题
|
||||
const modalTitle = computed(() => isEdit.value ? '编辑岗位' : '添加岗位');
|
||||
|
||||
// 重置表单
|
||||
function resetForm() {
|
||||
formData.value = {
|
||||
name: '',
|
||||
code: '',
|
||||
departmentId: undefined,
|
||||
remark: '',
|
||||
status: 1,
|
||||
};
|
||||
formRef.value?.resetFields();
|
||||
}
|
||||
|
||||
// 处理模态框关闭
|
||||
function handleCancel() {
|
||||
visible.value = false;
|
||||
}
|
||||
|
||||
// 处理表单提交
|
||||
async function handleSubmit() {
|
||||
try {
|
||||
await formRef.value?.validate();
|
||||
loading.value = true;
|
||||
|
||||
if (isEdit.value && currentPositionData.value) {
|
||||
await updatePositionApi(currentPositionData.value.id, formData.value);
|
||||
message.success('更新成功');
|
||||
} else {
|
||||
await createPositionApi(formData.value);
|
||||
message.success('创建成功');
|
||||
}
|
||||
|
||||
visible.value = false;
|
||||
emit('success');
|
||||
} catch (error) {
|
||||
console.error('保存岗位失败:', error);
|
||||
message.error('保存岗位失败');
|
||||
} finally {
|
||||
loading.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
// 暴露给父组件的方法
|
||||
function open(editMode: boolean = false, positionData?: PositionVO.PositionInfoVO | null) {
|
||||
isEdit.value = editMode;
|
||||
currentPositionData.value = positionData || null;
|
||||
|
||||
resetForm();
|
||||
|
||||
if (editMode && positionData) {
|
||||
formData.value = {
|
||||
name: positionData.name,
|
||||
code: positionData.code || '',
|
||||
departmentId: positionData.departmentId,
|
||||
remark: positionData.remark || '',
|
||||
status: positionData.status,
|
||||
};
|
||||
}
|
||||
|
||||
visible.value = true;
|
||||
}
|
||||
|
||||
// 暴露方法给父组件
|
||||
defineExpose({
|
||||
open,
|
||||
});
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<Modal
|
||||
:open="visible"
|
||||
:title="modalTitle"
|
||||
width="500px"
|
||||
:confirm-loading="loading"
|
||||
@ok="handleSubmit"
|
||||
@cancel="handleCancel"
|
||||
>
|
||||
<Form
|
||||
ref="formRef"
|
||||
:model="formData"
|
||||
:label-col="{ span: 6 }"
|
||||
:wrapper-col="{ span: 18 }"
|
||||
>
|
||||
<Form.Item label="岗位名称" name="name" :rules="[{ required: true, message: '请输入岗位名称' }]">
|
||||
<Input v-model:value="formData.name" placeholder="请输入岗位名称" />
|
||||
</Form.Item>
|
||||
|
||||
<Form.Item label="岗位编码" name="code">
|
||||
<Input v-model:value="formData.code" placeholder="请输入岗位编码" />
|
||||
</Form.Item>
|
||||
|
||||
<Form.Item label="所属部门" name="departmentId">
|
||||
<Select
|
||||
v-model:value="formData.departmentId"
|
||||
placeholder="请选择所属部门"
|
||||
:options="departmentOptions"
|
||||
allow-clear
|
||||
/>
|
||||
</Form.Item>
|
||||
|
||||
<Form.Item label="描述" name="remark">
|
||||
<Input.TextArea v-model:value="formData.remark" placeholder="请输入岗位描述" :rows="3" />
|
||||
</Form.Item>
|
||||
|
||||
<Form.Item label="状态" name="status">
|
||||
<Select
|
||||
v-model:value="formData.status"
|
||||
placeholder="请选择状态"
|
||||
:options="statusOptions"
|
||||
/>
|
||||
</Form.Item>
|
||||
</Form>
|
||||
</Modal>
|
||||
</template>
|
||||
+15
-102
@@ -2,17 +2,16 @@
|
||||
import { h, ref } from 'vue';
|
||||
|
||||
import { Page } from '@vben/common-ui';
|
||||
import { Button, Form, Input, Modal, Select, Table, message } from 'ant-design-vue';
|
||||
import { Button, Input, Select, Table, message } from 'ant-design-vue';
|
||||
|
||||
import {
|
||||
createPositionApi,
|
||||
deletePositionApi,
|
||||
getPositionPageApi,
|
||||
updatePositionApi,
|
||||
getPositionPageApi,
|
||||
type PositionVO,
|
||||
type PositionDTO
|
||||
} from '#/api/org';
|
||||
import type { PageQueryDTO } from '#/types/common';
|
||||
import PositionModal from './components/PositionModal.vue';
|
||||
|
||||
defineOptions({ name: 'PositionManagement' });
|
||||
|
||||
@@ -22,11 +21,7 @@ const positionList = ref<PositionVO.PositionInfoVO[]>([]);
|
||||
const total = ref(0);
|
||||
const current = ref(1);
|
||||
const pageSize = ref(10);
|
||||
const modalVisible = ref(false);
|
||||
const modalTitle = ref('');
|
||||
const isEdit = ref(false);
|
||||
const currentPositionId = ref<number | null>(null);
|
||||
const formRef = ref();
|
||||
const positionModalRef = ref();
|
||||
|
||||
// 搜索表单
|
||||
const searchForm = ref({
|
||||
@@ -34,15 +29,6 @@ const searchForm = ref({
|
||||
status: undefined as number | undefined,
|
||||
});
|
||||
|
||||
// 表单数据
|
||||
const formData = ref<PositionDTO.CreatePositionDTO>({
|
||||
name: '',
|
||||
code: '',
|
||||
departmentId: undefined,
|
||||
remark: '',
|
||||
status: 1,
|
||||
});
|
||||
|
||||
// 状态选项
|
||||
const statusOptions = [
|
||||
{ label: '禁用', value: 0 },
|
||||
@@ -174,32 +160,12 @@ function handleTableChange(pagination: any) {
|
||||
|
||||
// 处理添加岗位
|
||||
function handleAdd() {
|
||||
modalTitle.value = '添加岗位';
|
||||
isEdit.value = false;
|
||||
currentPositionId.value = null;
|
||||
formData.value = {
|
||||
name: '',
|
||||
code: '',
|
||||
departmentId: undefined,
|
||||
remark: '',
|
||||
status: 1,
|
||||
};
|
||||
modalVisible.value = true;
|
||||
positionModalRef.value?.open(false);
|
||||
}
|
||||
|
||||
// 处理编辑岗位
|
||||
function handleEdit(record: PositionVO.PositionInfoVO) {
|
||||
modalTitle.value = '编辑岗位';
|
||||
isEdit.value = true;
|
||||
currentPositionId.value = record.id;
|
||||
formData.value = {
|
||||
name: record.name,
|
||||
code: record.code || '',
|
||||
departmentId: record.departmentId,
|
||||
remark: record.remark || '',
|
||||
status: record.status,
|
||||
};
|
||||
modalVisible.value = true;
|
||||
positionModalRef.value?.open(true, record);
|
||||
}
|
||||
|
||||
// 处理删除岗位
|
||||
@@ -214,25 +180,9 @@ async function handleDelete(record: PositionVO.PositionInfoVO) {
|
||||
}
|
||||
}
|
||||
|
||||
// 处理表单提交
|
||||
async function handleSubmit() {
|
||||
try {
|
||||
await formRef.value?.validate();
|
||||
|
||||
if (isEdit.value && currentPositionId.value) {
|
||||
await updatePositionApi(currentPositionId.value, formData.value);
|
||||
message.success('更新成功');
|
||||
} else {
|
||||
await createPositionApi(formData.value);
|
||||
message.success('创建成功');
|
||||
}
|
||||
|
||||
modalVisible.value = false;
|
||||
await fetchPositionList();
|
||||
} catch (error) {
|
||||
console.error('保存岗位失败:', error);
|
||||
message.error('保存岗位失败');
|
||||
}
|
||||
// 处理模态框成功回调
|
||||
async function handleModalSuccess() {
|
||||
await fetchPositionList();
|
||||
}
|
||||
|
||||
// 初始化
|
||||
@@ -299,48 +249,11 @@ fetchPositionList();
|
||||
@change="handleTableChange"
|
||||
/>
|
||||
|
||||
<!-- 添加/编辑模态框 -->
|
||||
<Modal
|
||||
v-model:open="modalVisible"
|
||||
:title="modalTitle"
|
||||
width="500px"
|
||||
@ok="handleSubmit"
|
||||
>
|
||||
<Form
|
||||
ref="formRef"
|
||||
:model="formData"
|
||||
:label-col="{ span: 6 }"
|
||||
:wrapper-col="{ span: 18 }"
|
||||
>
|
||||
<Form.Item label="岗位名称" name="name" :rules="[{ required: true, message: '请输入岗位名称' }]">
|
||||
<Input v-model:value="formData.name" placeholder="请输入岗位名称" />
|
||||
</Form.Item>
|
||||
|
||||
<Form.Item label="岗位编码" name="code">
|
||||
<Input v-model:value="formData.code" placeholder="请输入岗位编码" />
|
||||
</Form.Item>
|
||||
|
||||
<Form.Item label="所属部门" name="departmentId">
|
||||
<Select
|
||||
v-model:value="formData.departmentId"
|
||||
placeholder="请选择所属部门"
|
||||
:options="departmentOptions"
|
||||
allow-clear
|
||||
/>
|
||||
</Form.Item>
|
||||
|
||||
<Form.Item label="描述" name="remark">
|
||||
<Input.TextArea v-model:value="formData.remark" placeholder="请输入岗位描述" :rows="3" />
|
||||
</Form.Item>
|
||||
|
||||
<Form.Item label="状态" name="status">
|
||||
<Select
|
||||
v-model:value="formData.status"
|
||||
placeholder="请选择状态"
|
||||
:options="statusOptions"
|
||||
/>
|
||||
</Form.Item>
|
||||
</Form>
|
||||
</Modal>
|
||||
<!-- 岗位模态框组件 -->
|
||||
<PositionModal
|
||||
ref="positionModalRef"
|
||||
:department-options="departmentOptions"
|
||||
@success="handleModalSuccess"
|
||||
/>
|
||||
</Page>
|
||||
</template>
|
||||
|
||||
+248
@@ -0,0 +1,248 @@
|
||||
<template>
|
||||
<Modal
|
||||
:open="visible"
|
||||
title="创建缺失的实体"
|
||||
width="800px"
|
||||
:confirm-loading="loading"
|
||||
@ok="handleSubmit"
|
||||
@cancel="handleCancel"
|
||||
>
|
||||
<div class="space-y-6">
|
||||
<!-- 店铺创建 -->
|
||||
<div v-if="entitiesToCreate.shops.length > 0" class="border border-gray-200 rounded-lg p-4">
|
||||
<h3 class="text-lg font-medium text-gray-900 mb-4">需要创建的店铺 ({{ entitiesToCreate.shops.length }} 个)</h3>
|
||||
<div class="space-y-3">
|
||||
<div
|
||||
v-for="(shopName, index) in entitiesToCreate.shops"
|
||||
:key="`shop-${index}`"
|
||||
class="flex items-center gap-4 p-3 bg-gray-50 rounded"
|
||||
>
|
||||
<span class="font-medium">{{ shopName }}</span>
|
||||
<Button
|
||||
type="primary"
|
||||
size="small"
|
||||
@click="openShopModal(shopName)"
|
||||
>
|
||||
创建店铺
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 商品创建 -->
|
||||
<div v-if="entitiesToCreate.products.length > 0" class="border border-gray-200 rounded-lg p-4">
|
||||
<h3 class="text-lg font-medium text-gray-900 mb-4">需要创建的商品 ({{ entitiesToCreate.products.length }} 个)</h3>
|
||||
<div class="space-y-3">
|
||||
<div
|
||||
v-for="(productName, index) in entitiesToCreate.products"
|
||||
:key="`product-${index}`"
|
||||
class="flex items-center gap-4 p-3 bg-gray-50 rounded"
|
||||
>
|
||||
<span class="font-medium">{{ productName }}</span>
|
||||
<Button
|
||||
type="primary"
|
||||
size="small"
|
||||
@click="openProductModal(productName)"
|
||||
>
|
||||
创建商品
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 商品规格创建 -->
|
||||
<div v-if="entitiesToCreate.specs.length > 0" class="border border-gray-200 rounded-lg p-4">
|
||||
<h3 class="text-lg font-medium text-gray-900 mb-4">需要创建的商品规格 ({{ entitiesToCreate.specs.length }} 个)</h3>
|
||||
<div class="space-y-3">
|
||||
<div
|
||||
v-for="(spec, index) in entitiesToCreate.specs"
|
||||
:key="`spec-${index}`"
|
||||
class="flex items-center gap-4 p-3 bg-gray-50 rounded"
|
||||
>
|
||||
<span class="font-medium">{{ spec.productName }} - {{ spec.specCode }}</span>
|
||||
<Button
|
||||
type="primary"
|
||||
size="small"
|
||||
@click="openProductSpecModal(spec)"
|
||||
>
|
||||
创建规格
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 提示信息 -->
|
||||
<div class="bg-blue-50 border border-blue-200 rounded-lg p-4">
|
||||
<div class="flex items-start">
|
||||
<div class="flex-shrink-0">
|
||||
<svg class="h-5 w-5 text-blue-400" viewBox="0 0 20 20" fill="currentColor">
|
||||
<path fill-rule="evenodd" d="M18 10a8 8 0 11-16 0 8 8 0 0116 0zm-7-4a1 1 0 11-2 0 1 1 0 012 0zM9 9a1 1 0 000 2v3a1 1 0 001 1h1a1 1 0 100-2v-3a1 1 0 00-1-1H9z" clip-rule="evenodd" />
|
||||
</svg>
|
||||
</div>
|
||||
<div class="ml-3">
|
||||
<h3 class="text-sm font-medium text-blue-800">提示</h3>
|
||||
<div class="mt-2 text-sm text-blue-700">
|
||||
<p>请先创建所有缺失的实体,然后再进行数据导入。创建完成后,系统将自动重新匹配数据。</p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</Modal>
|
||||
|
||||
<!-- 店铺创建模态框 -->
|
||||
<ShopFormModal
|
||||
ref="shopFormModalRef"
|
||||
@success="handleShopCreated"
|
||||
/>
|
||||
|
||||
<!-- 商品创建模态框 -->
|
||||
<ProductFormModal
|
||||
ref="productFormModalRef"
|
||||
@success="handleProductCreated"
|
||||
/>
|
||||
|
||||
<!-- 商品规格创建模态框 -->
|
||||
<ProductSpecModal
|
||||
ref="productSpecModalRef"
|
||||
@success="handleProductSpecCreated"
|
||||
/>
|
||||
</template>
|
||||
|
||||
<script lang="ts" setup>
|
||||
import { ref } from 'vue';
|
||||
import { Modal, Button, message } from 'ant-design-vue';
|
||||
import ShopFormModal from '../../shop-management/components/ShopFormModal.vue';
|
||||
import ProductFormModal from '../../product-management/components/ProductFormModal.vue';
|
||||
import ProductSpecModal from '../../product-management/components/ProductSpecModal.vue';
|
||||
|
||||
interface Props {
|
||||
visible: boolean;
|
||||
entitiesToCreate: {
|
||||
shops: string[];
|
||||
products: string[];
|
||||
specs: { productName: string; specCode: string; specName?: string }[];
|
||||
};
|
||||
csvData?: any[]; // 传递CSV数据用于获取更多信息
|
||||
}
|
||||
|
||||
interface Emits {
|
||||
(e: 'success'): void;
|
||||
(e: 'cancel'): void;
|
||||
}
|
||||
|
||||
const props = defineProps<Props>();
|
||||
const emit = defineEmits<Emits>();
|
||||
|
||||
// 响应式数据
|
||||
const loading = ref(false);
|
||||
const shopFormModalRef = ref();
|
||||
const productFormModalRef = ref();
|
||||
const productSpecModalRef = ref();
|
||||
|
||||
// 当前正在创建的实体
|
||||
const currentCreatingShop = ref<string>('');
|
||||
const currentCreatingProduct = ref<string>('');
|
||||
const currentCreatingSpec = ref<{ productName: string; specCode: string } | null>(null);
|
||||
|
||||
// 处理提交
|
||||
function handleSubmit() {
|
||||
// 检查是否还有未创建的实体
|
||||
const { entitiesToCreate } = defineProps<Props>();
|
||||
|
||||
if (entitiesToCreate.shops.length > 0 ||
|
||||
entitiesToCreate.products.length > 0 ||
|
||||
entitiesToCreate.specs.length > 0) {
|
||||
message.warning('请先创建所有缺失的实体');
|
||||
return;
|
||||
}
|
||||
|
||||
emit('success');
|
||||
}
|
||||
|
||||
// 处理取消
|
||||
function handleCancel() {
|
||||
emit('cancel');
|
||||
}
|
||||
|
||||
// 打开店铺创建模态框
|
||||
function openShopModal(shopName: string) {
|
||||
currentCreatingShop.value = shopName;
|
||||
// 从CSV数据中获取更多店铺信息
|
||||
const shopData = {
|
||||
name: shopName,
|
||||
description: `从CSV文件导入的店铺:${shopName}`,
|
||||
// 可以根据需要添加更多默认值
|
||||
};
|
||||
shopFormModalRef.value?.open(false, shopData);
|
||||
}
|
||||
|
||||
// 打开商品创建模态框
|
||||
function openProductModal(productName: string) {
|
||||
currentCreatingProduct.value = productName;
|
||||
|
||||
// 从CSV数据中获取该商品的所有规格信息
|
||||
const csvSpecs = props.csvData?.filter(row => row.productName === productName) || [];
|
||||
|
||||
// 对规格进行去重处理,使用Map来去重
|
||||
const specMap = new Map<string, { specCode: string; specName: string }>();
|
||||
csvSpecs.forEach(row => {
|
||||
const specCode = row.specCode;
|
||||
const specName = row.productSpecName;
|
||||
if (specCode && !specMap.has(specCode)) {
|
||||
specMap.set(specCode, {
|
||||
specCode: specCode,
|
||||
specName: specName || specCode // 如果没有规格名称,使用规格编码
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
// 转换为数组
|
||||
const uniqueSpecs = Array.from(specMap.values());
|
||||
|
||||
// 构建商品数据,包含规格信息
|
||||
const productData = {
|
||||
name: productName,
|
||||
description: `从CSV文件导入的商品:${productName}`,
|
||||
// 添加规格信息
|
||||
specs: uniqueSpecs
|
||||
};
|
||||
|
||||
console.log('商品数据:', productData);
|
||||
productFormModalRef.value?.open(false, productData);
|
||||
}
|
||||
|
||||
// 打开商品规格创建模态框
|
||||
function openProductSpecModal(spec: { productName: string; specCode: string; specName?: string }) {
|
||||
currentCreatingSpec.value = spec;
|
||||
// 由于ProductSpecModal需要完整的商品对象,这里需要先创建商品
|
||||
// 或者修改ProductSpecModal以支持部分数据
|
||||
message.info('请先创建对应的商品,然后再创建规格');
|
||||
}
|
||||
|
||||
// 处理店铺创建成功
|
||||
function handleShopCreated() {
|
||||
message.success(`店铺 "${currentCreatingShop.value}" 创建成功`);
|
||||
currentCreatingShop.value = '';
|
||||
// 这里可以触发重新匹配
|
||||
emit('success');
|
||||
}
|
||||
|
||||
// 处理商品创建成功
|
||||
function handleProductCreated() {
|
||||
message.success(`商品 "${currentCreatingProduct.value}" 创建成功`);
|
||||
currentCreatingProduct.value = '';
|
||||
// 这里可以触发重新匹配
|
||||
emit('success');
|
||||
}
|
||||
|
||||
// 处理商品规格创建成功
|
||||
function handleProductSpecCreated() {
|
||||
if (currentCreatingSpec.value) {
|
||||
message.success(`商品规格 "${currentCreatingSpec.value.productName} - ${currentCreatingSpec.value.specCode}" 创建成功`);
|
||||
currentCreatingSpec.value = null;
|
||||
// 这里可以触发重新匹配
|
||||
emit('success');
|
||||
}
|
||||
}
|
||||
</script>
|
||||
@@ -43,6 +43,50 @@
|
||||
<p class="text-sm text-green-600 mt-1">{{ uploadedFile.name }} ({{ formatFileSize(uploadedFile.size) }})</p>
|
||||
</div>
|
||||
|
||||
<!-- 实体匹配结果 -->
|
||||
<div v-if="matchingResult" class="space-y-4">
|
||||
<VbenCard title="实体匹配结果">
|
||||
<div class="grid grid-cols-1 md:grid-cols-3 gap-4 mb-4">
|
||||
<div class="bg-green-50 border border-green-200 rounded-lg p-4">
|
||||
<div class="text-green-800 font-medium">匹配成功</div>
|
||||
<div class="text-2xl font-bold text-green-600">{{ matchingResult.matchedOrders.length }}</div>
|
||||
<div class="text-sm text-green-600">条订单数据</div>
|
||||
</div>
|
||||
<div class="bg-red-50 border border-red-200 rounded-lg p-4">
|
||||
<div class="text-red-800 font-medium">匹配失败</div>
|
||||
<div class="text-2xl font-bold text-red-600">{{ matchingResult.failedRecords.length }}</div>
|
||||
<div class="text-sm text-red-600">条记录</div>
|
||||
</div>
|
||||
<div class="bg-blue-50 border border-blue-200 rounded-lg p-4">
|
||||
<div class="text-blue-800 font-medium">需要创建</div>
|
||||
<div class="text-2xl font-bold text-blue-600">
|
||||
{{ matchingResult.entitiesToCreate.shops.length + matchingResult.entitiesToCreate.products.length + matchingResult.entitiesToCreate.specs.length }}
|
||||
</div>
|
||||
<div class="text-sm text-blue-600">个实体</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="flex items-center space-x-2">
|
||||
<VbenButton
|
||||
@click="performImport"
|
||||
type="primary"
|
||||
:loading="importLoading"
|
||||
:disabled="matchingResult.matchedOrders.length === 0"
|
||||
>
|
||||
开始导入 ({{ matchingResult.matchedOrders.length }})
|
||||
</VbenButton>
|
||||
<VbenButton @click="performEntityMatching" size="small">
|
||||
重新匹配
|
||||
</VbenButton>
|
||||
</div>
|
||||
|
||||
<!-- 导入进度 -->
|
||||
<div v-if="importLoading" class="mt-4">
|
||||
<Progress :percent="importProgress" />
|
||||
</div>
|
||||
</VbenCard>
|
||||
</div>
|
||||
|
||||
<!-- 数据表格 -->
|
||||
<div v-if="csvData.length > 0" class="space-y-4">
|
||||
<div class="flex items-center justify-between">
|
||||
@@ -69,7 +113,16 @@
|
||||
<Grid
|
||||
@checkbox-change="handleCheckboxChange"
|
||||
@checkbox-all="handleCheckboxAll"
|
||||
/>
|
||||
>
|
||||
<template #action="{ row }">
|
||||
<VbenButton size="small" @click="handleEditRow(row)">
|
||||
编辑
|
||||
</VbenButton>
|
||||
<VbenButton size="small" type="primary" @click="handleMatchRow(row)">
|
||||
匹配
|
||||
</VbenButton>
|
||||
</template>
|
||||
</Grid>
|
||||
</div>
|
||||
</div>
|
||||
</VbenCard>
|
||||
@@ -88,14 +141,26 @@
|
||||
@cancel="showFieldMapping = false"
|
||||
/>
|
||||
</VbenModal>
|
||||
|
||||
<!-- 实体创建模态框 -->
|
||||
<EntityCreationModal
|
||||
:visible="showEntityCreationModal"
|
||||
:entities-to-create="matchingResult?.entitiesToCreate || { shops: [], products: [], specs: [] }"
|
||||
:csv-data="csvData"
|
||||
@success="handleEntityCreationSuccess"
|
||||
@cancel="handleEntityCreationCancel"
|
||||
/>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { ref } from 'vue'
|
||||
import { message } from 'ant-design-vue'
|
||||
import { message, Progress } from 'ant-design-vue'
|
||||
import FieldMapping from '#/views/ecommerce/data-import/components/FieldMapping.vue'
|
||||
import { parseCSV, extractShopNameFromFilename, validateCSVData, cleanAndFormatData } from '#/utils/csv-utils'
|
||||
import EntityCreationModal from './components/EntityCreationModal.vue'
|
||||
import { EntityMatchingService, type EntityMatchingResult } from './services/EntityMatchingService'
|
||||
import { batchImportOrdersApi } from '#/api/ecommerce'
|
||||
import { parseOrderCSV, extractShopNameFromFilename, convertToCSVOrderData } from './utils/csvParser'
|
||||
import { useVbenVxeGrid } from '#/adapter/vxe-table'
|
||||
import type { VxeGridProps } from '#/adapter/vxe-table'
|
||||
|
||||
@@ -108,6 +173,13 @@ const isDragOver = ref(false)
|
||||
const fileInputRef = ref<HTMLInputElement>()
|
||||
const selectedRows = ref<any[]>([])
|
||||
|
||||
// 实体匹配相关
|
||||
const entityMatchingService = new EntityMatchingService()
|
||||
const matchingResult = ref<EntityMatchingResult | null>(null)
|
||||
const showEntityCreationModal = ref(false)
|
||||
const importLoading = ref(false)
|
||||
const importProgress = ref(0)
|
||||
|
||||
// VxeTable 配置
|
||||
const gridOptions: VxeGridProps<any> = {
|
||||
columns: [
|
||||
@@ -122,6 +194,62 @@ const gridOptions: VxeGridProps<any> = {
|
||||
fixed: 'left',
|
||||
title: '序号',
|
||||
},
|
||||
{
|
||||
field: 'productName',
|
||||
title: '商品名称',
|
||||
width: 200,
|
||||
showOverflow: 'tooltip'
|
||||
},
|
||||
{
|
||||
field: 'orderNumber',
|
||||
title: '订单编号',
|
||||
width: 180,
|
||||
showOverflow: 'tooltip'
|
||||
},
|
||||
{
|
||||
field: 'orderStatusName',
|
||||
title: '订单状态',
|
||||
width: 120
|
||||
},
|
||||
{
|
||||
field: 'quantity',
|
||||
title: '数量',
|
||||
width: 80
|
||||
},
|
||||
{
|
||||
field: 'productId',
|
||||
title: '商品ID',
|
||||
width: 120,
|
||||
showOverflow: 'tooltip'
|
||||
},
|
||||
{
|
||||
field: 'productSpecName',
|
||||
title: '商品规格',
|
||||
width: 200,
|
||||
showOverflow: 'tooltip'
|
||||
},
|
||||
{
|
||||
field: 'shopName',
|
||||
title: '店铺名称',
|
||||
width: 150,
|
||||
showOverflow: 'tooltip'
|
||||
},
|
||||
{
|
||||
field: 'afterSaleStatus',
|
||||
title: '售后状态',
|
||||
width: 120
|
||||
},
|
||||
{
|
||||
field: 'shopActualAmount',
|
||||
title: '实收金额',
|
||||
width: 100
|
||||
},
|
||||
{
|
||||
title: '操作',
|
||||
width: 200,
|
||||
fixed: 'right',
|
||||
slots: { default: 'action' }
|
||||
}
|
||||
],
|
||||
data: [],
|
||||
height: 500,
|
||||
@@ -161,13 +289,68 @@ const updateTableColumns = () => {
|
||||
fixed: 'left',
|
||||
title: '序号',
|
||||
},
|
||||
...csvHeaders.value.map((header) => ({
|
||||
field: header,
|
||||
title: header,
|
||||
{
|
||||
field: 'productName',
|
||||
title: '商品名称',
|
||||
width: 200,
|
||||
showOverflow: 'tooltip',
|
||||
sortable: true,
|
||||
}))
|
||||
showOverflow: 'tooltip'
|
||||
},
|
||||
{
|
||||
field: 'orderNumber',
|
||||
title: '订单编号',
|
||||
width: 180,
|
||||
showOverflow: 'tooltip'
|
||||
},
|
||||
{
|
||||
field: 'orderStatusName',
|
||||
title: '订单状态',
|
||||
width: 120
|
||||
},
|
||||
{
|
||||
field: 'quantity',
|
||||
title: '数量',
|
||||
width: 80
|
||||
},
|
||||
{
|
||||
field: 'productId',
|
||||
title: '商品ID',
|
||||
width: 120,
|
||||
showOverflow: 'tooltip'
|
||||
},
|
||||
{
|
||||
field: 'productSpecName',
|
||||
title: '商品规格',
|
||||
width: 200,
|
||||
showOverflow: 'tooltip'
|
||||
},
|
||||
{
|
||||
field: 'specCode',
|
||||
title: '规格编码',
|
||||
width: 120,
|
||||
showOverflow: 'tooltip'
|
||||
},
|
||||
{
|
||||
field: 'shopName',
|
||||
title: '店铺名称',
|
||||
width: 150,
|
||||
showOverflow: 'tooltip'
|
||||
},
|
||||
{
|
||||
field: 'afterSaleStatus',
|
||||
title: '售后状态',
|
||||
width: 120
|
||||
},
|
||||
{
|
||||
field: 'shopActualAmount',
|
||||
title: '实收金额',
|
||||
width: 100
|
||||
},
|
||||
{
|
||||
title: '操作',
|
||||
width: 200,
|
||||
fixed: 'right',
|
||||
slots: { default: 'action' }
|
||||
}
|
||||
]
|
||||
|
||||
gridApi.setGridOptions({
|
||||
@@ -200,28 +383,21 @@ const processFile = async (file: File) => {
|
||||
uploadedFile.value = file
|
||||
|
||||
try {
|
||||
// 使用工具函数解析CSV
|
||||
const result = await parseCSV(file)
|
||||
// 使用新的CSV解析器
|
||||
const result = await parseOrderCSV(file)
|
||||
|
||||
if (result.errors.length > 0) {
|
||||
message.error('CSV文件解析失败: ' + result.errors[0].message)
|
||||
message.error('CSV文件解析失败: ' + result.errors.join(', '))
|
||||
return
|
||||
}
|
||||
|
||||
// 验证数据格式
|
||||
const validation = validateCSVData(result.data)
|
||||
if (!validation.isValid) {
|
||||
message.error('数据格式验证失败: ' + validation.errors.join(', '))
|
||||
return
|
||||
}
|
||||
|
||||
// 清洗和格式化数据,并添加唯一ID
|
||||
const cleanedData = cleanAndFormatData(result.data).map((item, index) => ({
|
||||
// 添加唯一ID并转换为表格数据
|
||||
const tableData = result.data.map((item, index) => ({
|
||||
...item,
|
||||
id: `row_${index}`,
|
||||
}))
|
||||
|
||||
csvData.value = cleanedData
|
||||
csvData.value = tableData
|
||||
csvHeaders.value = result.headers
|
||||
selectedRows.value = []
|
||||
|
||||
@@ -230,7 +406,10 @@ const processFile = async (file: File) => {
|
||||
|
||||
// 从文件名提取店铺名称
|
||||
const shopName = extractShopNameFromFilename(file.name)
|
||||
message.success(`成功解析 ${cleanedData.length} 条数据,店铺:${shopName}`)
|
||||
message.success(`成功解析 ${tableData.length} 条数据,店铺:${shopName}`)
|
||||
|
||||
// 自动进行实体匹配
|
||||
await performEntityMatching()
|
||||
|
||||
} catch (error: any) {
|
||||
message.error('文件读取失败: ' + error.message)
|
||||
@@ -314,6 +493,111 @@ const handleFieldMapping = (mappedData: any) => {
|
||||
message.success('字段映射配置完成')
|
||||
}
|
||||
|
||||
// 执行实体匹配
|
||||
const performEntityMatching = async () => {
|
||||
if (csvData.value.length === 0) return
|
||||
|
||||
try {
|
||||
// 转换为EntityMatchingService需要的格式
|
||||
const shopName = uploadedFile.value ? extractShopNameFromFilename(uploadedFile.value.name) : '未知店铺'
|
||||
const orderData = convertToCSVOrderData(csvData.value as any[], shopName)
|
||||
|
||||
// 执行实体匹配
|
||||
const result = await entityMatchingService.matchEntities(orderData)
|
||||
matchingResult.value = result
|
||||
|
||||
// 显示匹配结果
|
||||
if (result.matchedOrders.length > 0) {
|
||||
message.success(`成功匹配 ${result.matchedOrders.length} 条订单数据`)
|
||||
}
|
||||
|
||||
if (result.failedRecords.length > 0) {
|
||||
message.warning(`${result.failedRecords.length} 条记录需要手动处理,可以在表格中操作`)
|
||||
}
|
||||
|
||||
// 如果有需要创建的实体,显示创建模态框
|
||||
if (result.entitiesToCreate.shops.length > 0 ||
|
||||
result.entitiesToCreate.products.length > 0 ||
|
||||
result.entitiesToCreate.specs.length > 0) {
|
||||
showEntityCreationModal.value = true
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
console.error('实体匹配失败:', error)
|
||||
message.error('实体匹配失败: ' + (error instanceof Error ? error.message : '未知错误'))
|
||||
}
|
||||
}
|
||||
|
||||
// 执行数据导入
|
||||
const performImport = async () => {
|
||||
if (!matchingResult.value || matchingResult.value.matchedOrders.length === 0) {
|
||||
message.warning('没有可导入的数据')
|
||||
return
|
||||
}
|
||||
|
||||
try {
|
||||
importLoading.value = true
|
||||
importProgress.value = 0
|
||||
|
||||
// 模拟进度更新
|
||||
const progressInterval = setInterval(() => {
|
||||
if (importProgress.value < 90) {
|
||||
importProgress.value += 10
|
||||
}
|
||||
}, 200)
|
||||
|
||||
// 调用批量导入API
|
||||
const result = await batchImportOrdersApi({
|
||||
orders: matchingResult.value.matchedOrders
|
||||
})
|
||||
|
||||
clearInterval(progressInterval)
|
||||
importProgress.value = 100
|
||||
|
||||
message.success(`导入完成!成功导入 ${result.successCount} 条,失败 ${result.failureCount} 条`)
|
||||
|
||||
// 显示失败详情
|
||||
if (result.failureDetails.length > 0) {
|
||||
console.warn('导入失败详情:', result.failureDetails)
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
console.error('数据导入失败:', error)
|
||||
message.error('数据导入失败: ' + (error instanceof Error ? error.message : '未知错误'))
|
||||
} finally {
|
||||
importLoading.value = false
|
||||
importProgress.value = 0
|
||||
}
|
||||
}
|
||||
|
||||
// 处理实体创建成功
|
||||
const handleEntityCreationSuccess = async () => {
|
||||
showEntityCreationModal.value = false
|
||||
message.success('实体创建成功,正在重新匹配...')
|
||||
|
||||
// 重新执行实体匹配
|
||||
await performEntityMatching()
|
||||
}
|
||||
|
||||
// 处理实体创建取消
|
||||
const handleEntityCreationCancel = () => {
|
||||
showEntityCreationModal.value = false
|
||||
}
|
||||
|
||||
// 处理行编辑
|
||||
const handleEditRow = (row: any) => {
|
||||
// 可以打开编辑模态框或内联编辑
|
||||
console.log('编辑行:', row)
|
||||
message.info('编辑功能待实现')
|
||||
}
|
||||
|
||||
// 处理行匹配
|
||||
const handleMatchRow = (row: any) => {
|
||||
// 可以打开匹配模态框,让用户手动匹配实体
|
||||
console.log('匹配行:', row)
|
||||
message.info('匹配功能待实现')
|
||||
}
|
||||
|
||||
// 格式化文件大小
|
||||
const formatFileSize = (bytes: number) => {
|
||||
if (bytes === 0) return '0 Bytes'
|
||||
|
||||
+302
@@ -0,0 +1,302 @@
|
||||
/**
|
||||
* 实体匹配服务
|
||||
* 用于匹配CSV数据中的实体(店铺、商品、规格、订单状态)到数据库中的ID
|
||||
*/
|
||||
|
||||
import {
|
||||
getShopsByNamesApi,
|
||||
getProductsByNamesApi,
|
||||
getProductSpecsByProductAndSpecsApi,
|
||||
type ShopVO,
|
||||
type ProductVO,
|
||||
type OrderDTO
|
||||
} from '#/api/ecommerce';
|
||||
|
||||
export interface EntityMatchingResult {
|
||||
/** 匹配成功的订单数据 */
|
||||
matchedOrders: OrderDTO.CreateOrderDTO[];
|
||||
/** 匹配失败的记录 */
|
||||
failedRecords: FailedRecord[];
|
||||
/** 需要创建的实体 */
|
||||
entitiesToCreate: {
|
||||
shops: string[];
|
||||
products: string[];
|
||||
specs: { productName: string; specCode: string }[];
|
||||
};
|
||||
}
|
||||
|
||||
export interface FailedRecord {
|
||||
/** 行号 */
|
||||
rowIndex: number;
|
||||
/** 原始数据 */
|
||||
originalData: any;
|
||||
/** 失败原因 */
|
||||
reason: string;
|
||||
}
|
||||
|
||||
export interface CSVOrderData {
|
||||
/** 店铺名称 */
|
||||
shopName: string;
|
||||
/** 商品名称 */
|
||||
productName: string;
|
||||
/** 商品规格编码 */
|
||||
specCode: string;
|
||||
/** 订单状态名称 */
|
||||
orderStatusName: string;
|
||||
/** 商品数量 */
|
||||
quantity: number;
|
||||
/** 订单编号 */
|
||||
orderNumber: string;
|
||||
/** 商品规格名称 */
|
||||
productSpecName: string;
|
||||
/** 店铺实际收入 */
|
||||
shopActualAmount: number;
|
||||
}
|
||||
|
||||
export class EntityMatchingService {
|
||||
private shopCache: Map<string, ShopVO.ShopInfoVO> = new Map();
|
||||
private productCache: Map<string, ProductVO.ProductInfoVO> = new Map();
|
||||
private specCache: Map<string, ProductVO.ProductSpecInfoVO> = new Map();
|
||||
private orderStatusMap: Map<string, number> = new Map();
|
||||
|
||||
constructor() {
|
||||
this.initializeOrderStatusMap();
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化订单状态映射
|
||||
*/
|
||||
private initializeOrderStatusMap() {
|
||||
// 根据CSV文件中的订单状态,映射到对应的ID
|
||||
// 这里需要根据实际的订单状态配置来设置
|
||||
this.orderStatusMap.set('已发货,待收货', 1);
|
||||
this.orderStatusMap.set('未发货,退款成功', 2);
|
||||
this.orderStatusMap.set('已取消', 3);
|
||||
this.orderStatusMap.set('退款成功', 2);
|
||||
// 可以根据需要添加更多状态映射
|
||||
}
|
||||
|
||||
/**
|
||||
* 匹配实体并生成订单数据
|
||||
*/
|
||||
async matchEntities(csvData: CSVOrderData[]): Promise<EntityMatchingResult> {
|
||||
// 清空缓存
|
||||
this.clearCache();
|
||||
|
||||
// 提取所有需要查询的实体名称
|
||||
const shopNames = [...new Set(csvData.map(row => row.shopName))];
|
||||
const productNames = [...new Set(csvData.map(row => row.productName))];
|
||||
|
||||
// 对规格查询进行去重处理
|
||||
const specQueriesMap = new Map<string, { productName: string; specCode: string }>();
|
||||
csvData.forEach(row => {
|
||||
const key = `${row.productName}_${row.specCode}`;
|
||||
if (!specQueriesMap.has(key)) {
|
||||
specQueriesMap.set(key, { productName: row.productName, specCode: row.specCode });
|
||||
}
|
||||
});
|
||||
const specQueries = Array.from(specQueriesMap.values());
|
||||
|
||||
// 批量查询所有实体
|
||||
await this.loadEntitiesBatch(shopNames, productNames, specQueries);
|
||||
|
||||
const matchedOrders: OrderDTO.CreateOrderDTO[] = [];
|
||||
const failedRecords: FailedRecord[] = [];
|
||||
const entitiesToCreate = {
|
||||
shops: new Set<string>(),
|
||||
products: new Set<string>(),
|
||||
specs: new Set<{ productName: string; specCode: string }>()
|
||||
};
|
||||
|
||||
for (let i = 0; i < csvData.length; i++) {
|
||||
const row = csvData[i];
|
||||
|
||||
// 检查行数据是否存在
|
||||
if (!row) {
|
||||
failedRecords.push({
|
||||
rowIndex: i + 1,
|
||||
originalData: null,
|
||||
reason: '行数据为空'
|
||||
});
|
||||
continue;
|
||||
}
|
||||
|
||||
try {
|
||||
// 匹配店铺
|
||||
const shop = this.shopCache.get(row.shopName);
|
||||
if (!shop) {
|
||||
entitiesToCreate.shops.add(row.shopName);
|
||||
failedRecords.push({
|
||||
rowIndex: i + 1,
|
||||
originalData: row,
|
||||
reason: `店铺 "${row.shopName}" 不存在`
|
||||
});
|
||||
continue;
|
||||
}
|
||||
|
||||
// 匹配商品
|
||||
const product = this.productCache.get(row.productName);
|
||||
if (!product) {
|
||||
entitiesToCreate.products.add(row.productName);
|
||||
failedRecords.push({
|
||||
rowIndex: i + 1,
|
||||
originalData: row,
|
||||
reason: `商品 "${row.productName}" 不存在`
|
||||
});
|
||||
continue;
|
||||
}
|
||||
|
||||
// 匹配商品规格
|
||||
const specKey = `${row.productName}_${row.specCode}`;
|
||||
const spec = this.specCache.get(specKey);
|
||||
if (!spec) {
|
||||
entitiesToCreate.specs.add({ productName: row.productName, specCode: row.specCode });
|
||||
failedRecords.push({
|
||||
rowIndex: i + 1,
|
||||
originalData: row,
|
||||
reason: `商品规格 "${row.productName}" - "${row.specCode}" 不存在`
|
||||
});
|
||||
continue;
|
||||
}
|
||||
|
||||
// 匹配订单状态
|
||||
const orderStatusId = this.orderStatusMap.get(row.orderStatusName);
|
||||
if (!orderStatusId) {
|
||||
failedRecords.push({
|
||||
rowIndex: i + 1,
|
||||
originalData: row,
|
||||
reason: `订单状态 "${row.orderStatusName}" 不存在`
|
||||
});
|
||||
continue;
|
||||
}
|
||||
|
||||
// 从订单号中提取日期信息(格式:250913-341248633251447,前6位是日期)
|
||||
let orderDate = new Date().toISOString(); // 默认使用当前时间
|
||||
if (row.orderNumber && row.orderNumber.length >= 6) {
|
||||
try {
|
||||
const dateStr = row.orderNumber.substring(0, 6); // 提取前6位
|
||||
const year = '20' + dateStr.substring(0, 2); // 25 -> 2025
|
||||
const month = dateStr.substring(2, 4); // 09
|
||||
const day = dateStr.substring(4, 6); // 13
|
||||
orderDate = new Date(`${year}-${month}-${day}T00:00:00`).toISOString();
|
||||
} catch (error) {
|
||||
console.warn('无法解析订单号中的日期,使用当前时间:', row.orderNumber);
|
||||
}
|
||||
}
|
||||
|
||||
// 创建订单数据(简化结构,直接包含订单明细字段)
|
||||
const orderData: OrderDTO.CreateOrderDTO = {
|
||||
shopId: shop.id,
|
||||
platformOrderNo: row.orderNumber,
|
||||
statusId: orderStatusId,
|
||||
receivedAmount: row.shopActualAmount,
|
||||
shippingFee: 0, // 运费暂时设为0
|
||||
orderDate: orderDate,
|
||||
productId: product.id,
|
||||
productSpecId: spec.id,
|
||||
quantity: row.quantity,
|
||||
};
|
||||
|
||||
matchedOrders.push(orderData);
|
||||
} catch (error) {
|
||||
failedRecords.push({
|
||||
rowIndex: i + 1,
|
||||
originalData: row,
|
||||
reason: `处理失败: ${error instanceof Error ? error.message : '未知错误'}`
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
matchedOrders,
|
||||
failedRecords,
|
||||
entitiesToCreate: {
|
||||
shops: Array.from(entitiesToCreate.shops),
|
||||
products: Array.from(entitiesToCreate.products),
|
||||
specs: Array.from(entitiesToCreate.specs)
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量加载实体数据
|
||||
*/
|
||||
private async loadEntitiesBatch(
|
||||
shopNames: string[],
|
||||
productNames: string[],
|
||||
specQueries: { productName: string; specCode: string }[]
|
||||
): Promise<void> {
|
||||
try {
|
||||
// 并行查询所有实体
|
||||
const [shops, products, productsWithSpecs] = await Promise.all([
|
||||
shopNames.length > 0 ? getShopsByNamesApi(shopNames) : Promise.resolve([]),
|
||||
productNames.length > 0 ? getProductsByNamesApi(productNames) : Promise.resolve([]),
|
||||
specQueries.length > 0 ? getProductSpecsByProductAndSpecsApi(specQueries) : Promise.resolve([])
|
||||
]);
|
||||
|
||||
// 缓存店铺数据
|
||||
shops.forEach(shop => {
|
||||
this.shopCache.set(shop.name, shop);
|
||||
});
|
||||
|
||||
// 缓存商品数据
|
||||
products.forEach(product => {
|
||||
this.productCache.set(product.name, product);
|
||||
});
|
||||
|
||||
// 缓存包含规格信息的商品数据
|
||||
productsWithSpecs.forEach(product => {
|
||||
// 更新商品缓存
|
||||
this.productCache.set(product.name, product);
|
||||
|
||||
// 缓存该商品的规格数据
|
||||
if (product.specs && product.specs.length > 0) {
|
||||
product.specs.forEach(spec => {
|
||||
const key = `${product.name}_${spec.specCode || ''}`;
|
||||
this.specCache.set(key, spec);
|
||||
});
|
||||
}
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('批量加载实体数据失败:', error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 清空缓存
|
||||
*/
|
||||
private clearCache(): void {
|
||||
this.shopCache.clear();
|
||||
this.productCache.clear();
|
||||
this.specCache.clear();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取店铺信息
|
||||
*/
|
||||
getShopByName(name: string): ShopVO.ShopInfoVO | undefined {
|
||||
return this.shopCache.get(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取商品信息
|
||||
*/
|
||||
getProductByName(name: string): ProductVO.ProductInfoVO | undefined {
|
||||
return this.productCache.get(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取商品规格信息
|
||||
*/
|
||||
getProductSpecByKey(productName: string, specCode: string): ProductVO.ProductSpecInfoVO | undefined {
|
||||
const key = `${productName}_${specCode}`;
|
||||
return this.specCache.get(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取订单状态ID
|
||||
*/
|
||||
getOrderStatusId(statusName: string): number | undefined {
|
||||
return this.orderStatusMap.get(statusName);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,223 @@
|
||||
/**
|
||||
* CSV数据解析工具
|
||||
* 专门用于解析订单导入的CSV文件
|
||||
*/
|
||||
|
||||
import Papa from 'papaparse';
|
||||
|
||||
export interface ParsedOrderData {
|
||||
/** 商品名称 */
|
||||
productName: string;
|
||||
/** 订单编号 */
|
||||
orderNumber: string;
|
||||
/** 订单状态名称 */
|
||||
orderStatusName: string;
|
||||
/** 商品数量 */
|
||||
quantity: number;
|
||||
/** 商品ID */
|
||||
productId: string;
|
||||
/** 商品规格名称 */
|
||||
productSpecName: string;
|
||||
/** 规格编码 */
|
||||
specCode: string;
|
||||
/** 店铺名称 */
|
||||
shopName: string;
|
||||
/** 售后状态 */
|
||||
afterSaleStatus: string;
|
||||
/** 店铺实际收入 */
|
||||
shopActualAmount: number;
|
||||
}
|
||||
|
||||
export interface CSVParseResult {
|
||||
/** 解析成功的数据 */
|
||||
data: ParsedOrderData[];
|
||||
/** 解析错误 */
|
||||
errors: string[];
|
||||
/** 表头 */
|
||||
headers: string[];
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析CSV文件 - 使用papaparse库,支持多种编码
|
||||
*/
|
||||
export async function parseOrderCSV(file: File): Promise<CSVParseResult> {
|
||||
const errors: string[] = [];
|
||||
const data: ParsedOrderData[] = [];
|
||||
let headers: string[] = [];
|
||||
|
||||
// 尝试多种编码格式
|
||||
const encodings = ['GBK', 'UTF-8', 'GB2312'];
|
||||
let result: Papa.ParseResult<any> | null = null;
|
||||
|
||||
for (const encoding of encodings) {
|
||||
try {
|
||||
console.log(`尝试使用${encoding}编码解析文件...`);
|
||||
|
||||
result = await new Promise<Papa.ParseResult<any>>((resolve, reject) => {
|
||||
Papa.parse(file, {
|
||||
header: true,
|
||||
skipEmptyLines: true,
|
||||
encoding: encoding as any,
|
||||
complete: (results) => {
|
||||
resolve(results);
|
||||
},
|
||||
error: (error) => {
|
||||
reject(error);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// 检查是否成功解析出中文内容
|
||||
if (result.data && result.data.length > 0) {
|
||||
const firstRow = result.data[0];
|
||||
const hasChineseContent = Object.values(firstRow).some(value =>
|
||||
typeof value === 'string' && /[\u4e00-\u9fa5]/.test(value)
|
||||
);
|
||||
|
||||
if (hasChineseContent) {
|
||||
console.log(`成功使用${encoding}编码解析文件`);
|
||||
break;
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
console.log(`使用${encoding}编码解析失败:`, error);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
if (!result) {
|
||||
errors.push('无法解析CSV文件,请检查文件格式和编码');
|
||||
return { data, errors, headers };
|
||||
}
|
||||
|
||||
if (result.errors.length > 0) {
|
||||
errors.push(`CSV解析错误: ${result.errors.map(e => e.message).join(', ')}`);
|
||||
}
|
||||
|
||||
if (!result.data || result.data.length === 0) {
|
||||
errors.push('CSV文件为空或没有有效数据');
|
||||
return { data, errors, headers };
|
||||
}
|
||||
|
||||
// 获取表头
|
||||
headers = result.meta.fields || [];
|
||||
console.log('解析到的表头:', headers);
|
||||
|
||||
// 验证表头 - 使用更灵活的表头匹配
|
||||
const requiredHeaders = ['商品', '订单号', '订单状态', '商品数量(件)', '商品id', '商品规格', '商家编码-规格维度', '售后状态', '商家实收金额(元)'];
|
||||
const missingHeaders = requiredHeaders.filter(requiredHeader => {
|
||||
// 检查是否有匹配的表头(支持部分匹配)
|
||||
return !headers.some(header =>
|
||||
header.includes(requiredHeader) || requiredHeader.includes(header)
|
||||
);
|
||||
});
|
||||
|
||||
if (missingHeaders.length > 0) {
|
||||
console.log('缺少的列:', missingHeaders);
|
||||
console.log('实际表头:', headers);
|
||||
console.log('期望表头:', requiredHeaders);
|
||||
// 不再阻止导入,只是记录警告
|
||||
errors.push(`警告:缺少部分列: ${missingHeaders.join(', ')},可以在导入后手动处理`);
|
||||
}
|
||||
|
||||
// 解析数据行
|
||||
for (let i = 0; i < result.data.length; i++) {
|
||||
try {
|
||||
const row = result.data[i];
|
||||
const rowData = parseOrderRowFromObject(row, i + 2); // +2 因为从第2行开始(第1行是表头)
|
||||
if (rowData) {
|
||||
data.push(rowData);
|
||||
}
|
||||
} catch (error) {
|
||||
errors.push(`第${i + 2}行解析失败: ${error instanceof Error ? error.message : '未知错误'}`);
|
||||
console.log(`第${i + 2}行解析错误:`, error);
|
||||
}
|
||||
}
|
||||
|
||||
return { data, errors, headers };
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析订单行数据 - 从papaparse对象格式
|
||||
*/
|
||||
function parseOrderRowFromObject(row: any, rowNumber: number): ParsedOrderData | null {
|
||||
try {
|
||||
// 获取字段值,支持多种可能的字段名
|
||||
const getValue = (fieldNames: string[]): string => {
|
||||
for (const fieldName of fieldNames) {
|
||||
if (row[fieldName] !== undefined && row[fieldName] !== null && row[fieldName] !== '') {
|
||||
return String(row[fieldName]).trim();
|
||||
}
|
||||
}
|
||||
return '';
|
||||
};
|
||||
|
||||
const productName = getValue(['商品', '商品名称', 'productName']);
|
||||
const orderNumber = getValue(['订单号', '订单编号', 'orderNumber']);
|
||||
const orderStatusName = getValue(['订单状态', 'orderStatus', '状态']);
|
||||
const quantityStr = getValue(['商品数量(件)', '商品数量', '数量', 'quantity']);
|
||||
const productId = getValue(['商品id', '商品ID', 'productId', '商品编码']);
|
||||
const productSpecName = getValue(['商品规格', '规格', 'spec', 'productSpec']);
|
||||
const specCode = getValue(['商家编码-规格维度', '规格编码', 'specCode']);
|
||||
// 店铺名称应该从文件名获取,这里不设置shopName
|
||||
const afterSaleStatus = getValue(['售后状态', '售后', 'afterSale']);
|
||||
const shopActualAmountStr = getValue(['商家实收金额(元)', '店铺实际收入(元)', '店铺实际收入', '收入', 'amount']);
|
||||
|
||||
// 验证必填字段 - 只验证最核心的字段
|
||||
if (!productName) {
|
||||
throw new Error('商品名称不能为空');
|
||||
}
|
||||
if (!orderNumber) {
|
||||
throw new Error('订单编号不能为空');
|
||||
}
|
||||
|
||||
// 其他字段可以为空,在后续处理中处理
|
||||
|
||||
// 解析数量 - 允许为0或空
|
||||
const quantity = parseFloat(quantityStr) || 0;
|
||||
|
||||
// 解析金额 - 允许为0或空
|
||||
const shopActualAmount = parseFloat(shopActualAmountStr) || 0;
|
||||
|
||||
return {
|
||||
productName,
|
||||
orderNumber,
|
||||
orderStatusName,
|
||||
quantity,
|
||||
productId,
|
||||
productSpecName,
|
||||
specCode,
|
||||
shopName: '', // 店铺名称将从文件名获取
|
||||
afterSaleStatus,
|
||||
shopActualAmount,
|
||||
};
|
||||
} catch (error) {
|
||||
throw new Error(`第${rowNumber}行数据验证失败: ${error instanceof Error ? error.message : '未知错误'}`);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 从文件名提取店铺名称
|
||||
*/
|
||||
export function extractShopNameFromFilename(filename: string): string {
|
||||
// 移除文件扩展名,整个文件名就是店铺名称
|
||||
return filename.replace(/\.(csv|CSV)$/, '');
|
||||
}
|
||||
|
||||
/**
|
||||
* 转换为EntityMatchingService需要的格式
|
||||
*/
|
||||
export function convertToCSVOrderData(parsedData: ParsedOrderData[], shopName: string): any[] {
|
||||
return parsedData.map(row => ({
|
||||
productName: row.productName,
|
||||
orderNumber: row.orderNumber,
|
||||
orderStatusName: row.orderStatusName,
|
||||
quantity: row.quantity,
|
||||
productId: row.productId,
|
||||
productSpecName: row.productSpecName,
|
||||
specCode: row.specCode,
|
||||
shopName: shopName, // 使用从文件名提取的店铺名称
|
||||
afterSaleStatus: row.afterSaleStatus,
|
||||
shopActualAmount: row.shopActualAmount
|
||||
}));
|
||||
}
|
||||
+176
@@ -0,0 +1,176 @@
|
||||
<script lang="ts" setup>
|
||||
import { Card, Image, Tag, Button, Space, Popconfirm, Tooltip } from 'ant-design-vue';
|
||||
import { createIconifyIcon } from '@vben/icons';
|
||||
import type { ProductVO } from '#/api/ecommerce';
|
||||
|
||||
// 创建图标组件
|
||||
const EditIcon = createIconifyIcon('lucide:edit');
|
||||
const TrashIcon = createIconifyIcon('lucide:trash-2');
|
||||
const SettingsIcon = createIconifyIcon('lucide:settings');
|
||||
const EyeIcon = createIconifyIcon('lucide:eye');
|
||||
|
||||
interface Props {
|
||||
product: ProductVO.ProductInfoVO;
|
||||
}
|
||||
|
||||
interface Emits {
|
||||
(e: 'edit', product: ProductVO.ProductInfoVO): void;
|
||||
(e: 'delete', product: ProductVO.ProductInfoVO): void;
|
||||
(e: 'manage-specs', product: ProductVO.ProductInfoVO): void;
|
||||
(e: 'view-details', product: ProductVO.ProductInfoVO): void;
|
||||
}
|
||||
|
||||
defineProps<Props>();
|
||||
const emit = defineEmits<Emits>();
|
||||
|
||||
const handleEdit = (product: ProductVO.ProductInfoVO) => {
|
||||
emit('edit', product);
|
||||
};
|
||||
|
||||
const handleDelete = (product: ProductVO.ProductInfoVO) => {
|
||||
emit('delete', product);
|
||||
};
|
||||
|
||||
const handleManageSpecs = (product: ProductVO.ProductInfoVO) => {
|
||||
emit('manage-specs', product);
|
||||
};
|
||||
|
||||
const handleViewDetails = (product: ProductVO.ProductInfoVO) => {
|
||||
emit('view-details', product);
|
||||
};
|
||||
|
||||
const handleImageError = (event: Event) => {
|
||||
const img = event.target as HTMLImageElement;
|
||||
img.style.display = 'none';
|
||||
};
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<Card
|
||||
hoverable
|
||||
:body-style="{ padding: '12px' }"
|
||||
class="product-card"
|
||||
>
|
||||
<div class="relative">
|
||||
<!-- 商品图片 -->
|
||||
<div class="aspect-square bg-gray-100 rounded-lg overflow-hidden">
|
||||
<Image
|
||||
v-if="product.mainImage"
|
||||
:src="product.mainImage"
|
||||
:alt="product.name"
|
||||
class="w-full h-full object-cover"
|
||||
:preview="true"
|
||||
@error="handleImageError"
|
||||
/>
|
||||
<div v-else class="w-full h-full flex items-center justify-center text-gray-400">
|
||||
<div class="text-center">
|
||||
<div class="text-4xl mb-2">📦</div>
|
||||
<div class="text-sm">暂无图片</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 规格数量标签 -->
|
||||
<div v-if="product.specs && product.specs.length > 0" class="absolute top-2 left-2">
|
||||
<Tag color="blue">
|
||||
{{ product.specs.length }} 个规格
|
||||
</Tag>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 商品信息 -->
|
||||
<div class="mt-3">
|
||||
<div class="text-sm font-medium text-gray-900 mb-2 line-clamp-2" :title="product.name">
|
||||
{{ product.name }}
|
||||
</div>
|
||||
|
||||
<!-- 规格信息 -->
|
||||
<div v-if="product.specs && product.specs.length > 0" class="mb-2">
|
||||
<div class="text-xs text-gray-500 mb-1">规格信息:</div>
|
||||
<div class="space-y-1">
|
||||
<div
|
||||
v-for="spec in product.specs.slice(0, 2)"
|
||||
:key="spec.id"
|
||||
class="text-xs bg-gray-50 px-2 py-1 rounded"
|
||||
>
|
||||
<span class="font-medium">{{ spec.name }}</span>
|
||||
<span class="text-red-500 ml-1">¥{{ spec.costPrice.toFixed(2) }}</span>
|
||||
</div>
|
||||
<div v-if="product.specs.length > 2" class="text-xs text-gray-400">
|
||||
还有 {{ product.specs.length - 2 }} 个规格...
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 创建时间 -->
|
||||
<div class="text-xs text-gray-500 mb-3">
|
||||
{{ product.createTime }}
|
||||
</div>
|
||||
|
||||
<!-- 操作按钮 -->
|
||||
<div class="flex justify-between items-center">
|
||||
<Space size="small">
|
||||
<Tooltip title="编辑">
|
||||
<Button
|
||||
type="text"
|
||||
size="small"
|
||||
@click="handleEdit(product)"
|
||||
>
|
||||
<EditIcon class="w-4 h-4" />
|
||||
</Button>
|
||||
</Tooltip>
|
||||
<Tooltip title="管理规格">
|
||||
<Button
|
||||
type="text"
|
||||
size="small"
|
||||
@click="handleManageSpecs(product)"
|
||||
>
|
||||
<SettingsIcon class="w-4 h-4" />
|
||||
</Button>
|
||||
</Tooltip>
|
||||
<Tooltip title="查看详情">
|
||||
<Button
|
||||
type="text"
|
||||
size="small"
|
||||
@click="handleViewDetails(product)"
|
||||
>
|
||||
<EyeIcon class="w-4 h-4" />
|
||||
</Button>
|
||||
</Tooltip>
|
||||
</Space>
|
||||
<Popconfirm
|
||||
title="确定要删除这个商品吗?"
|
||||
@confirm="handleDelete(product)"
|
||||
>
|
||||
<Button
|
||||
type="text"
|
||||
size="small"
|
||||
danger
|
||||
>
|
||||
<TrashIcon class="w-4 h-4" />
|
||||
</Button>
|
||||
</Popconfirm>
|
||||
</div>
|
||||
</div>
|
||||
</Card>
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
.product-card {
|
||||
transition: all 0.3s ease;
|
||||
height: 100%;
|
||||
}
|
||||
|
||||
.product-card:hover {
|
||||
transform: translateY(-2px);
|
||||
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
|
||||
}
|
||||
|
||||
.line-clamp-2 {
|
||||
display: -webkit-box;
|
||||
-webkit-line-clamp: 2;
|
||||
line-clamp: 2;
|
||||
-webkit-box-orient: vertical;
|
||||
overflow: hidden;
|
||||
}
|
||||
</style>
|
||||
+419
@@ -0,0 +1,419 @@
|
||||
<template>
|
||||
<Modal
|
||||
:open="visible"
|
||||
:title="modalTitle"
|
||||
width="600px"
|
||||
:confirm-loading="loading"
|
||||
@ok="handleSubmit"
|
||||
@cancel="handleCancel"
|
||||
>
|
||||
<Form
|
||||
ref="formRef"
|
||||
:model="formData"
|
||||
:label-col="{ span: 6 }"
|
||||
:wrapper-col="{ span: 18 }"
|
||||
>
|
||||
<Form.Item label="商品主图" name="mainImageId">
|
||||
<div style="display: flex; align-items: center; gap: 16px;">
|
||||
|
||||
<!-- 主图预览区域 -->
|
||||
<div style="position: relative; display: inline-block;">
|
||||
<!-- 有图片时显示图片 -->
|
||||
<img
|
||||
v-if="currentImageUrl && !imageLoadError"
|
||||
:src="currentImageUrl"
|
||||
alt="商品主图"
|
||||
style="width: 80px; height: 80px; object-fit: cover; border-radius: 8px; border: 1px solid #d9d9d9; background-color: #f5f5f5;"
|
||||
@error="handleImageError"
|
||||
/>
|
||||
<!-- 无图片或加载失败时显示空状态 -->
|
||||
<div
|
||||
v-else
|
||||
style="width: 80px; height: 80px; border: 1px dashed #d9d9d9; border-radius: 8px; background-color: #fafafa; display: flex; align-items: center; justify-content: center; color: #999; font-size: 12px; text-align: center; line-height: 1.2;">
|
||||
<div style="display: flex; flex-direction: column; align-items: center; justify-content: center;">
|
||||
<CameraIcon style="font-size: 16px; margin-bottom: 4px;" />
|
||||
<div>上传主图</div>
|
||||
</div>
|
||||
</div>
|
||||
<!-- 点击更换遮罩 -->
|
||||
<div
|
||||
style="position: absolute; top: 0; left: 0; right: 0; bottom: 0; background: rgba(0,0,0,0.6); border-radius: 8px; display: flex; align-items: center; justify-content: center; opacity: 0; transition: opacity 0.3s; cursor: pointer;"
|
||||
@mouseenter="($event.target as HTMLElement).style.opacity = '1'"
|
||||
@mouseleave="($event.target as HTMLElement).style.opacity = '0'"
|
||||
@click="selectImageFile">
|
||||
<div style="color: white; text-align: center; display: flex; flex-direction: column; align-items: center; justify-content: center;">
|
||||
<CameraIcon style="font-size: 16px; margin-bottom: 4px;" />
|
||||
<div style="font-size: 12px;">{{ currentImageUrl && !imageLoadError ? '更换主图' : '上传主图' }}</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 重置按钮 -->
|
||||
<div v-if="newImageFile">
|
||||
<Button size="small" @click="removeNewImage">重置</Button>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
</Form.Item>
|
||||
|
||||
<Form.Item label="商品名称" name="name" :rules="[{ required: true, message: '请输入商品名称' }]">
|
||||
<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>
|
||||
|
||||
<Form.Item v-if="isEdit" label="商品SKU" name="skuCode">
|
||||
<Input v-model:value="formData.skuCode" placeholder="请输入商品SKU" />
|
||||
</Form.Item>
|
||||
|
||||
<Form.Item v-if="isEdit" label="成本价" name="costPrice" :rules="[{ required: true, message: '请输入成本价' }]">
|
||||
<InputNumber
|
||||
v-model:value="formData.costPrice"
|
||||
placeholder="请输入成本价"
|
||||
style="width: 100%;"
|
||||
:min="0"
|
||||
:precision="2"
|
||||
/>
|
||||
</Form.Item>
|
||||
|
||||
<!-- 规格管理 -->
|
||||
<Form.Item v-if="!isEdit" label="商品规格">
|
||||
<div class="space-y-4">
|
||||
<div class="flex items-center gap-2">
|
||||
<Button
|
||||
type="dashed"
|
||||
@click="showSpecs = !showSpecs"
|
||||
:icon="showSpecs ? 'up' : 'down'"
|
||||
>
|
||||
{{ showSpecs ? '收起规格' : '添加规格' }}
|
||||
</Button>
|
||||
<span class="text-sm text-gray-500">
|
||||
可以添加多个规格,每个规格包含名称、编码和成本价
|
||||
</span>
|
||||
</div>
|
||||
|
||||
<div v-if="showSpecs" class="border border-gray-200 rounded-lg p-4 bg-gray-50">
|
||||
<div class="space-y-3">
|
||||
<div
|
||||
v-for="(spec, index) in specs"
|
||||
:key="index"
|
||||
class="flex items-center gap-2 p-3 bg-white rounded border"
|
||||
>
|
||||
<Input
|
||||
v-model:value="spec.name"
|
||||
placeholder="规格名称"
|
||||
style="flex: 1;"
|
||||
/>
|
||||
<Input
|
||||
v-model:value="spec.specCode"
|
||||
placeholder="规格编码"
|
||||
style="flex: 1;"
|
||||
/>
|
||||
<InputNumber
|
||||
v-model:value="spec.costPrice"
|
||||
placeholder="成本价"
|
||||
style="width: 120px;"
|
||||
:min="0"
|
||||
:precision="2"
|
||||
/>
|
||||
<Button
|
||||
type="text"
|
||||
danger
|
||||
@click="removeSpec(index)"
|
||||
>
|
||||
删除
|
||||
</Button>
|
||||
</div>
|
||||
|
||||
<Button
|
||||
type="dashed"
|
||||
@click="addSpec"
|
||||
style="width: 100%;"
|
||||
>
|
||||
+ 添加规格
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</Form.Item>
|
||||
</Form>
|
||||
</Modal>
|
||||
</template>
|
||||
|
||||
<script lang="ts" setup>
|
||||
import { computed, ref, onUnmounted } from 'vue';
|
||||
import { Form, Input, Modal, InputNumber, Button, message } from 'ant-design-vue';
|
||||
import { createIconifyIcon } from '@vben/icons';
|
||||
import {
|
||||
createProductApi,
|
||||
updateProductApi,
|
||||
createProductWithSpecsApi,
|
||||
type ProductVO,
|
||||
type ProductDTO
|
||||
} from '#/api/ecommerce';
|
||||
import { uploadImageApi } from '#/api/media';
|
||||
|
||||
interface Emits {
|
||||
(e: 'success'): void;
|
||||
}
|
||||
|
||||
const emit = defineEmits<Emits>();
|
||||
|
||||
// 创建相机图标组件
|
||||
const CameraIcon = createIconifyIcon('mdi:camera');
|
||||
|
||||
// 内部状态管理
|
||||
const visible = ref(false);
|
||||
const isEdit = ref(false);
|
||||
const currentProductData = ref<ProductVO.ProductInfoVO | Partial<ProductVO.ProductInfoVO> | null>(null);
|
||||
|
||||
// 响应式数据
|
||||
const formRef = ref();
|
||||
const loading = ref(false);
|
||||
const newImageFile = ref<File | null>(null); // 新选择的图片文件
|
||||
const originalImageUrl = ref(''); // 原始图片 URL
|
||||
const imageLoadError = ref(false); // 图片加载错误状态
|
||||
|
||||
// 表单数据
|
||||
const formData = ref({
|
||||
name: '',
|
||||
specCode: '',
|
||||
skuCode: '',
|
||||
mainImageId: undefined as number | undefined,
|
||||
costPrice: 0,
|
||||
});
|
||||
|
||||
// 规格数据
|
||||
const specs = ref<ProductDTO.ProductSpecDTO[]>([]);
|
||||
const showSpecs = ref(false);
|
||||
|
||||
// 模态框标题
|
||||
const modalTitle = computed(() => isEdit.value ? '编辑商品' : '添加商品');
|
||||
|
||||
// 当前显示的图片 URL(优先显示新选择的)
|
||||
const currentImageUrl = computed(() => {
|
||||
if (newImageFile.value) {
|
||||
return URL.createObjectURL(newImageFile.value);
|
||||
}
|
||||
return originalImageUrl.value;
|
||||
});
|
||||
|
||||
// 初始化表单数据
|
||||
function initFormData() {
|
||||
if (isEdit.value && currentProductData.value) {
|
||||
// 编辑模式
|
||||
formData.value = {
|
||||
name: currentProductData.value.name || '',
|
||||
specCode: '',
|
||||
skuCode: '',
|
||||
mainImageId: undefined,
|
||||
costPrice: 0,
|
||||
};
|
||||
|
||||
// 设置原始图片 URL
|
||||
originalImageUrl.value = currentProductData.value.mainImage || '';
|
||||
} else {
|
||||
// 创建模式 - 如果有传入的productData,则使用它来初始化表单
|
||||
formData.value = {
|
||||
name: currentProductData.value?.name || '',
|
||||
specCode: '',
|
||||
skuCode: '',
|
||||
mainImageId: undefined,
|
||||
costPrice: 0,
|
||||
};
|
||||
originalImageUrl.value = currentProductData.value?.mainImage || '';
|
||||
}
|
||||
|
||||
// 重置新选择的图片和图片加载状态
|
||||
newImageFile.value = null;
|
||||
imageLoadError.value = false;
|
||||
|
||||
// 重置规格数据
|
||||
specs.value = [];
|
||||
showSpecs.value = false;
|
||||
|
||||
// 如果有传入的规格信息,则自动填入
|
||||
if (!isEdit.value && currentProductData.value && 'specs' in currentProductData.value && currentProductData.value.specs) {
|
||||
specs.value = currentProductData.value.specs.map(spec => ({
|
||||
name: (spec as any).specName || spec.specCode || '',
|
||||
specCode: spec.specCode || '',
|
||||
costPrice: spec.costPrice || 0
|
||||
}));
|
||||
showSpecs.value = specs.value.length > 0;
|
||||
}
|
||||
}
|
||||
|
||||
// 重置表单(在open方法中调用)
|
||||
function resetForm() {
|
||||
formRef.value?.resetFields();
|
||||
newImageFile.value = null;
|
||||
originalImageUrl.value = '';
|
||||
imageLoadError.value = false;
|
||||
specs.value = [];
|
||||
showSpecs.value = false;
|
||||
}
|
||||
|
||||
// 处理提交
|
||||
async function handleSubmit() {
|
||||
try {
|
||||
await formRef.value?.validate();
|
||||
loading.value = true;
|
||||
|
||||
// 准备提交数据
|
||||
const submitData = { ...formData.value };
|
||||
|
||||
// 如果有新选择的图片,先上传获取ID
|
||||
if (newImageFile.value) {
|
||||
const mainImageId = await uploadImageAndGetId();
|
||||
if (mainImageId) {
|
||||
submitData.mainImageId = mainImageId;
|
||||
}
|
||||
} else if (!isEdit.value) {
|
||||
// 创建模式且没有选择图片,移除mainImageId字段
|
||||
delete submitData.mainImageId;
|
||||
}
|
||||
|
||||
// 调用API
|
||||
if (isEdit.value && currentProductData.value && 'id' in currentProductData.value && currentProductData.value.id) {
|
||||
await updateProductApi(currentProductData.value.id, submitData as ProductDTO.UpdateProductDTO);
|
||||
message.success('更新成功');
|
||||
} else {
|
||||
// 创建模式:如果有规格,使用一键创建接口
|
||||
if (specs.value.length > 0) {
|
||||
const createWithSpecsData: ProductDTO.CreateProductWithSpecsDTO = {
|
||||
name: submitData.name,
|
||||
mainImageId: submitData.mainImageId,
|
||||
specs: specs.value,
|
||||
};
|
||||
await createProductWithSpecsApi(createWithSpecsData);
|
||||
message.success('商品和规格创建成功');
|
||||
} else {
|
||||
await createProductApi(submitData as ProductDTO.CreateProductDTO);
|
||||
message.success('创建成功');
|
||||
}
|
||||
}
|
||||
|
||||
visible.value = false;
|
||||
emit('success');
|
||||
} catch (error) {
|
||||
console.error('保存商品失败:', error);
|
||||
message.error('保存商品失败');
|
||||
} finally {
|
||||
loading.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
// 处理取消
|
||||
function handleCancel() {
|
||||
visible.value = false;
|
||||
}
|
||||
|
||||
// 暴露给父组件的方法
|
||||
function open(editMode: boolean = false, productData?: ProductVO.ProductInfoVO | Partial<ProductVO.ProductInfoVO> | null) {
|
||||
isEdit.value = editMode;
|
||||
currentProductData.value = productData || null;
|
||||
|
||||
resetForm(); // 先重置表单
|
||||
initFormData(); // 再初始化数据
|
||||
|
||||
visible.value = true;
|
||||
}
|
||||
|
||||
// 暴露方法给父组件
|
||||
defineExpose({
|
||||
open,
|
||||
});
|
||||
|
||||
// 选择图片文件
|
||||
function selectImageFile() {
|
||||
const input = document.createElement('input');
|
||||
input.type = 'file';
|
||||
input.accept = 'image/*';
|
||||
input.style.display = 'none';
|
||||
document.body.appendChild(input);
|
||||
|
||||
input.onchange = (e) => {
|
||||
const file = (e.target as HTMLInputElement).files?.[0];
|
||||
if (file) {
|
||||
// 验证文件
|
||||
const isImage = file.type.startsWith('image/');
|
||||
if (!isImage) {
|
||||
message.error('只能上传图片文件');
|
||||
document.body.removeChild(input);
|
||||
return;
|
||||
}
|
||||
|
||||
const isLt2M = file.size / 1024 / 1024 < 2;
|
||||
if (!isLt2M) {
|
||||
message.error('图片大小不能超过 2MB');
|
||||
document.body.removeChild(input);
|
||||
return;
|
||||
}
|
||||
|
||||
// 设置新选择的文件
|
||||
newImageFile.value = file;
|
||||
imageLoadError.value = false; // 重置图片加载错误状态
|
||||
message.success('图片选择成功');
|
||||
}
|
||||
|
||||
document.body.removeChild(input);
|
||||
};
|
||||
|
||||
input.click();
|
||||
}
|
||||
|
||||
// 重置新选择的图片
|
||||
function removeNewImage() {
|
||||
newImageFile.value = null;
|
||||
imageLoadError.value = false; // 重置图片加载错误状态
|
||||
message.success('已重置图片');
|
||||
}
|
||||
|
||||
// 添加规格
|
||||
function addSpec() {
|
||||
specs.value.push({
|
||||
name: '',
|
||||
specCode: '',
|
||||
costPrice: 0,
|
||||
});
|
||||
}
|
||||
|
||||
// 删除规格
|
||||
function removeSpec(index: number) {
|
||||
specs.value.splice(index, 1);
|
||||
}
|
||||
|
||||
// 上传图片并获取图片ID
|
||||
async function uploadImageAndGetId(): Promise<number | undefined> {
|
||||
if (!newImageFile.value) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await uploadImageApi(newImageFile.value);
|
||||
console.log('图片上传成功:', response);
|
||||
return response.id;
|
||||
} catch (error) {
|
||||
console.error('图片上传失败:', error);
|
||||
message.error('图片上传失败');
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
// 图片加载错误处理
|
||||
function handleImageError(event: Event) {
|
||||
console.error('图片加载失败:', event);
|
||||
imageLoadError.value = true;
|
||||
message.error('图片加载失败,请重新选择');
|
||||
}
|
||||
|
||||
// 组件卸载时清理
|
||||
onUnmounted(() => {
|
||||
if (newImageFile.value) {
|
||||
const url = URL.createObjectURL(newImageFile.value);
|
||||
URL.revokeObjectURL(url);
|
||||
}
|
||||
});
|
||||
</script>
|
||||
+322
@@ -0,0 +1,322 @@
|
||||
<template>
|
||||
<Modal
|
||||
:open="visible"
|
||||
:title="modalTitle"
|
||||
width="800px"
|
||||
:confirm-loading="loading"
|
||||
@ok="handleSubmit"
|
||||
@cancel="handleCancel"
|
||||
>
|
||||
<div class="space-y-4">
|
||||
<!-- 商品信息 -->
|
||||
<div class="bg-gray-50 p-4 rounded-lg">
|
||||
<h3 class="font-medium text-gray-900 mb-2">商品信息</h3>
|
||||
<div class="grid grid-cols-2 gap-4 text-sm">
|
||||
<div>
|
||||
<span class="text-gray-500">商品名称:</span>
|
||||
<span class="ml-2 font-medium">{{ currentProduct?.name }}</span>
|
||||
</div>
|
||||
<div>
|
||||
<span class="text-gray-500">商品ID:</span>
|
||||
<span class="ml-2 font-medium">{{ currentProduct?.id }}</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 规格列表 -->
|
||||
<div>
|
||||
<div class="flex items-center justify-between mb-4">
|
||||
<h3 class="font-medium text-gray-900">商品规格</h3>
|
||||
<Button type="primary" @click="handleAddSpec">
|
||||
添加规格
|
||||
</Button>
|
||||
</div>
|
||||
|
||||
<Table
|
||||
:columns="columns"
|
||||
:data-source="specList"
|
||||
:loading="specLoading"
|
||||
:pagination="{
|
||||
current: specCurrent,
|
||||
pageSize: specPageSize,
|
||||
total: specTotal,
|
||||
showSizeChanger: true,
|
||||
showQuickJumper: true,
|
||||
showTotal: (total, range) => `第 ${range[0]}-${range[1]} 条/共 ${total} 条`,
|
||||
}"
|
||||
row-key="id"
|
||||
@change="handleSpecTableChange"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
</Modal>
|
||||
|
||||
<!-- 规格表单模态框 -->
|
||||
<Modal
|
||||
:open="specFormVisible"
|
||||
:title="specFormTitle"
|
||||
width="500px"
|
||||
:confirm-loading="specFormLoading"
|
||||
@ok="handleSpecSubmit"
|
||||
@cancel="handleSpecCancel"
|
||||
>
|
||||
<Form
|
||||
ref="specFormRef"
|
||||
:model="specFormData"
|
||||
:label-col="{ span: 6 }"
|
||||
:wrapper-col="{ span: 18 }"
|
||||
>
|
||||
<Form.Item label="规格名称" name="name" :rules="[{ required: true, message: '请输入规格名称' }]">
|
||||
<Input v-model:value="specFormData.name" placeholder="请输入规格名称" />
|
||||
</Form.Item>
|
||||
|
||||
<Form.Item label="规格编码" name="specCode">
|
||||
<Input v-model:value="specFormData.specCode" placeholder="请输入规格编码" />
|
||||
</Form.Item>
|
||||
|
||||
<Form.Item label="成本价" name="costPrice" :rules="[{ required: true, message: '请输入成本价' }]">
|
||||
<InputNumber
|
||||
v-model:value="specFormData.costPrice"
|
||||
placeholder="请输入成本价"
|
||||
style="width: 100%;"
|
||||
:min="0"
|
||||
:precision="2"
|
||||
/>
|
||||
</Form.Item>
|
||||
</Form>
|
||||
</Modal>
|
||||
</template>
|
||||
|
||||
<script lang="ts" setup>
|
||||
import { computed, ref, h } from 'vue';
|
||||
import { Form, Input, Modal, InputNumber, Button, Table, message } from 'ant-design-vue';
|
||||
import {
|
||||
getProductSpecPageApi,
|
||||
createProductSpecApi,
|
||||
updateProductSpecApi,
|
||||
deleteProductSpecApi,
|
||||
type ProductVO,
|
||||
type ProductDTO
|
||||
} from '#/api/ecommerce';
|
||||
import type { PageQueryDTO } from '#/types/common';
|
||||
|
||||
interface Emits {
|
||||
(e: 'success'): void;
|
||||
}
|
||||
|
||||
const emit = defineEmits<Emits>();
|
||||
|
||||
// 内部状态管理
|
||||
const visible = ref(false);
|
||||
const currentProduct = ref<ProductVO.ProductInfoVO | null>(null);
|
||||
|
||||
// 响应式数据
|
||||
const loading = ref(false);
|
||||
const specLoading = ref(false);
|
||||
const specList = ref<ProductVO.ProductSpecInfoVO[]>([]);
|
||||
const specTotal = ref(0);
|
||||
const specCurrent = ref(1);
|
||||
const specPageSize = ref(10);
|
||||
|
||||
// 规格表单相关
|
||||
const specFormVisible = ref(false);
|
||||
const specFormLoading = ref(false);
|
||||
const specFormRef = ref();
|
||||
const isEditSpec = ref(false);
|
||||
const currentSpecId = ref<number | null>(null);
|
||||
|
||||
const specFormData = ref({
|
||||
name: '',
|
||||
specCode: '',
|
||||
costPrice: 0,
|
||||
});
|
||||
|
||||
// 模态框标题
|
||||
const modalTitle = computed(() =>
|
||||
currentProduct.value ? `管理规格 - ${currentProduct.value.name}` : '管理规格'
|
||||
);
|
||||
|
||||
// 规格表单标题
|
||||
const specFormTitle = computed(() => isEditSpec.value ? '编辑规格' : '添加规格');
|
||||
|
||||
// 表格列配置
|
||||
const columns = [
|
||||
{
|
||||
title: '规格名称',
|
||||
dataIndex: 'name',
|
||||
key: 'name',
|
||||
width: 200,
|
||||
},
|
||||
{
|
||||
title: '规格编码',
|
||||
dataIndex: 'specCode',
|
||||
key: 'specCode',
|
||||
width: 150,
|
||||
},
|
||||
{
|
||||
title: '成本价',
|
||||
dataIndex: 'costPrice',
|
||||
key: 'costPrice',
|
||||
width: 120,
|
||||
customRender: ({ record }: { record: ProductVO.ProductSpecInfoVO }) => {
|
||||
return `¥${record.costPrice.toFixed(2)}`;
|
||||
},
|
||||
},
|
||||
{
|
||||
title: '创建时间',
|
||||
dataIndex: 'createTime',
|
||||
key: 'createTime',
|
||||
width: 180,
|
||||
},
|
||||
{
|
||||
title: '操作',
|
||||
key: 'action',
|
||||
width: 150,
|
||||
fixed: 'right' as const,
|
||||
customRender: ({ record }: { record: ProductVO.ProductSpecInfoVO }) => {
|
||||
return h('div', { style: 'display: flex; gap: 8px;' }, [
|
||||
h(Button, {
|
||||
size: 'small',
|
||||
onClick: () => handleEditSpec(record),
|
||||
}, { default: () => '编辑' }),
|
||||
h(Button, {
|
||||
size: 'small',
|
||||
danger: true,
|
||||
onClick: () => handleDeleteSpec(record),
|
||||
}, { default: () => '删除' }),
|
||||
]);
|
||||
},
|
||||
},
|
||||
];
|
||||
|
||||
// 获取规格列表
|
||||
async function fetchSpecList() {
|
||||
if (!currentProduct.value) return;
|
||||
|
||||
try {
|
||||
specLoading.value = true;
|
||||
const params: PageQueryDTO<ProductDTO.ProductSpecFilterDTO> = {
|
||||
page: specCurrent.value,
|
||||
pageSize: specPageSize.value,
|
||||
filters: {
|
||||
productId: currentProduct.value.id,
|
||||
},
|
||||
};
|
||||
const data = await getProductSpecPageApi(params);
|
||||
specList.value = data.records;
|
||||
specTotal.value = data.total;
|
||||
} catch (error) {
|
||||
console.error('获取规格列表失败:', error);
|
||||
message.error('获取规格列表失败');
|
||||
} finally {
|
||||
specLoading.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
// 处理规格表格变化
|
||||
function handleSpecTableChange(pagination: any) {
|
||||
specCurrent.value = pagination.current;
|
||||
specPageSize.value = pagination.pageSize;
|
||||
fetchSpecList();
|
||||
}
|
||||
|
||||
// 处理添加规格
|
||||
function handleAddSpec() {
|
||||
isEditSpec.value = false;
|
||||
currentSpecId.value = null;
|
||||
specFormData.value = {
|
||||
name: '',
|
||||
specCode: '',
|
||||
costPrice: 0,
|
||||
};
|
||||
specFormVisible.value = true;
|
||||
}
|
||||
|
||||
// 处理编辑规格
|
||||
function handleEditSpec(record: ProductVO.ProductSpecInfoVO) {
|
||||
isEditSpec.value = true;
|
||||
currentSpecId.value = record.id;
|
||||
specFormData.value = {
|
||||
name: record.name,
|
||||
specCode: record.specCode || '',
|
||||
costPrice: record.costPrice,
|
||||
};
|
||||
specFormVisible.value = true;
|
||||
}
|
||||
|
||||
// 处理删除规格
|
||||
async function handleDeleteSpec(record: ProductVO.ProductSpecInfoVO) {
|
||||
try {
|
||||
await deleteProductSpecApi(record.id);
|
||||
message.success('删除成功');
|
||||
await fetchSpecList();
|
||||
} catch (error) {
|
||||
console.error('删除规格失败:', error);
|
||||
message.error('删除规格失败');
|
||||
}
|
||||
}
|
||||
|
||||
// 处理规格表单提交
|
||||
async function handleSpecSubmit() {
|
||||
try {
|
||||
await specFormRef.value?.validate();
|
||||
specFormLoading.value = true;
|
||||
|
||||
if (isEditSpec.value && currentSpecId.value) {
|
||||
await updateProductSpecApi(currentSpecId.value, specFormData.value as ProductDTO.UpdateProductSpecDTO);
|
||||
message.success('更新成功');
|
||||
} else {
|
||||
if (!currentProduct.value) {
|
||||
message.error('商品信息不存在');
|
||||
return;
|
||||
}
|
||||
|
||||
const createData: ProductDTO.CreateProductSpecDTO = {
|
||||
productId: currentProduct.value.id,
|
||||
...specFormData.value,
|
||||
};
|
||||
await createProductSpecApi(createData);
|
||||
message.success('创建成功');
|
||||
}
|
||||
|
||||
specFormVisible.value = false;
|
||||
await fetchSpecList();
|
||||
} catch (error) {
|
||||
console.error('保存规格失败:', error);
|
||||
message.error('保存规格失败');
|
||||
} finally {
|
||||
specFormLoading.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
// 处理规格表单取消
|
||||
function handleSpecCancel() {
|
||||
specFormVisible.value = false;
|
||||
}
|
||||
|
||||
// 处理提交
|
||||
function handleSubmit() {
|
||||
// 规格管理模态框不需要提交操作
|
||||
handleCancel();
|
||||
}
|
||||
|
||||
// 处理取消
|
||||
function handleCancel() {
|
||||
visible.value = false;
|
||||
emit('success');
|
||||
}
|
||||
|
||||
// 暴露给父组件的方法
|
||||
function open(product: ProductVO.ProductInfoVO) {
|
||||
currentProduct.value = product;
|
||||
specCurrent.value = 1;
|
||||
specPageSize.value = 10;
|
||||
visible.value = true;
|
||||
fetchSpecList();
|
||||
}
|
||||
|
||||
// 暴露方法给父组件
|
||||
defineExpose({
|
||||
open,
|
||||
});
|
||||
</script>
|
||||
+219
-205
@@ -1,18 +1,184 @@
|
||||
<template>
|
||||
<Page>
|
||||
<template #title>商品管理</template>
|
||||
|
||||
<!-- 搜索区域 -->
|
||||
<div class="mb-4 p-4 bg-white rounded-lg shadow-sm">
|
||||
<Form :model="searchForm" layout="inline">
|
||||
<Form.Item label="商品名称">
|
||||
<Input
|
||||
v-model:value="searchForm.name"
|
||||
placeholder="请输入商品名称"
|
||||
style="width: 200px"
|
||||
@press-enter="handleSearch"
|
||||
/>
|
||||
</Form.Item>
|
||||
<Form.Item label="商品编码">
|
||||
<Input
|
||||
v-model:value="searchForm.specCode"
|
||||
placeholder="请输入商品编码"
|
||||
style="width: 150px"
|
||||
@press-enter="handleSearch"
|
||||
/>
|
||||
</Form.Item>
|
||||
<Form.Item label="商品SKU">
|
||||
<Input
|
||||
v-model:value="searchForm.skuCode"
|
||||
placeholder="请输入商品SKU"
|
||||
style="width: 150px"
|
||||
@press-enter="handleSearch"
|
||||
/>
|
||||
</Form.Item>
|
||||
<Form.Item label="成本价范围">
|
||||
<div class="flex items-center gap-2">
|
||||
<InputNumber
|
||||
v-model:value="searchForm.minCostPrice"
|
||||
placeholder="最小值"
|
||||
style="width: 100px"
|
||||
:min="0"
|
||||
:precision="2"
|
||||
/>
|
||||
<span class="text-gray-500">-</span>
|
||||
<InputNumber
|
||||
v-model:value="searchForm.maxCostPrice"
|
||||
placeholder="最大值"
|
||||
style="width: 100px"
|
||||
:min="0"
|
||||
:precision="2"
|
||||
/>
|
||||
</div>
|
||||
</Form.Item>
|
||||
<Form.Item>
|
||||
<Space>
|
||||
<Button type="primary" @click="handleSearch">搜索</Button>
|
||||
<Button @click="handleReset">重置</Button>
|
||||
</Space>
|
||||
</Form.Item>
|
||||
</Form>
|
||||
</div>
|
||||
|
||||
<!-- 操作区域 -->
|
||||
<div class="mb-4 flex justify-between items-center">
|
||||
<div class="flex items-center gap-4">
|
||||
<Button type="primary" @click="handleAdd">
|
||||
<PlusIcon class="w-4 h-4 mr-2" />
|
||||
添加商品
|
||||
</Button>
|
||||
<span class="text-gray-500">支持商品和规格的一键创建</span>
|
||||
</div>
|
||||
<div class="flex items-center gap-4">
|
||||
<Button
|
||||
:type="viewMode === 'grid' ? 'primary' : 'default'"
|
||||
@click="toggleViewMode"
|
||||
>
|
||||
<GridIcon class="w-4 h-4 mr-2" />
|
||||
网格视图
|
||||
</Button>
|
||||
<Button
|
||||
:type="viewMode === 'table' ? 'primary' : 'default'"
|
||||
@click="toggleViewMode"
|
||||
>
|
||||
<ListIcon class="w-4 h-4 mr-2" />
|
||||
列表视图
|
||||
</Button>
|
||||
<span class="text-gray-500">
|
||||
共 {{ total }} 个商品
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 列表视图 -->
|
||||
<div v-if="viewMode === 'table'" class="bg-white rounded-lg shadow-sm">
|
||||
<Table
|
||||
:columns="columns"
|
||||
:data-source="productList"
|
||||
:loading="loading"
|
||||
:pagination="{
|
||||
current: current,
|
||||
pageSize: pageSize,
|
||||
total: total,
|
||||
showSizeChanger: true,
|
||||
showQuickJumper: true,
|
||||
showTotal: (total, range) => `第 ${range[0]}-${range[1]} 条/共 ${total} 条`,
|
||||
}"
|
||||
row-key="id"
|
||||
@change="handleTableChange"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<!-- 网格视图 -->
|
||||
<div v-if="viewMode === 'grid'" class="bg-white rounded-lg shadow-sm p-4">
|
||||
<Row :gutter="[16, 16]">
|
||||
<Col
|
||||
v-for="product in productList"
|
||||
:key="product.id"
|
||||
:xs="12"
|
||||
:sm="8"
|
||||
:md="6"
|
||||
:lg="4"
|
||||
:xl="3"
|
||||
>
|
||||
<ProductCard
|
||||
:product="product"
|
||||
@edit="handleEdit"
|
||||
@delete="handleDelete"
|
||||
@manage-specs="handleManageSpecs"
|
||||
@view-details="handleViewDetails"
|
||||
/>
|
||||
</Col>
|
||||
</Row>
|
||||
|
||||
<!-- 分页 -->
|
||||
<div class="mt-4 flex justify-center">
|
||||
<Pagination
|
||||
v-model:current="current"
|
||||
v-model:page-size="pageSize"
|
||||
:total="total"
|
||||
:show-size-changer="true"
|
||||
:show-quick-jumper="true"
|
||||
:show-total="(total, range) => `第 ${range[0]}-${range[1]} 条/共 ${total} 条`"
|
||||
@change="handleTableChange"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 商品表单模态框 -->
|
||||
<ProductFormModal
|
||||
ref="productFormModalRef"
|
||||
@success="handleModalSuccess"
|
||||
/>
|
||||
|
||||
<!-- 商品规格管理模态框 -->
|
||||
<ProductSpecModal
|
||||
ref="productSpecModalRef"
|
||||
@success="handleSpecModalSuccess"
|
||||
/>
|
||||
</Page>
|
||||
</template>
|
||||
|
||||
<script lang="ts" setup>
|
||||
import { h, ref } from 'vue';
|
||||
|
||||
import { Page } from '@vben/common-ui';
|
||||
import { Button, Form, Input, InputNumber, Modal, Table, message } from 'ant-design-vue';
|
||||
import { Button, Input, InputNumber, Table, message, Row, Col, Pagination, Form, Space, Tag } from 'ant-design-vue';
|
||||
import { createIconifyIcon } from '@vben/icons';
|
||||
|
||||
import {
|
||||
createProductApi,
|
||||
deleteProductApi,
|
||||
getProductPageApi,
|
||||
updateProductApi,
|
||||
type ProductVO,
|
||||
type ProductDTO
|
||||
} from '#/api/ecommerce';
|
||||
import type { PageQueryDTO } from '#/types/common';
|
||||
import ProductFormModal from './components/ProductFormModal.vue';
|
||||
// @ts-ignore
|
||||
import ProductSpecModal from './components/ProductSpecModal.vue';
|
||||
import ProductCard from './components/ProductCard.vue';
|
||||
|
||||
// 创建图标组件
|
||||
const GridIcon = createIconifyIcon('lucide:grid-3x3');
|
||||
const ListIcon = createIconifyIcon('lucide:list');
|
||||
const PlusIcon = createIconifyIcon('lucide:plus');
|
||||
|
||||
defineOptions({ name: 'ProductManagement' });
|
||||
|
||||
@@ -22,11 +188,9 @@ const productList = ref<ProductVO.ProductInfoVO[]>([]);
|
||||
const total = ref(0);
|
||||
const current = ref(1);
|
||||
const pageSize = ref(10);
|
||||
const modalVisible = ref(false);
|
||||
const modalTitle = ref('');
|
||||
const isEdit = ref(false);
|
||||
const currentProductId = ref<number | null>(null);
|
||||
const formRef = ref();
|
||||
const viewMode = ref<'table' | 'grid'>('grid');
|
||||
const productFormModalRef = ref();
|
||||
const productSpecModalRef = ref();
|
||||
|
||||
// 搜索表单
|
||||
const searchForm = ref({
|
||||
@@ -37,15 +201,6 @@ const searchForm = ref({
|
||||
maxCostPrice: undefined as number | undefined,
|
||||
});
|
||||
|
||||
// 表单数据
|
||||
const formData = ref<ProductDTO.CreateProductDTO>({
|
||||
name: '',
|
||||
specCode: '',
|
||||
skuCode: '',
|
||||
mainImage: '',
|
||||
costPrice: 0,
|
||||
});
|
||||
|
||||
// 表格列配置
|
||||
const columns = [
|
||||
{
|
||||
@@ -54,18 +209,6 @@ const columns = [
|
||||
key: 'name',
|
||||
width: 200,
|
||||
},
|
||||
{
|
||||
title: '商品编码',
|
||||
dataIndex: 'specCode',
|
||||
key: 'specCode',
|
||||
width: 150,
|
||||
},
|
||||
{
|
||||
title: '商品SKU',
|
||||
dataIndex: 'skuCode',
|
||||
key: 'skuCode',
|
||||
width: 180,
|
||||
},
|
||||
{
|
||||
title: '主图',
|
||||
dataIndex: 'mainImage',
|
||||
@@ -83,12 +226,13 @@ const columns = [
|
||||
},
|
||||
},
|
||||
{
|
||||
title: '成本价',
|
||||
dataIndex: 'costPrice',
|
||||
key: 'costPrice',
|
||||
width: 120,
|
||||
title: '规格数量',
|
||||
dataIndex: 'specs',
|
||||
key: 'specs',
|
||||
width: 100,
|
||||
customRender: ({ record }: { record: ProductVO.ProductInfoVO }) => {
|
||||
return h('span', { style: { color: '#f50' } }, `¥${record.costPrice.toFixed(2)}`);
|
||||
const specCount = record.specs?.length || 0;
|
||||
return h(Tag, { color: specCount > 0 ? 'blue' : 'default' }, () => `${specCount} 个规格`);
|
||||
},
|
||||
},
|
||||
{
|
||||
@@ -114,6 +258,10 @@ const columns = [
|
||||
size: 'small',
|
||||
onClick: () => handleEdit(record),
|
||||
}, { default: () => '编辑' }),
|
||||
h(Button, {
|
||||
size: 'small',
|
||||
onClick: () => handleManageSpecs(record),
|
||||
}, { default: () => '规格' }),
|
||||
h(Button, {
|
||||
size: 'small',
|
||||
danger: true,
|
||||
@@ -176,34 +324,19 @@ function handleTableChange(pagination: any) {
|
||||
fetchProductList();
|
||||
}
|
||||
|
||||
// 切换视图模式
|
||||
function toggleViewMode() {
|
||||
viewMode.value = viewMode.value === 'table' ? 'grid' : 'table';
|
||||
}
|
||||
|
||||
// 处理添加商品
|
||||
function handleAdd() {
|
||||
modalTitle.value = '添加商品';
|
||||
isEdit.value = false;
|
||||
currentProductId.value = null;
|
||||
formData.value = {
|
||||
name: '',
|
||||
specCode: '',
|
||||
skuCode: '',
|
||||
mainImage: '',
|
||||
costPrice: 0,
|
||||
};
|
||||
modalVisible.value = true;
|
||||
productFormModalRef.value?.open(false);
|
||||
}
|
||||
|
||||
// 处理编辑商品
|
||||
function handleEdit(record: ProductVO.ProductInfoVO) {
|
||||
modalTitle.value = '编辑商品';
|
||||
isEdit.value = true;
|
||||
currentProductId.value = record.id;
|
||||
formData.value = {
|
||||
name: record.name,
|
||||
specCode: record.specCode || '',
|
||||
skuCode: record.skuCode || '',
|
||||
mainImage: record.mainImage || '',
|
||||
costPrice: record.costPrice,
|
||||
};
|
||||
modalVisible.value = true;
|
||||
productFormModalRef.value?.open(true, record);
|
||||
}
|
||||
|
||||
// 处理删除商品
|
||||
@@ -218,158 +351,39 @@ async function handleDelete(record: ProductVO.ProductInfoVO) {
|
||||
}
|
||||
}
|
||||
|
||||
// 处理表单提交
|
||||
async function handleSubmit() {
|
||||
try {
|
||||
await formRef.value?.validate();
|
||||
|
||||
if (isEdit.value && currentProductId.value) {
|
||||
await updateProductApi(currentProductId.value, formData.value);
|
||||
message.success('更新成功');
|
||||
} else {
|
||||
await createProductApi(formData.value);
|
||||
message.success('创建成功');
|
||||
}
|
||||
|
||||
modalVisible.value = false;
|
||||
await fetchProductList();
|
||||
} catch (error) {
|
||||
console.error('保存商品失败:', error);
|
||||
message.error('保存商品失败');
|
||||
}
|
||||
// 处理管理规格
|
||||
function handleManageSpecs(record: ProductVO.ProductInfoVO) {
|
||||
productSpecModalRef.value?.open(record);
|
||||
}
|
||||
|
||||
// 处理查看详情
|
||||
function handleViewDetails() {
|
||||
// TODO: 实现查看详情功能
|
||||
message.info('查看详情功能待实现');
|
||||
}
|
||||
|
||||
// 处理模态框成功
|
||||
function handleModalSuccess() {
|
||||
fetchProductList();
|
||||
}
|
||||
|
||||
// 处理规格模态框成功
|
||||
function handleSpecModalSuccess() {
|
||||
// 规格管理成功后可能需要刷新商品列表
|
||||
fetchProductList();
|
||||
}
|
||||
|
||||
|
||||
// 初始化
|
||||
fetchProductList();
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<Page title="商品管理">
|
||||
<template #description>
|
||||
<div class="text-foreground/80 mt-2">
|
||||
管理商品信息,包括商品基本信息、编码、SKU和价格设置
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<template #extra>
|
||||
<Button type="primary" @click="handleAdd">
|
||||
添加商品
|
||||
</Button>
|
||||
</template>
|
||||
|
||||
<!-- 搜索表单 -->
|
||||
<div class="mb-4 p-4 bg-gray-50 rounded-lg">
|
||||
<div style="display: flex; gap: 16px; align-items: center; flex-wrap: wrap;">
|
||||
<div>
|
||||
<label style="display: block; margin-bottom: 4px; font-weight: 500;">商品名称</label>
|
||||
<Input
|
||||
v-model:value="searchForm.name"
|
||||
placeholder="请输入商品名称"
|
||||
style="width: 200px;"
|
||||
@press-enter="handleSearch"
|
||||
/>
|
||||
</div>
|
||||
<div>
|
||||
<label style="display: block; margin-bottom: 4px; font-weight: 500;">商品编码</label>
|
||||
<Input
|
||||
v-model:value="searchForm.specCode"
|
||||
placeholder="请输入商品编码"
|
||||
style="width: 150px;"
|
||||
@press-enter="handleSearch"
|
||||
/>
|
||||
</div>
|
||||
<div>
|
||||
<label style="display: block; margin-bottom: 4px; font-weight: 500;">商品SKU</label>
|
||||
<Input
|
||||
v-model:value="searchForm.skuCode"
|
||||
placeholder="请输入商品SKU"
|
||||
style="width: 150px;"
|
||||
@press-enter="handleSearch"
|
||||
/>
|
||||
</div>
|
||||
<div>
|
||||
<label style="display: block; margin-bottom: 4px; font-weight: 500;">成本价范围</label>
|
||||
<div style="display: flex; gap: 8px; align-items: center;">
|
||||
<InputNumber
|
||||
v-model:value="searchForm.minCostPrice"
|
||||
placeholder="最小值"
|
||||
style="width: 100px;"
|
||||
:min="0"
|
||||
:precision="2"
|
||||
/>
|
||||
<span>-</span>
|
||||
<InputNumber
|
||||
v-model:value="searchForm.maxCostPrice"
|
||||
placeholder="最大值"
|
||||
style="width: 100px;"
|
||||
:min="0"
|
||||
:precision="2"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
<div style="display: flex; gap: 8px; align-items: end;">
|
||||
<Button type="primary" @click="handleSearch">搜索</Button>
|
||||
<Button @click="handleReset">重置</Button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 数据表格 -->
|
||||
<Table
|
||||
:columns="columns"
|
||||
:data-source="productList"
|
||||
:loading="loading"
|
||||
:pagination="{
|
||||
current: current,
|
||||
pageSize: pageSize,
|
||||
total: total,
|
||||
showSizeChanger: true,
|
||||
showQuickJumper: true,
|
||||
showTotal: (total, range) => `第 ${range[0]}-${range[1]} 条/共 ${total} 条`,
|
||||
}"
|
||||
row-key="id"
|
||||
@change="handleTableChange"
|
||||
/>
|
||||
|
||||
<!-- 添加/编辑模态框 -->
|
||||
<Modal
|
||||
v-model:open="modalVisible"
|
||||
:title="modalTitle"
|
||||
width="600px"
|
||||
@ok="handleSubmit"
|
||||
>
|
||||
<Form
|
||||
ref="formRef"
|
||||
:model="formData"
|
||||
:label-col="{ span: 6 }"
|
||||
:wrapper-col="{ span: 18 }"
|
||||
>
|
||||
<Form.Item label="商品名称" name="name" :rules="[{ required: true, message: '请输入商品名称' }]">
|
||||
<Input v-model:value="formData.name" placeholder="请输入商品名称" />
|
||||
</Form.Item>
|
||||
|
||||
<Form.Item label="商品编码" name="specCode">
|
||||
<Input v-model:value="formData.specCode" placeholder="请输入商品编码" />
|
||||
</Form.Item>
|
||||
|
||||
<Form.Item label="商品SKU" name="skuCode">
|
||||
<Input v-model:value="formData.skuCode" placeholder="请输入商品SKU" />
|
||||
</Form.Item>
|
||||
|
||||
<Form.Item label="主图URL" name="mainImage">
|
||||
<Input v-model:value="formData.mainImage" placeholder="请输入主图URL" />
|
||||
</Form.Item>
|
||||
|
||||
<Form.Item label="成本价" name="costPrice" :rules="[{ required: true, message: '请输入成本价' }]">
|
||||
<InputNumber
|
||||
v-model:value="formData.costPrice"
|
||||
placeholder="请输入成本价"
|
||||
style="width: 100%;"
|
||||
:min="0"
|
||||
:precision="2"
|
||||
/>
|
||||
</Form.Item>
|
||||
</Form>
|
||||
</Modal>
|
||||
</Page>
|
||||
</template>
|
||||
<style scoped>
|
||||
.line-clamp-2 {
|
||||
display: -webkit-box;
|
||||
-webkit-line-clamp: 2;
|
||||
line-clamp: 2;
|
||||
-webkit-box-orient: vertical;
|
||||
overflow: hidden;
|
||||
}
|
||||
</style>
|
||||
|
||||
+305
@@ -0,0 +1,305 @@
|
||||
<template>
|
||||
<Modal
|
||||
:open="visible"
|
||||
:title="modalTitle"
|
||||
width="600px"
|
||||
:confirm-loading="loading"
|
||||
@ok="handleSubmit"
|
||||
@cancel="handleCancel"
|
||||
>
|
||||
<Form
|
||||
ref="formRef"
|
||||
:model="formData"
|
||||
:label-col="{ span: 6 }"
|
||||
:wrapper-col="{ span: 18 }"
|
||||
>
|
||||
<Form.Item label="店铺Logo" name="logoImageId">
|
||||
<div style="display: flex; align-items: center; gap: 16px;">
|
||||
|
||||
<!-- Logo预览区域 -->
|
||||
<div style="position: relative; display: inline-block;">
|
||||
<!-- 有图片时显示图片 -->
|
||||
<img
|
||||
v-if="currentLogoUrl && !imageLoadError"
|
||||
:src="currentLogoUrl"
|
||||
alt="店铺Logo"
|
||||
style="width: 80px; height: 80px; object-fit: cover; border-radius: 8px; border: 1px solid #d9d9d9; background-color: #f5f5f5;"
|
||||
@error="handleImageError"
|
||||
/>
|
||||
<!-- 无图片或加载失败时显示空状态 -->
|
||||
<div
|
||||
v-else
|
||||
style="width: 80px; height: 80px; border: 1px dashed #d9d9d9; border-radius: 8px; background-color: #fafafa; display: flex; align-items: center; justify-content: center; color: #999; font-size: 12px; text-align: center; line-height: 1.2;">
|
||||
<div style="display: flex; flex-direction: column; align-items: center; justify-content: center;">
|
||||
<CameraIcon style="font-size: 16px; margin-bottom: 4px;" />
|
||||
<div>上传Logo</div>
|
||||
</div>
|
||||
</div>
|
||||
<!-- 点击更换遮罩 -->
|
||||
<div
|
||||
style="position: absolute; top: 0; left: 0; right: 0; bottom: 0; background: rgba(0,0,0,0.6); border-radius: 8px; display: flex; align-items: center; justify-content: center; opacity: 0; transition: opacity 0.3s; cursor: pointer;"
|
||||
@mouseenter="($event.target as HTMLElement).style.opacity = '1'"
|
||||
@mouseleave="($event.target as HTMLElement).style.opacity = '0'"
|
||||
@click="selectLogoFile">
|
||||
<div style="color: white; text-align: center; display: flex; flex-direction: column; align-items: center; justify-content: center;">
|
||||
<CameraIcon style="font-size: 16px; margin-bottom: 4px;" />
|
||||
<div style="font-size: 12px;">{{ currentLogoUrl && !imageLoadError ? '更换Logo' : '上传Logo' }}</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 重置按钮 -->
|
||||
<div v-if="newLogoFile">
|
||||
<Button size="small" @click="removeNewLogo">重置</Button>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
</Form.Item>
|
||||
|
||||
<Form.Item label="店铺名称" name="name" :rules="[{ required: true, message: '请输入店铺名称' }]">
|
||||
<Input v-model:value="formData.name" placeholder="请输入店铺名称" />
|
||||
</Form.Item>
|
||||
|
||||
<Form.Item label="平台" name="platform">
|
||||
<Select
|
||||
v-model:value="formData.platform"
|
||||
placeholder="请选择平台"
|
||||
:options="platformOptions"
|
||||
allow-clear
|
||||
/>
|
||||
</Form.Item>
|
||||
</Form>
|
||||
</Modal>
|
||||
</template>
|
||||
|
||||
<script lang="ts" setup>
|
||||
import { computed, ref, onUnmounted } from 'vue';
|
||||
import { Form, Input, Modal, Select, Button, message } from 'ant-design-vue';
|
||||
import { createIconifyIcon } from '@vben/icons';
|
||||
import {
|
||||
uploadShopLogoApi,
|
||||
createShopApi,
|
||||
updateShopApi,
|
||||
type ShopVO,
|
||||
type ShopDTO
|
||||
} from '#/api/ecommerce';
|
||||
|
||||
interface Emits {
|
||||
(e: 'success'): void;
|
||||
}
|
||||
|
||||
const emit = defineEmits<Emits>();
|
||||
|
||||
// 创建相机图标组件
|
||||
const CameraIcon = createIconifyIcon('mdi:camera');
|
||||
|
||||
// 内部状态管理
|
||||
const visible = ref(false);
|
||||
const isEdit = ref(false);
|
||||
const currentShopData = ref<ShopVO.ShopInfoVO | Partial<ShopVO.ShopInfoVO> | null>(null);
|
||||
|
||||
// 响应式数据
|
||||
const formRef = ref();
|
||||
const loading = ref(false);
|
||||
const newLogoFile = ref<File | null>(null); // 新选择的logo文件
|
||||
const originalLogoUrl = ref(''); // 原始logo URL
|
||||
const imageLoadError = ref(false); // 图片加载错误状态
|
||||
|
||||
// 表单数据
|
||||
const formData = ref({
|
||||
name: '',
|
||||
platform: '',
|
||||
logoImageId: undefined as number | undefined,
|
||||
});
|
||||
|
||||
// 平台选项
|
||||
const platformOptions = [
|
||||
{ label: '淘宝', value: '淘宝' },
|
||||
{ label: '京东', value: '京东' },
|
||||
{ label: '拼多多', value: '拼多多' },
|
||||
{ label: '天猫', value: '天猫' },
|
||||
];
|
||||
|
||||
// 模态框标题
|
||||
const modalTitle = computed(() => isEdit.value ? '编辑店铺' : '添加店铺');
|
||||
|
||||
// 当前显示的logo URL(优先显示新选择的)
|
||||
const currentLogoUrl = computed(() => {
|
||||
if (newLogoFile.value) {
|
||||
return URL.createObjectURL(newLogoFile.value);
|
||||
}
|
||||
return originalLogoUrl.value;
|
||||
});
|
||||
|
||||
// 初始化表单数据
|
||||
function initFormData() {
|
||||
if (isEdit.value && currentShopData.value) {
|
||||
// 编辑模式
|
||||
formData.value = {
|
||||
name: currentShopData.value.name || '',
|
||||
platform: currentShopData.value.platform || '',
|
||||
logoImageId: undefined,
|
||||
};
|
||||
|
||||
// 设置原始Logo URL
|
||||
originalLogoUrl.value = currentShopData.value.logo || '';
|
||||
} else {
|
||||
// 创建模式 - 如果有传入的shopData,则使用它来初始化表单
|
||||
formData.value = {
|
||||
name: currentShopData.value?.name || '',
|
||||
platform: currentShopData.value?.platform || '',
|
||||
logoImageId: undefined,
|
||||
};
|
||||
originalLogoUrl.value = currentShopData.value?.logo || '';
|
||||
}
|
||||
|
||||
// 重置新选择的logo和图片加载状态
|
||||
newLogoFile.value = null;
|
||||
imageLoadError.value = false;
|
||||
}
|
||||
|
||||
// 重置表单(在open方法中调用)
|
||||
function resetForm() {
|
||||
formRef.value?.resetFields();
|
||||
newLogoFile.value = null;
|
||||
originalLogoUrl.value = '';
|
||||
imageLoadError.value = false;
|
||||
}
|
||||
|
||||
// 处理提交
|
||||
async function handleSubmit() {
|
||||
try {
|
||||
await formRef.value?.validate();
|
||||
loading.value = true;
|
||||
|
||||
// 准备提交数据
|
||||
const submitData = { ...formData.value };
|
||||
|
||||
// 如果有新选择的logo,先上传获取ID
|
||||
if (newLogoFile.value) {
|
||||
const logoImageId = await uploadLogoAndGetId();
|
||||
if (logoImageId) {
|
||||
submitData.logoImageId = logoImageId;
|
||||
}
|
||||
} else if (!isEdit.value) {
|
||||
// 创建模式且没有选择Logo,移除logoImageId字段
|
||||
delete submitData.logoImageId;
|
||||
}
|
||||
|
||||
// 调用API
|
||||
if (isEdit.value && currentShopData.value && 'id' in currentShopData.value && currentShopData.value.id) {
|
||||
await updateShopApi(currentShopData.value.id, submitData as ShopDTO.UpdateShopDTO);
|
||||
message.success('更新成功');
|
||||
} else {
|
||||
await createShopApi(submitData as ShopDTO.CreateShopDTO);
|
||||
message.success('创建成功');
|
||||
}
|
||||
|
||||
visible.value = false;
|
||||
emit('success');
|
||||
} catch (error) {
|
||||
console.error('保存店铺失败:', error);
|
||||
message.error('保存店铺失败');
|
||||
} finally {
|
||||
loading.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
// 处理取消
|
||||
function handleCancel() {
|
||||
visible.value = false;
|
||||
}
|
||||
|
||||
// 暴露给父组件的方法
|
||||
function open(editMode: boolean = false, shopData?: ShopVO.ShopInfoVO | Partial<ShopVO.ShopInfoVO> | null) {
|
||||
isEdit.value = editMode;
|
||||
currentShopData.value = shopData || null;
|
||||
|
||||
resetForm(); // 先重置表单
|
||||
initFormData(); // 再初始化数据
|
||||
|
||||
visible.value = true;
|
||||
}
|
||||
|
||||
// 暴露方法给父组件
|
||||
defineExpose({
|
||||
open,
|
||||
});
|
||||
|
||||
// 选择logo文件
|
||||
function selectLogoFile() {
|
||||
const input = document.createElement('input');
|
||||
input.type = 'file';
|
||||
input.accept = 'image/*';
|
||||
input.style.display = 'none';
|
||||
document.body.appendChild(input);
|
||||
|
||||
input.onchange = (e) => {
|
||||
const file = (e.target as HTMLInputElement).files?.[0];
|
||||
if (file) {
|
||||
// 验证文件
|
||||
const isImage = file.type.startsWith('image/');
|
||||
if (!isImage) {
|
||||
message.error('只能上传图片文件');
|
||||
document.body.removeChild(input);
|
||||
return;
|
||||
}
|
||||
|
||||
const isLt2M = file.size / 1024 / 1024 < 2;
|
||||
if (!isLt2M) {
|
||||
message.error('图片大小不能超过 2MB');
|
||||
document.body.removeChild(input);
|
||||
return;
|
||||
}
|
||||
|
||||
// 设置新选择的文件
|
||||
newLogoFile.value = file;
|
||||
imageLoadError.value = false; // 重置图片加载错误状态
|
||||
message.success('Logo选择成功');
|
||||
}
|
||||
|
||||
document.body.removeChild(input);
|
||||
};
|
||||
|
||||
input.click();
|
||||
}
|
||||
|
||||
// 重置新选择的logo
|
||||
function removeNewLogo() {
|
||||
newLogoFile.value = null;
|
||||
imageLoadError.value = false; // 重置图片加载错误状态
|
||||
message.success('已重置logo');
|
||||
}
|
||||
|
||||
// 上传Logo并获取图片ID
|
||||
async function uploadLogoAndGetId(): Promise<number | undefined> {
|
||||
if (!newLogoFile.value) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await uploadShopLogoApi(newLogoFile.value);
|
||||
console.log('Logo上传成功:', response);
|
||||
return response.id;
|
||||
} catch (error) {
|
||||
console.error('Logo上传失败:', error);
|
||||
message.error('Logo上传失败');
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
// 图片加载错误处理
|
||||
function handleImageError(event: Event) {
|
||||
console.error('图片加载失败:', event);
|
||||
imageLoadError.value = true;
|
||||
message.error('图片加载失败,请重新选择');
|
||||
}
|
||||
|
||||
// 组件卸载时清理
|
||||
onUnmounted(() => {
|
||||
if (newLogoFile.value) {
|
||||
const url = URL.createObjectURL(newLogoFile.value);
|
||||
URL.revokeObjectURL(url);
|
||||
}
|
||||
});
|
||||
</script>
|
||||
@@ -2,7 +2,7 @@
|
||||
import { h, ref } from 'vue';
|
||||
|
||||
import { Page } from '@vben/common-ui';
|
||||
import { Button, Form, Input, Modal, Select, Table, message } from 'ant-design-vue';
|
||||
import { Button, Input, Select, Table, message } from 'ant-design-vue';
|
||||
|
||||
import {
|
||||
createShopApi,
|
||||
@@ -13,6 +13,7 @@ import {
|
||||
type ShopDTO
|
||||
} from '#/api/ecommerce';
|
||||
import type { PageQueryDTO } from '#/types/common';
|
||||
import ShopFormModal from './components/ShopFormModal.vue';
|
||||
|
||||
defineOptions({ name: 'ShopManagement' });
|
||||
|
||||
@@ -22,11 +23,7 @@ const shopList = ref<ShopVO.ShopInfoVO[]>([]);
|
||||
const total = ref(0);
|
||||
const current = ref(1);
|
||||
const pageSize = ref(10);
|
||||
const modalVisible = ref(false);
|
||||
const modalTitle = ref('');
|
||||
const isEdit = ref(false);
|
||||
const currentShopId = ref<number | null>(null);
|
||||
const formRef = ref();
|
||||
const shopFormModalRef = ref();
|
||||
|
||||
// 搜索表单
|
||||
const searchForm = ref({
|
||||
@@ -34,12 +31,7 @@ const searchForm = ref({
|
||||
platform: undefined as string | undefined,
|
||||
});
|
||||
|
||||
// 表单数据
|
||||
const formData = ref<ShopDTO.CreateShopDTO>({
|
||||
name: '',
|
||||
logo: '',
|
||||
platform: '',
|
||||
});
|
||||
// 表单数据(已移至ShopFormModal组件)
|
||||
|
||||
// 平台选项
|
||||
const platformOptions = [
|
||||
@@ -160,28 +152,12 @@ function handleTableChange(pagination: any) {
|
||||
|
||||
// 处理添加店铺
|
||||
function handleAdd() {
|
||||
modalTitle.value = '添加店铺';
|
||||
isEdit.value = false;
|
||||
currentShopId.value = null;
|
||||
formData.value = {
|
||||
name: '',
|
||||
logo: '',
|
||||
platform: '',
|
||||
};
|
||||
modalVisible.value = true;
|
||||
shopFormModalRef.value?.open(false);
|
||||
}
|
||||
|
||||
// 处理编辑店铺
|
||||
function handleEdit(record: ShopVO.ShopInfoVO) {
|
||||
modalTitle.value = '编辑店铺';
|
||||
isEdit.value = true;
|
||||
currentShopId.value = record.id;
|
||||
formData.value = {
|
||||
name: record.name,
|
||||
logo: record.logo || '',
|
||||
platform: record.platform || '',
|
||||
};
|
||||
modalVisible.value = true;
|
||||
shopFormModalRef.value?.open(true, record);
|
||||
}
|
||||
|
||||
// 处理删除店铺
|
||||
@@ -196,25 +172,9 @@ async function handleDelete(record: ShopVO.ShopInfoVO) {
|
||||
}
|
||||
}
|
||||
|
||||
// 处理表单提交
|
||||
async function handleSubmit() {
|
||||
try {
|
||||
await formRef.value?.validate();
|
||||
|
||||
if (isEdit.value && currentShopId.value) {
|
||||
await updateShopApi(currentShopId.value, formData.value);
|
||||
message.success('更新成功');
|
||||
} else {
|
||||
await createShopApi(formData.value);
|
||||
message.success('创建成功');
|
||||
}
|
||||
|
||||
modalVisible.value = false;
|
||||
await fetchShopList();
|
||||
} catch (error) {
|
||||
console.error('保存店铺失败:', error);
|
||||
message.error('保存店铺失败');
|
||||
}
|
||||
// 处理模态框成功
|
||||
function handleModalSuccess() {
|
||||
fetchShopList();
|
||||
}
|
||||
|
||||
// 初始化
|
||||
@@ -281,36 +241,10 @@ fetchShopList();
|
||||
@change="handleTableChange"
|
||||
/>
|
||||
|
||||
<!-- 添加/编辑模态框 -->
|
||||
<Modal
|
||||
v-model:open="modalVisible"
|
||||
:title="modalTitle"
|
||||
width="600px"
|
||||
@ok="handleSubmit"
|
||||
>
|
||||
<Form
|
||||
ref="formRef"
|
||||
:model="formData"
|
||||
:label-col="{ span: 6 }"
|
||||
:wrapper-col="{ span: 18 }"
|
||||
>
|
||||
<Form.Item label="店铺名称" name="name" :rules="[{ required: true, message: '请输入店铺名称' }]">
|
||||
<Input v-model:value="formData.name" placeholder="请输入店铺名称" />
|
||||
</Form.Item>
|
||||
|
||||
<Form.Item label="平台" name="platform">
|
||||
<Select
|
||||
v-model:value="formData.platform"
|
||||
placeholder="请选择平台"
|
||||
:options="platformOptions"
|
||||
allow-clear
|
||||
/>
|
||||
</Form.Item>
|
||||
|
||||
<Form.Item label="Logo URL" name="logo">
|
||||
<Input v-model:value="formData.logo" placeholder="请输入Logo URL" />
|
||||
</Form.Item>
|
||||
</Form>
|
||||
</Modal>
|
||||
<!-- 店铺表单模态框 -->
|
||||
<ShopFormModal
|
||||
ref="shopFormModalRef"
|
||||
@success="handleModalSuccess"
|
||||
/>
|
||||
</Page>
|
||||
</template>
|
||||
</template>
|
||||
|
||||
Reference in New Issue
Block a user