'use client' import { useEffect, useState, Suspense } from 'react' import { useRouter, useSearchParams } from 'next/navigation' import { signIn } from 'next-auth/react' import Image from 'next/image' interface Department { id: string name: string } function CompleteProfileContent() { const router = useRouter() const searchParams = useSearchParams() const [department, setDepartment] = useState('') const [departments, setDepartments] = useState([]) const [loading, setLoading] = useState(false) const [error, setError] = useState('') const [success, setSuccess] = useState('') const [userInfo, setUserInfo] = useState(null) // 系统设置 const [loginLogoUrl, setLoginLogoUrl] = useState(process.env.NEXT_PUBLIC_DEFAULT_LOGIN_LOGO_URL || '') const [siteName, setSiteName] = useState('由壹点3D素材') useEffect(() => { // 加载系统设置 const loadSettings = async () => { try { const response = await fetch('/api/system-settings') if (response.ok) { const settings = await response.json() setLoginLogoUrl(settings.loginLogoUrl || loginLogoUrl) setSiteName(settings.siteName || siteName) } } catch (error) { console.error('加载系统设置失败:', error) } } // 加载部门列表 const loadDepartments = async () => { try { const response = await fetch('/api/departments') if (response.ok) { const data = await response.json() setDepartments(data || []) } } catch (error) { console.error('加载部门列表失败:', error) } } // 从URL参数获取用户信息 const userInfoParam = searchParams.get('userInfo') if (userInfoParam) { try { const info = JSON.parse(decodeURIComponent(userInfoParam)) setUserInfo(info) } catch (error) { console.error('解析用户信息失败:', error) setError('无效的用户信息,请重新登录') } } else { setError('缺少用户信息,请重新登录') } loadSettings() loadDepartments() }, [searchParams]) const handleSubmit = async (e: React.FormEvent) => { e.preventDefault() setError('') if (!department) { setError('请选择部门') return } setLoading(true) setError('') // 清空错误信息 setSuccess('') // 清空成功信息 try { // 调用API完善用户信息(只需要选择部门) const response = await fetch('/api/dingtalk/complete-profile', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ userId: userInfo?.id, department, }), }) const data = await response.json() if (!response.ok || !data.success) { throw new Error(data.message || '信息提交失败') } // 如果是账号关联(关联到已有账号),提示用户使用原账号密码登录 if (data.isAccountLink) { setSuccess('账号关联成功!正在跳转到登录页面,您可以使用原账号密码登录或继续使用钉钉扫码登录。') setTimeout(() => { // 智能跳转:保持在内网或外网环境 if (typeof window !== 'undefined' && window.location.hostname.includes('192.168.')) { window.location.href = `${window.location.origin}/login` } else { router.push('/login') } }, 3000) return } // 检查新用户是否已激活 if (data.user && !data.user.isActive) { setSuccess('信息提交成功!您的账号需要管理员激活后才能登录使用。') setTimeout(() => { // 智能跳转:保持在内网或外网环境 if (typeof window !== 'undefined' && window.location.hostname.includes('192.168.')) { window.location.href = `${window.location.origin}/login` } else { router.push('/login') } }, 3000) return } // 已激活用户:使用 NextAuth 自动登录 const result = await signIn('credentials', { email: data.user.email, password: 'dingtalk_auto_login', redirect: false, }) if (result?.error) { throw new Error('登录失败') } // 智能跳转:保持在内网或外网环境 if (typeof window !== 'undefined' && window.location.hostname.includes('192.168.')) { window.location.href = window.location.origin } else { router.push('/') router.refresh() } } catch (error: any) { console.error('提交失败:', error) setError(error.message || '提交失败,请稍后再试') } finally { setLoading(false) } } if (error && !userInfo) { return (

出错了

{error}

) } return (
{/* Logo */}
{siteName}

完善个人信息

首次登录,请完善您的用户信息

{/* Form */}
{userInfo && (
{userInfo.avatar && ( {userInfo.realName} )}

钉钉账号

{userInfo.realName}

)}
{error && (
{error}
)} {success && (
{success}
)} {/* 显示钉钉用户名(只读) */}
{userInfo?.username || userInfo?.realName || '加载中...'}

使用您的钉钉姓名作为系统用户名

) } export default function CompleteProfilePage() { return (

加载中...

}>
) }