refactor: 优化登录逻辑并添加注释

This commit is contained in:
yangsy
2025-12-17 13:27:02 +08:00
parent 495dc001a1
commit 073a29a83a
2 changed files with 18 additions and 20 deletions

View File

@@ -32,7 +32,17 @@ const { mutate: login, isPending: loading } = useMutation({
onError: (error) => {
console.error(error);
const errorFeedback = parseErrorFeedback(error);
window.$message.error(errorFeedback);
window.$dialog.destroyAll();
window.$dialog.error({
closable: false,
maskClosable: false,
title: '错误提示',
content: errorFeedback,
positiveText: '确认',
onPositiveClick: () => {
window.$message.destroyAll();
},
});
},
});
</script>

View File

@@ -1,13 +1,13 @@
import { useStationStore } from './station';
import { usePollingStore } from './polling';
import { userClient, type LoginParams, type LoginResult, type Station } from '@/apis';
import { NDM_USER_STORE_ID } from '@/constants';
import type { Result } from '@/types';
import { AesEncryption, getAppEnvConfig } from '@/utils';
import axios, { AxiosError } from 'axios';
import dayjs from 'dayjs';
import { defineStore } from 'pinia';
import { computed, ref } from 'vue';
import { NDM_USER_STORE_ID } from '@/constants';
const getHeaders = () => {
const { lampAuthorization, lampClientId, lampClientSecret } = getAppEnvConfig();
@@ -49,24 +49,12 @@ export const useUserStore = defineStore(
key,
grantType,
};
const { data: respData } = await axios.post<Result<LoginResult>>(`/api/oauth/anyTenant/login`, body, { headers: getHeaders() });
if (!respData.isSuccess) {
console.error(respData);
window.$dialog.destroyAll();
window.$dialog.error({
closable: false,
maskClosable: false,
title: '错误提示',
content: respData.msg,
positiveText: '确认',
onPositiveClick: () => {
window.$message.destroyAll();
},
});
throw new AxiosError(respData.msg, `${respData.code}`);
} else {
userLoginResult.value = respData.data;
}
// 如果发生 http 错误,会被登录页中的 useMutation 捕获,此处无需 try-catch
const { data: result } = await axios.post<Result<LoginResult>>(`/api/oauth/anyTenant/login`, body, { headers: getHeaders() });
// 尽管请求成功,但需要判断是否发生业务错误
// 如果发生业务错误,则需要显式抛出,登录页会捕获该错误并提示用户
if (!result.isSuccess) throw new Error(result.msg);
userLoginResult.value = result.data;
};
const userLogout = async () => {