feat: 添加按钮权限
This commit is contained in:
parent
d6eb94e8b8
commit
753b73972b
|
@ -0,0 +1,10 @@
|
||||||
|
import type { App } from 'vue'
|
||||||
|
import { setupPermissionDirective } from './permission/hasPermi'
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 导出指令:v-xxx
|
||||||
|
* @methods hasPermi 按钮权限,用法: v-hasPermi
|
||||||
|
*/
|
||||||
|
export const setupPermission = (app: App<Element>) => {
|
||||||
|
setupPermissionDirective(app)
|
||||||
|
}
|
|
@ -0,0 +1,46 @@
|
||||||
|
import type { App, Directive, DirectiveBinding } from 'vue'
|
||||||
|
import { useI18n } from '@/hooks/web/useI18n'
|
||||||
|
import { useCache } from '@/hooks/web/useCache'
|
||||||
|
import { intersection } from 'lodash-es'
|
||||||
|
import { isArray } from '@/utils/is'
|
||||||
|
|
||||||
|
const { t } = useI18n()
|
||||||
|
const { wsCache } = useCache()
|
||||||
|
// 全部权限
|
||||||
|
const all_permission = '*:*:*'
|
||||||
|
const permissions = wsCache.get('userInfo').permissions
|
||||||
|
|
||||||
|
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)) {
|
||||||
|
el.parentNode?.removeChild(el)
|
||||||
|
} else {
|
||||||
|
el.parentNode && el.parentNode.removeChild(el)
|
||||||
|
throw new Error(t('permission.hasPermission'))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const mounted = (el: Element, binding: DirectiveBinding<any>) => {
|
||||||
|
hasPermi(el, binding)
|
||||||
|
}
|
||||||
|
|
||||||
|
const permiDirective: Directive = {
|
||||||
|
mounted
|
||||||
|
}
|
||||||
|
|
||||||
|
export function setupPermissionDirective(app: App<Element>) {
|
||||||
|
app.directive('hasPermi', permiDirective)
|
||||||
|
}
|
||||||
|
|
||||||
|
export default permiDirective
|
|
@ -143,6 +143,9 @@ export default {
|
||||||
inputPassword: 'InputPassword',
|
inputPassword: 'InputPassword',
|
||||||
sticky: 'Sticky'
|
sticky: 'Sticky'
|
||||||
},
|
},
|
||||||
|
permission: {
|
||||||
|
hasPermission: 'Please set the operation permission value'
|
||||||
|
},
|
||||||
analysis: {
|
analysis: {
|
||||||
newUser: 'New user',
|
newUser: 'New user',
|
||||||
unreadInformation: 'Unread information',
|
unreadInformation: 'Unread information',
|
||||||
|
|
|
@ -143,6 +143,9 @@ export default {
|
||||||
inputPassword: '密码输入框',
|
inputPassword: '密码输入框',
|
||||||
sticky: '黏性'
|
sticky: '黏性'
|
||||||
},
|
},
|
||||||
|
permission: {
|
||||||
|
hasPermission: '请设置操作权限值'
|
||||||
|
},
|
||||||
analysis: {
|
analysis: {
|
||||||
newUser: '新增用户',
|
newUser: '新增用户',
|
||||||
unreadInformation: '未读消息',
|
unreadInformation: '未读消息',
|
||||||
|
|
|
@ -25,6 +25,9 @@ import '@/plugins/animate.css'
|
||||||
// 路由
|
// 路由
|
||||||
import { setupRouter } from './router'
|
import { setupRouter } from './router'
|
||||||
|
|
||||||
|
// 权限
|
||||||
|
import { setupPermission } from './directives'
|
||||||
|
|
||||||
import { createApp } from 'vue'
|
import { createApp } from 'vue'
|
||||||
|
|
||||||
import App from './App.vue'
|
import App from './App.vue'
|
||||||
|
@ -44,6 +47,8 @@ const setupAll = async () => {
|
||||||
|
|
||||||
setupRouter(app)
|
setupRouter(app)
|
||||||
|
|
||||||
|
setupPermission(app)
|
||||||
|
|
||||||
app.mount('#app')
|
app.mount('#app')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -237,7 +237,7 @@ const save = async () => {
|
||||||
<ElButton type="success" @click="action(row, 'detail')">
|
<ElButton type="success" @click="action(row, 'detail')">
|
||||||
{{ t('exampleDemo.detail') }}
|
{{ t('exampleDemo.detail') }}
|
||||||
</ElButton>
|
</ElButton>
|
||||||
<ElButton type="danger" @click="delData(row, false)">
|
<ElButton type="danger" v-hasPermi="['example:dialog:delete']" @click="delData(row, false)">
|
||||||
{{ t('exampleDemo.del') }}
|
{{ t('exampleDemo.del') }}
|
||||||
</ElButton>
|
</ElButton>
|
||||||
</template>
|
</template>
|
||||||
|
|
Loading…
Reference in New Issue