selectTenant.vue
3.02 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
<template>
<div class="flex" v-if="interfaceType === 'SYSTEM' && isAdmin(role)">
<Select
v-model:value="selectTenant"
@change="onSelect"
style="width: 150px"
placeholder="请选择租户"
:options="selectOptions"
show-search
:filter-option="filterOption"
/>
<Select
v-model:value="selectSysTenant"
@change="onSelctTenant"
style="width: 150px; margin-left: 10px"
v-if="selectTenantOptions.length > 0"
placeholder="请选择租户"
:options="selectTenantOptions"
show-search
:filter-option="filterTenantOption"
/>
</div>
</template>
<script lang="ts" setup name="selectTenant">
import { onMounted, ref, onUnmounted } from 'vue';
import { selectType } from '../../../config/types';
import { Select } from 'ant-design-vue';
import { getUserToken } from '/@/api/sys/user';
import { USER_INFO_KEY } from '/@/enums/cacheEnum';
import { getAuthCache } from '/@/utils/auth';
import { isAdmin } from '/@/enums/roleEnum';
import { getTenantAllPageLists, getTenantPageList } from '/@/api/tenant/tenantApi';
defineProps({
interfaceType: {
type: String,
},
});
const emits = defineEmits(['emitToken']);
const userInfo: any = getAuthCache(USER_INFO_KEY);
const role: string = userInfo?.roles[0];
onMounted(async () => {
if (isAdmin(role)) {
const res = await getTenantPageList();
selectOptions.value = res.map((m) => ({ label: m.name, value: m.tenantId }));
} else {
//租户
const { token } = await getUserToken(userInfo?.userId);
getToken.value = token;
emits('emitToken', getToken.value, testDisabled.value);
}
});
const selectOptions = ref<selectType[]>([]);
const testDisabled = ref(true);
const selectTenantOptions = ref<selectType[]>([]);
const selectTenant = ref(undefined);
const selectSysTenant = ref(undefined);
const getToken = ref('');
const filterOption = (input: string, option: any) => {
return option.label.includes(input);
};
const filterTenantOption = (input: string, option: any) => {
return option.label.includes(input);
};
const onSelect = async (e) => {
if (e) {
testDisabled.value = false;
} else {
testDisabled.value = true;
}
selectSysTenant.value = undefined;
const rest = (await getTenantAllPageLists(e)) as any;
selectTenantOptions.value = rest?.map((m) => ({ label: m.realName, value: m.id }));
};
const onSelctTenant = async (e) => {
if (e) {
testDisabled.value = false;
} else {
testDisabled.value = true;
}
const { token } = await getUserToken(e);
getToken.value = token;
emits('emitToken', getToken.value, testDisabled.value);
};
onUnmounted(() => {
selectTenant.value = undefined;
selectSysTenant.value = undefined;
selectTenantOptions.value = [];
getToken.value = '';
});
</script>
<style scoped lang="less"></style>