github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/static_source/admin/src/directives/permission/hasPermi.ts (about) 1 import type { App, Directive, DirectiveBinding } from 'vue' 2 import { useI18n } from '@/hooks/web/useI18n' 3 import { useCache } from '@/hooks/web/useCache' 4 import { intersection } from 'lodash-es' 5 import { isArray } from '@/utils/is' 6 import { useAppStoreWithOut } from '@/store/modules/app' 7 8 const { t } = useI18n() 9 const { wsCache } = useCache() 10 const appStore = useAppStoreWithOut() 11 12 // 全部权限 13 const all_permission = ['*.*.*'] 14 const hasPermission = (value: string | string[]): boolean => { 15 // const permissions = wsCache.get(appStore.getUserInfo).permissions as string[] 16 // if (!value) { 17 // throw new Error(t('permission.hasPermission')) 18 // } 19 // if (!isArray(value)) { 20 // return permissions?.includes(value as string) 21 // } 22 // if (all_permission[0] === permissions[0]) { 23 // return true 24 // } 25 // return (intersection(value, permissions) as string[]).length > 0 26 return true 27 } 28 function hasPermi(el: Element, binding: DirectiveBinding) { 29 const value = binding.value 30 31 const flag = hasPermission(value) 32 if (!flag) { 33 el.parentNode?.removeChild(el) 34 } 35 } 36 const mounted = (el: Element, binding: DirectiveBinding<any>) => { 37 hasPermi(el, binding) 38 } 39 40 const permiDirective: Directive = { 41 mounted 42 } 43 44 export const setupPermissionDirective = (app: App<Element>) => { 45 app.directive('hasPermi', permiDirective) 46 } 47 48 export default permiDirective