【新增】前端代码第一次提交
This commit is contained in:
31
src/directives/permission/hasPermi.ts
Normal file
31
src/directives/permission/hasPermi.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
import type { App } from 'vue'
|
||||
import { useUserStore } from '@/store/modules/user'
|
||||
|
||||
const { t } = useI18n() // 国际化
|
||||
|
||||
/** 判断权限的指令 directive */
|
||||
export function hasPermi(app: App<Element>) {
|
||||
app.directive('hasPermi', (el, binding) => {
|
||||
const { value } = binding
|
||||
|
||||
if (value && value instanceof Array && value.length > 0) {
|
||||
const hasPermissions = hasPermission(value)
|
||||
|
||||
if (!hasPermissions) {
|
||||
el.parentNode && el.parentNode.removeChild(el)
|
||||
}
|
||||
} else {
|
||||
throw new Error(t('permission.hasPermission'))
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/** 判断权限的方法 function */
|
||||
const userStore = useUserStore()
|
||||
const all_permission = '*:*:*'
|
||||
export const hasPermission = (permission: string[]) => {
|
||||
return (
|
||||
userStore.permissions.has(all_permission) ||
|
||||
permission.some((permission) => userStore.permissions.has(permission))
|
||||
)
|
||||
}
|
||||
28
src/directives/permission/hasRole.ts
Normal file
28
src/directives/permission/hasRole.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
import type { App } from 'vue'
|
||||
import { CACHE_KEY, useCache } from '@/hooks/web/useCache'
|
||||
|
||||
const { t } = useI18n() // 国际化
|
||||
|
||||
export function hasRole(app: App<Element>) {
|
||||
app.directive('hasRole', (el, binding) => {
|
||||
const { wsCache } = useCache()
|
||||
const { value } = binding
|
||||
const super_admin = 'super_admin'
|
||||
const userInfo = wsCache.get(CACHE_KEY.USER)
|
||||
const roles = userInfo?.roles || []
|
||||
|
||||
if (value && value instanceof Array && value.length > 0) {
|
||||
const roleFlag = value
|
||||
|
||||
const hasRole = roles.some((role: string) => {
|
||||
return super_admin === role || roleFlag.includes(role)
|
||||
})
|
||||
|
||||
if (!hasRole) {
|
||||
el.parentNode && el.parentNode.removeChild(el)
|
||||
}
|
||||
} else {
|
||||
throw new Error(t('permission.hasRole'))
|
||||
}
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user