import type { MetaFunction, LoaderFunctionArgs } from '@remix-run/node'; import { useLoaderData, useNavigate } from '@remix-run/react'; import { getContractTemplate } from '~/api/contract-template/templates'; import type { ContractTemplate } from '~/api/contract-template/templates'; import styles from '~/styles/pages/contract-template.css?url'; import filePreviewStyles from '~/styles/components/file-preview-isolation.css?url'; import { getUserSession } from '~/api/login/auth.server'; // 导入FilePreview组件 import { FilePreview } from '~/components/reviews'; // 导入统一的下载方法和提示服务 import { downloadFile } from '~/api/axios-client'; import { toastService } from '~/components/ui/Toast'; export const links = () => [ { rel: 'stylesheet', href: styles }, { rel: 'stylesheet', href: filePreviewStyles }, ]; export const meta: MetaFunction = ({ data }) => { return [ { title: `${data?.template.title || '合同模板详情'} - 智慧法务` }, { name: 'description', content: data?.template.description || '查看合同模板详细信息' } ]; }; // 面包屑导航配置 export const handle = { breadcrumb: (data: { template: { title: string } }) => { return data?.template?.title || "模板详情"; } }; export async function loader({ params, request }: LoaderFunctionArgs) { const templateId = params.id!; // 获取 JWT const { frontendJWT } = await getUserSession(request); const jwt = frontendJWT || undefined; try { const response = await getContractTemplate(templateId, jwt); if (response.error) { throw new Response(response.error, { status: response.status || 404 }); } if (!response.data) { throw new Response('模板未找到', { status: 404 }); } // 添加调试信息 // console.log('模板详情数据:', response.data); // console.log('分类信息:', response.data.category); return { template: response.data }; } catch (error) { console.error('加载模板详情失败:', error); throw new Response('模板未找到', { status: 404 }); } } export default function ContractTemplateDetail() { const { template }: { template: ContractTemplate } = useLoaderData(); const navigate = useNavigate(); // 注释掉收藏功能 // const [isFavorited, setIsFavorited] = useState(false); const handleBack = () => { navigate(-1); }; // 使用统一的下载方法(与 rules-files.tsx 相同) const handleDownload = async () => { if (!template.file_path) { toastService.error('文件路径不存在,无法下载'); return; } try { // 使用axios封装的下载方法 const blob = await downloadFile(template.file_path); // 创建Blob URL const blobUrl = URL.createObjectURL(blob); // 清理文件名,移除可能导致问题的字符 const fileExtension = template.file_format || 'docx'; const fileName = `${template.title}.${fileExtension}`; const cleanFileName = fileName.replace(/[<>:"/\\|?*]/g, '_'); // 创建一个隐藏的a标签并点击它 const a = document.createElement('a'); a.style.display = 'none'; a.href = blobUrl; a.download = cleanFileName; document.body.appendChild(a); a.click(); // 清理 setTimeout(() => { document.body.removeChild(a); URL.revokeObjectURL(blobUrl); }, 100); } catch (error) { console.error('下载文件失败:', error); toastService.error(`下载文件失败: ${error instanceof Error ? error.message : '未知错误'}`); } }; const handlePreview = () => { // console.log('预览模板:', template.id); // 页面内预览,滚动到预览区域 const previewElement = document.getElementById('template-preview'); if (previewElement) { previewElement.scrollIntoView({ behavior: 'smooth' }); } }; /* const handleFavorite = () => { setIsFavorited(!isFavorited); console.log('收藏状态:', !isFavorited); }; */ /* const handleShare = () => { // 复制当前页面URL到剪贴板 navigator.clipboard.writeText(window.location.href).then(() => { alert('链接已复制到剪贴板'); }).catch(() => { alert('复制失败,请手动复制链接'); }); }; */ // 注释掉评分相关功能 /* const renderStars = (rating: number) => { const stars = []; const fullStars = Math.floor(rating); for (let i = 0; i < 5; i++) { if (i < fullStars) { stars.push(); } else { stars.push(); } } return stars; }; */ // 创建文件内容对象用于FilePreview组件 // 优先使用原始文件路径(支持docx),如果没有则使用pdf_file_path const previewPath = template.file_path || template.pdf_file_path; const fileContent = previewPath ? { title: template.title, contractNumber: template.template_code, // 使用file_path以支持多种格式(docx/pdf) path: previewPath, parties: { partyA: { name: '', address: '', representative: '', phone: '' }, partyB: { name: '', address: '', representative: '', phone: '' } }, sections: [] } : null; return (
{/* 返回按钮 */}
{/* 模板详情 */}
{/* 详情头部 */}
{/* 注释掉类型和评分显示 */} {/*
{template.type}
{renderStars(template.rating)} {template.rating} (156评价)
*/}

{template.title}

模板编号: {template.template_code}
更新时间: {new Date(template.updated_at).toLocaleDateString('zh-CN')}
所属分类: {template.category?.name || '其他'}
文件格式: {template.file_format?.toUpperCase()}
{template.is_featured && (
特色推荐:
)} {/* 注释掉使用次数、文件大小、适用范围、法律依据等字段 */} {/*
使用次数: {template.useCount.toLocaleString()}次
文件大小: {template.fileSize}
适用范围: {template.scope}
法律依据: {template.legalBasis}
*/}
{template.pdf_file_path && ( )} {/* 注释掉收藏功能 */} {/* */} {/* */}
{/* 详情内容 */}
{/* 模板简介 */}

模板简介

{template.description || '该合同模板为标准格式,包含完整的合同条款结构,适用于相关业务场景的合同签署。'} {template.category?.description && ( <>

适用范围:{template.category.description} )}

{/* 注释掉主要特点模块 */} {/*

主要特点

{template.features.map((feature, index) => (
{feature.title}

{feature.description}

))}
*/} {/* 注释掉合同条款结构模块 */} {/*

合同条款结构

{template.structure.map((item) => (
{item.step}
{item.title}
{item.description}
))}
*/} {/* 合同预览 - 只有当存在pdf_file_path时才显示 */} {fileContent && (

合同预览

{/* 使用更强的样式隔离 */}
)} {/* 注释掉用户评价模块 */} {/*

用户评价

{template.reviews.map((review, index) => (
{review.user[0]}
{review.user}
{renderStars(review.rating)}
{review.date}

{review.comment}

))}
*/}
); }