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) => { onError: (error) => {
console.error(error); console.error(error);
const errorFeedback = parseErrorFeedback(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> </script>

View File

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