571 lines
23 KiB
TypeScript
571 lines
23 KiB
TypeScript
/**
|
||
* 个人中心页面
|
||
*
|
||
* 参考:flnxi_sucai/app/profile/page.tsx
|
||
* 包含"我的素材"和"我的收藏"两个标签页
|
||
*/
|
||
|
||
'use client'
|
||
|
||
import { useSession } from 'next-auth/react'
|
||
import { useRouter } from 'next/navigation'
|
||
import { useState, useEffect } from 'react'
|
||
import Image from 'next/image'
|
||
import Header from '@/components/Header'
|
||
import VideoPlayerModal from '@/components/VideoPlayerModal'
|
||
import { useToast } from '@/hooks/useToast'
|
||
import ToastBubble from '@/components/ToastBubble'
|
||
import {
|
||
DOUYIN_ACCOUNT_MAINTENANCE_VIDEO_TYPE,
|
||
DOUYIN_QIANCHUAN_VIDEO_TYPE,
|
||
INSTALL_VIDEO_TYPE,
|
||
TAOXI_GUANGGUANG_VIDEO_TYPE,
|
||
TAOXI_MAIN_VIDEO_TYPE,
|
||
isVideoMaterialType,
|
||
} from '@/lib/video-material-types'
|
||
|
||
interface UserProfile {
|
||
id: string
|
||
username: string
|
||
email: string
|
||
avatar: string | null
|
||
department?: string
|
||
realName?: string | null
|
||
createdAt?: string
|
||
}
|
||
|
||
interface Material {
|
||
id: string
|
||
userId: string
|
||
productModel: string
|
||
previewUrl: string
|
||
displayImages?: string[]
|
||
materialType?: string
|
||
categoryName?: string
|
||
sceneName?: string
|
||
renderer?: string
|
||
authorName?: string
|
||
downloadCount: number
|
||
likeCount: number
|
||
createdAt: string
|
||
isFavorited?: boolean
|
||
isLiked?: boolean
|
||
zipFileName?: string
|
||
zipFileSize?: string | number
|
||
videoCoverUrl?: string | null
|
||
videoPreview540Url?: string | null
|
||
videoPreviewClipUrl?: string | null
|
||
videoSourceUrl?: string | null
|
||
videoProcessingStatus?: string | null
|
||
videoDuration?: number | null
|
||
}
|
||
|
||
export default function ProfilePage() {
|
||
const { data: session, status, update } = useSession()
|
||
const router = useRouter()
|
||
const [profile, setProfile] = useState<UserProfile | null>(null)
|
||
const [materials, setMaterials] = useState<Material[]>([])
|
||
const [favorites, setFavorites] = useState<Material[]>([])
|
||
const [loading, setLoading] = useState(true)
|
||
const [previewImage, setPreviewImage] = useState<{ url: string; title: string } | null>(null)
|
||
const [showVideoModal, setShowVideoModal] = useState(false)
|
||
const [videoSrc, setVideoSrc] = useState<string | null>(null)
|
||
const [showAvatarModal, setShowAvatarModal] = useState(false)
|
||
const { toast, showToast } = useToast()
|
||
const [avatarFile, setAvatarFile] = useState<File | null>(null)
|
||
const [avatarPreview, setAvatarPreview] = useState<string>('')
|
||
const [uploading, setUploading] = useState(false)
|
||
|
||
useEffect(() => {
|
||
if (status === 'unauthenticated') {
|
||
router.push('/login')
|
||
return
|
||
}
|
||
|
||
if (status === 'authenticated' && session) {
|
||
fetchProfile()
|
||
fetchMaterials()
|
||
fetchFavorites()
|
||
}
|
||
}, [status, session, router])
|
||
|
||
const fetchProfile = async () => {
|
||
try {
|
||
const res = await fetch('/api/user/profile')
|
||
if (res.ok) {
|
||
const data = await res.json()
|
||
setProfile(data)
|
||
}
|
||
} catch (error) {
|
||
console.error('Failed to fetch profile:', error)
|
||
}
|
||
}
|
||
|
||
const fetchMaterials = async () => {
|
||
try {
|
||
const res = await fetch('/api/user/materials')
|
||
if (res.ok) {
|
||
const data = await res.json()
|
||
setMaterials(data)
|
||
}
|
||
} catch (error) {
|
||
console.error('Failed to fetch materials:', error)
|
||
} finally {
|
||
setLoading(false)
|
||
}
|
||
}
|
||
|
||
const fetchFavorites = async () => {
|
||
try {
|
||
const res = await fetch('/api/user/favorites')
|
||
if (res.ok) {
|
||
const data = await res.json()
|
||
setFavorites(data)
|
||
}
|
||
} catch (error) {
|
||
console.error('Failed to fetch favorites:', error)
|
||
}
|
||
}
|
||
|
||
// 判断是否为视频素材
|
||
const isVideoMaterial = (materialType?: string) => {
|
||
return isVideoMaterialType(materialType)
|
||
}
|
||
|
||
// 格式化时长
|
||
const formatDuration = (seconds: number) => {
|
||
const mins = Math.floor(seconds / 60)
|
||
const secs = seconds % 60
|
||
return `${mins}:${secs.toString().padStart(2, '0')}`
|
||
}
|
||
|
||
// 获取素材类型标签样式
|
||
const getMaterialTypeBadgeClass = (materialType?: string) => {
|
||
const typeMap: Record<string, string> = {
|
||
'3D场景': 'bg-blue-500',
|
||
'详情页': 'bg-purple-500',
|
||
'详情模板': 'bg-purple-500',
|
||
'矢量文件': 'bg-green-500',
|
||
'C4D模型': 'bg-orange-500',
|
||
'视频': 'bg-red-500',
|
||
[TAOXI_MAIN_VIDEO_TYPE]: 'bg-red-500',
|
||
'主图视频': 'bg-red-500',
|
||
[INSTALL_VIDEO_TYPE]: 'bg-red-500',
|
||
[TAOXI_GUANGGUANG_VIDEO_TYPE]: 'bg-red-500',
|
||
'种草视频': 'bg-red-500',
|
||
[DOUYIN_QIANCHUAN_VIDEO_TYPE]: 'bg-red-500',
|
||
[DOUYIN_ACCOUNT_MAINTENANCE_VIDEO_TYPE]: 'bg-red-500',
|
||
'模特': 'bg-pink-500',
|
||
'主图模版': 'bg-indigo-500',
|
||
'SKU模版': 'bg-teal-500',
|
||
'音乐': 'bg-yellow-500',
|
||
'标准饰品': 'bg-cyan-500',
|
||
'品牌VI灵感': 'bg-rose-500',
|
||
'CAD': 'bg-gray-600',
|
||
'标准模型': 'bg-violet-500',
|
||
}
|
||
return typeMap[materialType || ''] || 'bg-gray-500'
|
||
}
|
||
|
||
const handleAvatarSelect = (e: React.ChangeEvent<HTMLInputElement>) => {
|
||
const file = e.target.files?.[0]
|
||
if (file) {
|
||
if (!file.type.startsWith('image/')) {
|
||
showToast('请选择图片文件', { variant: 'error' })
|
||
return
|
||
}
|
||
setAvatarFile(file)
|
||
setAvatarPreview(URL.createObjectURL(file))
|
||
setShowAvatarModal(true)
|
||
}
|
||
}
|
||
|
||
const handleAvatarUpload = async () => {
|
||
if (!avatarFile) return
|
||
|
||
setUploading(true)
|
||
try {
|
||
const formData = new FormData()
|
||
formData.append('avatar', avatarFile)
|
||
|
||
const res = await fetch('/api/user/avatar', {
|
||
method: 'POST',
|
||
body: formData,
|
||
})
|
||
|
||
if (res.ok) {
|
||
const data = await res.json()
|
||
console.log('[Profile] Avatar uploaded successfully:', data.avatarUrl)
|
||
|
||
// 关键:立即更新本地状态,显示新头像
|
||
setProfile(prev => prev ? { ...prev, avatar: data.avatarUrl } : null)
|
||
|
||
setShowAvatarModal(false)
|
||
setAvatarFile(null)
|
||
setAvatarPreview('')
|
||
|
||
// 立即更新 NextAuth 会话,触发 Header 中头像同步更新
|
||
await update({ avatar: data.avatarUrl })
|
||
} else {
|
||
const { parseFetchError } = await import('@/lib/error-handler')
|
||
const parsedError = await parseFetchError(res)
|
||
showToast(parsedError.message, { variant: 'error' })
|
||
}
|
||
} catch (error) {
|
||
console.error('Failed to upload avatar:', error)
|
||
const { handleError } = await import('@/lib/error-handler')
|
||
handleError(error, showToast)
|
||
} finally {
|
||
setUploading(false)
|
||
}
|
||
}
|
||
|
||
if (status === 'loading' || loading) {
|
||
return (
|
||
<div className="min-h-screen flex items-center justify-center bg-gray-50 dark:bg-gray-900">
|
||
<p className="text-gray-500 dark:text-gray-400">加载中...</p>
|
||
</div>
|
||
)
|
||
}
|
||
|
||
if (!session) {
|
||
return null
|
||
}
|
||
|
||
return (
|
||
<div className="min-h-screen bg-gray-50 dark:bg-gray-900">
|
||
<Header session={session} />
|
||
|
||
<div className="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-8">
|
||
{/* 用户信息卡片 */}
|
||
<div className="card-apple mb-6 p-6 dark:bg-gray-800 dark:border-gray-700">
|
||
<div className="flex flex-col sm:flex-row items-center sm:items-start gap-6">
|
||
{/* 头像 */}
|
||
<div className="relative">
|
||
{profile?.avatar ? (
|
||
<div className="w-20 h-20 rounded-full overflow-hidden border-2 border-gray-300 dark:border-gray-600">
|
||
<Image
|
||
src={profile.avatar}
|
||
alt="头像"
|
||
width={80}
|
||
height={80}
|
||
className="w-full h-full object-cover"
|
||
unoptimized
|
||
/>
|
||
</div>
|
||
) : (
|
||
<div className="w-20 h-20 rounded-full bg-gradient-to-br from-blue-500 to-purple-500 flex items-center justify-center text-white text-2xl font-bold shadow-lg">
|
||
{(profile?.username || session.user?.name || session.user?.email || 'U').charAt(0).toUpperCase()}
|
||
</div>
|
||
)}
|
||
<label className="absolute bottom-0 right-0 w-8 h-8 bg-blue-600 text-white rounded-full flex items-center justify-center cursor-pointer hover:bg-blue-700 transition-colors shadow-lg">
|
||
<svg className="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
|
||
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M3 9a2 2 0 012-2h.93a2 2 0 001.664-.89l.812-1.22A2 2 0 0110.07 4h3.86a2 2 0 011.664.89l.812 1.22A2 2 0 0018.07 7H19a2 2 0 012 2v9a2 2 0 01-2 2H5a2 2 0 01-2-2V9z" />
|
||
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M15 13a3 3 0 11-6 0 3 3 0 016 0z" />
|
||
</svg>
|
||
<input
|
||
type="file"
|
||
accept="image/*"
|
||
onChange={handleAvatarSelect}
|
||
className="hidden"
|
||
/>
|
||
</label>
|
||
</div>
|
||
|
||
{/* 用户信息 */}
|
||
<div className="flex-1 text-center sm:text-left">
|
||
<h1 className="text-2xl font-bold text-gray-900 dark:text-white mb-2">
|
||
{profile?.username || session.user?.name || session.user?.email || '用户'}
|
||
</h1>
|
||
<div className="text-sm text-gray-500 dark:text-gray-400 space-y-1 mb-4">
|
||
<p>@{(profile?.email || session.user?.email || 'user').split('@')[0]}</p>
|
||
<p>{profile?.email || session.user?.email}</p>
|
||
{profile?.department ? (
|
||
<p>部门:{profile.department}</p>
|
||
) : null}
|
||
{profile?.createdAt ? (
|
||
<p>注册时间:{new Date(profile.createdAt as any).toLocaleDateString()}</p>
|
||
) : null}
|
||
</div>
|
||
<div className="flex flex-wrap gap-4 text-sm">
|
||
<div>
|
||
<span className="text-gray-500 dark:text-gray-400">我的素材:</span>
|
||
<span className="text-gray-700 dark:text-gray-300 font-semibold">{materials.length}</span>
|
||
</div>
|
||
<div>
|
||
<span className="text-gray-500 dark:text-gray-400">我的收藏:</span>
|
||
<span className="text-gray-700 dark:text-gray-300 font-semibold">{favorites.length}</span>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
|
||
<div className="space-y-6">
|
||
{/* 我的素材卡片(上) */}
|
||
<div className="card-apple dark:bg-gray-800 dark:border-gray-700">
|
||
<div className="flex items-center justify-between px-4 pt-4">
|
||
<div>
|
||
<h2 className="text-lg font-semibold text-gray-900 dark:text-white">我的素材</h2>
|
||
<p className="text-sm text-gray-500 dark:text-gray-400">已上传 {materials.length} 个</p>
|
||
</div>
|
||
</div>
|
||
|
||
{materials.length === 0 ? (
|
||
<div className="text-center py-12">
|
||
<p className="text-gray-500 dark:text-gray-400">还没有上传过素材</p>
|
||
</div>
|
||
) : (
|
||
<div className="grid grid-cols-2 sm:grid-cols-3 md:grid-cols-4 lg:grid-cols-5 gap-4 p-4">
|
||
{materials.map((material) => {
|
||
const isVideo = isVideoMaterial(material.materialType)
|
||
const clipUrl = material.videoPreviewClipUrl || material.videoPreview540Url
|
||
const poster = material.videoCoverUrl || material.previewUrl
|
||
|
||
return (
|
||
<div
|
||
key={material.id}
|
||
className="relative aspect-[3/4] rounded-2xl overflow-hidden cursor-pointer group bg-white dark:bg-gray-800 border border-gray-200 dark:border-gray-700 shadow-lg hover:shadow-xl transition-shadow"
|
||
onClick={() => {
|
||
// 所有素材类型统一跳转到详情页面
|
||
router.push(`/share/${material.id}`)
|
||
}}
|
||
>
|
||
{/* 预览图/视频 */}
|
||
<div className="relative w-full h-full bg-gray-200 dark:bg-gray-700 overflow-hidden">
|
||
{isVideo && clipUrl ? (
|
||
<video
|
||
src={clipUrl}
|
||
poster={poster || undefined}
|
||
className="w-full h-full object-cover transition-transform group-hover:scale-105"
|
||
muted
|
||
loop
|
||
playsInline
|
||
preload="metadata"
|
||
/>
|
||
) : material.previewUrl ? (
|
||
<Image
|
||
src={material.previewUrl}
|
||
alt={material.productModel}
|
||
fill
|
||
unoptimized
|
||
className="object-cover transition-transform group-hover:scale-105"
|
||
sizes="(max-width: 768px) 100vw, 300px"
|
||
/>
|
||
) : (
|
||
<div className="w-full h-full flex items-center justify-center bg-gray-300 dark:bg-gray-600">
|
||
<span className="text-gray-500 dark:text-gray-400">暂无预览图</span>
|
||
</div>
|
||
)}
|
||
|
||
{/* SPU标签 */}
|
||
<div className="absolute top-2 left-2 bg-black/70 text-white px-2 py-1 rounded text-xs font-medium">
|
||
{material.productModel}
|
||
</div>
|
||
|
||
|
||
|
||
{/* 视频时长角标 */}
|
||
{isVideo && material.videoDuration && (
|
||
<div className="absolute bottom-2 left-2 bg-black/70 text-white px-2 py-1 rounded text-xs font-medium">
|
||
{formatDuration(material.videoDuration)}
|
||
</div>
|
||
)}
|
||
</div>
|
||
|
||
{/* 素材信息 */}
|
||
<div className="absolute bottom-0 left-0 right-0 bg-gradient-to-t from-black/80 to-transparent p-3">
|
||
<p className="text-sm font-medium text-white truncate mb-1">
|
||
{material.productModel}
|
||
</p>
|
||
<div className="flex items-center justify-between text-xs text-white/80">
|
||
<span>下载 {material.downloadCount}</span>
|
||
<span>点赞 {material.likeCount}</span>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
)
|
||
})}
|
||
</div>
|
||
)}
|
||
</div>
|
||
|
||
{/* 我的收藏卡片(下) */}
|
||
<div className="card-apple dark:bg-gray-800 dark:border-gray-700">
|
||
<div className="flex items-center justify-between px-4 pt-4">
|
||
<div>
|
||
<h2 className="text-lg font-semibold text-gray-900 dark:text-white">我的收藏</h2>
|
||
<p className="text-sm text-gray-500 dark:text-gray-400">已收藏 {favorites.length} 个</p>
|
||
</div>
|
||
</div>
|
||
|
||
{favorites.length === 0 ? (
|
||
<div className="text-center py-12">
|
||
<p className="text-gray-500 dark:text-gray-400">还没有收藏素材</p>
|
||
</div>
|
||
) : (
|
||
<div className="grid grid-cols-2 sm:grid-cols-3 md:grid-cols-4 lg:grid-cols-5 gap-4 p-4">
|
||
{favorites.map((material) => {
|
||
const isVideo = isVideoMaterial(material.materialType)
|
||
const clipUrl = material.videoPreviewClipUrl || material.videoPreview540Url
|
||
const poster = material.videoCoverUrl || material.previewUrl
|
||
|
||
return (
|
||
<div
|
||
key={material.id}
|
||
className="relative aspect-[3/4] rounded-2xl overflow-hidden cursor-pointer group bg-white dark:bg-gray-800 border border-gray-200 dark:border-gray-700 shadow-lg hover:shadow-xl transition-shadow"
|
||
onClick={() => {
|
||
// 所有素材类型统一跳转到详情页面
|
||
router.push(`/share/${material.id}`)
|
||
}}
|
||
>
|
||
<div className="relative w-full h-full bg-gray-200 dark:bg-gray-700 overflow-hidden">
|
||
{isVideo && clipUrl ? (
|
||
<video
|
||
src={clipUrl}
|
||
poster={poster || undefined}
|
||
className="w-full h-full object-cover transition-transform group-hover:scale-105"
|
||
muted
|
||
loop
|
||
playsInline
|
||
preload="metadata"
|
||
/>
|
||
) : material.previewUrl ? (
|
||
<Image
|
||
src={material.previewUrl}
|
||
alt={material.productModel}
|
||
fill
|
||
unoptimized
|
||
className="object-cover transition-transform group-hover:scale-105"
|
||
sizes="(max-width: 768px) 100vw, 300px"
|
||
/>
|
||
) : (
|
||
<div className="w-full h-full flex items-center justify-center bg-gray-300 dark:bg-gray-600">
|
||
<span className="text-gray-500 dark:text-gray-400">暂无预览图</span>
|
||
</div>
|
||
)}
|
||
|
||
<div className="absolute top-2 left-2 bg-black/70 text-white px-2 py-1 rounded text-xs font-medium">
|
||
{material.productModel}
|
||
</div>
|
||
|
||
{isVideo && material.videoDuration && (
|
||
<div className="absolute bottom-2 left-2 bg-black/70 text-white px-2 py-1 rounded text-xs font-medium">
|
||
{formatDuration(material.videoDuration)}
|
||
</div>
|
||
)}
|
||
|
||
{/* 收藏角标 */}
|
||
<div className="absolute bottom-2 right-2 bg-red-500 text-white p-1.5 rounded-full">
|
||
<svg className="w-4 h-4" fill="currentColor" viewBox="0 0 24 24">
|
||
<path d="M12 21.35l-1.45-1.32C5.4 15.36 2 12.28 2 8.5 2 5.42 4.42 3 7.5 3c1.74 0 3.41.81 4.5 2.09C13.09 3.81 14.76 3 16.5 3 19.58 3 22 5.42 22 8.5c0 3.78-3.4 6.86-8.55 11.54L12 21.35z" />
|
||
</svg>
|
||
</div>
|
||
</div>
|
||
|
||
<div className="absolute bottom-0 left-0 right-0 bg-gradient-to-t from-black/80 to-transparent p-3">
|
||
<p className="text-sm font-medium text-white truncate mb-1">
|
||
{material.productModel}
|
||
</p>
|
||
<div className="flex items-center justify-between text-xs text-white/80">
|
||
<span>下载 {material.downloadCount}</span>
|
||
<span>点赞 {material.likeCount}</span>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
)
|
||
})}
|
||
</div>
|
||
)}
|
||
</div>
|
||
</div>
|
||
</div>
|
||
|
||
{/* 图片预览模态框 */}
|
||
{previewImage && (
|
||
<div
|
||
className="fixed inset-0 z-[100] bg-black/90 flex items-center justify-center p-4"
|
||
onClick={() => setPreviewImage(null)}
|
||
>
|
||
<div className="relative max-w-7xl max-h-[90vh] w-full h-full flex items-center justify-center">
|
||
<Image
|
||
src={previewImage.url}
|
||
alt={previewImage.title}
|
||
fill
|
||
unoptimized
|
||
className="object-contain"
|
||
sizes="90vw"
|
||
/>
|
||
<button
|
||
onClick={() => setPreviewImage(null)}
|
||
className="absolute top-4 right-4 text-white bg-black/50 hover:bg-black/70 rounded-full p-2 transition-all"
|
||
>
|
||
<svg className="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24">
|
||
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M6 18L18 6M6 6l12 12" />
|
||
</svg>
|
||
</button>
|
||
</div>
|
||
</div>
|
||
)}
|
||
|
||
{/* Video Player Modal(参考首页实现) */}
|
||
{showVideoModal && videoSrc && (
|
||
<VideoPlayerModal
|
||
videoSrc={videoSrc}
|
||
onClose={() => {
|
||
setShowVideoModal(false)
|
||
setVideoSrc(null)
|
||
}}
|
||
/>
|
||
)}
|
||
|
||
{/* 头像上传模态框 */}
|
||
{showAvatarModal && (
|
||
<div className="fixed inset-0 z-[100] bg-black/50 flex items-center justify-center p-4" onClick={() => setShowAvatarModal(false)}>
|
||
<div className="bg-white dark:bg-gray-800 rounded-lg p-6 max-w-md w-full" onClick={(e) => e.stopPropagation()}>
|
||
<h3 className="text-lg font-bold text-gray-900 dark:text-white mb-4">上传头像</h3>
|
||
{avatarPreview && (
|
||
<div className="mb-4 flex justify-center">
|
||
<div className="w-32 h-32 rounded-full overflow-hidden border-2 border-gray-300 dark:border-gray-600">
|
||
<Image
|
||
src={avatarPreview}
|
||
alt="预览"
|
||
width={128}
|
||
height={128}
|
||
className="w-full h-full object-cover"
|
||
unoptimized
|
||
/>
|
||
</div>
|
||
</div>
|
||
)}
|
||
<div className="flex gap-3">
|
||
<button
|
||
onClick={() => {
|
||
setShowAvatarModal(false)
|
||
setAvatarFile(null)
|
||
setAvatarPreview('')
|
||
}}
|
||
className="flex-1 px-4 py-2 rounded-lg bg-gray-200 dark:bg-gray-700 text-gray-700 dark:text-gray-300 hover:bg-gray-300 dark:hover:bg-gray-600 transition-colors"
|
||
>
|
||
取消
|
||
</button>
|
||
<button
|
||
onClick={handleAvatarUpload}
|
||
disabled={uploading}
|
||
className="flex-1 px-4 py-2 rounded-lg bg-blue-600 text-white hover:bg-blue-700 disabled:opacity-50 disabled:cursor-not-allowed transition-colors"
|
||
>
|
||
{uploading ? '上传中...' : '确认上传'}
|
||
</button>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
)}
|
||
|
||
{/* 统一的Toast提示 */}
|
||
{toast && toast.show && (
|
||
<ToastBubble message={toast.message} variant={toast.variant} />
|
||
)}
|
||
</div>
|
||
)
|
||
}
|