Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
H
haoban-4
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
office
haoban-4
Commits
e97df29e
Commit
e97df29e
authored
Jul 31, 2020
by
caoyanzhi
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
页面访问拦截和按钮操作拦截
parent
aa83c3aa
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
100 additions
and
0 deletions
+100
-0
permission.js
src/utils/permission.js
+100
-0
No files found.
src/utils/permission.js
0 → 100644
View file @
e97df29e
import
{
request
}
from
'./request.js'
;
import
{
origin
}
from
'@/config'
;
// 项目中配置的路由信息
let
projectRoutes
=
[];
// 有权限访问的路由
let
accessPath
=
[];
// 无权限的操作项
let
itemPermission
=
{};
function
getGicMenu
()
{
const
getUserMenu
=
request
({
url
:
'/hb-manage-web/list-menus-auth'
}).
then
(
res
=>
{
accessPath
=
getProjectMenu
(
res
.
result
||
[]);
});
const
getUserOperation
=
request
({
url
:
'/hb-manage-web/list-operation-auth'
}).
then
(
res
=>
{
itemPermission
=
getItemPerm
(
res
.
result
||
[]);
})
return
Promise
.
all
([
getUserMenu
,
getUserOperation
]);
}
function
getProjectMenu
(
menu
)
{
let
result
=
[];
menu
.
forEach
(
el
=>
{
if
(
el
.
menuUrl
)
{
result
.
push
(
el
.
menuUrl
);
}
if
(
Array
.
isArray
(
el
.
children
))
{
result
=
result
.
concat
(
getProjectMenu
(
el
.
children
));
}
})
return
result
;
}
function
getItemPerm
(
itemPerms
)
{
let
result
=
{};
itemPerms
.
forEach
(
el
=>
{
result
[
el
.
menuCode
]
=
true
;
})
return
result
;
}
function
flatRoutes
(
routes
)
{
let
result
=
[];
routes
.
forEach
(
el
=>
{
if
(
Array
.
isArray
(
el
.
children
))
{
result
=
result
.
concat
(
flatRoutes
(
el
.
children
));
}
result
.
push
(
el
.
path
);
})
return
result
;
}
// 路由跳转拦截
function
intercept
(
to
,
from
,
next
)
{
const
hasPerm
=
accessPath
.
some
(
el
=>
{
return
el
.
indexOf
(
'/#'
)
>=
0
?
el
==
`/#
${
to
.
path
}
`
:
el
==
to
.
path
;
})
// 有权限访问
if
(
hasPerm
)
{
return
next
();
};
// 无权限访问
if
(
projectRoutes
.
some
(
el
=>
el
==
to
.
path
)
||
accessPath
.
length
==
0
)
{
window
.
location
.
href
=
`
${
origin
}
/damo-system/no-access`
;
}
else
{
next
(
accessPath
[
0
]);
}
}
// 操作项权限拦截(包括按钮、下拉列表选项、switch开关、复选框等等操作项)
function
itemPerm
(
itemPermCode
)
{
return
!!
itemPermission
[
itemPermCode
];
}
let
permission
=
function
(
config
)
{
const
{
writeRoute
,
router
,
routes
,
createApp
}
=
config
;
window
.
onload
=
async
function
()
{
// 获取菜单数据
// 获取权限数据
const
path
=
window
.
location
.
pathname
;
if
(
path
.
indexOf
(
'/login'
)
==
-
1
)
{
await
getGicMenu
();
}
if
(
Array
.
isArray
(
writeRoute
))
{
accessPath
=
accessPath
.
concat
(
writeRoute
);
}
if
(
Array
.
isArray
(
routes
))
{
// 项目中路由文件配置的路径
projectRoutes
=
flatRoutes
(
routes
);
}
router
.
beforeEach
(
intercept
);
// 创建vue实例,渲染页面
if
(
typeof
createApp
==
'function'
)
createApp
();
};
}
export
{
permission
,
itemPerm
}
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