SwitchTenant.vue 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <!-- 切换租户 -->
  2. <template>
  3. <div class="switchTenantLable-wrapper" v-if="options.length > 0 ">
  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, watch } 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 { useRoute } from 'vue-router';
  24. import useMiniMap from '@/views/map-config/mini-map/use-mini-map.ts'
  25. import router from '@/router';
  26. const miniMap = useMiniMap();
  27. const {isInConfigEditor} = miniMap;
  28. interface TenantOption {
  29. label: string;
  30. value: string;
  31. }
  32. const userStore = useUserStore();
  33. const { setValue, getValue } = useTargetTenantIdSetting();
  34. const localTId = getValue();
  35. const tenantId = computed(() => {
  36. return userStore.info.tenantId;
  37. });
  38. const currentTenant = ref(localTId ? Number(localTId) : Number(tenantId.value));
  39. const options = ref<TenantOption[]>([]);
  40. onMounted(() => {
  41. queryListTenant().then((res) => {
  42. if (!res) return;
  43. options.value = replaceParams(res || [], 'tenantName', 'id')
  44. });
  45. });
  46. const route = useRoute();
  47. /* 选择租户添加二次确认弹窗 */
  48. const handleChange = (targetTenantId: string) => {
  49. ElMessageBox.confirm('是否切换租户?', '提示', {
  50. confirmButtonText: '确定',
  51. cancelButtonText: '取消',
  52. type: 'warning',
  53. }).then(() => {
  54. setValue(targetTenantId);
  55. currentTenant.value = Number(targetTenantId);
  56. /**
  57. * 如果处于小地图编辑状态,切换租户时候,返回布局列表
  58. */
  59. if (isInConfigEditor(route.name)) {
  60. sessionStorage.removeItem('selectCompanyId'); //切换租户的时候,下拉公司列表清空
  61. router.back();
  62. } else {
  63. window.location.reload();
  64. }
  65. }).catch(() => {
  66. ElMessage({
  67. type: 'info',
  68. message: '取消删除',
  69. });
  70. });
  71. };
  72. </script>
  73. <style scoped>
  74. .switchTenantLable {
  75. margin-right: 15px;
  76. display: inline-block;
  77. width: 100px;
  78. }
  79. .switchTenantWrapper {
  80. margin-right: 20px;
  81. }
  82. .switchTenantLable-wrapper {
  83. display: flex;
  84. align-items: center;
  85. justify-content: center;
  86. width: 200px;
  87. }
  88. </style>