'use client' import { useEffect, useState, Suspense } from 'react' import { useRouter, useSearchParams } from 'next/navigation' import Image from 'next/image' import BrandLogo from '@/components/BrandLogo' import { parseApiError, parseFetchError } from '@/lib/error-handler' import { DEFAULT_LOGIN_LOGO_URL, DEFAULT_SITE_NAME } from '@/lib/brand-assets' 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(DEFAULT_LOGIN_LOGO_URL) const [siteName, setSiteName] = useState(DEFAULT_SITE_NAME) useEffect(() => { // 加载系统设置 const loadSettings = async () => { try { const response = await fetch('/api/system-settings') if (response.ok) { const settings = await response.json() setLoginLogoUrl(settings.loginLogoUrl || DEFAULT_LOGIN_LOGO_URL) setSiteName(settings.siteName || DEFAULT_SITE_NAME) } } 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 } setSuccess('信息提交成功') router.push('/') router.refresh() } catch (error: any) { console.error('提交失败:', error) const { message } = parseApiError(error) setError(message) } finally { setLoading(false) } } if (error && !userInfo) { return (

出错了

{error}

) } return (
{/* Logo */}

完善个人信息

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

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

钉钉账号

{userInfo.realName}

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

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

) } export default function CompleteProfilePage() { return (

加载中...

}>
) }