277 lines
11 KiB
React
277 lines
11 KiB
React
/*
|
|
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 <https://www.gnu.org/licenses/>.
|
|
|
|
For commercial licensing, please contact support@quantumnous.com
|
|
*/
|
|
|
|
import {
|
|
Home as ActionHome,
|
|
RefreshCw as ActionRefreshCw,
|
|
} from '@/components/common/icons/ActionIcons';
|
|
import React, { useState, useCallback, useEffect, useContext } from 'react';
|
|
import { useTranslation } from 'react-i18next';
|
|
import { useNavigate } from 'react-router-dom';
|
|
import { Building2, AlertTriangle, HelpCircle } from 'lucide-react';
|
|
import { Button } from '@/components/ui';
|
|
import { API } from '../../helpers/api';
|
|
import { UserContext } from '../../context/User';
|
|
import Loading from '../../components/common/ui/Loading';
|
|
import PageBackground from '../../components/common/PageBackground';
|
|
|
|
// 企业中心入口解析:本地没有可用的 current_org_id 时,由企业中心就地渲染本组件(不再跳转独立页面)。
|
|
// 企业与成员均由管理员创建,不提供用户自助加入。按归属状态区分:
|
|
// none —— 用户不属于任何企业,引导联系企业管理员开通账号
|
|
// inactive —— 用户有企业但均非活跃(冻结/欠费/待激活),引导联系管理员
|
|
// error —— 企业列表加载失败,提供重试
|
|
const ORG_STATUS_TEXT = {
|
|
2: '已被冻结',
|
|
3: '已欠费',
|
|
4: '待激活',
|
|
};
|
|
|
|
const OrgResolver = ({
|
|
onResolved,
|
|
excludeOrgIds = [],
|
|
hasMarquee = false,
|
|
}) => {
|
|
const { t } = useTranslation();
|
|
const navigate = useNavigate();
|
|
const [userState] = useContext(UserContext);
|
|
const isOrgOnly = userState?.user?.org_only_mode === true;
|
|
// 'checking' | 'none' | 'inactive' | 'error'
|
|
const [mode, setMode] = useState('checking');
|
|
const [inactiveOrgs, setInactiveOrgs] = useState([]);
|
|
|
|
// 有 active(status=1) 企业 → 写入 current_org_id 并交回企业中心(仅认 active,防冻结企业死循环)。
|
|
// excludeOrgIds 为本次加载失败过的企业:自动解析时一律跳过,避免"选中→失败→再选中"的循环;
|
|
// 可用企业若全部失败过,按加载失败处理(可能只是网络抖动),由用户手动重试时才再次尝试。
|
|
const runCheck = useCallback(
|
|
async (retryExcluded = false) => {
|
|
setMode('checking');
|
|
try {
|
|
const res = await API.get('/api/org/list');
|
|
if (res.data.success) {
|
|
const orgs = res.data.data || [];
|
|
const activeOrgs = orgs.filter((o) => o.status === 1);
|
|
const candidates = retryExcluded
|
|
? activeOrgs
|
|
: activeOrgs.filter((o) => !excludeOrgIds.includes(o.id));
|
|
if (candidates.length > 0) {
|
|
const savedOrgId = parseInt(
|
|
localStorage.getItem('current_org_id') || '0',
|
|
10,
|
|
);
|
|
const savedOrg =
|
|
savedOrgId > 0
|
|
? candidates.find((o) => o.id === savedOrgId)
|
|
: null;
|
|
const targetOrg = savedOrg || candidates[0];
|
|
localStorage.setItem('current_org_id', String(targetOrg.id));
|
|
onResolved(targetOrg.id);
|
|
return;
|
|
}
|
|
if (activeOrgs.length > 0) {
|
|
setMode('error');
|
|
return;
|
|
}
|
|
if (orgs.length > 0) {
|
|
setInactiveOrgs(orgs);
|
|
setMode('inactive');
|
|
return;
|
|
}
|
|
setMode('none');
|
|
return;
|
|
}
|
|
setMode('error');
|
|
} catch {
|
|
setMode('error');
|
|
}
|
|
},
|
|
[onResolved, excludeOrgIds],
|
|
);
|
|
|
|
useEffect(() => {
|
|
runCheck();
|
|
}, [runCheck]);
|
|
|
|
if (mode === 'checking') {
|
|
return <Loading />;
|
|
}
|
|
|
|
// 非 org_only 用户提供「返回首页」出口,避免被困在本页;org_only 员工受守卫限制在 /org,不提供。
|
|
const backHome = !isOrgOnly && (
|
|
<div className='mt-6 text-center'>
|
|
<button
|
|
onClick={() => navigate('/')}
|
|
className='app-action-button inline-flex items-center gap-1.5 text-sm text-gray-400 dark:text-gray-500 hover:text-brand-deep transition-colors'
|
|
>
|
|
<ActionHome size={14} />
|
|
{t('返回首页')}
|
|
</button>
|
|
</div>
|
|
);
|
|
|
|
return (
|
|
// /org 下 PageLayout 关闭外层滚动、也不插跑马灯占位:顶部让位与滚动都由本页自理(同企业中心主布局)。
|
|
<div
|
|
className={`org-center relative h-screen overflow-y-auto bg-[var(--surface-bg-base)] transition-colors duration-300 ${hasMarquee ? 'pt-[96px] sm:pt-[104px]' : 'pt-14 sm:pt-16'}`}
|
|
>
|
|
{/* 统一页面背景层 —— 唯一背景来源,走 bg-glow-* 主题变量 */}
|
|
<PageBackground />
|
|
|
|
<div className='relative z-10 min-h-full flex items-center justify-center py-10 px-4'>
|
|
<div className='w-full max-w-md'>
|
|
{mode === 'none' && (
|
|
<>
|
|
<div className='text-center mb-7'>
|
|
<div className='inline-flex p-4 rounded-2xl bg-gradient-to-br from-brand-from via-brand-via to-brand-to mb-5 shadow-lg shadow-brand/25 dark:shadow-brand/15'>
|
|
<Building2 size={34} className='text-white' />
|
|
</div>
|
|
<h1 className='text-3xl font-bold text-gray-900 dark:text-white'>
|
|
{t('暂未加入企业')}
|
|
</h1>
|
|
<p className='text-gray-500 dark:text-gray-400 mt-2.5 leading-relaxed'>
|
|
{t(
|
|
'企业成员账号由企业管理员统一创建,请联系你的企业管理员开通',
|
|
)}
|
|
</p>
|
|
</div>
|
|
|
|
<div className='rounded-xl border border-[var(--border-subtle)] bg-[var(--surface-card)] shadow-[var(--elevation-sm)] p-6 sm:p-7 transition-colors duration-300'>
|
|
<div className='flex items-start gap-2.5'>
|
|
<HelpCircle
|
|
size={16}
|
|
className='text-gray-400 dark:text-gray-500 mt-0.5 flex-shrink-0'
|
|
/>
|
|
<p className='text-xs text-gray-500 dark:text-gray-400 leading-relaxed'>
|
|
{t(
|
|
'管理员创建账号后,请使用企业账号通过「企业登录」进入企业中心。',
|
|
)}
|
|
</p>
|
|
</div>
|
|
<Button
|
|
size='large'
|
|
block
|
|
icon={<ActionRefreshCw size={16} />}
|
|
onClick={() => runCheck()}
|
|
className='mt-4'
|
|
>
|
|
{t('刷新状态')}
|
|
</Button>
|
|
</div>
|
|
{backHome}
|
|
</>
|
|
)}
|
|
|
|
{mode === 'inactive' && (
|
|
<>
|
|
<div className='text-center mb-7'>
|
|
<div className='inline-flex p-4 rounded-2xl bg-gradient-to-br from-amber-400 to-amber-500 mb-5 shadow-lg shadow-amber-500/25 dark:shadow-amber-500/15'>
|
|
<AlertTriangle size={34} className='text-white' />
|
|
</div>
|
|
<h1 className='text-3xl font-bold text-gray-900 dark:text-white'>
|
|
{t('企业暂不可用')}
|
|
</h1>
|
|
<p className='text-gray-500 dark:text-gray-400 mt-2.5 leading-relaxed'>
|
|
{t('你所属的企业当前无法访问,请联系管理员处理')}
|
|
</p>
|
|
</div>
|
|
|
|
<div className='rounded-xl border border-[var(--border-subtle)] bg-[var(--surface-card)] shadow-[var(--elevation-sm)] p-6 sm:p-7 transition-colors duration-300'>
|
|
<div className='space-y-2.5'>
|
|
{inactiveOrgs.map((org) => (
|
|
<div
|
|
key={org.id}
|
|
className='flex items-center justify-between gap-3 p-3 rounded-xl bg-gray-50 dark:bg-neutral-800/60 border border-gray-100 dark:border-neutral-800'
|
|
>
|
|
<div className='flex items-center gap-2.5 min-w-0'>
|
|
<Building2
|
|
size={16}
|
|
className='text-gray-400 flex-shrink-0'
|
|
/>
|
|
<span className='text-sm font-medium text-gray-900 dark:text-white truncate'>
|
|
{org.name || t('企业') + ' #' + org.id}
|
|
</span>
|
|
</div>
|
|
<span className='text-xs font-medium text-amber-600 dark:text-amber-400 flex-shrink-0'>
|
|
{t(ORG_STATUS_TEXT[org.status] || '不可用')}
|
|
</span>
|
|
</div>
|
|
))}
|
|
</div>
|
|
|
|
<div className='mt-5 pt-5 border-t border-gray-100 dark:border-neutral-800 flex items-start gap-2.5'>
|
|
<HelpCircle
|
|
size={16}
|
|
className='text-gray-400 dark:text-gray-500 mt-0.5 flex-shrink-0'
|
|
/>
|
|
<p className='text-xs text-gray-500 dark:text-gray-400 leading-relaxed'>
|
|
{t(
|
|
'企业激活、解冻或续费由平台管理员处理,请联系企业管理员或平台客服。',
|
|
)}
|
|
</p>
|
|
</div>
|
|
|
|
<Button
|
|
size='large'
|
|
block
|
|
icon={<ActionRefreshCw size={16} />}
|
|
onClick={() => runCheck()}
|
|
className='mt-4'
|
|
>
|
|
{t('刷新状态')}
|
|
</Button>
|
|
</div>
|
|
{backHome}
|
|
</>
|
|
)}
|
|
|
|
{mode === 'error' && (
|
|
<>
|
|
<div className='text-center mb-7'>
|
|
<div className='inline-flex p-4 rounded-2xl bg-gradient-to-br from-red-400 to-red-500 mb-5 shadow-lg shadow-red-500/25 dark:shadow-red-500/15'>
|
|
<AlertTriangle size={34} className='text-white' />
|
|
</div>
|
|
<h1 className='text-3xl font-bold text-gray-900 dark:text-white'>
|
|
{t('加载失败')}
|
|
</h1>
|
|
<p className='text-gray-500 dark:text-gray-400 mt-2.5 leading-relaxed'>
|
|
{t('无法获取你的企业信息,请检查网络后重试')}
|
|
</p>
|
|
</div>
|
|
|
|
<div className='rounded-xl border border-[var(--border-subtle)] bg-[var(--surface-card)] shadow-[var(--elevation-sm)] p-6 sm:p-7 transition-colors duration-300'>
|
|
<Button
|
|
type='primary'
|
|
size='large'
|
|
block
|
|
icon={<ActionRefreshCw size={16} />}
|
|
onClick={() => runCheck(true)}
|
|
>
|
|
{t('重试')}
|
|
</Button>
|
|
</div>
|
|
{backHome}
|
|
</>
|
|
)}
|
|
</div>
|
|
</div>
|
|
</div>
|
|
);
|
|
};
|
|
|
|
export default OrgResolver;
|