feat: 登录页增加允许修改服务端地址的方法

pull/49/head
moonrailgun 3 years ago
parent 1fa779beaa
commit 520364b08c

@ -83,15 +83,15 @@ export const App: React.FC = React.memo(() => {
<AppProvider> <AppProvider>
<AppHeader /> <AppHeader />
<AppContainer> <AppContainer>
<Switch> <FallbackPortalHost>
<Route path="/entry" component={EntryRoute} /> {/* 这个host用于处理其他页面(非main)的modal */}
<Route path="/main" component={MainRoute} /> <Switch>
<Route path="/panel" component={PanelRoute} /> <Route path="/entry" component={EntryRoute} />
<Route path="/invite/:inviteCode" component={InviteRoute} /> <Route path="/main" component={MainRoute} />
<Route path="/plugin/*"> <Route path="/panel" component={PanelRoute} />
{/* 这个host用于处理独立页面的modal */} <Route path="/invite/:inviteCode" component={InviteRoute} />
{/* NOTICE: Switch里不能出现动态路由 */} <Route path="/plugin/*">
<FallbackPortalHost> {/* NOTICE: Switch里不能出现动态路由 */}
{pluginRootRoute.map((r, i) => ( {pluginRootRoute.map((r, i) => (
<Route <Route
key={r.name} key={r.name}
@ -99,10 +99,10 @@ export const App: React.FC = React.memo(() => {
component={r.component} component={r.component}
/> />
))} ))}
</FallbackPortalHost> </Route>
</Route> <Redirect to="/entry" />
<Redirect to="/entry" /> </Switch>
</Switch> </FallbackPortalHost>
</AppContainer> </AppContainer>
</AppProvider> </AppProvider>
); );

@ -0,0 +1,42 @@
import { Button, Input } from 'antd';
import React, { useState } from 'react';
import { t } from 'tailchat-shared';
import { ModalWrapper } from '../Modal';
export const ServiceUrlSettings: React.FC = React.memo(() => {
const [url, setUrl] = useState(
window.localStorage.getItem('serviceUrl') ?? ''
);
return (
<ModalWrapper title={t('服务端地址')}>
<Input
placeholder={t('请输入服务器地址(示例: http://127.0.0.1:11000)')}
value={url}
onChange={(e) => setUrl(e.target.value)}
/>
<div className="space-x-2 text-right mt-8">
<Button
onClick={() => {
window.localStorage.removeItem('serviceUrl');
window.location.reload();
}}
>
{t('重置为默认地址')}
</Button>
<Button
type="primary"
disabled={!url}
onClick={() => {
window.localStorage.setItem('serviceUrl', url);
window.location.reload();
}}
>
{t('确认修改并刷新页面')}
</Button>
</div>
</ModalWrapper>
);
});
ServiceUrlSettings.displayName = 'ServiceUrlSettings';

@ -33,8 +33,10 @@ setTokenGetter(async () => {
return await getUserJWT(); return await getUserJWT();
}); });
if (window.localStorage.getItem('serviceUrl')) { const localStorageServiceUrl = window.localStorage.getItem('serviceUrl');
setServiceUrl(() => String(window.localStorage.getItem('serviceUrl')));
if (localStorageServiceUrl) {
setServiceUrl(() => localStorageServiceUrl);
} else if (process.env.SERVICE_URL) { } else if (process.env.SERVICE_URL) {
setServiceUrl(() => String(process.env.SERVICE_URL)); setServiceUrl(() => String(process.env.SERVICE_URL));
} }
@ -100,7 +102,8 @@ request
baseURL: '', baseURL: '',
}) })
.then(({ data: config }) => { .then(({ data: config }) => {
if (isValidStr(_get(config, 'serviceUrl'))) { if (!localStorageServiceUrl && isValidStr(_get(config, 'serviceUrl'))) {
// 配置的优先级低于localStorage的优先级
setServiceUrl(() => _get(config, 'serviceUrl')); setServiceUrl(() => _get(config, 'serviceUrl'));
} }
}) })

@ -9,6 +9,9 @@ import { setUserJWT } from '../../utils/jwt-helper';
import { setGlobalUserLoginInfo, tryAutoLogin } from '../../utils/user-helper'; import { setGlobalUserLoginInfo, tryAutoLogin } from '../../utils/user-helper';
import { useSearchParam } from '@/hooks/useSearchParam'; import { useSearchParam } from '@/hooks/useSearchParam';
import { useNavToView } from './utils'; import { useNavToView } from './utils';
import { IconBtn } from '@/components/IconBtn';
import { openModal } from '@/components/Modal';
import { ServiceUrlSettings } from '@/components/modals/ServiceUrlSettings';
/** /**
* TODO: * TODO:
@ -70,7 +73,7 @@ export const LoginView: React.FC = React.memo(() => {
const navToView = useNavToView(); const navToView = useNavToView();
return ( return (
<div className="w-96 text-white"> <div className="w-96 text-white relative">
<div className="mb-4 text-2xl">{t('登录 Tailchat')}</div> <div className="mb-4 text-2xl">{t('登录 Tailchat')}</div>
<div> <div>
@ -136,6 +139,14 @@ export const LoginView: React.FC = React.memo(() => {
<Icon icon="mdi:arrow-right" className="ml-1 inline" /> <Icon icon="mdi:arrow-right" className="ml-1 inline" />
</button> </button>
</div> </div>
<div className="absolute bottom-4 left-0">
<IconBtn
icon="mdi:cog"
shape="square"
onClick={() => openModal(<ServiceUrlSettings />)}
/>
</div>
</div> </div>
); );
}); });

Loading…
Cancel
Save