Merge branch 'dev' of https://gitee.com/yudaocode/yudao-ui-admin-vben into dev
commit
785bd00e97
|
@ -1,6 +1,8 @@
|
||||||
<script lang="ts" setup>
|
<script lang="ts" setup>
|
||||||
import type { SelectValue } from 'ant-design-vue/es/select';
|
import type { SelectValue } from 'ant-design-vue/es/select';
|
||||||
|
|
||||||
|
import type { SystemTenantApi } from '#/api/system/tenant';
|
||||||
|
|
||||||
import { onMounted, ref } from 'vue';
|
import { onMounted, ref } from 'vue';
|
||||||
|
|
||||||
import { useAccess } from '@vben/access';
|
import { useAccess } from '@vben/access';
|
||||||
|
@ -9,56 +11,51 @@ import { useAccessStore } from '@vben/stores';
|
||||||
|
|
||||||
import { message, Select } from 'ant-design-vue';
|
import { message, Select } from 'ant-design-vue';
|
||||||
|
|
||||||
import { getTenantList } from '#/api/system/tenant';
|
import { getSimpleTenantList } from '#/api/system/tenant';
|
||||||
import { $t } from '#/locales';
|
import { $t } from '#/locales';
|
||||||
import { resetRoutes } from '#/router';
|
|
||||||
import { useAuthStore } from '#/store';
|
|
||||||
|
|
||||||
const { closeOtherTabs, refreshTab, closeAllTabs } = useTabs();
|
const { closeOtherTabs, refreshTab } = useTabs();
|
||||||
|
|
||||||
const { hasAccessByCodes } = useAccess();
|
const { hasAccessByCodes } = useAccess();
|
||||||
const accessStore = useAccessStore();
|
const accessStore = useAccessStore();
|
||||||
|
|
||||||
const authStore = useAuthStore();
|
|
||||||
|
|
||||||
const tenantEnable = isTenantEnable();
|
const tenantEnable = isTenantEnable();
|
||||||
|
|
||||||
const visitTenantList = accessStore.visitTenantId;
|
const value = ref<number>(accessStore.visitTenantId ?? undefined); // 当前访问的租户 ID
|
||||||
|
const tenants = ref<SystemTenantApi.Tenant[]>([]); // 租户列表
|
||||||
|
|
||||||
const tenant = ref<SelectValue>(accessStore.tenantId ?? 0);
|
async function handleChange(id: SelectValue) {
|
||||||
|
// 设置访问租户 ID
|
||||||
async function handleClick(id: SelectValue) {
|
accessStore.setVisitTenantId(id as number);
|
||||||
accessStore.setTenantId(Number(id));
|
// 关闭其他标签页,只保留当前页
|
||||||
await authStore.fetchUserInfo();
|
|
||||||
await resetRoutes();
|
|
||||||
await closeOtherTabs();
|
await closeOtherTabs();
|
||||||
await closeAllTabs();
|
// 刷新当前页面
|
||||||
await refreshTab();
|
await refreshTab();
|
||||||
message.success($t('page.tenant.success'));
|
// 提示切换成功
|
||||||
|
const tenant = tenants.value.find((item) => item.id === id);
|
||||||
|
if (tenant) {
|
||||||
|
message.success(`切换当前租户为: ${tenant.name}`);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
onMounted(async () => {
|
onMounted(async () => {
|
||||||
if (tenantEnable) {
|
if (!tenantEnable) {
|
||||||
const resp = await getTenantList();
|
return;
|
||||||
accessStore.setVisitTenantId(
|
|
||||||
resp
|
|
||||||
.map((item) => ({ id: item.id, name: item.name }))
|
|
||||||
.filter((item): item is { id: number; name: string } => !!item.id),
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
tenants.value = await getSimpleTenantList();
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
<template>
|
<template>
|
||||||
<div v-if="tenantEnable && hasAccessByCodes(['system:tenant:visit'])">
|
<div v-if="tenantEnable && hasAccessByCodes(['system:tenant:visit'])">
|
||||||
<Select
|
<Select
|
||||||
v-model:value="tenant"
|
v-model:value="value"
|
||||||
:field-names="{ label: 'name', value: 'id' }"
|
:field-names="{ label: 'name', value: 'id' }"
|
||||||
:options="visitTenantList"
|
:options="tenants"
|
||||||
:placeholder="$t('page.tenant.placeholder')"
|
:placeholder="$t('page.tenant.placeholder')"
|
||||||
:dropdown-style="{ position: 'fixed', zIndex: 1666 }"
|
:dropdown-style="{ position: 'fixed', zIndex: 1666 }"
|
||||||
allow-clear
|
allow-clear
|
||||||
class="w-40"
|
class="w-40"
|
||||||
@change="handleClick"
|
@change="handleChange"
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
|
|
@ -6,11 +6,6 @@ import { acceptHMRUpdate, defineStore } from 'pinia';
|
||||||
|
|
||||||
type AccessToken = null | string;
|
type AccessToken = null | string;
|
||||||
|
|
||||||
type VisitTenantId = {
|
|
||||||
id: number;
|
|
||||||
name: string;
|
|
||||||
};
|
|
||||||
|
|
||||||
interface AccessState {
|
interface AccessState {
|
||||||
/**
|
/**
|
||||||
* 权限码
|
* 权限码
|
||||||
|
@ -55,7 +50,7 @@ interface AccessState {
|
||||||
/**
|
/**
|
||||||
* 访问租户编号
|
* 访问租户编号
|
||||||
*/
|
*/
|
||||||
visitTenantId: VisitTenantId[];
|
visitTenantId: null | number;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -110,7 +105,7 @@ export const useAccessStore = defineStore('core-access', {
|
||||||
setTenantId(tenantId: null | number) {
|
setTenantId(tenantId: null | number) {
|
||||||
this.tenantId = tenantId;
|
this.tenantId = tenantId;
|
||||||
},
|
},
|
||||||
setVisitTenantId(visitTenantId: VisitTenantId[]) {
|
setVisitTenantId(visitTenantId: number) {
|
||||||
this.visitTenantId = visitTenantId;
|
this.visitTenantId = visitTenantId;
|
||||||
},
|
},
|
||||||
unlockScreen() {
|
unlockScreen() {
|
||||||
|
@ -141,7 +136,7 @@ export const useAccessStore = defineStore('core-access', {
|
||||||
loginExpired: false,
|
loginExpired: false,
|
||||||
refreshToken: null,
|
refreshToken: null,
|
||||||
tenantId: null,
|
tenantId: null,
|
||||||
visitTenantId: [],
|
visitTenantId: null,
|
||||||
}),
|
}),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue