Unverified Commit 16d9370d authored by 陈帅's avatar 陈帅 Committed by GitHub

Login title (#3564)

* Added document title to UserLayout by identifying the current route object and using its name kaey to set the title

* Adjusment to document title

* feat: use same getPageTile function
parent ece38278
...@@ -5,9 +5,13 @@ export default [ ...@@ -5,9 +5,13 @@ export default [
component: '../layouts/UserLayout', component: '../layouts/UserLayout',
routes: [ routes: [
{ path: '/user', redirect: '/user/login' }, { path: '/user', redirect: '/user/login' },
{ path: '/user/login', component: './User/Login' }, { path: '/user/login', name: 'login', component: './User/Login' },
{ path: '/user/register', component: './User/Register' }, { path: '/user/register', name: 'register', component: './User/Register' },
{ path: '/user/register-result', component: './User/RegisterResult' }, {
path: '/user/register-result',
name: 'register.result',
component: './User/RegisterResult',
},
], ],
}, },
// app // app
......
import React, { Suspense } from 'react'; import React, { Suspense } from 'react';
import { Layout } from 'antd'; import { Layout } from 'antd';
import DocumentTitle from 'react-document-title'; import DocumentTitle from 'react-document-title';
import isEqual from 'lodash/isEqual';
import memoizeOne from 'memoize-one';
import { connect } from 'dva'; import { connect } from 'dva';
import { ContainerQuery } from 'react-container-query'; import { ContainerQuery } from 'react-container-query';
import classNames from 'classnames'; import classNames from 'classnames';
import pathToRegexp from 'path-to-regexp'; import pathToRegexp from 'path-to-regexp';
import Media from 'react-media'; import Media from 'react-media';
import { formatMessage } from 'umi/locale';
import Authorized from '@/utils/Authorized'; import Authorized from '@/utils/Authorized';
import logo from '../assets/logo.svg'; import logo from '../assets/logo.svg';
import Footer from './Footer'; import Footer from './Footer';
...@@ -17,8 +14,7 @@ import Context from './MenuContext'; ...@@ -17,8 +14,7 @@ import Context from './MenuContext';
import Exception403 from '../pages/Exception/403'; import Exception403 from '../pages/Exception/403';
import PageLoading from '@/components/PageLoading'; import PageLoading from '@/components/PageLoading';
import SiderMenu from '@/components/SiderMenu'; import SiderMenu from '@/components/SiderMenu';
import { menu, title } from '../defaultSettings'; import getPageTitle from '@/utils/getPageTitle';
import styles from './BasicLayout.less'; import styles from './BasicLayout.less';
// lazy load SettingDrawer // lazy load SettingDrawer
...@@ -52,12 +48,6 @@ const query = { ...@@ -52,12 +48,6 @@ const query = {
}; };
class BasicLayout extends React.Component { class BasicLayout extends React.Component {
constructor(props) {
super(props);
this.getPageTitle = memoizeOne(this.getPageTitle);
this.matchParamsPath = memoizeOne(this.matchParamsPath, isEqual);
}
componentDidMount() { componentDidMount() {
const { const {
dispatch, dispatch,
...@@ -83,11 +73,6 @@ class BasicLayout extends React.Component { ...@@ -83,11 +73,6 @@ class BasicLayout extends React.Component {
}; };
} }
matchParamsPath = (pathname, breadcrumbNameMap) => {
const pathKey = Object.keys(breadcrumbNameMap).find(key => pathToRegexp(key).test(pathname));
return breadcrumbNameMap[pathKey];
};
getRouteAuthority = (pathname, routeData) => { getRouteAuthority = (pathname, routeData) => {
const routes = routeData.slice(); // clone const routes = routeData.slice(); // clone
...@@ -111,22 +96,6 @@ class BasicLayout extends React.Component { ...@@ -111,22 +96,6 @@ class BasicLayout extends React.Component {
return getAuthority(routes, pathname); return getAuthority(routes, pathname);
}; };
getPageTitle = (pathname, breadcrumbNameMap) => {
const currRouterData = this.matchParamsPath(pathname, breadcrumbNameMap);
if (!currRouterData) {
return title;
}
const pageName = menu.disableLocal
? currRouterData.name
: formatMessage({
id: currRouterData.locale || currRouterData.name,
defaultMessage: currRouterData.name,
});
return `${pageName} - ${title}`;
};
getLayoutStyle = () => { getLayoutStyle = () => {
const { fixSiderbar, isMobile, collapsed, layout } = this.props; const { fixSiderbar, isMobile, collapsed, layout } = this.props;
if (fixSiderbar && layout !== 'topmenu' && !isMobile) { if (fixSiderbar && layout !== 'topmenu' && !isMobile) {
...@@ -206,7 +175,7 @@ class BasicLayout extends React.Component { ...@@ -206,7 +175,7 @@ class BasicLayout extends React.Component {
); );
return ( return (
<React.Fragment> <React.Fragment>
<DocumentTitle title={this.getPageTitle(pathname, breadcrumbNameMap)}> <DocumentTitle title={getPageTitle(pathname, breadcrumbNameMap)}>
<ContainerQuery query={query}> <ContainerQuery query={query}>
{params => ( {params => (
<Context.Provider value={this.getContext()}> <Context.Provider value={this.getContext()}>
......
import React, { Fragment } from 'react'; import React, { Component, Fragment } from 'react';
import { formatMessage } from 'umi/locale'; import { formatMessage } from 'umi/locale';
import { connect } from 'dva';
import Link from 'umi/link'; import Link from 'umi/link';
import { Icon } from 'antd'; import { Icon } from 'antd';
import GlobalFooter from '@/components/GlobalFooter'; import GlobalFooter from '@/components/GlobalFooter';
import DocumentTitle from 'react-document-title';
import SelectLang from '@/components/SelectLang'; import SelectLang from '@/components/SelectLang';
import styles from './UserLayout.less'; import styles from './UserLayout.less';
import logo from '../assets/logo.svg'; import logo from '../assets/logo.svg';
import getPageTitle from '@/utils/getPageTitle';
const links = [ const links = [
{ {
...@@ -31,26 +34,50 @@ const copyright = ( ...@@ -31,26 +34,50 @@ const copyright = (
</Fragment> </Fragment>
); );
const UserLayout = ({ children }) => ( class UserLayout extends Component {
// @TODO <DocumentTitle title={this.getPageTitle()}> componentDidMount() {
<div className={styles.container}> const {
<div className={styles.lang}> dispatch,
<SelectLang /> route: { routes, authority },
</div> } = this.props;
<div className={styles.content}> dispatch({
<div className={styles.top}> type: 'menu/getMenuData',
<div className={styles.header}> payload: { routes, authority },
<Link to="/"> });
<img alt="logo" className={styles.logo} src={logo} /> }
<span className={styles.title}>Ant Design</span>
</Link> render() {
const {
children,
location: { pathname },
breadcrumbNameMap,
} = this.props;
return (
<DocumentTitle title={getPageTitle(pathname, breadcrumbNameMap)}>
<div className={styles.container}>
<div className={styles.lang}>
<SelectLang />
</div>
<div className={styles.content}>
<div className={styles.top}>
<div className={styles.header}>
<Link to="/">
<img alt="logo" className={styles.logo} src={logo} />
<span className={styles.title}>Ant Design</span>
</Link>
</div>
<div className={styles.desc}>Ant Design 是西湖区最具影响力的 Web 设计规范</div>
</div>
{children}
</div>
<GlobalFooter links={links} copyright={copyright} />
</div> </div>
<div className={styles.desc}>Ant Design 是西湖区最具影响力的 Web 设计规范</div> </DocumentTitle>
</div> );
{children} }
</div> }
<GlobalFooter links={links} copyright={copyright} />
</div>
);
export default UserLayout; export default connect(({ menu: menuModel }) => ({
menuData: menuModel.menuData,
breadcrumbNameMap: menuModel.breadcrumbNameMap,
}))(UserLayout);
export default { export default {
'menu.home': 'Home', 'menu.home': 'Home',
'menu.login': 'Login',
'menu.register': 'Register',
'menu.register.result': 'Register Result',
'menu.dashboard': 'Dashboard', 'menu.dashboard': 'Dashboard',
'menu.dashboard.analysis': 'Analysis', 'menu.dashboard.analysis': 'Analysis',
'menu.dashboard.monitor': 'Monitor', 'menu.dashboard.monitor': 'Monitor',
......
export default { export default {
'menu.home': 'Início', 'menu.home': 'Início',
'menu.login': 'Login',
'menu.register': 'Registro',
'menu.register.result': 'Resultado de registro',
'menu.dashboard': 'Dashboard', 'menu.dashboard': 'Dashboard',
'menu.dashboard.analysis': 'Análise', 'menu.dashboard.analysis': 'Análise',
'menu.dashboard.monitor': 'Monitor', 'menu.dashboard.monitor': 'Monitor',
......
export default { export default {
'menu.home': '首页', 'menu.home': '首页',
'menu.login': '登录',
'menu.register': '注册',
'menu.register.result': '注册结果',
'menu.dashboard': 'Dashboard', 'menu.dashboard': 'Dashboard',
'menu.dashboard.analysis': '分析页', 'menu.dashboard.analysis': '分析页',
'menu.dashboard.monitor': '监控页', 'menu.dashboard.monitor': '监控页',
......
export default { export default {
'menu.home': '首頁', 'menu.home': '首頁',
'menu.login': '登錄',
'menu.register': '註冊',
'menu.register.resultt': '註冊結果',
'menu.dashboard': 'Dashboard', 'menu.dashboard': 'Dashboard',
'menu.dashboard.analysis': '分析頁', 'menu.dashboard.analysis': '分析頁',
'menu.dashboard.monitor': '監控頁', 'menu.dashboard.monitor': '監控頁',
......
@import '~antd/lib/style/themes/default.less'; @import '~antd/lib/style/themes/default.less';
.main { .main {
width: 368px; width: 388px;
margin: 0 auto; margin: 0 auto;
@media screen and (max-width: @screen-sm) { @media screen and (max-width: @screen-sm) {
width: 95%; width: 95%;
......
@import '~antd/lib/style/themes/default.less'; @import '~antd/lib/style/themes/default.less';
.main { .main {
width: 368px; width: 388px;
margin: 0 auto; margin: 0 auto;
:global { :global {
......
import { formatMessage } from 'umi/locale';
import pathToRegexp from 'path-to-regexp';
import isEqual from 'lodash/isEqual';
import memoizeOne from 'memoize-one';
import { menu, title } from '../defaultSettings';
export const matchParamsPath = (pathname, breadcrumbNameMap) => {
const pathKey = Object.keys(breadcrumbNameMap).find(key => pathToRegexp(key).test(pathname));
return breadcrumbNameMap[pathKey];
};
const getPageTitle = (pathname, breadcrumbNameMap) => {
const currRouterData = matchParamsPath(pathname, breadcrumbNameMap);
if (!currRouterData) {
return title;
}
const pageName = menu.disableLocal
? currRouterData.name
: formatMessage({
id: currRouterData.locale || currRouterData.name,
defaultMessage: currRouterData.name,
});
return `${pageName} - ${title}`;
};
export default memoizeOne(getPageTitle, isEqual);
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment