Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
phsl
/
admin
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
d710c9d2
authored
Jan 21, 2025
by
zws
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
perf: 添加权限Set 替换检查权限数组 提高检查权限性能
parent
5c0ee866
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
14 additions
and
28 deletions
+14
-28
src/directives/permission/hasPermi.ts
+7
-13
src/store/modules/user.ts
+3
-0
src/utils/permission.ts
+4
-15
No files found.
src/directives/permission/hasPermi.ts
View file @
d710c9d2
import
type
{
App
}
from
'vue'
import
type
{
App
}
from
'vue'
import
{
CACHE_KEY
,
useCache
}
from
'@/hooks/web/useCache'
import
{
useUserStore
}
from
"@/store/modules/user"
;
const
{
t
}
=
useI18n
()
// 国际化
const
{
t
}
=
useI18n
()
// 国际化
...
@@ -18,14 +18,9 @@ export function hasPermi(app: App<Element>) {
...
@@ -18,14 +18,9 @@ export function hasPermi(app: App<Element>) {
}
}
})
})
}
}
const
userStore
=
useUserStore
();
const
all_permission
=
'*:*:*'
export
const
hasPermission
=
(
permission
:
string
[])
=>
{
export
const
hasPermission
=
(
permission
:
string
[])
=>
{
const
{
wsCache
}
=
useCache
()
return
userStore
.
permissionsSet
.
has
(
all_permission
)
||
const
all_permission
=
'*:*:*'
permission
.
some
(
permission
=>
userStore
.
permissionsSet
.
has
(
permission
))
const
userInfo
=
wsCache
.
get
(
CACHE_KEY
.
USER
)
}
const
permissions
=
userInfo
?.
permissions
||
[]
return
permissions
.
some
((
p
:
string
)
=>
{
return
all_permission
===
p
||
permission
.
includes
(
p
)
})
}
\ No newline at end of file
src/store/modules/user.ts
View file @
d710c9d2
...
@@ -16,6 +16,7 @@ interface UserVO {
...
@@ -16,6 +16,7 @@ interface UserVO {
interface
UserInfoVO
{
interface
UserInfoVO
{
// USER 缓存
// USER 缓存
permissions
:
string
[]
permissions
:
string
[]
permissionsSet
:
Set
<
string
>
roles
:
string
[]
roles
:
string
[]
isSetUser
:
boolean
isSetUser
:
boolean
user
:
UserVO
user
:
UserVO
...
@@ -24,6 +25,7 @@ interface UserInfoVO {
...
@@ -24,6 +25,7 @@ interface UserInfoVO {
export
const
useUserStore
=
defineStore
(
'admin-user'
,
{
export
const
useUserStore
=
defineStore
(
'admin-user'
,
{
state
:
():
UserInfoVO
=>
({
state
:
():
UserInfoVO
=>
({
permissions
:
[],
permissions
:
[],
permissionsSet
:
new
Set
<
string
>
(),
roles
:
[],
roles
:
[],
isSetUser
:
false
,
isSetUser
:
false
,
user
:
{
user
:
{
...
@@ -58,6 +60,7 @@ export const useUserStore = defineStore('admin-user', {
...
@@ -58,6 +60,7 @@ export const useUserStore = defineStore('admin-user', {
userInfo
=
await
getInfo
()
userInfo
=
await
getInfo
()
}
}
this
.
permissions
=
userInfo
.
permissions
this
.
permissions
=
userInfo
.
permissions
this
.
permissionsSet
=
new
Set
(
userInfo
.
permissions
)
this
.
roles
=
userInfo
.
roles
this
.
roles
=
userInfo
.
roles
this
.
user
=
userInfo
.
user
this
.
user
=
userInfo
.
user
this
.
isSetUser
=
true
this
.
isSetUser
=
true
...
...
src/utils/permission.ts
View file @
d710c9d2
import
{
CACHE_KEY
,
useCache
}
from
'@/hooks/web/useCache'
import
{
CACHE_KEY
,
useCache
}
from
'@/hooks/web/useCache'
import
{
hasPermission
}
from
"@/directives/permission/hasPermi"
;
const
{
t
}
=
useI18n
()
// 国际化
const
{
t
}
=
useI18n
()
// 国际化
...
@@ -7,21 +9,8 @@ const { t } = useI18n() // 国际化
...
@@ -7,21 +9,8 @@ const { t } = useI18n() // 国际化
* @param {Array} value 校验值
* @param {Array} value 校验值
* @returns {Boolean}
* @returns {Boolean}
*/
*/
export
function
checkPermi
(
value
:
string
[])
{
export
function
checkPermi
(
permission
:
string
[])
{
if
(
value
&&
value
instanceof
Array
&&
value
.
length
>
0
)
{
return
hasPermission
(
permission
)
const
{
wsCache
}
=
useCache
()
const
permissionDatas
=
value
const
all_permission
=
'*:*:*'
const
userInfo
=
wsCache
.
get
(
CACHE_KEY
.
USER
)
const
permissions
=
userInfo
?.
permissions
||
[]
const
hasPermission
=
permissions
.
some
((
permission
:
string
)
=>
{
return
all_permission
===
permission
||
permissionDatas
.
includes
(
permission
)
})
return
!!
hasPermission
}
else
{
console
.
error
(
t
(
'permission.hasPermission'
))
return
false
}
}
}
/**
/**
...
...
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