mirror of
https://gitee.com/hhyykk/ipms-sjy-ui.git
synced 2025-07-16 11:55:07 +08:00
初始化项目,自 v1.7.1 版本开始
This commit is contained in:
13
src/directives/index.ts
Normal file
13
src/directives/index.ts
Normal file
@ -0,0 +1,13 @@
|
||||
import type { App } from 'vue'
|
||||
import { hasRole } from './permission/hasRole'
|
||||
import { hasPermi } from './permission/hasPermi'
|
||||
|
||||
/**
|
||||
* 导出指令:v-xxx
|
||||
* @methods hasRole 用户权限,用法: v-hasRole
|
||||
* @methods hasPermi 按钮权限,用法: v-hasPermi
|
||||
*/
|
||||
export const setupAuth = (app: App<Element>) => {
|
||||
hasRole(app)
|
||||
hasPermi(app)
|
||||
}
|
27
src/directives/permission/hasPermi.ts
Normal file
27
src/directives/permission/hasPermi.ts
Normal file
@ -0,0 +1,27 @@
|
||||
import type { App } from 'vue'
|
||||
import { CACHE_KEY, useCache } from '@/hooks/web/useCache'
|
||||
|
||||
const { t } = useI18n() // 国际化
|
||||
|
||||
export function hasPermi(app: App<Element>) {
|
||||
app.directive('hasPermi', (el, binding) => {
|
||||
const { wsCache } = useCache()
|
||||
const { value } = binding
|
||||
const all_permission = '*:*:*'
|
||||
const permissions = wsCache.get(CACHE_KEY.USER).permissions
|
||||
|
||||
if (value && value instanceof Array && value.length > 0) {
|
||||
const permissionFlag = value
|
||||
|
||||
const hasPermissions = permissions.some((permission: string) => {
|
||||
return all_permission === permission || permissionFlag.includes(permission)
|
||||
})
|
||||
|
||||
if (!hasPermissions) {
|
||||
el.parentNode && el.parentNode.removeChild(el)
|
||||
}
|
||||
} else {
|
||||
throw new Error(t('permission.hasPermission'))
|
||||
}
|
||||
})
|
||||
}
|
27
src/directives/permission/hasRole.ts
Normal file
27
src/directives/permission/hasRole.ts
Normal file
@ -0,0 +1,27 @@
|
||||
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 = 'admin'
|
||||
const roles = wsCache.get(CACHE_KEY.USER).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