useTargetTenantIdSetting.ts 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import { useUserStore } from '@/store/modules/user';
  2. import { watch } from 'vue';
  3. /** 系统租户的id为0 */
  4. export const SYS_TENANT_ID = 0;
  5. /** 设置targetTenantId */
  6. export function useTargetTenantIdSetting() {
  7. let fullKey = '';
  8. const userStore = useUserStore();
  9. watch(
  10. () => [userStore.info.username, userStore.info.tenantCode],
  11. ([username, tenantCode]) => {
  12. fullKey = `${tenantCode || ''}_${username}_targetTenantId`;
  13. },
  14. { immediate: true },
  15. );
  16. function setValue(tenantId: string | string) {
  17. if (!fullKey) {
  18. return;
  19. }
  20. localStorage.setItem(fullKey, String(tenantId));
  21. }
  22. function getValue() {
  23. if (!fullKey) {
  24. return;
  25. }
  26. return localStorage.getItem(fullKey);
  27. }
  28. // 系统租户
  29. const sysTenantId = getValue() || userStore.info.tenantId?.toString();
  30. /** 当前选择的租户是否是系统租户 */
  31. const isTargetTenantSys = sysTenantId === SYS_TENANT_ID.toString();
  32. return { setValue, getValue, isTargetTenantSys };
  33. }