初始化项目

This commit is contained in:
ah--desktop
2025-09-10 00:33:25 +08:00
commit a305dcdc3f
1406 changed files with 114976 additions and 0 deletions
@@ -0,0 +1,159 @@
# 电商数据中台系统
## 功能概述
这是一个基于Vue Vben Admin框架开发的电商数据中台系统,主要用于处理电商店铺的营业数据。
## 主要功能
### 1. 数据导入 (Data Import)
- **CSV文件上传**: 支持拖拽或点击上传CSV格式的营业数据文件
- **数据预览**: 上传后可以预览前10条数据,了解数据结构
- **字段映射**: 将CSV字段映射到标准字段,支持自定义字段名
- **数据清洗**: 自动清洗和格式化数据(去除空格、数字格式化等)
- **JSON导出**: 将处理后的数据导出为JSON格式
### 2. 数据分析 (Data Analysis)
- 数据统计和分析功能(开发中)
### 3. 店铺管理 (Shop Management)
- 店铺信息管理功能(开发中)
## 使用流程
### 数据导入流程
1. **上传CSV文件**
- 点击"电商数据中台" -> "数据导入"
- 拖拽或点击上传CSV文件
- 系统会自动解析和验证数据
2. **预览数据**
- 查看解析后的数据预览
- 确认数据格式正确
3. **配置字段映射**
- 点击"配置字段映射"按钮
- 为每个CSV字段选择对应的标准字段
- 可选择自定义字段名称
- 实时预览映射后的数据
4. **导出JSON**
- 确认字段映射无误后,点击"导出JSON"
- 系统会生成包含元数据的JSON文件
- 文件会保存到本地下载目录
## 支持的数据格式
### CSV文件要求
- 文件格式:`.csv`
- 文件大小:最大10MB
- 编码格式:UTF-8
- 必要字段:
- 商品
- 订单号
- 订单状态
- 商品数量(件)
- 商家实收金额(元)
### 标准字段映射
系统支持以下标准字段:
- `product_name`: 商品名称
- `order_id`: 订单号
- `order_status`: 订单状态
- `quantity`: 商品数量
- `product_id`: 商品ID
- `product_spec`: 商品规格
- `after_sale_status`: 售后状态
- `amount`: 实收金额
- `shop_name`: 店铺名称
- `order_date`: 订单日期
- `customer_id`: 客户ID
- `payment_method`: 支付方式
- `delivery_address`: 收货地址
- `remark`: 备注
## 导出的JSON格式
```json
{
"metadata": {
"source": "CSV导入",
"totalRecords": 100,
"exportTime": "2024-01-15T10:30:00.000Z",
"fieldMappings": {
"商品": "product_name",
"订单号": "order_id"
},
"customFieldNames": {},
"shopName": "店铺名称"
},
"data": [
{
"product_name": "商品名称",
"order_id": "订单号",
"order_status": "订单状态"
}
]
}
```
## 技术栈
- **前端框架**: Vue 3 + TypeScript
- **UI组件库**: Ant Design Vue
- **构建工具**: Vite
- **CSV解析**: PapaParse
- **状态管理**: Pinia
- **路由**: Vue Router
## 开发说明
### 项目结构
```
src/views/ecommerce/
├── data-import/
│ ├── components/
│ │ └── FieldMapping.vue # 字段映射组件
│ └── index.vue # 数据导入主页面
├── data-analysis/
│ └── index.vue # 数据分析页面
├── shop-management/
│ └── index.vue # 店铺管理页面
└── README.md # 说明文档
```
### 工具函数
`src/utils/csv-utils.ts` 提供了以下工具函数:
- `parseCSV()`: 解析CSV文件
- `transformDataWithMapping()`: 根据字段映射转换数据
- `generateExportData()`: 生成导出数据
- `downloadJSON()`: 下载JSON文件
- `validateCSVData()`: 验证CSV数据格式
- `cleanAndFormatData()`: 数据清洗和格式化
## 注意事项
1. 确保CSV文件格式正确,包含必要的字段
2. 字段映射时,建议保留所有重要字段
3. 导出的JSON文件包含完整的元数据,便于后续处理
4. 系统会自动从文件名提取店铺名称
5. 数据清洗会自动处理数字字段和去除空格
## 后续开发计划
- [ ] 数据分析功能
- [ ] 店铺管理功能
- [ ] 数据可视化图表
- [ ] 批量数据处理
- [ ] 数据导入历史记录
- [ ] 数据导出模板管理
@@ -0,0 +1,15 @@
<template>
<div class="p-6">
<VbenCard title="数据分析">
<div class="text-center py-12">
<VbenIcon icon="lucide:bar-chart-3" class="w-16 h-16 mx-auto text-gray-400 mb-4" />
<h3 class="text-lg font-medium text-gray-700 mb-2">数据分析功能</h3>
<p class="text-gray-500">此功能正在开发中,敬请期待...</p>
</div>
</VbenCard>
</div>
</template>
<script setup lang="ts">
// 数据分析页面占位
</script>
@@ -0,0 +1,209 @@
<template>
<div class="space-y-6">
<!-- 字段映射说明 -->
<div class="bg-blue-50 border border-blue-200 rounded-lg p-4">
<div class="flex items-start space-x-2">
<VbenIcon icon="lucide:info" class="w-5 h-5 text-blue-500 mt-0.5" />
<div>
<p class="font-medium text-blue-800">字段映射说明</p>
<p class="text-sm text-blue-600 mt-1">
请为每个CSV字段选择对应的标准字段,未映射的字段将被忽略。您也可以自定义字段名称。
</p>
</div>
</div>
</div>
<!-- 字段映射表格 -->
<div class="border rounded-lg">
<VbenTable
:columns="mappingColumns"
:data-source="mappingData"
:pagination="false"
size="small"
/>
</div>
<!-- 数据预览 -->
<div v-if="previewData.length > 0">
<h4 class="text-md font-medium mb-3">映射后数据预览</h4>
<VbenTable
:columns="previewColumns"
:data-source="previewData.slice(0, 5)"
:pagination="false"
size="small"
:scroll="{ x: 800 }"
/>
<p class="text-sm text-gray-500 mt-2">
显示前5条映射后的数据,共 {{ previewData.length }} 条记录
</p>
</div>
<!-- 操作按钮 -->
<div class="flex justify-end space-x-3 pt-4 border-t">
<VbenButton @click="$emit('cancel')">
取消
</VbenButton>
<VbenButton @click="handleExport" type="primary">
导出JSON
</VbenButton>
</div>
</div>
</template>
<script setup lang="ts">
import { ref, computed, watch } from 'vue'
import { message } from 'ant-design-vue'
import { transformDataWithMapping, generateExportData, downloadJSON } from '#/utils/csv-utils'
// Props
interface Props {
csvHeaders: string[]
csvData: any[]
}
const props = defineProps<Props>()
// Emits
const emit = defineEmits<{
confirm: [data: any]
cancel: []
}>()
// 响应式数据
const fieldMappings = ref<Record<string, string>>({})
const customFieldNames = ref<Record<string, string>>({})
// 标准字段选项
const standardFields = [
{ value: 'product_name', label: '商品名称' },
{ value: 'order_id', label: '订单号' },
{ value: 'order_status', label: '订单状态' },
{ value: 'quantity', label: '商品数量' },
{ value: 'product_id', label: '商品ID' },
{ value: 'product_spec', label: '商品规格' },
{ value: 'after_sale_status', label: '售后状态' },
{ value: 'amount', label: '实收金额' },
{ value: 'shop_name', label: '店铺名称' },
{ value: 'order_date', label: '订单日期' },
{ value: 'customer_id', label: '客户ID' },
{ value: 'payment_method', label: '支付方式' },
{ value: 'delivery_address', label: '收货地址' },
{ value: 'remark', label: '备注' },
]
// 计算属性
const mappingData = computed(() => {
return props.csvHeaders.map((header, index) => ({
key: index,
csvField: header,
standardField: fieldMappings.value[header] || '',
customName: customFieldNames.value[header] || '',
}))
})
const mappingColumns = [
{
title: 'CSV字段',
dataIndex: 'csvField',
key: 'csvField',
width: 200,
},
{
title: '标准字段',
dataIndex: 'standardField',
key: 'standardField',
width: 200,
customRender: ({ record }: any) => {
return h('VbenSelect', {
value: record.standardField,
placeholder: '选择标准字段',
allowClear: true,
options: standardFields,
onChange: (value: string) => {
fieldMappings.value[record.csvField] = value
}
})
}
},
{
title: '自定义字段名',
dataIndex: 'customName',
key: 'customName',
width: 200,
customRender: ({ record }: any) => {
return h('VbenInput', {
value: record.customName,
placeholder: '可选:自定义字段名',
onChange: (e: any) => {
customFieldNames.value[record.csvField] = e.target.value
}
})
}
},
{
title: '示例数据',
dataIndex: 'example',
key: 'example',
ellipsis: true,
customRender: ({ record }: any) => {
const exampleValue = props.csvData[0]?.[record.csvField] || ''
return exampleValue.toString().substring(0, 50) + (exampleValue.toString().length > 50 ? '...' : '')
}
}
]
const previewData = computed(() => {
if (Object.keys(fieldMappings.value).length === 0) return []
return transformDataWithMapping(
props.csvData,
fieldMappings.value,
customFieldNames.value
)
})
const previewColumns = computed(() => {
const columns = Object.keys(previewData.value[0] || {}).map(field => ({
title: field,
dataIndex: field,
key: field,
width: 150,
ellipsis: true,
}))
return columns
})
// 监听字段映射变化,自动生成预览
watch([fieldMappings, customFieldNames], () => {
// 预览数据会自动更新
}, { deep: true })
// 导出JSON
const handleExport = () => {
if (previewData.value.length === 0) {
message.warning('请先配置字段映射')
return
}
const exportData = generateExportData(
previewData.value,
fieldMappings.value,
customFieldNames.value
)
// 使用工具函数下载JSON
downloadJSON(exportData)
message.success('JSON文件导出成功')
emit('confirm', exportData)
}
// 导入h函数
import { h } from 'vue'
</script>
<style scoped>
.ant-table-tbody > tr > td {
padding: 8px 12px;
}
</style>
@@ -0,0 +1,355 @@
<template>
<div class="p-6">
<VbenCard title="电商数据导入" class="mb-6">
<div class="space-y-6">
<!-- 文件拖拽上传区域 -->
<div
class="border-2 border-dashed border-gray-300 rounded-lg p-8 text-center transition-colors"
:class="{ 'border-blue-500 bg-blue-50': isDragOver }"
@dragover.prevent="handleDragOver"
@dragleave.prevent="handleDragLeave"
@drop.prevent="handleDrop"
>
<div class="space-y-4">
<VbenIcon icon="lucide:upload" class="w-12 h-12 mx-auto text-gray-400" />
<div>
<p class="text-lg font-medium text-gray-700">拖拽CSV文件到此处或点击上传</p>
<p class="text-sm text-gray-500">支持.csv格式,最大10MB</p>
</div>
<VbenButton @click="triggerFileInput" type="primary" ghost>
选择文件
</VbenButton>
</div>
<input
ref="fileInputRef"
type="file"
accept=".csv"
style="display: none"
@change="handleFileSelect"
/>
</div>
<!-- 文件信息显示 -->
<div v-if="uploadedFile" class="bg-green-50 border border-green-200 rounded-lg p-4">
<div class="flex items-center justify-between">
<div class="flex items-center space-x-2">
<VbenIcon icon="lucide:check-circle" class="w-5 h-5 text-green-500" />
<span class="font-medium text-green-800">文件上传成功</span>
</div>
<VbenButton @click="clearData" size="small" type="text" danger>
清除数据
</VbenButton>
</div>
<p class="text-sm text-green-600 mt-1">{{ uploadedFile.name }} ({{ formatFileSize(uploadedFile.size) }})</p>
</div>
<!-- 数据表格 -->
<div v-if="csvData.length > 0" class="space-y-4">
<div class="flex items-center justify-between">
<div class="flex items-center space-x-4">
<h3 class="text-lg font-medium">数据预览与选择</h3>
<div class="text-sm text-gray-500">
共 {{ csvData.length }} 条记录,已选择 {{ selectedRows.length }} 条
</div>
</div>
<div class="flex items-center space-x-2">
<VbenButton @click="selectAll" size="small">
全选
</VbenButton>
<VbenButton @click="clearSelection" size="small">
清空选择
</VbenButton>
<VbenButton @click="showFieldMapping = true" type="primary" :disabled="selectedRows.length === 0">
配置字段映射 ({{ selectedRows.length }})
</VbenButton>
</div>
</div>
<!-- VxeTable 表格 -->
<Grid
@checkbox-change="handleCheckboxChange"
@checkbox-all="handleCheckboxAll"
/>
</div>
</div>
</VbenCard>
<!-- 字段映射弹窗 -->
<VbenModal
v-model:open="showFieldMapping"
title="字段映射配置"
width="1000px"
:footer="null"
>
<FieldMapping
:csv-headers="csvHeaders"
:csv-data="selectedRows"
@confirm="handleFieldMapping"
@cancel="showFieldMapping = false"
/>
</VbenModal>
</div>
</template>
<script setup lang="ts">
import { ref } from 'vue'
import { message } from 'ant-design-vue'
import FieldMapping from '#/views/ecommerce/data-import/components/FieldMapping.vue'
import { parseCSV, extractShopNameFromFilename, validateCSVData, cleanAndFormatData } from '#/utils/csv-utils'
import { useVbenVxeGrid } from '#/adapter/vxe-table'
import type { VxeGridProps } from '#/adapter/vxe-table'
// 响应式数据
const uploadedFile = ref<File | null>(null)
const csvData = ref<any[]>([])
const csvHeaders = ref<string[]>([])
const showFieldMapping = ref(false)
const isDragOver = ref(false)
const fileInputRef = ref<HTMLInputElement>()
const selectedRows = ref<any[]>([])
// VxeTable 配置
const gridOptions: VxeGridProps<any> = {
columns: [
{
type: 'checkbox',
width: 60,
fixed: 'left',
},
{
type: 'seq',
width: 80,
fixed: 'left',
title: '序号',
},
],
data: [],
height: 500,
border: true,
stripe: true,
showOverflow: true,
keepSource: true,
checkboxConfig: {
labelField: 'id',
reserve: true,
highlight: true,
range: true,
},
scrollY: {
enabled: true,
},
scrollX: {
enabled: true,
},
}
const [Grid, gridApi] = useVbenVxeGrid({
gridOptions,
})
// 更新表格列配置
const updateTableColumns = () => {
const newColumns: any[] = [
{
type: 'checkbox',
width: 60,
fixed: 'left',
},
{
type: 'seq',
width: 80,
fixed: 'left',
title: '序号',
},
...csvHeaders.value.map((header) => ({
field: header,
title: header,
width: 200,
showOverflow: 'tooltip',
sortable: true,
}))
]
gridApi.setGridOptions({
columns: newColumns,
data: csvData.value,
})
}
// 文件验证
const validateFile = (file: File): boolean => {
const isCSV = file.type === 'text/csv' || file.name.endsWith('.csv')
if (!isCSV) {
message.error('只能上传CSV格式的文件!')
return false
}
const isLt10M = file.size / 1024 / 1024 < 10
if (!isLt10M) {
message.error('文件大小不能超过10MB!')
return false
}
return true
}
// 处理文件
const processFile = async (file: File) => {
if (!validateFile(file)) return
uploadedFile.value = file
try {
// 使用工具函数解析CSV
const result = await parseCSV(file)
if (result.errors.length > 0) {
message.error('CSV文件解析失败: ' + result.errors[0].message)
return
}
// 验证数据格式
const validation = validateCSVData(result.data)
if (!validation.isValid) {
message.error('数据格式验证失败: ' + validation.errors.join(', '))
return
}
// 清洗和格式化数据,并添加唯一ID
const cleanedData = cleanAndFormatData(result.data).map((item, index) => ({
...item,
id: `row_${index}`,
}))
csvData.value = cleanedData
csvHeaders.value = result.headers
selectedRows.value = []
// 更新表格配置
updateTableColumns()
// 从文件名提取店铺名称
const shopName = extractShopNameFromFilename(file.name)
message.success(`成功解析 ${cleanedData.length} 条数据,店铺:${shopName}`)
} catch (error: any) {
message.error('文件读取失败: ' + error.message)
}
}
// 拖拽处理
const handleDragOver = (e: DragEvent) => {
e.preventDefault()
isDragOver.value = true
}
const handleDragLeave = (e: DragEvent) => {
e.preventDefault()
isDragOver.value = false
}
const handleDrop = (e: DragEvent) => {
e.preventDefault()
isDragOver.value = false
const files = e.dataTransfer?.files
if (files && files.length > 0) {
const file = files[0]
if (file) {
processFile(file)
}
}
}
// 文件选择处理
const triggerFileInput = () => {
fileInputRef.value?.click()
}
const handleFileSelect = (e: Event) => {
const target = e.target as HTMLInputElement
const files = target.files
if (files && files.length > 0) {
const file = files[0]
if (file) {
processFile(file)
}
}
// 清空input值,允许重复选择同一文件
target.value = ''
}
// 表格选择处理
const handleCheckboxChange = ({ records }: { records: any[] }) => {
selectedRows.value = records
}
const handleCheckboxAll = ({ records }: { records: any[] }) => {
selectedRows.value = records
}
// 选择操作
const selectAll = () => {
gridApi.grid?.setAllCheckboxRow(true)
}
const clearSelection = () => {
gridApi.grid?.clearCheckboxRow()
}
// 清除数据
const clearData = () => {
csvData.value = []
csvHeaders.value = []
selectedRows.value = []
uploadedFile.value = null
gridApi.grid?.clearCheckboxRow()
}
// 字段映射处理
const handleFieldMapping = (mappedData: any) => {
showFieldMapping.value = false
// 这里可以处理映射后的数据
console.log('映射后的数据:', mappedData)
message.success('字段映射配置完成')
}
// 格式化文件大小
const formatFileSize = (bytes: number) => {
if (bytes === 0) return '0 Bytes'
const k = 1024
const sizes = ['Bytes', 'KB', 'MB', 'GB']
const i = Math.floor(Math.log(bytes) / Math.log(k))
return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i]
}
</script>
<style scoped>
/* 拖拽区域样式 */
.border-dashed {
transition: all 0.3s ease;
}
.border-dashed:hover {
border-color: #1890ff !important;
background-color: #f0f8ff !important;
}
/* VxeTable 样式调整 */
:deep(.vxe-table) {
font-size: 14px;
}
:deep(.vxe-table .vxe-header--column) {
background-color: #fafafa;
font-weight: 600;
}
:deep(.vxe-table .vxe-body--row:hover) {
background-color: #f5f5f5;
}
:deep(.vxe-table .vxe-body--row.row--checked) {
background-color: #e6f7ff;
}
</style>
@@ -0,0 +1,15 @@
<template>
<div class="p-6">
<VbenCard title="店铺管理">
<div class="text-center py-12">
<VbenIcon icon="lucide:store" class="w-16 h-16 mx-auto text-gray-400 mb-4" />
<h3 class="text-lg font-medium text-gray-700 mb-2">店铺管理功能</h3>
<p class="text-gray-500">此功能正在开发中,敬请期待...</p>
</div>
</VbenCard>
</div>
</template>
<script setup lang="ts">
// 店铺管理页面占位
</script>