forked from wangziqi/gongxue-base
57 lines
2.1 KiB
TypeScript
57 lines
2.1 KiB
TypeScript
import { useEffect, useState } from 'react';
|
|
import Taro, { useRouter } from '@tarojs/taro';
|
|
import { Button, Text, View } from '@tarojs/components';
|
|
import { appEnv, assertFrontendSecretsAreAbsent, ensureRuntimeConfigLoaded, isH5Runtime } from '@/env';
|
|
import { resolveTenant } from '@/services/api';
|
|
import { landingPath, requirePlatformAdmin, requireSignedIn, requireTenantAdmin, safeRedirectPath } from '@/services/routeGuard';
|
|
import './index.css';
|
|
|
|
function hostFromRuntime() {
|
|
if (isH5Runtime() && typeof window !== 'undefined') return window.location.host;
|
|
return '';
|
|
}
|
|
|
|
export default function BootstrapPage() {
|
|
const router = useRouter();
|
|
const [status, setStatus] = useState('正在解析租户');
|
|
const [error, setError] = useState('');
|
|
|
|
useEffect(() => {
|
|
const redirectPath = safeRedirectPath(router.params?.redirect ? decodeURIComponent(router.params.redirect) : landingPath());
|
|
assertFrontendSecretsAreAbsent();
|
|
ensureRuntimeConfigLoaded()
|
|
.then(() => resolveTenant({ host: hostFromRuntime() }))
|
|
.then(async () => {
|
|
setStatus('正在校验登录状态');
|
|
if (appEnv.portal === 'student') return requireSignedIn(redirectPath);
|
|
if (appEnv.portal === 'platform-admin') return requirePlatformAdmin(redirectPath);
|
|
return requireTenantAdmin(redirectPath);
|
|
})
|
|
.then(authPayload => {
|
|
if (!authPayload) return;
|
|
setStatus('租户解析完成');
|
|
Taro.redirectTo({ url: redirectPath });
|
|
})
|
|
.catch((nextError: Error) => {
|
|
setError(nextError.message);
|
|
setStatus('租户解析失败');
|
|
});
|
|
}, []);
|
|
|
|
return (
|
|
<View className='bootstrap-page'>
|
|
<View className='bootstrap-shell'>
|
|
<Text className='eyebrow'>Tiku SaaS</Text>
|
|
<Text className='title'>正在进入题库系统</Text>
|
|
<Text className='status'>{status}</Text>
|
|
{error ? <Text className='error'>{error}</Text> : null}
|
|
{error ? (
|
|
<Button className='primary-button' onClick={() => Taro.reLaunch({ url: '/pages/bootstrap/index' })}>
|
|
重新尝试
|
|
</Button>
|
|
) : null}
|
|
</View>
|
|
</View>
|
|
);
|
|
}
|