Commit 3beb940c authored by 张烨's avatar 张烨

feat: add authority components

parent 210d1741
# PUBLIC_PATH = reactOMS
PROXY = http://192.168.19.102:8005/
PROXY = http://localhost:8005/
HOST = localhost
PORT = 3001
\ No newline at end of file
......@@ -171,6 +171,10 @@ module.exports = options => ({
modules: ['node_modules', 'src'],
extensions: ['.js', '.jsx', '.react.js'],
mainFields: ['browser', 'jsnext:main', 'main'],
alias: {
'@': path.resolve(process.cwd(), './src'),
components: path.resolve(process.cwd(), './src/components'),
},
},
devtool: options.devtool,
target: 'web', // Make web variables accessible to webpack, e.g. window
......
// import React from 'react';
// import { Result } from 'antd';
// import check from './CheckPermissions';
import React from 'react';
import { Result } from 'antd';
import check from './CheckPermissions';
// const Authorized = {
// children,
// authority,
// };
const Authorized = ({
children,
authority,
noMatch = (
<Result
status="403"
title="403"
subTitle="Sorry, you are not authorized to access this page."
/>
),
}) => {
const childrenRender = typeof children === 'undefined' ? null : children;
const dom = check(authority, childrenRender, noMatch);
return <>{dom}</>;
};
export default Authorized;
// import React from 'react';
import { Route, Redirect } from 'react-router-dom';
import React from 'react';
import Authorized from './Authorized';
const AuthorizedRoute = ({
component,
component: Component,
render,
authority,
redirectPath,
// eslint-disable-next-line no-unused-vars
...rest
}) => {};
}) => (
<Authorized
authority={authority}
noMatch={
<Route
{...rest}
render={() => <Redirect to={{ pathname: redirectPath }} />}
/>
}
>
<Route
{...rest}
render={props => (Component ? <Component {...props} /> : render(props))}
/>
</Authorized>
);
export default AuthorizedRoute;
import Authorized from './Authorized';
import Secured from './Secured';
import check from './CheckPermissions';
import renderAuthorize from './renderAuthorize';
Authorized.Secured = Secured;
Authorized.check = check;
const RenderAuthorize = renderAuthorize(Authorized);
export default RenderAuthorize;
......@@ -23,4 +23,4 @@ const renderAuthorize = Authorized => currentAuthority => {
};
export { CURRENT };
export default Authorized => renderAuthorize(Authorized);
export default renderAuthorize;
import React from 'react';
import { Helmet } from 'react-helmet';
import { renderRoutes } from 'react-router-config';
import { BrowserRouter as Router, Switch } from 'react-router-dom';
import config from '../../routes/config';
export default function App() {
......
import RenderAuthorize from '@/components/Authorized';
/* eslint-disable eslint-comments/disable-enable-pair */
/* eslint-disable import/no-mutable-exports */
let Authorized = RenderAuthorize(getAuthority());
// Reload the rights component
const reloadAuthorized = () => {
Authorized = RenderAuthorize(getAuthority());
};
/**
* hard code
* block need it。
*/
window.reloadAuthorized = reloadAuthorized;
export { reloadAuthorized };
export default Authorized;
// use localStorage to store the authority info, which might be sent from server in actual project.
export function getAuthority(str) {
const authorityString =
typeof str === 'undefined' && localStorage
? localStorage.getItem('panda-oms-authority')
: str;
// authorityString could be admin, "admin", ["admin"]
let authority;
try {
if (authorityString) {
authority = JSON.parse(authorityString);
}
} catch (e) {
authority = authorityString;
}
if (typeof authority === 'string') {
return [authority];
}
return authority;
}
export function setAuthority(authority) {
const proAuthority = typeof authority === 'string' ? [authority] : authority;
localStorage.setItem('panda-oms-authority', JSON.stringify(proAuthority));
// auto reload
reloadAuthorized();
}
export default (...routerProps) =>
new Promise((resolve, reject) => {
setTimeout(() => {
resolve();
}, 1500);
}).then(() => {});
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