Browse Source

Merge pull request #1154 from colinin/feat-login

Feat login
pull/1162/head
yx lin 1 year ago
committed by GitHub
parent
commit
9cd22b22e7
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 5
      apps/vben5/apps/app-antd/src/adapter/request/index.ts
  2. 76
      apps/vben5/apps/app-antd/src/store/auth.ts
  3. 8
      apps/vben5/packages/@abp/core/src/store/abp.ts
  4. 2
      apps/vben5/packages/@abp/saas/package.json
  5. 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;
}, },
}); });

76
apps/vben5/apps/app-antd/src/store/auth.ts

@ -40,8 +40,13 @@ export const useAuthStore = defineStore('auth', () => {
tenantId?: string, tenantId?: string,
onSuccess?: () => Promise<void> | void, onSuccess?: () => Promise<void> | void,
) { ) {
const result = await qrcodeLoginApi({ key, tenantId }); try {
return await _loginSuccess(result, onSuccess); loginLoading.value = true;
const result = await qrcodeLoginApi({ key, tenantId });
return await _loginSuccess(result, onSuccess);
} finally {
loginLoading.value = false;
}
} }
/** /**
@ -53,8 +58,13 @@ export const useAuthStore = defineStore('auth', () => {
params: Recordable<any>, params: Recordable<any>,
onSuccess?: () => Promise<void> | void, onSuccess?: () => Promise<void> | void,
) { ) {
const result = await loginApi(params as any); try {
return await _loginSuccess(result, onSuccess); loginLoading.value = true;
const result = await loginApi(params as any);
return await _loginSuccess(result, onSuccess);
} finally {
loginLoading.value = false;
}
} }
async function logout(redirect: boolean = true) { async function logout(redirect: boolean = true) {
@ -116,38 +126,34 @@ export const useAuthStore = defineStore('auth', () => {
) { ) {
// 异步处理用户登录操作并获取 accessToken // 异步处理用户登录操作并获取 accessToken
let userInfo: null | UserInfo = null; let userInfo: null | UserInfo = null;
try { loginLoading.value = true;
loginLoading.value = true; const { accessToken, tokenType, refreshToken } = loginResult;
const { accessToken, tokenType, refreshToken } = loginResult; // 如果成功获取到 accessToken
// 如果成功获取到 accessToken if (accessToken) {
if (accessToken) { accessStore.setAccessToken(`${tokenType} ${accessToken}`);
accessStore.setAccessToken(`${tokenType} ${accessToken}`); accessStore.setRefreshToken(refreshToken);
accessStore.setRefreshToken(refreshToken);
userInfo = await fetchUserInfo();
userInfo = await fetchUserInfo();
userStore.setUserInfo(userInfo);
userStore.setUserInfo(userInfo);
publish(Events.UserLogin, userInfo);
publish(Events.UserLogin, userInfo);
if (accessStore.loginExpired) {
if (accessStore.loginExpired) { accessStore.setLoginExpired(false);
accessStore.setLoginExpired(false); } else {
} else { onSuccess
onSuccess ? await onSuccess?.()
? await onSuccess?.() : await router.push(userInfo.homePath || DEFAULT_HOME_PATH);
: await router.push(userInfo.homePath || DEFAULT_HOME_PATH); }
}
if (userInfo?.realName) {
if (userInfo?.realName) { notification.success({
notification.success({ description: `${$t('authentication.loginSuccessDesc')}:${userInfo?.realName}`,
description: `${$t('authentication.loginSuccessDesc')}:${userInfo?.realName}`, duration: 3,
duration: 3, message: $t('authentication.loginSuccess'),
message: $t('authentication.loginSuccess'), });
});
}
} }
} finally {
loginLoading.value = false;
} }
return { return {

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