/* Copyright (C) 2025 QuantumNous This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see . For commercial licensing, please contact support@quantumnous.com */ import React, { useContext, useRef, useState } from 'react'; import { useNavigate } from 'react-router-dom'; import { useTranslation } from 'react-i18next'; import { Form, Button, Toast, Banner } from '@/components/ui'; import Turnstile from 'react-turnstile'; import { API } from '../../helpers/api'; import { showError, getLogo, getSystemName } from '../../helpers'; import { StatusContext } from '../../context/Status'; import OrgCredentialResult from '../../components/auth/OrgCredentialResult'; import GlassCard from '../../components/auth/GlassCard'; import AuthCardHeader from '../../components/auth/AuthCardHeader'; import AuthLoginTabs from '../../components/auth/AuthLoginTabs'; import AuthDivider from '../../components/auth/AuthDivider'; import AuthBackground from '../../components/common/AuthBackground'; import { LOGIN_NAME_PATTERN, isReservedLoginName, } from '../../helpers/orgLoginName'; /** * 企业自助注册页(原 OrgRegisterModal 弹窗升级而来)。 * 与 /login、/register、/org/login 共用同一套认证页布局(背景 + 个人/企业切换 + 玻璃卡片), * 拥有独立 URL 便于直达与分享;注册成功后在本页展示一次性凭证, * 「立即登录」经 /org/login?org= 预填企业代码直达登录第二步。 */ const OrgRegister = () => { const { t } = useTranslation(); const navigate = useNavigate(); const [statusState] = useContext(StatusContext); const status = statusState?.status || {}; const statusLoaded = statusState?.status !== undefined; const selfRegisterEnabled = !!status.org_self_register; const emailVerifyEnabled = !!status.email_verification; const turnstileEnabled = !!status.turnstile_check; const turnstileSiteKey = status.turnstile_site_key || ''; const logo = getLogo(); const systemName = getSystemName(); const [values, setValues] = useState({}); const formApi = useRef(null); const [submitting, setSubmitting] = useState(false); const [turnstileToken, setTurnstileToken] = useState(''); const [codeLoading, setCodeLoading] = useState(false); const [countdown, setCountdown] = useState(0); const [result, setResult] = useState(null); // 注册成功后的一次性凭证 const startCountdown = () => { setCountdown(30); const timer = setInterval(() => { setCountdown((c) => { if (c <= 1) { clearInterval(timer); return 0; } return c - 1; }); }, 1000); }; const sendCode = async () => { const email = (values.contact_email || '').trim(); if (!email) { Toast.warning(t('请先输入联系邮箱')); return; } if (turnstileEnabled && !turnstileToken) { Toast.info(t('请稍候,正在进行人机校验')); return; } setCodeLoading(true); try { // 企业注册专用发码:不校验邮箱是否已被个人账号占用(联系邮箱非登录名) const res = await API.get( `/api/org/register/verification?email=${encodeURIComponent(email)}&turnstile=${turnstileToken}`, ); if (res.data.success) { Toast.success(t('验证码已发送,请查收邮箱')); startCountdown(); } else { showError(res.data.message); } } catch (err) { if (!err?.isAxiosError) showError(err.message); } finally { setCodeLoading(false); } }; const handleSubmit = async () => { let formValues = values; try { formValues = await formApi.current?.validate(); } catch { return; // 校验不通过,Form 已高亮 } if (turnstileEnabled && !turnstileToken) { Toast.info(t('请稍候,正在进行人机校验')); return; } setSubmitting(true); try { const res = await API.post( `/api/org/register?turnstile=${turnstileToken}`, { name: (formValues.name || '').trim(), owner_login_name: (formValues.owner_login_name || '').trim(), owner_display_name: (formValues.owner_display_name || '').trim(), contact_name: (formValues.contact_name || '').trim(), contact_email: (formValues.contact_email || '').trim(), contact_phone: (formValues.contact_phone || '').trim(), verify_code: (formValues.verify_code || '').trim(), }, ); if (res.data.success) { setResult(res.data.data); // 展示一次性凭证 } else { showError(res.data.message); } } catch (err) { if (!err?.isAxiosError) showError(err.message); } finally { setSubmitting(false); } }; const renderSkeleton = () => (
); // 平台未开放自助注册:直达此 URL 时给出说明并引导回企业登录 const renderDisabled = () => ( ); const renderForm = () => ( {/* key 随表单/凭证两态切换,触发与企业登录页一致的步骤过渡动画 */}
{result ? ( // 带上企业代码与登录名直达登录第二步,用户只需输入初始密码 navigate( `/org/login?org=${encodeURIComponent(result.org_code)}&login=${encodeURIComponent(result.owner_login_name || '')}`, ) } /> ) : ( <>
(formApi.current = api)} onValueChange={(v) => setValues(v)} > !isReservedLoginName(value), message: t('该登录名为系统保留名称,请换一个登录名'), }, ]} /> 0} > {countdown > 0 ? `${t('重新发送')} (${countdown})` : t('获取验证码')} ) : undefined } /> {emailVerifyEnabled && ( )}
{t('已有企业?')} )}
); return (
{/* 凭证展示态隐藏切换入口:初始密码仅显示一次,避免误点导航导致凭证丢失 */} {statusLoaded && !status.self_use_mode_enabled && !result && (
)} {!statusLoaded ? renderSkeleton() : selfRegisterEnabled ? renderForm() : renderDisabled()} {turnstileEnabled && !result && (
setTurnstileToken(token)} />
)}
); }; export default OrgRegister;