d2-auth-btn.js
4.05 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
export default {
name: 'AuthButton',
props: {
label: {
type: String,
default: ''
},
size: {
type: String,
default: 'mini'
},
// type: {
// type: String,
// default: 'text'
// },
category: {
type: String,
default: 'operation'
},
loading: {
type: Boolean,
default: false
},
disabled: {
type: Boolean,
default: false
},
id: {
type: String,
default: null
},
accept: {
type: String,
default: null
},
mapping: {
type: Object,
default: null
},
showBtnIds: {
type: Array,
// props类型为Object/Array 使用函数方法解决
default: () => []
},
data: Object
},
functional: true, // 开启函数化组件
render: (h, ctx) => {
const _this = ctx.parent // 获取 vue this 实例
// console.log(ctx);
/**
* 获取传入参数
* category 非必填 button 类别 暂时两种 1 表格操作:operation ,2 Button 按钮: action 默认 operation ----- route 中不标明 视为默认值 operation
* data 点击按钮时 需要得到的参数 比如 table 中的 当前行数据 row
* mapping 非必填 但: 当列表中的操作选项 根据 某个字段 变化的话 必填 且 必须 注明 field: 字段名
* label 当传入label 字段时 标识明确指定渲染该label 的按钮
* size 按钮样式,默认mini
* showBtnIds 按需显示按钮
*/
const { category, data, mapping, label, size, showBtnIds } = ctx.props
let curBtns = []
/**
* 判断一个按钮(btn)是否存在与当前行(data) 的映射关系(mapping)中
* @param {*} btn
*/
const filter = btn =>
mapping ? mapping[data[mapping.field]].includes(btn.label) : true
const $store = _this.$store.getters['d2admin/auth/curBtns']
if (!$store) return h('div')
if (label) {
// 指定label
const __item = $store.find(btn => btn.label === label && filter(btn))
__item && curBtns.push(__item)
} else {
/**
* 拿到当前模块(页面) 所拥有按钮列表 过滤type属性 与 映射关系 返回需要显示的按钮列表
*/
curBtns = $store.filter(btn => {
if (category === 'operation') {
return (btn.category === 'operation' || !btn.category) && filter(btn)
} else if (!category) {
// 未指定类别
return !btn.category && filter(btn)
}
return btn.category === category && filter(btn)
})
}
if (curBtns.length === 0) return h('div')
const __aBtns = []
/**
* 循环最终显示的按钮列表
* 并渲染到页面
*/
curBtns = showBtnIds.length
? curBtns.filter(item => showBtnIds.includes(item.authId))
: curBtns
curBtns.forEach(btn => {
// var judgeRes = data ? (data.yqMerchantId === _this.$store.state.d2admin.user.info.detail.merchantId) : true
// var isShow = !btn.judgeShow ? true : judgeRes
// if ((btn.category === 'Button') || (data && isShow)) {
__aBtns.push(
h(
'el-button',
{
props: {
type: btn.type || (btn.category === 'Button' ? '' : 'text'),
icon: btn.icon ? `el-icon-${btn.icon}` : '',
size: size,
disabled: btn.ruleObj
? btn.ruleObj.hasOwnProperty('disabledStatusList')
? btn.ruleObj.disabledStatusList.includes(
data[btn.ruleObj.attr]
)
: false
: false
},
style: {
color: btn.color
},
on: {
click: e => btn.handle && _this[btn.handle](data, e)
}
},
[btn.ruleObj ? btn.ruleObj.rule[data[btn.ruleObj.attr]] : btn.label]
)
)
// }
})
return h('span', {}, [...__aBtns, ctx.children])
}
}