SwitchTenant.vue 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <!-- 切换租户 -->
  2. <template>
  3. <div class="switchTenantLable-wrapper" v-if="optionsShown">
  4. <div class="switchTenantLable">选择租户</div>
  5. <el-tree-select
  6. :model-value="currentTenant"
  7. :data="options"
  8. check-strictly
  9. show-all-levels
  10. default-expand-all
  11. placeholder="请选择租户"
  12. @change="handleChange"
  13. />
  14. </div>
  15. </template>
  16. <script lang="ts" setup>
  17. import { ref, computed, onMounted } from 'vue';
  18. import { ElMessageBox, ElMessage } from 'element-plus';
  19. import { useUserStore } from '@/store/modules/user';
  20. import { queryListTenant } from '@/api/tenant';
  21. import { useTargetTenantIdSetting } from '@/utils/useTargetTenantIdSetting';
  22. import { replaceParams } from '@/utils/helper/treeHelper';
  23. import router from '@/router';
  24. import { useRoute } from 'vue-router';
  25. interface TenantOption {
  26. label: string;
  27. value: string;
  28. children?: TenantOption[];
  29. }
  30. const userStore = useUserStore();
  31. const { setValue, getValue } = useTargetTenantIdSetting();
  32. const localTId = getValue();
  33. const tenantId = computed(() => {
  34. return userStore.info.tenantId;
  35. });
  36. const currentTenant = ref(localTId ? Number(localTId) : Number(tenantId.value));
  37. const options = ref<TenantOption[]>([]);
  38. const route = useRoute();
  39. const optionsShown = computed(() => {
  40. // 列表空
  41. if (!options.value.length) return false;
  42. // 当前租户本身,且没有子租户
  43. if (options.value.length === 1 && !options.value[0].children?.length) return false;
  44. return true;
  45. });
  46. onMounted(() => {
  47. const routeTenantId = route.query.targetTenantId as string;
  48. if (routeTenantId) {
  49. const nextTenantId = Number(routeTenantId);
  50. currentTenant.value = nextTenantId;
  51. setValue(routeTenantId);
  52. }
  53. queryListTenant().then((res) => {
  54. if (!res) return;
  55. options.value = replaceParams(res || [], 'tenantName', 'id');
  56. });
  57. });
  58. /* 选择租户添加二次确认弹窗 */
  59. const handleChange = (targetTenantId: string) => {
  60. ElMessageBox.confirm('是否切换租户?', '提示', {
  61. confirmButtonText: '确定',
  62. cancelButtonText: '取消',
  63. type: 'warning',
  64. })
  65. .then(async () => {
  66. setValue(targetTenantId);
  67. currentTenant.value = Number(targetTenantId);
  68. // 统一返回首页
  69. await router.push({ name: 'DashboardConsole' });
  70. window.location.reload();
  71. })
  72. .catch(() => {
  73. ElMessage({
  74. type: 'info',
  75. message: '取消切换',
  76. });
  77. });
  78. };
  79. </script>
  80. <style scoped>
  81. .switchTenantLable {
  82. margin-right: 15px;
  83. display: inline-block;
  84. width: 100px;
  85. }
  86. .switchTenantWrapper {
  87. margin-right: 20px;
  88. }
  89. .switchTenantLable-wrapper {
  90. display: flex;
  91. align-items: center;
  92. justify-content: center;
  93. width: 200px;
  94. }
  95. </style>