role-select.vue
4.4 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
132
133
134
135
136
137
<script setup lang="ts">
import { computed, onMounted, ref } from 'vue';
import useTagApi from '@/http/Tag';
import { TreeNodeData, TreeSelect, Checkbox } from '@arco-design/web-vue';
import { pull, startsWith, truncate, reject, find } from 'lodash';
const formValue = defineModel<string[]>({ default: () => [] });
const parentKey = { broker: 'captain', manager: 'project' };
const handleExtCheckTrue = (key: string, parentKey: string) => {
if (!formValue.value.includes(key)) {
formValue.value.push(key);
}
pull(formValue.value, parentKey);
};
const handleExtCheckFalse = (key: string, parentKey: string) => {
pull(formValue.value, key);
if (!formValue.value.includes(parentKey)) {
formValue.value.push(parentKey);
}
};
const handleExtCheck = (key: string, value: boolean) =>
value ? handleExtCheckTrue(key, parentKey[key]) : handleExtCheckFalse(key, parentKey[key]);
const brokerValue = computed({
get: (): boolean => formValue.value.includes('broker'),
set: (value: boolean) => handleExtCheck('broker', value),
});
const managerValue = computed({
get: (): boolean => formValue.value.includes('manager'),
set: (value: boolean) => handleExtCheck('manager', value),
});
const treeValue = computed({
get: () => {
const values = formValue.value.filter((item) => !['broker', 'manager'].includes(item));
if (formValue.value.includes('broker') && !formValue.value.includes('captain')) {
values.push('captain');
}
if (formValue.value.includes('manager') && !formValue.value.includes('project')) {
values.push('project');
}
return values;
},
set: (value) => {
if (!value.includes('project')) {
pull(formValue.value, 'manager');
}
if (!value.includes('captain')) {
pull(formValue.value, 'broker');
}
const values = formValue.value?.filter((item) => ['broker', 'manager'].includes(item));
formValue.value = [...reject(value, (item) => values?.map((item) => parentKey[item]).includes(item)), ...values];
},
});
const userAuthTags = ref<TreeNodeData[]>([]);
const options = computed((): TreeNodeData[] => {
return [
{ key: 'public', title: '未登录' },
{ key: 'visitor', title: '游客' },
{ key: 'musician', title: '音乐人', children: userAuthTags.value },
{ key: 'captain', title: '队长' },
{ key: 'project', title: '厂牌管理员' },
{ key: 'system', title: '平台管理员' },
];
});
onMounted(() => {
useTagApi.get({ type: 4, fetchType: 'all', setColumn: ['id', 'name'] }).then(({ data }) => {
userAuthTags.value = data.map((item) => ({ key: `musician_tag_${item.id}`, title: item.name }));
});
});
defineExpose({
formatRole: (role: string[]) => {
const values = [...options.value, { key: 'broker', title: '经纪人' }, { key: 'manager', title: '厂牌主理人' }];
return role
.map((item) => {
if (!startsWith(item, 'musician_tag_')) {
return find(values, { key: item })?.title;
}
const tagTitle = find(userAuthTags.value, { key: item })?.title;
if (tagTitle) {
return `音乐人 / ${tagTitle}`;
}
return tagTitle;
})
.filter((item) => item?.length !== 0)
.join('、');
},
});
</script>
<template>
<TreeSelect
v-model="treeValue"
:multiple="true"
:allow-clear="true"
:tree-checkable="true"
tree-checked-strategy="parent"
:fallback-option="false"
:data="options"
placeholder="请选择"
>
<template #label="{ data }">
<template v-if="data.value === 'captain' && brokerValue">经纪人</template>
<template v-else-if="data.value === 'project' && managerValue">厂牌主理人</template>
<template v-else-if="startsWith(data.value, 'musician_tag_')">音乐人 / {{ truncate(data.label, { length: 6 }) }}</template>
<template v-else>{{ data.label }}</template>
</template>
<template #tree-slot-extra="row">
<Checkbox v-if="row.key === 'captain'" v-model="brokerValue">仅通知经纪人</Checkbox>
<Checkbox v-if="row.key === 'project'" v-model="managerValue">仅通知主理人</Checkbox>
</template>
</TreeSelect>
</template>
<style>
.arco-tree-node:hover {
color: var(--color-text-1) !important;
background-color: var(--color-fill-2) !important;
}
</style>