feat: 添加按钮权限

This commit is contained in:
xingyu
2022-07-18 16:02:20 +08:00
parent b3918b9c3c
commit 7bef662db1
3 changed files with 27 additions and 22 deletions

View File

@@ -3,32 +3,33 @@ import { useI18n } from '@/hooks/web/useI18n'
import { useCache } from '@/hooks/web/useCache'
import { intersection } from 'lodash-es'
import { isArray } from '@/utils/is'
import { useAppStoreWithOut } from '@/store/modules/app'
const { t } = useI18n()
const { wsCache } = useCache()
// 全部权限
const all_permission = '*:*:*'
const permissions = wsCache.get('userInfo').permissions
const appStore = useAppStoreWithOut()
// 全部权限
const all_permission = ['*.*.*']
const hasPermission = (value: string | string[]): boolean => {
const permissions = wsCache.get(appStore.getUserInfo).permissions as string[]
if (!value) {
throw new Error(t('permission.hasPermission'))
}
if (!isArray(value)) {
return permissions?.includes(value as string)
}
if (all_permission[0] === permissions[0]) {
return true
}
return (intersection(value, permissions) as string[]).length > 0
}
function hasPermi(el: Element, binding: DirectiveBinding) {
const value = binding.value
const hasPermission = (value: string | string[]): boolean => {
if (all_permission === permissions) return true
if (!value) return true
if (!isArray(value)) {
return permissions?.includes(value as string)
}
return (intersection(value, permissions) as string[]).length > 0
}
if (!hasPermission(value)) {
const flag = hasPermission(value)
if (!flag) {
el.parentNode?.removeChild(el)
} else {
el.parentNode && el.parentNode.removeChild(el)
throw new Error(t('permission.hasPermission'))
}
}
const mounted = (el: Element, binding: DirectiveBinding<any>) => {
@@ -39,7 +40,7 @@ const permiDirective: Directive = {
mounted
}
export function setupPermissionDirective(app: App<Element>) {
export const setupPermissionDirective = (app: App<Element>) => {
app.directive('hasPermi', permiDirective)
}