feat(core): sgw-device-ui
This commit is contained in:
109
src/app.tsx
Normal file
109
src/app.tsx
Normal file
@@ -0,0 +1,109 @@
|
||||
import { history, RunTimeLayoutConfig } from '@umijs/max';
|
||||
import { handleRequestConfig } from '../config/Request';
|
||||
import logo from '../public/logo.png';
|
||||
import UnAccessPage from './components/403/403Page';
|
||||
import Footer from './components/Footer/Footer';
|
||||
import { ROUTE_LOGIN } from './constants';
|
||||
import { parseJwt } from './utils/jwtTokenUtils';
|
||||
import { getToken, removeToken } from './utils/localStorageUtils';
|
||||
// 全局初始化数据配置,用于 Layout 用户信息和权限初始化
|
||||
// 更多信息见文档:https://umijs.org/docs/api/runtime-config#getinitialstate
|
||||
export async function getInitialState() {
|
||||
const token: string = getToken();
|
||||
const { pathname } = history.location;
|
||||
|
||||
if (!token) {
|
||||
if (pathname !== ROUTE_LOGIN) {
|
||||
history.push(`${ROUTE_LOGIN}?redirect=${pathname}`);
|
||||
} else {
|
||||
history.push(ROUTE_LOGIN);
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
const parsed = parseJwt(token);
|
||||
if (!parsed) {
|
||||
removeToken();
|
||||
if (pathname !== ROUTE_LOGIN) {
|
||||
history.push(`${ROUTE_LOGIN}?redirect=${pathname}`);
|
||||
} else {
|
||||
history.push(ROUTE_LOGIN);
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
const { sub, exp } = parsed;
|
||||
const now = Math.floor(Date.now() / 1000);
|
||||
const oneHour = 60 * 60;
|
||||
|
||||
if (exp - now < oneHour) {
|
||||
console.warn('Token expired or nearly expired, redirecting...');
|
||||
removeToken();
|
||||
if (pathname !== ROUTE_LOGIN) {
|
||||
history.push(`${ROUTE_LOGIN}?redirect=${pathname}`);
|
||||
} else {
|
||||
history.push(ROUTE_LOGIN);
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
return {
|
||||
currentUser: sub,
|
||||
exp,
|
||||
};
|
||||
}
|
||||
|
||||
export const layout: RunTimeLayoutConfig = (initialState) => {
|
||||
console.log('initialState', initialState);
|
||||
|
||||
return {
|
||||
logo: logo,
|
||||
fixedHeader: true,
|
||||
contentWidth: 'Fluid',
|
||||
navTheme: 'light',
|
||||
splitMenus: true,
|
||||
title: 'SGW',
|
||||
iconfontUrl: '//at.alicdn.com/t/c/font_1970684_76mmjhln75w.js',
|
||||
menu: {
|
||||
locale: false,
|
||||
},
|
||||
contentStyle: {
|
||||
padding: 0,
|
||||
},
|
||||
layout: 'mix',
|
||||
logout: () => {
|
||||
removeToken();
|
||||
history.push(ROUTE_LOGIN);
|
||||
},
|
||||
footerRender: () => <Footer />,
|
||||
onPageChange: () => {
|
||||
if (!initialState.initialState) {
|
||||
history.push(ROUTE_LOGIN);
|
||||
}
|
||||
},
|
||||
menuHeaderRender: undefined,
|
||||
bgLayoutImgList: [
|
||||
{
|
||||
src: 'https://mdn.alipayobjects.com/yuyan_qk0oxh/afts/img/D2LWSqNny4sAAAAAAAAAAAAAFl94AQBr',
|
||||
left: 85,
|
||||
bottom: 100,
|
||||
height: '303px',
|
||||
},
|
||||
{
|
||||
src: 'https://mdn.alipayobjects.com/yuyan_qk0oxh/afts/img/C2TWRpJpiC0AAAAAAAAAAAAAFl94AQBr',
|
||||
bottom: -68,
|
||||
right: -45,
|
||||
height: '303px',
|
||||
},
|
||||
{
|
||||
src: 'https://mdn.alipayobjects.com/yuyan_qk0oxh/afts/img/F6vSTbj8KpYAAAAAAAAAAAAAFl94AQBr',
|
||||
bottom: 0,
|
||||
left: 0,
|
||||
width: '331px',
|
||||
},
|
||||
],
|
||||
unAccessible: <UnAccessPage />,
|
||||
};
|
||||
};
|
||||
|
||||
export const request = handleRequestConfig;
|
||||
Reference in New Issue
Block a user