Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
A
ant-design-pro
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Incidents
Environments
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
duanledexianxianxian
ant-design-pro
Commits
b5cb5b84
Commit
b5cb5b84
authored
5 years ago
by
duanledexianxianxian
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
sync
parent
fe5b0a3c
Changes
12
Hide whitespace changes
Inline
Side-by-side
Showing
12 changed files
with
144 additions
and
71 deletions
+144
-71
config/config.ts
config/config.ts
+10
-6
src/components/Authorized/Authorized.tsx
src/components/Authorized/Authorized.tsx
+1
-0
src/components/Authorized/CheckPermissions.tsx
src/components/Authorized/CheckPermissions.tsx
+5
-0
src/components/Authorized/renderAuthorize.ts
src/components/Authorized/renderAuthorize.ts
+3
-0
src/layouts/BasicLayout.tsx
src/layouts/BasicLayout.tsx
+75
-46
src/models/global.ts
src/models/global.ts
+1
-0
src/models/user.ts
src/models/user.ts
+6
-1
src/pages/404.tsx
src/pages/404.tsx
+1
-1
src/pages/Authorized.tsx
src/pages/Authorized.tsx
+15
-5
src/pages/user/login/models/index.js
src/pages/user/login/models/index.js
+14
-8
src/services/user.ts
src/services/user.ts
+9
-4
src/utils/authority.ts
src/utils/authority.ts
+4
-0
No files found.
config/config.ts
View file @
b5cb5b84
...
...
@@ -89,6 +89,10 @@ export default {
path
:
'
/
'
,
component
:
'
../layouts/BlankLayout
'
,
routes
:
[
{
path
:
'
/404
'
,
component
:
'
./404
'
,
},
{
path
:
'
/user
'
,
component
:
'
../layouts/UserLayout
'
,
...
...
@@ -108,8 +112,12 @@ export default {
path
:
'
/
'
,
component
:
'
../layouts/BasicLayout
'
,
Routes
:
[
'
src/pages/Authorized
'
],
authority
:
[
'
admin
'
,
'
user
'
],
authority
:
[
'
user
'
],
routes
:
[
{
path
:
'
/
'
,
redirect
:
'
/dashboard/analysis
'
,
},
{
path
:
'
/dashboard
'
,
name
:
'
dashboard
'
,
...
...
@@ -302,11 +310,7 @@ export default {
icon
:
'
highlight
'
,
path
:
'
/parameter
'
,
component
:
'
./system/parameter
'
,
},
{
path
:
'
/
'
,
redirect
:
'
/dashboard/analysis
'
,
authority
:
[
'
admin
'
,
'
user
'
],
authority
:
[
'
user
'
],
},
],
},
...
...
This diff is collapsed.
Click to expand it.
src/components/Authorized/Authorized.tsx
View file @
b5cb5b84
...
...
@@ -20,6 +20,7 @@ const Authorized: React.FunctionComponent<AuthorizedProps> = ({
authority
,
noMatch
=
null
,
})
=>
{
console
.
log
(
'
noMatch1
'
,
noMatch
);
const
childrenRender
:
React
.
ReactNode
=
typeof
children
===
'
undefined
'
?
null
:
children
;
const
dom
=
check
(
authority
,
childrenRender
,
noMatch
);
return
<>
{
dom
}
</>;
...
...
This diff is collapsed.
Click to expand it.
src/components/Authorized/CheckPermissions.tsx
View file @
b5cb5b84
...
...
@@ -24,6 +24,10 @@ const checkPermissions = <T, K>(
target
:
T
,
Exception
:
K
,
):
T
|
K
|
React
.
ReactNode
=>
{
console
.
log
(
'
authority:
'
,
authority
);
console
.
log
(
'
currentAuthority:
'
,
currentAuthority
);
console
.
log
(
'
target:
'
,
target
);
console
.
log
(
'
noMatch2
'
,
Exception
);
// 没有判定权限.默认查看所有
// Retirement authority, return target;
if
(
!
authority
)
{
...
...
@@ -38,6 +42,7 @@ const checkPermissions = <T, K>(
}
else
if
(
authority
.
includes
(
currentAuthority
))
{
return
target
;
}
console
.
log
(
'
noMatch3
'
,
Exception
);
return
Exception
;
}
// string 处理
...
...
This diff is collapsed.
Click to expand it.
src/components/Authorized/renderAuthorize.ts
View file @
b5cb5b84
...
...
@@ -10,6 +10,8 @@ type CurrentAuthorityType = string | string[] | (() => typeof CURRENT);
const
renderAuthorize
=
<
T
>
(
Authorized
:
T
):
((
currentAuthority
:
CurrentAuthorityType
)
=>
T
)
=>
(
currentAuthority
:
CurrentAuthorityType
,
):
T
=>
{
console
.
log
(
'
currentAuthoritycurrentAuthority
'
,
currentAuthority
);
if
(
currentAuthority
)
{
if
(
typeof
currentAuthority
===
'
function
'
)
{
CURRENT
=
currentAuthority
();
...
...
@@ -23,6 +25,7 @@ const renderAuthorize = <T>(Authorized: T): ((currentAuthority: CurrentAuthority
}
else
{
CURRENT
=
'
NULL
'
;
}
console
.
log
(
'
CURRENT
'
,
CURRENT
);
return
Authorized
;
};
...
...
This diff is collapsed.
Click to expand it.
src/layouts/BasicLayout.tsx
View file @
b5cb5b84
...
...
@@ -3,7 +3,7 @@
* You can view component api by:
* https://github.com/ant-design/ant-design-pro-layout
*/
import
{
ConnectProps
,
ConnectState
}
from
'
@/models/connect
'
;
import
{
ConnectProps
,
ConnectState
,
Dispatch
}
from
'
@/models/connect
'
;
import
ProLayout
,
{
MenuDataItem
,
BasicLayoutProps
as
ProLayoutProps
,
...
...
@@ -13,12 +13,15 @@ import ProLayout, {
import
React
,
{
useState
}
from
'
react
'
;
import
Authorized
from
'
@/utils/Authorized
'
;
import
Link
from
'
umi/link
'
;
import
router
from
'
umi/router
'
;
import
RightContent
from
'
@/components/GlobalHeader/RightContent
'
;
import
{
connect
}
from
'
dva
'
;
import
{
formatMessage
}
from
'
umi-plugin-react/locale
'
;
import
{
isAntDesignPro
}
from
'
@/utils/utils
'
;
import
logo
from
'
../assets/logo.svg
'
;
import
{
message
}
from
'
antd
'
;
import
store
from
'
@/utils/store
'
;
import
user
from
'
mock/user
'
;
export
interface
BasicLayoutProps
extends
ProLayoutProps
,
Omit
<
ConnectProps
,
'
location
'
>
{
breadcrumbNameMap
:
{
[
path
:
string
]:
MenuDataItem
;
...
...
@@ -69,20 +72,44 @@ const footerRender: BasicLayoutProps['footerRender'] = (_, defaultDom) => {
);
};
const
loadInitData
=
(
dispatch
:
Dispatch
)
=>
{
Promise
.
all
([
dispatch
({
type
:
'
public/getUserInfo
'
}),
dispatch
({
type
:
'
public/getUserInfo
'
})]);
};
const
BasicLayout
:
React
.
FC
<
BasicLayoutProps
>
=
props
=>
{
const
{
dispatch
,
children
,
settings
}
=
props
;
/**
* constructor
*/
const
[
loaded
,
setLoaded
]
=
useState
(
false
);
useState
(()
=>
{
if
(
dispatch
)
{
// 查看当前用户是否在登录状态
dispatch
({
type
:
'
user/fetchCurrent
'
,
});
dispatch
({
type
:
'
settings/getSetting
'
,
type
:
'
user/checkUserLoginStatus
'
,
}).
then
(({
code
,
data
=
false
}:
{
code
:
string
;
data
:
boolean
})
=>
{
setLoaded
(
true
);
if
(
code
===
'
sys.success
'
)
{
// 登录成功
if
(
data
)
{
loadInitData
(
dispatch
);
}
else
if
(
store
.
get
(
'
userId
'
))
{
store
.
set
(
'
token
'
,
''
);
store
.
set
(
'
userId
'
,
''
);
message
.
error
(
'
登录已过期,请重新登录!
'
);
router
.
push
(
'
/user/login
'
);
}
else
{
// 此处应该跳转到404或者403页面为佳
// message.error("用户未登录,请先登录系统!");
router
.
push
(
'
/404
'
);
}
}
});
// dispatch({
// type: 'settings/getSetting',
// });
}
});
/**
...
...
@@ -97,45 +124,47 @@ const BasicLayout: React.FC<BasicLayoutProps> = props => {
});
return
(
<
div
className
=
"kim-layout"
>
<
ProLayout
siderWidth
=
{
200
}
logo
=
{
false
}
navTheme
=
"dark"
onCollapse
=
{
handleMenuCollapse
}
menuItemRender
=
{
(
menuItemProps
,
defaultDom
)
=>
(
<
Link
to
=
{
menuItemProps
.
path
}
>
{
defaultDom
}
</
Link
>
)
}
breadcrumbRender
=
{
(
routers
=
[])
=>
[
{
path
:
'
/
'
,
breadcrumbName
:
formatMessage
({
id
:
'
menu.home
'
,
defaultMessage
:
'
Home
'
,
}),
},
...
routers
,
]
}
footerRender
=
{
false
}
// 不显示footer
menuDataRender
=
{
menuDataRender
}
// 渲染导航菜单列表
formatMessage
=
{
formatMessage
}
rightContentRender
=
{
rightProps
=>
<
RightContent
{
...
rightProps
}
/>
}
{
...
props
}
{
...
settings
}
>
{
children
}
</
ProLayout
>
{
/* 主题设置抽屉 */
}
<
SettingDrawer
settings
=
{
settings
}
onSettingChange
=
{
config
=>
dispatch
({
type
:
'
settings/changeSetting
'
,
payload
:
config
,
})
}
/>
</
div
>
loaded
&&
(
<
div
className
=
"kim-layout"
>
<
ProLayout
siderWidth
=
{
200
}
logo
=
{
false
}
navTheme
=
"dark"
onCollapse
=
{
handleMenuCollapse
}
menuItemRender
=
{
(
menuItemProps
,
defaultDom
)
=>
(
<
Link
to
=
{
menuItemProps
.
path
}
>
{
defaultDom
}
</
Link
>
)
}
breadcrumbRender
=
{
(
routers
=
[])
=>
[
{
path
:
'
/
'
,
breadcrumbName
:
formatMessage
({
id
:
'
menu.home
'
,
defaultMessage
:
'
Home
'
,
}),
},
...
routers
,
]
}
footerRender
=
{
false
}
// 不显示footer
menuDataRender
=
{
menuDataRender
}
// 渲染导航菜单列表
formatMessage
=
{
formatMessage
}
rightContentRender
=
{
rightProps
=>
<
RightContent
{
...
rightProps
}
/>
}
{
...
props
}
{
...
settings
}
>
{
children
}
</
ProLayout
>
{
/* 主题设置抽屉 */
}
<
SettingDrawer
settings
=
{
settings
}
onSettingChange
=
{
config
=>
dispatch
({
type
:
'
settings/changeSetting
'
,
payload
:
config
,
})
}
/>
</
div
>
)
);
};
...
...
This diff is collapsed.
Click to expand it.
src/models/global.ts
View file @
b5cb5b84
...
...
@@ -22,6 +22,7 @@ export interface GlobalModelType {
fetchNotices
:
Effect
;
clearNotices
:
Effect
;
changeNoticeReadState
:
Effect
;
checkUserLoginStatus
:
Effect
;
};
reducers
:
{
changeLayoutCollapsed
:
Reducer
<
GlobalModelState
>
;
...
...
This diff is collapsed.
Click to expand it.
src/models/user.ts
View file @
b5cb5b84
import
{
queryCurrent
,
query
as
queryUsers
}
from
'
@/services/user
'
;
import
{
queryCurrent
,
query
as
queryUsers
,
checkUserLoginStatus
}
from
'
@/services/user
'
;
import
{
Effect
}
from
'
dva
'
;
import
{
Reducer
}
from
'
redux
'
;
...
...
@@ -26,6 +26,7 @@ export interface UserModelType {
effects
:
{
fetch
:
Effect
;
fetchCurrent
:
Effect
;
checkUserLoginStatus
:
Effect
;
};
reducers
:
{
saveCurrentUser
:
Reducer
<
UserModelState
>
;
...
...
@@ -55,6 +56,10 @@ const UserModel: UserModelType = {
payload
:
response
,
});
},
*
checkUserLoginStatus
({
payload
},
{
call
})
{
const
res
=
yield
call
(
checkUserLoginStatus
,
payload
);
return
res
;
},
},
reducers
:
{
...
...
This diff is collapsed.
Click to expand it.
src/pages/404.tsx
View file @
b5cb5b84
...
...
@@ -18,7 +18,7 @@ const NoFoundPage: React.FC<{}> = () => (
<
br
/>
<
h1
>
404
</
h1
>
<
p
>
Sorry, the page you visited does not exist.
</
p
>
<
Button
type
=
"primary"
onClick
=
{
()
=>
router
.
push
(
'
/
'
)
}
>
<
Button
type
=
"primary"
onClick
=
{
()
=>
router
.
push
(
'
/
user/login
'
)
}
>
Back Home
</
Button
>
</
div
>
...
...
This diff is collapsed.
Click to expand it.
src/pages/Authorized.tsx
View file @
b5cb5b84
...
...
@@ -15,17 +15,23 @@ const getRouteAuthority = (path: string, routeData: Route[]) => {
routeData
.
forEach
(
route
=>
{
// match prefix
if
(
pathToRegexp
(
`
${
route
.
path
}
(.*)`
).
test
(
path
))
{
// exact match
if
(
route
.
path
===
path
)
{
authorities
=
route
.
authority
||
authorities
;
}
console
.
log
(
'
----------------------
'
);
console
.
log
(
'
route.path:
'
,
route
.
path
);
console
.
log
(
'
path:
'
,
path
);
console
.
log
(
'
route.authority:
'
,
route
.
authority
);
authorities
=
route
.
authority
||
authorities
;
// 官方代码好像有问题https://github.com/ant-design/ant-design-pro/commit/4a10734dcf858c6363af719a5886a24ec1115b33#diff-2041540b332693486a24a543b6ba0cc8
// // exact match
// if (route.path === path) {
// authorities = route.authority || authorities;
// }
// get children authority recursively
if
(
route
.
routes
)
{
authorities
=
getRouteAuthority
(
path
,
route
.
routes
)
||
authorities
;
}
}
});
console
.
log
(
'
authorities:
'
,
authorities
);
return
authorities
;
};
...
...
@@ -42,6 +48,10 @@ const AuthComponent: React.FC<AuthComponentProps> = ({
const
{
currentUser
}
=
user
;
const
{
routes
=
[]
}
=
route
;
const
isLogin
=
currentUser
&&
currentUser
.
name
;
console
.
log
(
'
routes:
'
,
location
.
pathname
,
routes
);
console
.
log
(
'
authority:
'
,
getRouteAuthority
(
location
.
pathname
,
routes
));
console
.
log
(
'
isLogin:
'
,
isLogin
);
return
(
<
Authorized
authority
=
{
getRouteAuthority
(
location
.
pathname
,
routes
)
||
''
}
...
...
This diff is collapsed.
Click to expand it.
src/pages/user/login/models/index.js
View file @
b5cb5b84
...
...
@@ -3,6 +3,8 @@ import { login } from '../services';
import
store
from
'
@/utils/store
'
;
import
{
getPageQuery
}
from
'
@/utils
'
;
import
config
from
'
@/config
'
;
import
{
setAuthority
}
from
'
@/utils/authority
'
;
import
{
reloadAuthorized
}
from
'
@/utils/Authorized
'
;
const
initData
=
{};
export
default
{
...
...
@@ -14,6 +16,11 @@ export default {
*
login
({
payload
},
{
call
,
put
})
{
const
{
code
,
data
}
=
yield
call
(
login
,
payload
);
if
(
code
===
'
sys.success
'
)
{
yield
put
({
type
:
'
changeLoginStatus
'
,
payload
:
{},
});
reloadAuthorized
();
const
{
token
,
userId
}
=
data
;
store
.
set
(
'
token
'
,
token
);
store
.
set
(
'
userId
'
,
userId
);
...
...
@@ -34,9 +41,10 @@ export default {
window
.
location
.
href
=
redirect
;
return
;
}
}
else
{
redirect
=
config
.
homePage
;
}
// else {
// redirect = config.homePage;
// }
yield
put
(
routerRedux
.
replace
(
redirect
||
'
/
'
));
}
},
...
...
@@ -44,12 +52,10 @@ export default {
reducers
:
{
clearData
:
()
=>
({
...
initData
}),
changeLoginStatus
(
state
,
{
payload
})
{
// setAuthority(payload.currentAuthority);
// return {
// ...state,
// status: payload.status,
// type: payload.type,
// };
setAuthority
(
'
user
'
);
return
{
...
state
,
};
},
},
};
This diff is collapsed.
Click to expand it.
src/services/user.ts
View file @
b5cb5b84
import
request
from
'
@/utils/request
'
;
import
{
get
}
from
'
@/utils/request
'
;
export
async
function
query
():
Promise
<
any
>
{
return
reques
t
(
'
/api/users
'
);
return
ge
t
(
'
/api/users
'
);
}
export
async
function
queryCurrent
():
Promise
<
any
>
{
return
reques
t
(
'
/api/currentUser
'
);
return
ge
t
(
'
/api/currentUser
'
);
}
export
async
function
queryNotices
():
Promise
<
any
>
{
return
reques
t
(
'
/api/notices
'
);
return
ge
t
(
'
/api/notices
'
);
}
/**
* 查看是否已经登录
*/
export
const
checkUserLoginStatus
=
()
=>
get
(
`/api/v1/login/status`
);
This diff is collapsed.
Click to expand it.
src/utils/authority.ts
View file @
b5cb5b84
...
...
@@ -15,6 +15,10 @@ export function getAuthority(str?: string): string | string[] {
if
(
typeof
authority
===
'
string
'
)
{
return
[
authority
];
}
console
.
log
(
'
ANT_DESIGN_PRO_ONLY_DO_NOT_USE_IN_YOUR_PRODUCTION
'
,
ANT_DESIGN_PRO_ONLY_DO_NOT_USE_IN_YOUR_PRODUCTION
,
);
// preview.pro.ant.design only do not use in your production.
// preview.pro.ant.design 专用环境变量,请不要在你的项目中使用它。
if
(
!
authority
&&
ANT_DESIGN_PRO_ONLY_DO_NOT_USE_IN_YOUR_PRODUCTION
===
'
site
'
)
{
...
...
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment