2025.11.27.22.40

This commit is contained in:
RUI
2025-11-27 22:43:24 +08:00
parent 5dbb30b32c
commit 0d73d0c63b
20 changed files with 1154 additions and 226 deletions

View File

@@ -1,12 +1,16 @@
import React, { useState, useEffect } from 'react';
import { useRouter } from 'next/router';
import Image from 'next/image';
import Link from 'next/link';
import { useAuth } from '@/hooks/useAuth';
import MainLayout from '@/components/layouts/MainLayout';
import CommentSection from '@/components/article/CommentSection';
import { Button } from "@/components/ui/button";
import { Badge } from "@/components/ui/badge";
import { Skeleton } from "@/components/ui/skeleton";
import { Loader2, Lock, Download, Calendar, User as UserIcon, Tag as TagIcon } from 'lucide-react';
import { Card, CardContent, CardHeader, CardTitle, CardDescription, CardFooter } from "@/components/ui/card";
import { Avatar, AvatarImage, AvatarFallback } from "@/components/ui/avatar";
import { Loader2, Lock, Download, Calendar, User as UserIcon, Tag as TagIcon, Share2, Crown, Sparkles, FileText, Eye, ShoppingCart, Copy, Check, Box, HardDrive } from 'lucide-react';
import { format } from 'date-fns';
import { zhCN } from 'date-fns/locale';
import { toast } from 'sonner';
@@ -22,22 +26,27 @@ interface Article {
头像: string;
};
ID: {
_id: string;
分类名称: string;
};
ID列表: {
标签名称: string;
}[];
价格: number;
: 'points' | 'cash';
: 'points' | 'cash' | 'membership_free';
?: {
下载链接: string;
提取码: string;
解压密码: string;
隐藏内容: string;
版本号?: string;
文件大小?: string;
?: { 属性名: string; 属性值: string }[];
};
createdAt: string;
: {
阅读数: number;
销量: number;
};
}
@@ -46,13 +55,16 @@ export default function ArticleDetail() {
const { id } = router.query;
const { user, loading: authLoading } = useAuth();
const [article, setArticle] = useState<Article | null>(null);
const [recentArticles, setRecentArticles] = useState<Article[]>([]);
const [loading, setLoading] = useState(true);
const [hasAccess, setHasAccess] = useState(false);
const [purchasing, setPurchasing] = useState(false);
const [copiedField, setCopiedField] = useState<string | null>(null);
useEffect(() => {
if (id) {
fetchArticle();
fetchRecentArticles();
}
}, [id]);
@@ -74,6 +86,20 @@ export default function ArticleDetail() {
}
};
const fetchRecentArticles = async () => {
try {
const res = await fetch('/api/articles?limit=5');
if (res.ok) {
const data = await res.json();
// Filter out current article if present
const filtered = data.articles.filter((a: Article) => a._id !== id);
setRecentArticles(filtered.slice(0, 5));
}
} catch (error) {
console.error('Failed to fetch recent articles', error);
}
};
const handlePurchase = async () => {
if (!user) {
router.push(`/auth/login?redirect=${encodeURIComponent(router.asPath)}`);
@@ -113,22 +139,43 @@ export default function ArticleDetail() {
}
};
const handleShare = () => {
const url = window.location.href;
navigator.clipboard.writeText(url).then(() => {
toast.success('链接已复制到剪贴板');
}).catch(() => {
toast.error('复制失败');
});
};
const copyToClipboard = (text: string, field: string) => {
navigator.clipboard.writeText(text).then(() => {
setCopiedField(field);
toast.success('复制成功');
setTimeout(() => setCopiedField(null), 2000);
}).catch(() => {
toast.error('复制失败');
});
};
if (loading) {
return (
<MainLayout>
<div className="container mx-auto px-4 py-8 max-w-4xl">
<div className="space-y-4 mb-8">
<Skeleton className="h-12 w-3/4" />
<div className="flex gap-4">
<Skeleton className="h-4 w-24" />
<Skeleton className="h-4 w-24" />
<div className="container mx-auto px-4 py-8 max-w-6xl">
<div className="grid grid-cols-1 lg:grid-cols-4 gap-8">
<div className="lg:col-span-3 space-y-8">
<Skeleton className="h-12 w-3/4" />
<Skeleton className="h-[400px] w-full rounded-xl" />
<div className="space-y-4">
<Skeleton className="h-4 w-full" />
<Skeleton className="h-4 w-full" />
<Skeleton className="h-4 w-5/6" />
</div>
</div>
<div className="lg:col-span-1 space-y-6">
<Skeleton className="h-[300px] w-full rounded-xl" />
<Skeleton className="h-[200px] w-full rounded-xl" />
</div>
</div>
<Skeleton className="h-[400px] w-full rounded-xl mb-8" />
<div className="space-y-4">
<Skeleton className="h-4 w-full" />
<Skeleton className="h-4 w-full" />
<Skeleton className="h-4 w-5/6" />
</div>
</div>
</MainLayout>
@@ -145,137 +192,271 @@ export default function ArticleDetail() {
keywords: article.标签ID列表?.map(t => t.).join(',')
}}
>
<div className="max-w-4xl mx-auto px-4 py-8">
{/* Article Header */}
<div className="mb-8">
<div className="flex items-center gap-2 mb-4">
<Badge variant="secondary" className="text-primary bg-primary/10 hover:bg-primary/20">
{article.ID?. || '未分类'}
</Badge>
{article.ID列表?.map((tag, index) => (
<Badge key={index} variant="outline" className="text-gray-500">
{tag.}
</Badge>
))}
</div>
<div className="max-w-7xl mx-auto px-4 py-8">
<div className="grid grid-cols-1 lg:grid-cols-4 gap-8">
{/* Main Content - Left Column (75%) */}
<div className="lg:col-span-3">
{/* Article Header */}
<div className="mb-8">
<div className="flex items-center gap-2 mb-4">
<Badge variant="secondary" className="text-primary bg-primary/10 hover:bg-primary/20">
{article.ID?. || '未分类'}
</Badge>
{article.ID列表?.map((tag, index) => (
<Badge key={index} variant="outline" className="text-gray-500">
{tag.}
</Badge>
))}
</div>
<h1 className="text-3xl md:text-4xl font-bold text-gray-900 mb-6 leading-tight">
{article.}
</h1>
<h1 className="text-3xl md:text-4xl font-bold text-gray-900 mb-6 leading-tight">
{article.}
</h1>
<div className="flex items-center gap-6 text-sm text-gray-500 border-b border-gray-100 pb-8">
<div className="flex items-center gap-2">
<UserIcon className="w-4 h-4" />
<span>{article.ID?. || '匿名'}</span>
</div>
<div className="flex items-center gap-2">
<Calendar className="w-4 h-4" />
<span>{format(new Date(article.createdAt), 'yyyy-MM-dd', { locale: zhCN })}</span>
</div>
<div>
{article.?. || 0}
</div>
</div>
</div>
{/* Cover Image */}
{article. && (
<div className="mb-10 rounded-xl overflow-hidden shadow-lg">
<img
src={article.}
alt={article.}
className="w-full h-auto object-cover max-h-[500px]"
/>
</div>
)}
{/* Article Content */}
<article className="prose prose-lg max-w-none mb-12 prose-headings:text-gray-900 prose-p:text-gray-700 prose-a:text-primary hover:prose-a:text-primary/80 prose-img:rounded-xl dark:prose-invert">
<div dangerouslySetInnerHTML={{ __html: article.正文内容 }} />
</article>
{/* Resource Download / Paywall Section */}
<div className="bg-gray-50 rounded-xl p-8 mb-12 border border-gray-100">
{hasAccess ? (
<div className="space-y-6">
<h3 className="text-xl font-bold flex items-center gap-2 text-green-700">
<Download className="w-6 h-6" />
</h3>
{article. ? (
<div className="grid gap-4 md:grid-cols-2">
<div className="bg-white p-4 rounded-lg border border-gray-200">
<span className="text-gray-500 text-sm block mb-1"></span>
<a
href={article..}
target="_blank"
rel="noopener noreferrer"
className="text-primary font-medium hover:underline break-all"
>
{article..}
</a>
<div className="flex flex-wrap items-center justify-between gap-4 border-b border-gray-100 pb-8">
<div className="flex items-center gap-6 text-sm text-gray-500">
<div className="flex items-center gap-2">
<UserIcon className="w-4 h-4" />
<span>{article.ID?. || '匿名'}</span>
</div>
{article.. && (
<div className="bg-white p-4 rounded-lg border border-gray-200">
<span className="text-gray-500 text-sm block mb-1"></span>
<code className="bg-gray-100 px-2 py-1 rounded text-gray-900 font-mono">
{article..}
</code>
<div className="flex items-center gap-2">
<Calendar className="w-4 h-4" />
<span>{format(new Date(article.createdAt), 'yyyy-MM-dd', { locale: zhCN })}</span>
</div>
<div className="flex items-center gap-2">
<Eye className="w-4 h-4" />
<span>{article.?. || 0} </span>
</div>
</div>
<Button variant="ghost" size="sm" onClick={handleShare} className="text-gray-500 hover:text-primary">
<Share2 className="w-4 h-4 mr-2" />
</Button>
</div>
</div>
{/* Cover Image */}
{article. && (
<div className="mb-10 rounded-xl overflow-hidden shadow-lg relative aspect-video bg-gray-100">
<Image
src={article.}
alt={article.}
fill
className="object-cover"
priority
sizes="(max-width: 768px) 100vw, (max-width: 1200px) 80vw, 800px"
/>
</div>
)}
{/* Article Content */}
<article className="prose prose-lg max-w-none mb-12 prose-headings:text-gray-900 prose-p:text-gray-700 prose-a:text-primary hover:prose-a:text-primary/80 prose-img:rounded-xl dark:prose-invert">
<div dangerouslySetInnerHTML={{ __html: article.正文内容 }} />
</article>
{/* Comments Section */}
<CommentSection articleId={article._id} isLoggedIn={!!user} />
</div>
{/* Sidebar - Right Column (25%) */}
<div className="lg:col-span-1 space-y-6">
{/* Resource Card */}
<Card className="border-primary/20 shadow-md overflow-hidden">
<div className="bg-linear-to-r from-primary/10 to-primary/5 p-4 border-b border-primary/10">
<h3 className="font-bold text-lg flex items-center gap-2 text-gray-900">
<Download className="w-5 h-5 text-primary" />
</h3>
</div>
<CardContent className="p-6 space-y-6">
<div className="flex items-baseline justify-between">
<span className="text-sm text-gray-500"></span>
<div className="flex items-baseline gap-1">
<span className="text-2xl font-bold text-primary">
{article. > 0 ? `¥${article.}` : '免费'}
</span>
{article. > 0 && <span className="text-xs text-gray-400">/ </span>}
</div>
</div>
{/* Resource Attributes (Version, Size, etc.) */}
{article. && (
<div className="grid grid-cols-2 gap-2 text-xs text-gray-600 bg-gray-50 p-3 rounded-lg border border-gray-100">
{article.. && (
<div className="flex items-center gap-1.5">
<Box className="w-3.5 h-3.5 text-gray-400" />
<span>: {article..}</span>
</div>
)}
{article.. && (
<div className="flex items-center gap-1.5">
<HardDrive className="w-3.5 h-3.5 text-gray-400" />
<span>: {article..}</span>
</div>
)}
{article..?.map((attr, idx) => (
<div key={idx} className="flex items-center gap-1.5 col-span-2">
<TagIcon className="w-3.5 h-3.5 text-gray-400" />
<span>{attr.}: {attr.}</span>
</div>
))}
</div>
)}
{hasAccess ? (
<div className="space-y-4 animate-in fade-in duration-500">
<div className="bg-green-50 text-green-700 px-4 py-3 rounded-lg text-sm flex items-center gap-2 border border-green-100">
<Check className="w-4 h-4" />
</div>
)}
{article.. && (
<div className="bg-white p-4 rounded-lg border border-gray-200">
<span className="text-gray-500 text-sm block mb-1"></span>
<code className="bg-gray-100 px-2 py-1 rounded text-gray-900 font-mono">
{article..}
</code>
{article. && (
<div className="space-y-3">
<Button className="w-full" asChild>
<a href={article..} target="_blank" rel="noopener noreferrer">
<Download className="w-4 h-4 mr-2" />
</a>
</Button>
{article.. && (
<div className="flex items-center justify-between bg-gray-50 p-2 rounded text-sm border border-gray-100">
<span className="text-gray-500 pl-1">: {article..}</span>
<Button
variant="ghost"
size="icon"
className="h-6 w-6"
onClick={() => copyToClipboard(article.!., 'code')}
>
{copiedField === 'code' ? <Check className="w-3 h-3 text-green-600" /> : <Copy className="w-3 h-3" />}
</Button>
</div>
)}
{article.. && (
<div className="flex items-center justify-between bg-gray-50 p-2 rounded text-sm border border-gray-100">
<span className="text-gray-500 pl-1">: {article..}</span>
<Button
variant="ghost"
size="icon"
className="h-6 w-6"
onClick={() => copyToClipboard(article.!., 'pwd')}
>
{copiedField === 'pwd' ? <Check className="w-3 h-3 text-green-600" /> : <Copy className="w-3 h-3" />}
</Button>
</div>
)}
{article.. && (
<div className="bg-amber-50 p-3 rounded text-sm border border-amber-100 text-amber-800">
<div className="font-medium mb-1 flex items-center gap-1">
<Lock className="w-3 h-3" />
</div>
<div className="whitespace-pre-wrap text-xs opacity-90">{article..}</div>
</div>
)}
</div>
)}
</div>
) : (
<div className="space-y-4">
<Button
className="w-full bg-linear-to-r from-primary to-purple-600 hover:from-primary/90 hover:to-purple-600/90 text-white shadow-lg shadow-primary/20"
onClick={handlePurchase}
disabled={purchasing}
>
{purchasing ? (
<Loader2 className="w-4 h-4 mr-2 animate-spin" />
) : (
<ShoppingCart className="w-4 h-4 mr-2" />
)}
</Button>
{article. === 'membership_free' ? (
<div className="text-center">
<Link href="/membership" className="text-xs text-primary font-medium hover:underline flex items-center justify-center gap-1">
<Crown className="w-3 h-3 text-yellow-500" />
()
</Link>
</div>
) : (
<div className="text-center">
<Link href="/membership" className="text-xs text-gray-500 hover:text-primary flex items-center justify-center gap-1">
<Crown className="w-3 h-3 text-yellow-500" />
</Link>
</div>
)}
</div>
)}
<div className="pt-4 border-t border-gray-100 grid grid-cols-2 gap-4 text-center text-sm text-gray-500">
<div>
<div className="text-gray-900 font-medium">{article.?. || 0}</div>
<div className="text-xs mt-1"></div>
</div>
<div>
<div className="text-gray-900 font-medium">{article.?. || 0}</div>
<div className="text-xs mt-1"></div>
</div>
</div>
</CardContent>
</Card>
{/* Recent Posts Card */}
<Card>
<CardHeader className="pb-3">
<CardTitle className="text-base flex items-center gap-2">
<FileText className="w-4 h-4 text-primary" />
</CardTitle>
</CardHeader>
<CardContent className="px-0 pb-2">
<div className="flex flex-col">
{recentArticles.length > 0 ? (
recentArticles.map((item, index) => (
<Link
key={item._id}
href={`/article/${item._id}`}
className="group flex items-start gap-3 px-6 py-3 hover:bg-gray-50 transition-colors border-b border-gray-50 last:border-0"
>
<div className="relative w-16 h-12 shrink-0 rounded overflow-hidden bg-gray-100">
{item. ? (
<Image
src={item.}
alt={item.}
fill
className="object-cover group-hover:scale-105 transition-transform duration-300"
sizes="64px"
/>
) : (
<div className="w-full h-full flex items-center justify-center text-gray-300">
<FileText className="w-6 h-6" />
</div>
)}
</div>
<div className="flex-1 min-w-0">
<h4 className="text-sm font-medium text-gray-900 line-clamp-2 group-hover:text-primary transition-colors">
{item.}
</h4>
<div className="flex items-center gap-2 mt-1 text-xs text-gray-400">
<span>{format(new Date(item.createdAt), 'MM-dd')}</span>
</div>
</div>
</Link>
))
) : (
<div className="px-6 py-4 text-sm text-gray-500 text-center">
</div>
)}
</div>
) : (
<p className="text-gray-500"></p>
)}
</div>
) : (
<div className="text-center py-8">
<div className="w-16 h-16 bg-blue-100 rounded-full flex items-center justify-center mx-auto mb-6">
<Lock className="w-8 h-8 text-primary" />
</div>
<h3 className="text-2xl font-bold text-gray-900 mb-2">
</h3>
<p className="text-gray-600 mb-8 max-w-md mx-auto">
</p>
<div className="flex flex-col sm:flex-row gap-4 justify-center">
<Button
size="lg"
className="px-8"
onClick={handlePurchase}
disabled={purchasing}
>
{purchasing ? (
<Loader2 className="w-4 h-4 mr-2 animate-spin" />
) : (
<span className="mr-2">¥{article.}</span>
)}
</Button>
<Button
size="lg"
variant="outline"
onClick={() => router.push('/membership')}
>
()
</Button>
</div>
</div>
)}
</CardContent>
</Card>
</div>
</div>
{/* Comments Section */}
<CommentSection articleId={article._id} isLoggedIn={!!user} />
</div>
</MainLayout>
);