刘光辉
11 小时以前 0dfe84494048ce27ba8449831782128412d3eb13
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
<script lang="ts" setup>
import { inject, nextTick, onMounted, reactive, toRefs } from 'vue';
 
import { useModal } from '@jnpf/ui/modal';
 
import { preferences } from '@vben/preferences';
import { useAccessStore } from '@vben/stores';
 
import { DownOutlined } from '@ant-design/icons-vue';
import { Dropdown, Menu } from 'ant-design-vue';
 
import { saasLoginApi } from '#/api';
import { getSaasList } from '#/api/permission/userSetting';
 
import DropMenuItem from '../user-dropdown/DropMenuItem.vue';
import Form from './Form.vue';
 
defineOptions({ name: 'TenantDropdown' });
 
interface State {
  tenantList: any[];
  activeItem: any;
}
 
const state = reactive<State>({
  tenantList: [],
  activeItem: {},
});
const { tenantList, activeItem } = toRefs(state);
const accessStore = useAccessStore();
const MenuItem = DropMenuItem;
const MenuDivider = Menu.Divider;
const [registerFormModal, { openModal: openFormModal }] = useModal();
const emitter: any = inject('emitter');
 
emitter.on('updateSassList', () => {
  initData();
});
 
function initData() {
  getSaasList().then((res) => {
    state.tenantList = res.data || [];
    state.activeItem = state.tenantList.filter((o) => o.currentTenant)[0] || {};
  });
}
function handleLoginTenant(tenantId, account) {
  saasLoginApi({ tenantId, account }).then((res) => {
    accessStore.setAccessToken(res.data.token);
    nextTick(() => {
      window.location.href = preferences.app.defaultHomePath;
    });
  });
}
 
onMounted(() => {
  initData();
});
</script>
<template>
  <Dropdown overlay-class-name="jnpf-header-tenant-dropdown-overlay" placement="bottom">
    <span class="jnpf-header-tenant-dropdown flex hover:bg-accent">{{ activeItem.companyName }}<DownOutlined class="ml-[4px]" /></span>
    <template #overlay>
      <Menu>
        <template v-for="item in tenantList" :key="item.tenantId">
          <MenuItem
            :disabled="item.tenantId === activeItem.tenantId"
            :text="item.companyName"
            :item-key="item.tenantId"
            @click="handleLoginTenant(item.tenantId, item.account)" />
        </template>
        <MenuDivider />
        <MenuItem key="add" item-key="add" text="加入企业" icon="icon-ym icon-ym-join-enterprise" @click="openFormModal(true, {})" />
      </Menu>
    </template>
  </Dropdown>
  <Form @register="registerFormModal" @reload="initData" />
</template>
<style lang="scss">
.jnpf-header-tenant-dropdown {
  align-items: center;
  height: 32px;
  padding: 10px;
  overflow: hidden;
  font-size: 14px;
  cursor: pointer;
  border-radius: var(--radius);
 
  &-overlay {
    min-width: 160px !important;
  }
}
 
html:not(.dark) {
  .jnpf-header-tenant-dropdown {
    &:hover {
      background-color: rgb(255 255 255 / 40%);
    }
  }
}
</style>