Skip to content

Commit

Permalink
fix(权限控制): 不通过登录方式进入系统时权限问题
Browse files Browse the repository at this point in the history
存在token,但没有hsweb-autz权限信息时刷新界面错误
  • Loading branch information
Lind-pro committed Nov 26, 2020
1 parent 56ede03 commit d6b8a8a
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
2 changes: 2 additions & 0 deletions src/models/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Reducer } from 'redux';

import { queryCurrent, query as queryUsers } from '@/services/user';
import { router } from 'umi';
import { reloadAuthorized } from '@/utils/Authorized';

export interface CurrentUser {
avatar?: string;
Expand Down Expand Up @@ -71,6 +72,7 @@ const UserModel: UserModelType = {
} else {
router.push('/user/login');
}
reloadAuthorized();
},
},

Expand Down
28 changes: 20 additions & 8 deletions src/pages/Authorized.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { connect } from 'dva';
import Authorized from '@/utils/Authorized';
import { getRouteAuthority } from '@/utils/utils';
import { ConnectProps, ConnectState, UserModelState } from '@/models/connect';
import { PageLoading } from '@ant-design/pro-layout';

interface AuthComponentProps extends ConnectProps {
user: UserModelState;
Expand All @@ -18,18 +19,29 @@ const AuthComponent: React.FC<AuthComponentProps> = ({
pathname: '',
},
user,
dispatch
}) => {
const { currentUser } = user;
const { routes = [] } = route;
const isLogin = currentUser && currentUser.name;
return (
<Authorized
authority={getRouteAuthority(location.pathname, routes) || ''}
noMatch={isLogin ? <Redirect to="/exception/403" /> : <Redirect to="/" />}
>
{children}
</Authorized>
);
const autz = localStorage.getItem('hsweb-autz');
if (!autz) {
dispatch!({
type: 'user/fetchCurrent',
});
return <PageLoading tip="获取用户数据中" />
} else {
console.log(getRouteAuthority(location.pathname, routes), location.pathname, 'auth');
// reloadAuthorized();
return (
<Authorized
authority={getRouteAuthority(location.pathname, routes) || ''}
noMatch={!isLogin ? <Redirect to="/exception/403" /> : <Redirect to={location.pathname} />}
>
{children}
</Authorized>
);
}
};

export default connect(({ user }: ConnectState) => ({
Expand Down
6 changes: 4 additions & 2 deletions src/utils/authority.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export function getAuthority(): string | string[] {
// return ['admin'];
// }
// return authority;
const storage = localStorage.getItem('hsweb-autz');
const storage = localStorage.getItem('hsweb-autz');
if (storage) {
try {
const autz = storage && JSON.parse(storage)
Expand All @@ -41,6 +41,8 @@ export function getAuthority(): string | string[] {
location.reload();
return ['guest'];
}
} else {
return ['guest'];
}
}

Expand Down Expand Up @@ -195,7 +197,7 @@ export function clearAutz() {
export function setAutz(info: Authentication): Authentication {
window.top.hsweb_autz = new Authentication(info);
const autz = new Authentication(info);
if(JSON.stringify(info)!=='undefined'){
if (JSON.stringify(info) !== 'undefined') {
localStorage.setItem('hsweb-autz', JSON.stringify(info));
}
return autz;
Expand Down

0 comments on commit d6b8a8a

Please sign in to comment.