refactor: 优化登录逻辑并添加注释
This commit is contained in:
@@ -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>
|
||||
|
||||
@@ -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 () => {
|
||||
|
||||
Reference in New Issue
Block a user