feat(chat): 完善固资预览与下载权限

This commit is contained in:
runny committed 2026-07-07 02:27:08 +08:00
1 parent 6ce0942fef
commit bf18a7ef1d
19 files changed
+1163 -86

No files matched your search

@@ -65,6 +65,7 @@ export interface ChatPageMarkdownContext {
parsePlmPreviewLink: (href: string) => SourceFileLink | null;
openPreview: (file: SourceFileLink) => Promise<void>;
openAssetHistory: (assetId: string, assetName?: string) => void;
openAssetImage: (assetId: string, assetName?: string) => void;
getMessageLinks: (message: Record<string, unknown>) => SourceFileLink[];
getDisplayRetrieverResources: (msgItem: Record<string, unknown>) => Record<string, unknown>[];
getSourceLinks: (resources?: unknown[]) => SourceFileLink[];
@@ -185,14 +186,109 @@ function normalizeAssetHistoryText(value: string): string {
return normalized;
}
const FIXED_ASSET_TABLE_TARGET_HEADERS = [
'序号',
'资产编号',
'资产名称',
'保管人',
'保管部门',
'图片',
'账套',
'供应商',
'资产状态',
'数量',
'取得时间',
];
const FIXED_ASSET_TABLE_HEADER_ALIASES: Record<string, string[]> = {
序号: ['序号', 'id'],
资产编号: ['资产编号', 'asset_id', 'asset_no'],
资产名称: ['资产名称', 'asset_name'],
保管人: ['保管人', '保管人姓名', 'custodian_name', 'custodian_person'],
保管部门: ['保管部门', '保管部门名称', 'department_name', 'custodian_dept'],
图片: ['图片', 'image', 'picture'],
账套: ['账套', 'account_period', 'operating_center_code'],
供应商: ['供应商', '供应厂商', 'supplier', 'vendor_name'],
资产状态: ['资产状态', 'asset_status'],
数量: ['数量', 'quantity'],
取得时间: ['取得时间', '取得日期', 'acquisition_date', 'acquired_date'],
};
function normalizeFixedAssetHeader(value: string): string {
const normalized = normalizeAssetHistoryText(value).toLowerCase();
if (!normalized) return '';
const targetHeader = FIXED_ASSET_TABLE_TARGET_HEADERS.find((header) => (
(FIXED_ASSET_TABLE_HEADER_ALIASES[header] || []).some((alias) => alias.toLowerCase() === normalized)
));
return targetHeader || '';
}
function isFixedAssetPreviewTable(headerLabels: string[]): boolean {
const normalizedHeaders = headerLabels
.map((label) => normalizeFixedAssetHeader(label))
.filter(Boolean);
return ['资产编号', '资产名称', '保管人', '保管部门', '账套', '资产状态', '数量', '取得时间']
.every((header) => normalizedHeaders.includes(header));
}
/**
* 把聊天结果表格里的“资产编号”列增强为可点击入口。
* 核心逻辑:只处理包含“资产编号”表头的表格,并从同行“资产名称”列一并带出检索关键词,避免误伤其他普通 Markdown 表格。
* 将 Dify 返回的旧版固定资产表格收敛成统一展示列顺序。
* 核心逻辑:按中文/英文别名识别表头后重建 thead/tbody,并补出“图片=查看”列,让旧会话与新会话都按同一业务结构展示。
*/
function normalizeFixedAssetPreviewTables(container: HTMLElement): void {
const tables = Array.from(container.querySelectorAll('table'));
tables.forEach((table) => {
const headerCells = Array.from(table.querySelectorAll('thead th'));
if (headerCells.length === 0) return;
const headerLabels = headerCells.map((cell) => normalizeAssetHistoryText(cell.textContent || ''));
if (!isFixedAssetPreviewTable(headerLabels)) return;
const headerIndexMap = new Map<string, number>();
headerLabels.forEach((label, index) => {
const normalized = normalizeFixedAssetHeader(label);
if (normalized && !headerIndexMap.has(normalized)) {
headerIndexMap.set(normalized, index);
}
});
const bodyRows = Array.from(table.querySelectorAll('tbody tr'));
const normalizedRows = bodyRows.map((row, rowIndex) => {
const cells = Array.from(row.querySelectorAll('td'));
const pickCellText = (header: string): string => {
if (header === '序号') return String(rowIndex + 1);
if (header === '图片') {
const assetIdIndex = headerIndexMap.get('资产编号');
const assetId = assetIdIndex !== undefined ? normalizeAssetHistoryText(cells[assetIdIndex]?.textContent || '') : '';
return assetId ? '查看' : '无';
}
const cellIndex = headerIndexMap.get(header);
return cellIndex !== undefined ? normalizeAssetHistoryText(cells[cellIndex]?.textContent || '') || '无' : '无';
};
return FIXED_ASSET_TABLE_TARGET_HEADERS.map((header) => pickCellText(header));
});
const thead = table.querySelector('thead');
const tbody = table.querySelector('tbody');
if (!thead || !tbody) return;
thead.innerHTML = `<tr>${FIXED_ASSET_TABLE_TARGET_HEADERS.map((header) => (
`<th class="${header === '资产编号' || header === '图片' ? 'chat-fixed-asset-highlight-header' : ''}">${header}</th>`
)).join('')}</tr>`;
tbody.innerHTML = normalizedRows.map((row) => (
`<tr>${row.map((cell) => `<td>${cell || '无'}</td>`).join('')}</tr>`
)).join('');
});
}
/**
* 把聊天结果表格里的“资产编号”和“图片”列增强为可点击入口。
* 核心逻辑:只处理包含“资产编号”表头的表格,并从同行“资产名称”列一并带出检索关键词;资产编号走履历检索,图片走图片预览检索。
*/
function enhanceAssetHistoryTableLinks(html: string): string {
if (!html || typeof document === 'undefined' || !html.includes('<table')) return html;
const container = document.createElement('div');
container.innerHTML = html;
normalizeFixedAssetPreviewTables(container);
const tables = Array.from(container.querySelectorAll('table'));
tables.forEach((table) => {
const headerCells = Array.from(table.querySelectorAll('thead th'));
@@ -201,6 +297,7 @@ function enhanceAssetHistoryTableLinks(html: string): string {
const assetIdIndex = headerLabels.findIndex((label) => label === '资产编号');
if (assetIdIndex < 0) return;
const assetNameIndex = headerLabels.findIndex((label) => label === '资产名称');
const imageIndex = headerLabels.findIndex((label) => label === '图片');
const rows = Array.from(table.querySelectorAll('tbody tr'));
rows.forEach((row) => {
const cells = Array.from(row.querySelectorAll('td'));
@@ -215,6 +312,18 @@ function enhanceAssetHistoryTableLinks(html: string): string {
link.textContent = assetId;
assetIdCell.innerHTML = '';
assetIdCell.appendChild(link);
if (imageIndex >= 0) {
const imageCell = cells[imageIndex];
if (!imageCell) return;
const imageText = normalizeAssetHistoryText(imageCell.textContent || '') || '查看';
const imageLink = document.createElement('a');
imageLink.href = `#asset-image?asset_id=${encodeURIComponent(assetId)}&asset_name=${encodeURIComponent(assetName)}`;
imageLink.className = 'chat-asset-history-link';
imageLink.textContent = imageText;
imageCell.innerHTML = '';
imageCell.appendChild(imageLink);
}
});
});
return container.innerHTML;
@@ -295,6 +404,12 @@ export function createChatPageMarkdownApi(ctx: ChatPageMarkdownContext) {
ctx.openAssetHistory((params.get('asset_id') || '').trim(), (params.get('asset_name') || '').trim());
return;
}
if (rawHref.startsWith('#asset-image?')) {
event.preventDefault();
const params = new URLSearchParams(rawHref.slice('#asset-image?'.length));
ctx.openAssetImage((params.get('asset_id') || '').trim(), (params.get('asset_name') || '').trim());
return;
}
const preview = ctx.parsePlmPreviewLink(rawHref);
if (preview) {
event.preventDefault();
@@ -333,6 +448,7 @@ export function createChatPageMarkdownApi(ctx: ChatPageMarkdownContext) {
renderMarkdown,
onMarkdownClick: handleChatMarkdownClick,
openAssetHistory: ctx.openAssetHistory,
openAssetImage: ctx.openAssetImage,
getInlineDownloadFiles: safeExtractInlineDownloadFiles,
downloadInlineFile,
getShippingInventoryMappingPayload: ctx.getShippingInventoryMappingPayload,