Browse Source

feat: 租户cookie改为store

pull/1154/head
colin 10 months ago
parent
commit
37391f840c
  1. 5
      apps/vben5/apps/app-antd/src/adapter/request/index.ts
  2. 8
      apps/vben5/packages/@abp/core/src/store/abp.ts
  3. 2
      apps/vben5/packages/@abp/saas/package.json
  4. 14
      apps/vben5/packages/@abp/saas/src/components/tenants/TenantSelectModal.vue

5
apps/vben5/apps/app-antd/src/adapter/request/index.ts

@ -6,6 +6,7 @@ import {
import { useAccessStore } from '@vben/stores'; import { useAccessStore } from '@vben/stores';
import { useOAuthError, useTokenApi } from '@abp/account'; import { useOAuthError, useTokenApi } from '@abp/account';
import { useAbpStore } from '@abp/core';
import { requestClient, useWrapperResult } from '@abp/request'; import { requestClient, useWrapperResult } from '@abp/request';
import { message } from 'ant-design-vue'; import { message } from 'ant-design-vue';
@ -58,12 +59,16 @@ export function initRequestClient() {
// 请求头处理 // 请求头处理
requestClient.addRequestInterceptor({ requestClient.addRequestInterceptor({
fulfilled: async (config) => { fulfilled: async (config) => {
const abpStore = useAbpStore();
const accessStore = useAccessStore(); const accessStore = useAccessStore();
if (accessStore.accessToken) { if (accessStore.accessToken) {
config.headers.Authorization = `${accessStore.accessToken}`; config.headers.Authorization = `${accessStore.accessToken}`;
} }
config.headers['Accept-Language'] = preferences.app.locale; config.headers['Accept-Language'] = preferences.app.locale;
config.headers['X-Request-From'] = 'vben'; config.headers['X-Request-From'] = 'vben';
if (abpStore.tenantId) {
config.headers.__tenant = abpStore.tenantId;
}
return config; return config;
}, },
}); });

8
apps/vben5/packages/@abp/core/src/store/abp.ts

@ -10,6 +10,7 @@ import { acceptHMRUpdate, defineStore } from 'pinia';
export const useAbpStore = defineStore( export const useAbpStore = defineStore(
'abp', 'abp',
() => { () => {
const tenantId = ref<string>();
const application = ref<ApplicationConfigurationDto>(); const application = ref<ApplicationConfigurationDto>();
const localization = ref<ApplicationLocalizationDto>(); const localization = ref<ApplicationLocalizationDto>();
/** 获取 i18n 格式本地化文本 */ /** 获取 i18n 格式本地化文本 */
@ -44,6 +45,11 @@ export const useAbpStore = defineStore(
}); });
return abpLocales; return abpLocales;
} }
function setTenantId(val?: string) {
tenantId.value = val;
}
function setApplication(val: ApplicationConfigurationDto) { function setApplication(val: ApplicationConfigurationDto) {
application.value = val; application.value = val;
} }
@ -63,6 +69,8 @@ export const useAbpStore = defineStore(
localization, localization,
setApplication, setApplication,
setLocalization, setLocalization,
setTenantId,
tenantId,
}; };
}, },
{ {

2
apps/vben5/packages/@abp/saas/package.json

@ -31,11 +31,9 @@
"@vben/hooks": "workspace:*", "@vben/hooks": "workspace:*",
"@vben/icons": "workspace:*", "@vben/icons": "workspace:*",
"@vben/locales": "workspace:*", "@vben/locales": "workspace:*",
"@vueuse/integrations": "catalog:",
"ant-design-vue": "catalog:", "ant-design-vue": "catalog:",
"dayjs": "catalog:", "dayjs": "catalog:",
"lodash.debounce": "catalog:", "lodash.debounce": "catalog:",
"universal-cookie": "catalog:",
"vue": "catalog:*", "vue": "catalog:*",
"vxe-table": "catalog:" "vxe-table": "catalog:"
}, },

14
apps/vben5/packages/@abp/saas/src/components/tenants/TenantSelectModal.vue

@ -4,7 +4,6 @@ import { ref } from 'vue';
import { useVbenForm, useVbenModal } from '@vben/common-ui'; import { useVbenForm, useVbenModal } from '@vben/common-ui';
import { $t } from '@vben/locales'; import { $t } from '@vben/locales';
import { useCookies } from '@vueuse/integrations/useCookies';
import { message } from 'ant-design-vue'; import { message } from 'ant-design-vue';
import { useMultiTenancyApi } from '../../api/useMultiTenancyApi'; import { useMultiTenancyApi } from '../../api/useMultiTenancyApi';
@ -19,7 +18,6 @@ const emits = defineEmits<{
}>(); }>();
const tenant = ref<Tenant>(); const tenant = ref<Tenant>();
const cookies = useCookies();
const { findTenantByNameApi } = useMultiTenancyApi(); const { findTenantByNameApi } = useMultiTenancyApi();
const [Form, formApi] = useVbenForm({ const [Form, formApi] = useVbenForm({
@ -55,10 +53,7 @@ async function onSubmit(values: Record<string, any>) {
modalApi.setState({ submitting: true }); modalApi.setState({ submitting: true });
try { try {
tenant.value = undefined; tenant.value = undefined;
cookies.remove('__tenant', { localStorage.removeItem('__tenant');
path: '/',
});
// localStorage.removeItem('__tenant');
if (values.name) { if (values.name) {
const result = await findTenantByNameApi(values.name); const result = await findTenantByNameApi(values.name);
if (!result.success) { if (!result.success) {
@ -74,12 +69,7 @@ async function onSubmit(values: Record<string, any>) {
return; return;
} }
tenant.value = { id: result.tenantId, name: result.normalizedName }; tenant.value = { id: result.tenantId, name: result.normalizedName };
if (result.tenantId) { localStorage.setItem('__tenant', result.tenantId!);
// localStorage.setItem('__tenant', result.tenantId);
cookies.set('__tenant', result.tenantId, {
path: '/',
});
}
} }
emits('change', tenant.value); emits('change', tenant.value);
modalApi.close(); modalApi.close();

Loading…
Cancel
Save