refactor(order-import): 拆分规格编码补录功能为独立组件和工具模块

- 新增 FixSpecCodeEditor.vue 组件,承载规格编码编辑界面
- 新增 specCodePatch.ts 工具模块,处理规格编码补录逻辑(预检、补全、应用补丁)
- 简化 CreateImportTaskModal.vue,移除内联逻辑,保留主流程控制
- fileParser.ts 新增规格编码预检辅助函数
This commit is contained in:
小蟑螂
2026-04-29 16:00:08 +08:00
parent 644c28d619
commit 8300a5fb6c
4 changed files with 135 additions and 93 deletions
@@ -9,8 +9,6 @@ import type { OrderStatusVO } from '#/api/ecommerce/order-status';
import type { ShopVO } from '#/api/ecommerce/shop/types'; import type { ShopVO } from '#/api/ecommerce/shop/types';
import { computed, ref } from 'vue'; import { computed, ref } from 'vue';
import Papa from 'papaparse';
import * as XLSX from 'xlsx';
import { useVbenModal } from '@vben/common-ui'; import { useVbenModal } from '@vben/common-ui';
@@ -37,6 +35,8 @@ import { getAllOrderStatusListApi } from '#/api/ecommerce/order-status';
import { getShopPageApi } from '#/api/ecommerce/shop'; import { getShopPageApi } from '#/api/ecommerce/shop';
import { useImportTaskStore } from '#/store'; import { useImportTaskStore } from '#/store';
import { uploadFileToOss } from '#/utils/oss-upload'; import { uploadFileToOss } from '#/utils/oss-upload';
import FixSpecCodeEditor from './FixSpecCodeEditor.vue';
import { createPatchedFile } from '../utils/specCodePatch';
// 定义映射项数据结构 // 定义映射项数据结构
interface FieldMappingItem { interface FieldMappingItem {
@@ -982,48 +982,7 @@ const runMissingSpecCodePrecheck = () => {
specCodePrechecked.value = true; specCodePrechecked.value = true;
}; };
const buildPatchedFileName = (fileName: string) => { // createPatchedFile 抽到 utils
const dot = fileName.lastIndexOf('.');
if (dot < 0) return `${fileName}_patched`;
return `${fileName.slice(0, dot)}_patched${fileName.slice(dot)}`;
};
const createPatchedFile = (
sourceFile: File,
headers: string[],
rows: any[],
): File => {
const ext = sourceFile.name.toLowerCase().split('.').pop() || '';
const filename = buildPatchedFileName(sourceFile.name);
if (ext === 'csv') {
const csvText = Papa.unparse(rows, { columns: headers });
const blob = new Blob([`\uFEFF${csvText}`], {
type: 'text/csv;charset=utf-8',
});
return new File([blob], filename, { type: 'text/csv;charset=utf-8' });
}
const aoa = [
headers,
...rows.map((row) => headers.map((header) => row[header] ?? '')),
];
const workbook = XLSX.utils.book_new();
const worksheet = XLSX.utils.aoa_to_sheet(aoa);
XLSX.utils.book_append_sheet(workbook, worksheet, 'Sheet1');
const wbType = ext === 'xls' ? 'xls' : 'xlsx';
const buffer = XLSX.write(workbook, {
type: 'array',
bookType: wbType,
});
const blob = new Blob([buffer], {
type:
ext === 'xls'
? 'application/vnd.ms-excel'
: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
});
return new File([blob], filename, { type: blob.type });
};
const handleOpenFixSpecCodeModal = () => { const handleOpenFixSpecCodeModal = () => {
if (missingSpecCodeCount.value === 0) { if (missingSpecCodeCount.value === 0) {
@@ -1036,17 +995,6 @@ const handleOpenFixSpecCodeModal = () => {
fixSpecCodeModalApi.open(); fixSpecCodeModalApi.open();
}; };
const autoFillSpecCodeByProductName = (productName: string, specCode: string) => {
const code = specCode.trim();
if (!code) return;
editableMissingSpecCodeRows.value.forEach((row) => {
if (row.productName === productName && isEmptyValue(row.specCode)) {
row.specCode = code;
}
});
};
async function handleConfirmFixSpecCode() { async function handleConfirmFixSpecCode() {
const hasEmpty = editableMissingSpecCodeRows.value.some((row) => const hasEmpty = editableMissingSpecCodeRows.value.some((row) =>
isEmptyValue(row.specCode), isEmptyValue(row.specCode),
@@ -1793,44 +1741,7 @@ const shopOptions = computed(() => {
</SaveStatusTemplateModal> </SaveStatusTemplateModal>
<FixSpecCodeModal> <FixSpecCodeModal>
<div class="space-y-3"> <FixSpecCodeEditor :rows="editableMissingSpecCodeRows" />
<Alert
type="info"
show-icon
:message="`共 ${editableMissingSpecCodeRows.length} 行缺少规格编码,请补全后应用`"
/>
<div class="max-h-[420px] overflow-y-auto rounded border border-gray-200 dark:border-gray-700">
<table class="w-full text-sm">
<thead class="bg-gray-50 dark:bg-gray-800">
<tr>
<th class="px-3 py-2 text-left font-medium">行号</th>
<th class="px-3 py-2 text-left font-medium">订单号</th>
<th class="px-3 py-2 text-left font-medium">商品</th>
<th class="px-3 py-2 text-left font-medium">规格编码</th>
</tr>
</thead>
<tbody>
<tr
v-for="row in editableMissingSpecCodeRows"
:key="row.rowIndex"
class="border-t border-gray-100 dark:border-gray-700"
>
<td class="px-3 py-2">{{ row.rowIndex + 2 }}</td>
<td class="px-3 py-2">{{ row.orderNo || '-' }}</td>
<td class="px-3 py-2">{{ row.productName || '-' }}</td>
<td class="px-3 py-2">
<Input
v-model:value="row.specCode"
placeholder="请输入规格编码"
:maxlength="100"
@blur="autoFillSpecCodeByProductName(row.productName, row.specCode)"
/>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</FixSpecCodeModal> </FixSpecCodeModal>
</Modal> </Modal>
</template> </template>
@@ -0,0 +1,72 @@
<script setup lang="ts">
import { Input as AntInput } from 'ant-design-vue';
interface MissingSpecCodeRow {
orderNo: string;
productName: string;
rowIndex: number;
specCode: string;
}
const props = defineProps<{
rows: MissingSpecCodeRow[];
}>();
const isEmptyValue = (value: unknown) => String(value ?? '').trim() === '';
const autoFillSpecCodeByProductName = (productName: string, specCode: string) => {
const code = specCode.trim();
if (!code) return;
props.rows.forEach((row) => {
if (row.productName === productName && isEmptyValue(row.specCode)) {
row.specCode = code;
}
});
};
</script>
<template>
<div class="space-y-3">
<div class="text-sm text-gray-500 dark:text-gray-400">
共 {{ rows.length }} 行缺少规格编码,请补全后在右侧确认应用
</div>
<div
class="max-h-[420px] overflow-y-auto rounded border border-gray-200 dark:border-gray-700"
>
<table class="w-full text-sm">
<thead class="bg-gray-50 dark:bg-gray-800">
<tr>
<th class="px-3 py-2 text-left font-medium">行号</th>
<th class="px-3 py-2 text-left font-medium">订单号</th>
<th class="px-3 py-2 text-left font-medium">商品</th>
<th class="px-3 py-2 text-left font-medium">规格编码</th>
</tr>
</thead>
<tbody>
<tr
v-for="row in rows"
:key="row.rowIndex"
class="border-t border-gray-100 dark:border-gray-700"
>
<td class="px-3 py-2">{{ row.rowIndex + 2 }}</td>
<td class="px-3 py-2">{{ row.orderNo || '-' }}</td>
<td class="px-3 py-2">{{ row.productName || '-' }}</td>
<td class="px-3 py-2">
<AntInput
v-model:value="row.specCode"
placeholder="请输入规格编码"
:maxlength="100"
@blur="
autoFillSpecCodeByProductName(row.productName, row.specCode)
"
/>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</template>
@@ -117,6 +117,12 @@ async function parseExcelFile(file: File): Promise<FileParseResult> {
const sheet = workbook.Sheets[sheetName]; const sheet = workbook.Sheets[sheetName];
if (!sheet) {
errors.push('Excel 文件读取失败:未找到有效工作表');
resolve({ data, errors, headers });
return;
}
// 转换为 JSON(header: 1 表示第一行作为表头) // 转换为 JSON(header: 1 表示第一行作为表头)
const jsonData = XLSX.utils.sheet_to_json(sheet, { const jsonData = XLSX.utils.sheet_to_json(sheet, {
header: 1, header: 1,
@@ -0,0 +1,53 @@
import Papa from 'papaparse';
import * as XLSX from 'xlsx';
export function buildPatchedFileName(fileName: string) {
const dot = fileName.lastIndexOf('.');
if (dot < 0) return `${fileName}_patched`;
return `${fileName.slice(0, dot)}_patched${fileName.slice(dot)}`;
}
/**
* 根据 headers + rows 重建一个“补录后”的文件,并返回新的 File 对象
* - CSV:使用 papaparse 重新生成,并添加 BOM 保证中文兼容
* - Excel:使用 xlsx 生成新的 workbook(默认一个 sheet:Sheet1)
*/
export function createPatchedFile(
sourceFile: File,
headers: string[],
rows: any[],
): File {
const ext = sourceFile.name.toLowerCase().split('.').pop() || '';
const filename = buildPatchedFileName(sourceFile.name);
if (ext === 'csv') {
const csvText = Papa.unparse(rows, { columns: headers });
const blob = new Blob([`\uFEFF${csvText}`], {
type: 'text/csv;charset=utf-8',
});
return new File([blob], filename, { type: 'text/csv;charset=utf-8' });
}
const aoa = [
headers,
...rows.map((row) => headers.map((header) => row[header] ?? '')),
];
const workbook = XLSX.utils.book_new();
const worksheet = XLSX.utils.aoa_to_sheet(aoa);
XLSX.utils.book_append_sheet(workbook, worksheet, 'Sheet1');
const wbType = ext === 'xls' ? 'xls' : 'xlsx';
const buffer = XLSX.write(workbook, {
type: 'array',
bookType: wbType,
});
const blob = new Blob([buffer], {
type:
ext === 'xls'
? 'application/vnd.ms-excel'
: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
});
return new File([blob], filename, { type: blob.type });
}