From 5fc195d9fed982a92cefdf5dfd77e479fa422719 Mon Sep 17 00:00:00 2001 From: WuYongJie Date: Sat, 24 Oct 2026 13:32:54 +0800 Subject: [PATCH] =?UTF-8?q?feat(supply-chain):=20=E4=BA=A7=E5=93=81?= =?UTF-8?q?=E5=85=A8=E9=87=8F=E6=A1=A3=E6=A1=88=E5=AF=BC=E5=87=BA=E5=AD=97?= =?UTF-8?q?=E6=AE=B5=E5=8B=BE=E9=80=89=E4=B8=8E=E5=9B=BE=E7=89=87URL?= =?UTF-8?q?=E5=AF=BC=E5=87=BA=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/app/api/downloads/route.ts | 2 + .../product-archive/export/route.ts | 26 ++ .../ProductArchiveExportFieldsDialog.tsx | 243 ++++++++++++++++++ .../product-archive/ProductArchiveView.tsx | 18 +- ...ain-product-archive-export.service.test.ts | 74 ++++++ ...oduct-archive-image-export.service.test.ts | 11 + .../download-center.executors.ts | 12 + ...ly-chain-product-archive-export.service.ts | 22 ++ ...in-product-archive-image-export.service.ts | 4 +- 9 files changed, 408 insertions(+), 4 deletions(-) create mode 100644 furniture-3d-system/src/app/supply-chain/database/product-archive/ProductArchiveExportFieldsDialog.tsx diff --git a/furniture-3d-system/src/app/api/downloads/route.ts b/furniture-3d-system/src/app/api/downloads/route.ts index 1c6c7b0c..642200f9 100644 --- a/furniture-3d-system/src/app/api/downloads/route.ts +++ b/furniture-3d-system/src/app/api/downloads/route.ts @@ -48,6 +48,8 @@ export async function POST(request: NextRequest) { ) const blockedFields = await productArchiveFieldPermissionService.getBlockedFields(ctx.user) params.fieldBlocks = [...blockedFields] + // 图片列相对路径归一成绝对 URL 用的站点 Origin,同样在创建时快照。 + params.siteOrigin = request.nextUrl.origin } } diff --git a/furniture-3d-system/src/app/api/supply-chain/product-archive/export/route.ts b/furniture-3d-system/src/app/api/supply-chain/product-archive/export/route.ts index ffba3834..6d4448c3 100644 --- a/furniture-3d-system/src/app/api/supply-chain/product-archive/export/route.ts +++ b/furniture-3d-system/src/app/api/supply-chain/product-archive/export/route.ts @@ -26,6 +26,10 @@ async function createExportResponse(params: { blockedFields: ReadonlySet includeImage?: boolean columnSet?: 'archive' | 'limit' + /** 自定义导出字段(列 prop 清单):仅产品档案列单生效,全勾/未传=全部列。 */ + fields?: string[] + /** 图片列相对路径归一成绝对 URL 用的站点 Origin。 */ + siteOrigin?: string requestId: string signal: AbortSignal }) { @@ -34,6 +38,8 @@ async function createExportResponse(params: { blockedFields: params.blockedFields, includeImage: params.includeImage, columnSet: params.columnSet, + fields: params.fields, + siteOrigin: params.siteOrigin, }) result.completion.catch((error) => { @@ -88,12 +94,21 @@ export async function GET(request: NextRequest) { includeImage: request.nextUrl.searchParams.get('includeImage') !== '0', // 限价管理页导出用「限价界面抬头」列单。 columnSet: isLimitExport ? 'limit' : 'archive', + fields: parseExportFields(request.nextUrl.searchParams.get('fields')), + siteOrigin: request.nextUrl.origin, requestId: ctx.requestId, signal: request.signal, }) }) } +/** fields=spu,barcode,costPrice(逗号分隔的列 prop);仅接受字符串,未知项由服务端过滤。 */ +function parseExportFields(raw: string | null): string[] | undefined { + if (!raw) return undefined + const fields = raw.split(',').map((field) => field.trim()).filter(Boolean) + return fields.length > 0 ? fields : undefined +} + // Same export with the filters carried in the body for large multi-selects // that would exceed the URL length limit. export async function POST(request: NextRequest) { @@ -120,8 +135,19 @@ export async function POST(request: NextRequest) { blockedFields, includeImage: body?.includeImage !== false, columnSet: body?.columnSet === 'limit' ? 'limit' : 'archive', + fields: parseExportFieldsBody(body?.fields), + siteOrigin: request.nextUrl.origin, requestId: ctx.requestId, signal: request.signal, }) }) } + +/** POST 体里的字段勾选清单:字符串数组;未知项由服务端按列单过滤。 */ +function parseExportFieldsBody(raw: unknown): string[] | undefined { + if (!Array.isArray(raw)) return undefined + const fields = raw.filter((field): field is string => typeof field === 'string') + .map((field) => field.trim()) + .filter(Boolean) + return fields.length > 0 ? fields : undefined +} diff --git a/furniture-3d-system/src/app/supply-chain/database/product-archive/ProductArchiveExportFieldsDialog.tsx b/furniture-3d-system/src/app/supply-chain/database/product-archive/ProductArchiveExportFieldsDialog.tsx new file mode 100644 index 00000000..7826a53e --- /dev/null +++ b/furniture-3d-system/src/app/supply-chain/database/product-archive/ProductArchiveExportFieldsDialog.tsx @@ -0,0 +1,243 @@ +'use client' + +import { useEffect, useMemo, useState } from 'react' +import { X } from 'lucide-react' +import { ARCHIVE_COLUMNS } from './columns' + +const STORAGE_KEY = 'product-archive-export-fields-v1' + +type ProductArchiveExportFieldsDialogProps = { + open: boolean + onClose: () => void + /** 字段权限屏蔽的列 prop:不出现在勾选清单里。 */ + blockedFields: ReadonlySet + /** 当前页面列设置里勾显的列 prop,供「按当前显示列」快捷键。 */ + visibleProps: string[] + /** 确认导出:回传勾选的列 prop 清单(全选=全部列)。 */ + onExport: (fields: string[]) => void +} + +function loadSavedSelection(): string[] | null { + try { + const raw = window.localStorage.getItem(STORAGE_KEY) + if (!raw) return null + const list = JSON.parse(raw) + if (!Array.isArray(list)) return null + const fields = list.filter((field): field is string => typeof field === 'string') + .map((field) => field.trim()) + .filter((field) => field.length > 0) + return fields.length > 0 ? fields : null + } catch { + return null + } +} + +/** + * 导出资料前的字段勾选弹窗:按分组勾选,快捷键 全选/清空/按当前显示列, + * 上次选择本地记住;全勾=全量导出,勾部分=只导该部分。 + */ +export function ProductArchiveExportFieldsDialog({ + open, + onClose, + blockedFields, + visibleProps, + onExport, +}: ProductArchiveExportFieldsDialogProps) { + const groups = useMemo( + () => ARCHIVE_COLUMNS.map((group) => ({ + group: group.group, + columns: group.columns.filter((column) => !blockedFields.has(column.prop)), + })).filter((group) => group.columns.length > 0), + [blockedFields], + ) + const allProps = useMemo( + () => groups.flatMap((group) => group.columns.map((column) => column.prop)), + [groups], + ) + const [selected, setSelected] = useState>(new Set(allProps)) + + // 每次打开时初始化:记住的上次选择 > 全选(首次打开=全量口径) + useEffect(() => { + if (!open) return + const saved = loadSavedSelection() + const valid = saved ? saved.filter((field) => allProps.includes(field)) : allProps + setSelected(new Set(valid.length > 0 ? valid : allProps)) + }, [open, allProps]) + + const toggle = (prop: string) => { + setSelected((current) => { + const next = new Set(current) + if (next.has(prop)) next.delete(prop) + else next.add(prop) + return next + }) + } + const setGroupAll = (props: string[], checked: boolean) => { + setSelected((current) => { + const next = new Set(current) + props.forEach((prop) => (checked ? next.add(prop) : next.delete(prop))) + return next + }) + } + const applyVisible = () => { + const visible = new Set(visibleProps.filter((prop) => allProps.includes(prop))) + setSelected(visible.size > 0 ? visible : new Set(allProps)) + } + const startExport = () => { + if (selected.size === 0) return + // 按分组顺序回传,导出列顺序稳定 + const ordered = allProps.filter((prop) => selected.has(prop)) + try { + window.localStorage.setItem(STORAGE_KEY, JSON.stringify(ordered)) + } catch { + /* 本地记住失败不阻断导出 */ + } + onExport(ordered) + onClose() + } + + // 样式块常驻渲染、弹窗主体条件渲染:避免弹窗开关伴随样式挂载/卸载带来的布局闪动 + return ( + <> + {!open ? null : ( + <> +
+
+
+
+
选择导出字段
+
全选即导出全量字段;勾选部分则只导出勾选列(上次选择已自动恢复)
+
+ +
+ +
+ + + + 已选 {selected.size} / {allProps.length} 个字段 +
+ +
+ {groups.map((group) => { + const props = group.columns.map((column) => column.prop) + const checkedCount = props.filter((prop) => selected.has(prop)).length + return ( +
+
+ {group.group} + {checkedCount}/{props.length} +
+
+ {group.columns.map((column) => ( + + ))} +
+
+ ) + })} +
+ +
+ 开始导出后进入下载中心队列,完成后在右上角下载 +
+ + +
+
+
+ + )} + + + + ) +} diff --git a/furniture-3d-system/src/app/supply-chain/database/product-archive/ProductArchiveView.tsx b/furniture-3d-system/src/app/supply-chain/database/product-archive/ProductArchiveView.tsx index 4355aa1b..7187d468 100644 --- a/furniture-3d-system/src/app/supply-chain/database/product-archive/ProductArchiveView.tsx +++ b/furniture-3d-system/src/app/supply-chain/database/product-archive/ProductArchiveView.tsx @@ -20,6 +20,7 @@ import { ProductArchiveDetailDialog } from './ProductArchiveDetailDialog' import { ProductArchiveChangeLogDialog } from './ProductArchiveChangeLogDialog' import { useProductArchivePageImages } from './useProductArchivePageImages' import { useProductArchiveImageExport } from './useProductArchiveImageExport' +import { ProductArchiveExportFieldsDialog } from './ProductArchiveExportFieldsDialog' import { ProductArchiveThemeStyles } from './ProductArchiveThemeStyles' import { ProductArchiveSalesPeriodFilter } from './ProductArchiveSalesPeriodFilter' import { ProductArchiveFilterBar } from './ProductArchiveFilterBar' @@ -128,6 +129,7 @@ export function ProductArchiveView({ && (Array.isArray(value) ? value.length > 0 : Boolean(value)), ).length const { exporting, exportImages } = useProductArchiveImageExport(filters) + const [exportFieldsOpen, setExportFieldsOpen] = useState(false) const [creatingArchiveTask, setCreatingArchiveTask] = useState(false) const [archiveTaskMessage, setArchiveTaskMessage] = useState(null) useEffect(() => { @@ -135,14 +137,14 @@ export function ProductArchiveView({ const timer = setTimeout(() => setArchiveTaskMessage(null), 5000) return () => clearTimeout(timer) }, [archiveTaskMessage]) - const createArchiveExportTask = useCallback(async () => { + const createArchiveExportTask = useCallback(async (fields: string[]) => { setCreatingArchiveTask(true) setArchiveTaskMessage(null) try { const response = await fetch('/api/downloads', { method: 'POST', headers: { 'Content-Type': 'application/json' }, - body: JSON.stringify({ type: 'archive-export', params: { filters } }), + body: JSON.stringify({ type: 'archive-export', params: { filters, fields } }), }) const payload = await response.json().catch(() => null) if (!response.ok || !payload?.success) { @@ -180,7 +182,8 @@ export function ProductArchiveView({ label: creatingArchiveTask ? '导出资料(加入队列中…)' : '导出资料', icon: , disabled: creatingArchiveTask, - onClick: () => void createArchiveExportTask(), + // 先弹「选择导出字段」:全勾=全量,勾部分=只导该部分 + onClick: () => setExportFieldsOpen(true), }, // 图片列被屏蔽时,图片导出入口一并隐藏(服务端同样拒绝) ...(blockedFieldSet.has('image') ? [] : [{ @@ -495,6 +498,15 @@ export function ProductArchiveView({ onClose={() => setChangeLogBarcode(null)} /> )} + {canDownload && ( + setExportFieldsOpen(false)} + blockedFields={blockedFieldSet} + visibleProps={effectiveColumns.flatMap((group) => group.columns.map((column) => column.prop))} + onExport={(fields) => void createArchiveExportTask(fields)} + /> + )}