index.js
1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
const DISPLAY = Object.freeze({
NONE: '',
DISABLED: 'disabled'
})
// 权限校验
function permission(ctx) {
const {
props: { fn, mode, display },
parent
} = ctx
if (__includeFn(parent, fn)) {
return mode
} else if (display === DISPLAY.DISABLED) {
ctx.data.attrs.disabled = true
return mode
}
return DISPLAY.NONE
}
export function hasPermission(fn, display) {
return __includeFn(this, fn) || display === DISPLAY.DISABLED
}
function __includeFn(ctx, fn) {
const router = __getRoutes(ctx)
return router && router.meta && router.meta.permissions && router.meta.permissions.includes(fn)
}
function __getRoutes(ctx) {
const {
$GLBUTTION_PERMISSIONS: { routes = [], routerBase }
} = ctx
return routes.find(route => location.pathname === `${routerBase}${route.path.split(':/')[0]}`)
}
// GLButton权限按钮组件
export default {
functional: true,
name: 'GLButton',
props: {
// fn权限对应的名字
fn: {
type: String
},
// 生成的组件类型
mode: {
type: String,
default: 'el-button'
},
// 是否显示
display: {
type: String,
default: ''
}
},
render: function(createElement, context) {
return createElement(permission(context), context.data, context.children)
}
}