|
@@ -3,32 +3,33 @@ import { useI18n } from '@/hooks/web/useI18n'
|
|
import { useCache } from '@/hooks/web/useCache'
|
|
import { useCache } from '@/hooks/web/useCache'
|
|
import { intersection } from 'lodash-es'
|
|
import { intersection } from 'lodash-es'
|
|
import { isArray } from '@/utils/is'
|
|
import { isArray } from '@/utils/is'
|
|
|
|
+import { useAppStoreWithOut } from '@/store/modules/app'
|
|
|
|
|
|
const { t } = useI18n()
|
|
const { t } = useI18n()
|
|
const { wsCache } = useCache()
|
|
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) {
|
|
function hasPermi(el: Element, binding: DirectiveBinding) {
|
|
const value = binding.value
|
|
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)
|
|
el.parentNode?.removeChild(el)
|
|
- } else {
|
|
|
|
- el.parentNode && el.parentNode.removeChild(el)
|
|
|
|
- throw new Error(t('permission.hasPermission'))
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
const mounted = (el: Element, binding: DirectiveBinding<any>) => {
|
|
const mounted = (el: Element, binding: DirectiveBinding<any>) => {
|
|
@@ -39,7 +40,7 @@ const permiDirective: Directive = {
|
|
mounted
|
|
mounted
|
|
}
|
|
}
|
|
|
|
|
|
-export function setupPermissionDirective(app: App<Element>) {
|
|
|
|
|
|
+export const setupPermissionDirective = (app: App<Element>) => {
|
|
app.directive('hasPermi', permiDirective)
|
|
app.directive('hasPermi', permiDirective)
|
|
}
|
|
}
|
|
|
|
|