mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-24 20:29:19 +08:00
prettier: 代码格式化
This commit is contained in:
@@ -1,27 +1,27 @@
|
||||
import Papa from 'papaparse'
|
||||
import Papa from 'papaparse';
|
||||
|
||||
export interface CSVParseResult {
|
||||
data: any[]
|
||||
headers: string[]
|
||||
errors: any[]
|
||||
data: any[];
|
||||
headers: string[];
|
||||
errors: any[];
|
||||
}
|
||||
|
||||
export interface FieldMapping {
|
||||
csvField: string
|
||||
standardField: string
|
||||
customName?: string
|
||||
csvField: string;
|
||||
standardField: string;
|
||||
customName?: string;
|
||||
}
|
||||
|
||||
export interface ExportData {
|
||||
metadata: {
|
||||
source: string
|
||||
totalRecords: number
|
||||
exportTime: string
|
||||
fieldMappings: Record<string, string>
|
||||
customFieldNames: Record<string, string>
|
||||
shopName?: string
|
||||
}
|
||||
data: any[]
|
||||
customFieldNames: Record<string, string>;
|
||||
exportTime: string;
|
||||
fieldMappings: Record<string, string>;
|
||||
shopName?: string;
|
||||
source: string;
|
||||
totalRecords: number;
|
||||
};
|
||||
data: any[];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -32,19 +32,19 @@ export function parseCSV(file: File): Promise<CSVParseResult> {
|
||||
Papa.parse(file, {
|
||||
header: true,
|
||||
skipEmptyLines: true,
|
||||
encoding: 'UTF-8',
|
||||
encoding: 'utf8',
|
||||
complete: (results) => {
|
||||
resolve({
|
||||
data: results.data,
|
||||
headers: results.meta.fields || [],
|
||||
errors: results.errors
|
||||
})
|
||||
errors: results.errors,
|
||||
});
|
||||
},
|
||||
error: (error) => {
|
||||
reject(error)
|
||||
}
|
||||
})
|
||||
})
|
||||
reject(error);
|
||||
},
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -53,20 +53,20 @@ export function parseCSV(file: File): Promise<CSVParseResult> {
|
||||
export function transformDataWithMapping(
|
||||
csvData: any[],
|
||||
fieldMappings: Record<string, string>,
|
||||
customFieldNames: Record<string, string> = {}
|
||||
customFieldNames: Record<string, string> = {},
|
||||
): any[] {
|
||||
return csvData.map(row => {
|
||||
const transformedRow: any = {}
|
||||
|
||||
return csvData.map((row) => {
|
||||
const transformedRow: any = {};
|
||||
|
||||
Object.entries(fieldMappings).forEach(([csvField, standardField]) => {
|
||||
if (standardField && row[csvField] !== undefined) {
|
||||
const fieldName = customFieldNames[csvField] || standardField
|
||||
transformedRow[fieldName] = row[csvField]
|
||||
const fieldName = customFieldNames[csvField] || standardField;
|
||||
transformedRow[fieldName] = row[csvField];
|
||||
}
|
||||
})
|
||||
|
||||
return transformedRow
|
||||
})
|
||||
});
|
||||
|
||||
return transformedRow;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -76,7 +76,7 @@ export function generateExportData(
|
||||
transformedData: any[],
|
||||
fieldMappings: Record<string, string>,
|
||||
customFieldNames: Record<string, string> = {},
|
||||
shopName?: string
|
||||
shopName?: string,
|
||||
): ExportData {
|
||||
return {
|
||||
metadata: {
|
||||
@@ -85,27 +85,27 @@ export function generateExportData(
|
||||
exportTime: new Date().toISOString(),
|
||||
fieldMappings,
|
||||
customFieldNames,
|
||||
shopName
|
||||
shopName,
|
||||
},
|
||||
data: transformedData
|
||||
}
|
||||
data: transformedData,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 下载JSON文件
|
||||
*/
|
||||
export function downloadJSON(data: ExportData, filename?: string): void {
|
||||
const jsonString = JSON.stringify(data, null, 2)
|
||||
const blob = new Blob([jsonString], { type: 'application/json' })
|
||||
const url = URL.createObjectURL(blob)
|
||||
|
||||
const link = document.createElement('a')
|
||||
link.href = url
|
||||
link.download = filename || `ecommerce-data-${new Date().getTime()}.json`
|
||||
document.body.appendChild(link)
|
||||
link.click()
|
||||
document.body.removeChild(link)
|
||||
URL.revokeObjectURL(url)
|
||||
const jsonString = JSON.stringify(data, null, 2);
|
||||
const blob = new Blob([jsonString], { type: 'application/json' });
|
||||
const url = URL.createObjectURL(blob);
|
||||
|
||||
const link = document.createElement('a');
|
||||
link.href = url;
|
||||
link.download = filename || `ecommerce-data-${Date.now()}.json`;
|
||||
document.body.append(link);
|
||||
link.click();
|
||||
link.remove();
|
||||
URL.revokeObjectURL(url);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -113,67 +113,76 @@ export function downloadJSON(data: ExportData, filename?: string): void {
|
||||
*/
|
||||
export function extractShopNameFromFilename(filename: string): string {
|
||||
// 移除文件扩展名
|
||||
const nameWithoutExt = filename.replace(/\.[^/.]+$/, '')
|
||||
return nameWithoutExt
|
||||
const nameWithoutExt = filename.replace(/\.[^/.]+$/, '');
|
||||
return nameWithoutExt;
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证CSV数据格式
|
||||
*/
|
||||
export function validateCSVData(data: any[]): { isValid: boolean; errors: string[] } {
|
||||
const errors: string[] = []
|
||||
|
||||
export function validateCSVData(data: any[]): {
|
||||
errors: string[];
|
||||
isValid: boolean;
|
||||
} {
|
||||
const errors: string[] = [];
|
||||
|
||||
if (data.length === 0) {
|
||||
errors.push('CSV文件为空')
|
||||
errors.push('CSV文件为空');
|
||||
}
|
||||
|
||||
|
||||
// 检查必要字段
|
||||
const requiredFields = ['商品', '订单号', '订单状态', '商品数量(件)', '商家实收金额(元)']
|
||||
const firstRow = data[0]
|
||||
|
||||
const requiredFields = [
|
||||
'商品',
|
||||
'订单号',
|
||||
'订单状态',
|
||||
'商品数量(件)',
|
||||
'商家实收金额(元)',
|
||||
];
|
||||
const firstRow = data[0];
|
||||
|
||||
if (firstRow) {
|
||||
requiredFields.forEach(field => {
|
||||
requiredFields.forEach((field) => {
|
||||
if (!(field in firstRow)) {
|
||||
errors.push(`缺少必要字段: ${field}`)
|
||||
errors.push(`缺少必要字段: ${field}`);
|
||||
}
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
return {
|
||||
isValid: errors.length === 0,
|
||||
errors
|
||||
}
|
||||
errors,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 数据清洗和格式化
|
||||
*/
|
||||
export function cleanAndFormatData(data: any[]): any[] {
|
||||
return data.map(row => {
|
||||
const cleanedRow: any = {}
|
||||
|
||||
return data.map((row) => {
|
||||
const cleanedRow: any = {};
|
||||
|
||||
Object.entries(row).forEach(([key, value]) => {
|
||||
if (value !== null && value !== undefined) {
|
||||
// 去除首尾空格
|
||||
let cleanedValue = String(value).trim()
|
||||
|
||||
let cleanedValue = String(value).trim();
|
||||
|
||||
// 处理数字字段
|
||||
if (key.includes('数量') || key.includes('金额')) {
|
||||
cleanedValue = cleanedValue.replace(/[^\d.-]/g, '')
|
||||
if (cleanedValue && !isNaN(Number(cleanedValue))) {
|
||||
cleanedValue = Number(cleanedValue).toString()
|
||||
cleanedValue = cleanedValue.replaceAll(/[^\d.-]/g, '');
|
||||
if (cleanedValue && !Number.isNaN(Number(cleanedValue))) {
|
||||
cleanedValue = Number(cleanedValue).toString();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// 处理日期字段
|
||||
if (key.includes('日期') || key.includes('时间')) {
|
||||
// 这里可以添加日期格式化逻辑
|
||||
}
|
||||
|
||||
cleanedRow[key] = cleanedValue
|
||||
|
||||
cleanedRow[key] = cleanedValue;
|
||||
}
|
||||
})
|
||||
|
||||
return cleanedRow
|
||||
})
|
||||
});
|
||||
|
||||
return cleanedRow;
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user