diff --git a/mock/role/index.ts b/mock/role/index.ts index 3797928d2..e0cde3c22 100644 --- a/mock/role/index.ts +++ b/mock/role/index.ts @@ -141,7 +141,7 @@ const adminList = [ component: 'views/Components/Table/TreeTable', name: 'TreeTable', meta: { - title: 'router.TreeTable' + title: 'TreeTable' } }, { @@ -151,15 +151,15 @@ const adminList = [ meta: { title: 'router.PicturePreview' } - }, - { - path: 'ref-table', - component: 'views/Components/Table/RefTable', - name: 'RefTable', - meta: { - title: 'RefTable' - } } + // { + // path: 'ref-table', + // component: 'views/Components/Table/RefTable', + // name: 'RefTable', + // meta: { + // title: 'RefTable' + // } + // } ] }, { @@ -269,14 +269,6 @@ const adminList = [ meta: { title: 'router.inputPassword' } - }, - { - path: 'sticky', - component: 'views/Components/Sticky', - name: 'Sticky', - meta: { - title: 'router.sticky' - } } ] }, @@ -472,6 +464,59 @@ const adminList = [ } } ] + }, + { + path: '/authorization', + component: '#', + redirect: '/authorization/user', + name: 'Authorization', + meta: { + title: 'router.authorization', + icon: 'eos-icons:role-binding', + alwaysShow: true + }, + children: [ + { + path: 'department', + component: 'views/Authorization/Department/Department', + name: 'Department', + meta: { + title: 'router.department' + } + }, + { + path: 'user', + component: 'views/Authorization/User/User', + name: 'User', + meta: { + title: 'router.user' + } + }, + { + path: 'menu', + component: 'views/Authorization/Menu/Menu', + name: 'Menu', + meta: { + title: 'router.menuManagement' + } + }, + { + path: 'role', + component: 'views/Authorization/Role/Role', + name: 'Role', + meta: { + title: 'router.role' + } + }, + { + path: 'test', + component: 'views/Authorization/Test/Test', + name: 'Test', + meta: { + title: 'router.permission' + } + } + ] } ] @@ -523,6 +568,12 @@ const testList: string[] = [ '/example/example-add', '/example/example-edit', '/example/example-detail', + '/authorization', + '/authorization/department', + '/authorization/user', + '/authorization/role', + '/authorization/menu', + '/authorization/test', '/error', '/error/404-demo', '/error/403-demo', diff --git a/src/components/Permission/index.ts b/src/components/Permission/index.ts new file mode 100644 index 000000000..250ae75b8 --- /dev/null +++ b/src/components/Permission/index.ts @@ -0,0 +1,4 @@ +import Permission from './src/Permission.vue' +import { hasPermi } from './src/utils' + +export { Permission, hasPermi } diff --git a/src/components/Permission/src/Permission.vue b/src/components/Permission/src/Permission.vue new file mode 100644 index 000000000..8581a9ddf --- /dev/null +++ b/src/components/Permission/src/Permission.vue @@ -0,0 +1,29 @@ + + + diff --git a/src/components/Permission/src/utils.ts b/src/components/Permission/src/utils.ts new file mode 100644 index 000000000..4c0a9b5f8 --- /dev/null +++ b/src/components/Permission/src/utils.ts @@ -0,0 +1,14 @@ +import { useI18n } from '@/hooks/web/useI18n' +import router from '@/router' + +export const hasPermi = (value: string) => { + const { t } = useI18n() + const permission = (router.currentRoute.value.meta.permission || []) as string[] + if (!value) { + throw new Error(t('permission.hasPermission')) + } + if (permission.includes(value)) { + return true + } + return false +} diff --git a/src/components/index.ts b/src/components/index.ts index 4d030c379..b20c4ec9e 100644 --- a/src/components/index.ts +++ b/src/components/index.ts @@ -1,6 +1,8 @@ import type { App } from 'vue' import { Icon } from './Icon' +import { Permission } from './Permission' export const setupGlobCom = (app: App): void => { app.component('Icon', Icon) + app.component('Permission', Permission) } diff --git a/src/directives/permission/hasPermi.ts b/src/directives/permission/hasPermi.ts index e51019c1e..0a74d9496 100644 --- a/src/directives/permission/hasPermi.ts +++ b/src/directives/permission/hasPermi.ts @@ -1,28 +1,18 @@ import type { App, Directive, DirectiveBinding } from 'vue' import { useI18n } from '@/hooks/web/useI18n' -import { useStorage } from '@/hooks/web/useStorage' -import { intersection } from 'lodash-es' -import { isArray } from '@/utils/is' -import { useAppStoreWithOut } from '@/store/modules/app' +import router from '@/router' const { t } = useI18n() -const { getStorage } = useStorage() -const appStore = useAppStoreWithOut() -// 全部权限 -const all_permission = ['*.*.*'] -const hasPermission = (value: string | string[]): boolean => { - const permissions = getStorage(appStore.getUserInfo).permissions as string[] +const hasPermission = (value: string): boolean => { + const permission = (router.currentRoute.value.meta.permission || []) 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]) { + if (permission.includes(value)) { return true } - return (intersection(value, permissions) as string[]).length > 0 + return false } function hasPermi(el: Element, binding: DirectiveBinding) { const value = binding.value diff --git a/src/locales/en.ts b/src/locales/en.ts index 8ec268b8a..ba217d9d5 100644 --- a/src/locales/en.ts +++ b/src/locales/en.ts @@ -162,7 +162,9 @@ export default { treeTable: 'Tree table', PicturePreview: 'Table Image Preview', department: 'Department management', - menuManagement: 'Menu management' + menuManagement: 'Menu management', + // 权限测试页面 + permission: 'Permission test page' }, permission: { hasPermission: 'Please set the operation permission value' diff --git a/src/locales/zh-CN.ts b/src/locales/zh-CN.ts index b44a59493..095748122 100644 --- a/src/locales/zh-CN.ts +++ b/src/locales/zh-CN.ts @@ -162,7 +162,8 @@ export default { treeTable: '树形表格', PicturePreview: '表格图片预览', department: '部门管理', - menuManagement: '菜单管理' + menuManagement: '菜单管理', + permission: '权限测试页' }, permission: { hasPermission: '请设置操作权限值' diff --git a/src/router/index.ts b/src/router/index.ts index cf338ab3b..72540c22c 100644 --- a/src/router/index.ts +++ b/src/router/index.ts @@ -548,6 +548,15 @@ export const asyncRouterMap: AppRouteRecordRaw[] = [ meta: { title: t('router.role') } + }, + { + path: 'test', + component: () => import('@/views/Authorization/Test/Test.vue'), + name: 'Test', + meta: { + title: t('router.permission'), + permission: ['add', 'edit', 'delete'] + } } ] } diff --git a/src/views/Authorization/Test/Test.vue b/src/views/Authorization/Test/Test.vue new file mode 100644 index 000000000..2209eeff2 --- /dev/null +++ b/src/views/Authorization/Test/Test.vue @@ -0,0 +1,70 @@ + + + diff --git a/types/components.d.ts b/types/components.d.ts index 85db5663b..33f73392c 100644 --- a/types/components.d.ts +++ b/types/components.d.ts @@ -1,6 +1,7 @@ declare module 'vue' { export interface GlobalComponents { Icon: typeof import('../components/Icon/src/Icon.vue')['default'] + Permission: typeof import('../components/Permission/src/Permission.vue')['default'] } }