fix localized login network error
This commit is contained in:
@@ -7,14 +7,19 @@ export type PageResult<T> = { total: number; list: T[] };
|
|||||||
|
|
||||||
export async function request<T>(path: string, init?: RequestInit): Promise<T> {
|
export async function request<T>(path: string, init?: RequestInit): Promise<T> {
|
||||||
const token = localStorage.getItem(tokenStorageKey);
|
const token = localStorage.getItem(tokenStorageKey);
|
||||||
const response = await fetch(`${apiBaseURL}${path}`, {
|
let response: Response;
|
||||||
...init,
|
try {
|
||||||
headers: {
|
response = await fetch(`${apiBaseURL}${path}`, {
|
||||||
'Content-Type': 'application/json',
|
...init,
|
||||||
...(token ? { Authorization: token } : {}),
|
headers: {
|
||||||
...(init?.headers ?? {}),
|
'Content-Type': 'application/json',
|
||||||
},
|
...(token ? { Authorization: token } : {}),
|
||||||
});
|
...(init?.headers ?? {}),
|
||||||
|
},
|
||||||
|
});
|
||||||
|
} catch {
|
||||||
|
throw new Error('无法连接服务器,请确认服务已启动');
|
||||||
|
}
|
||||||
const payload = (await response.json()) as { code?: number; message?: string; details?: T };
|
const payload = (await response.json()) as { code?: number; message?: string; details?: T };
|
||||||
if (!response.ok || payload.code !== 0) throw new Error(payload.message || '请求失败');
|
if (!response.ok || payload.code !== 0) throw new Error(payload.message || '请求失败');
|
||||||
return payload.details as T;
|
return payload.details as T;
|
||||||
|
|||||||
@@ -2,7 +2,6 @@
|
|||||||
<div class="login-form-wrapper">
|
<div class="login-form-wrapper">
|
||||||
<div class="login-form-title">登录和气平台</div>
|
<div class="login-form-title">登录和气平台</div>
|
||||||
<div class="login-form-sub-title">使用平台管理员账户登录</div>
|
<div class="login-form-sub-title">使用平台管理员账户登录</div>
|
||||||
<div class="login-form-error-msg">{{ errorMessage }}</div>
|
|
||||||
<a-form
|
<a-form
|
||||||
ref="loginForm"
|
ref="loginForm"
|
||||||
:model="userInfo"
|
:model="userInfo"
|
||||||
@@ -51,6 +50,9 @@
|
|||||||
记住用户名
|
记住用户名
|
||||||
</a-checkbox>
|
</a-checkbox>
|
||||||
</div>
|
</div>
|
||||||
|
<div v-if="errorMessage" class="login-form-error-msg">
|
||||||
|
{{ errorMessage }}
|
||||||
|
</div>
|
||||||
<a-button type="primary" html-type="submit" long :loading="loading">
|
<a-button type="primary" html-type="submit" long :loading="loading">
|
||||||
登录
|
登录
|
||||||
</a-button>
|
</a-button>
|
||||||
@@ -93,6 +95,7 @@ const handleSubmit = async ({
|
|||||||
}) => {
|
}) => {
|
||||||
if (loading.value) return;
|
if (loading.value) return;
|
||||||
if (!errors) {
|
if (!errors) {
|
||||||
|
errorMessage.value = '';
|
||||||
setLoading(true);
|
setLoading(true);
|
||||||
try {
|
try {
|
||||||
await userStore.login(values as LoginData);
|
await userStore.login(values as LoginData);
|
||||||
@@ -105,7 +108,7 @@ const handleSubmit = async ({
|
|||||||
});
|
});
|
||||||
Message.success('登录成功');
|
Message.success('登录成功');
|
||||||
const { rememberPassword } = loginConfig.value;
|
const { rememberPassword } = loginConfig.value;
|
||||||
const { username, password } = values;
|
const { username } = values;
|
||||||
loginConfig.value.username = rememberPassword ? username : '';
|
loginConfig.value.username = rememberPassword ? username : '';
|
||||||
loginConfig.value.password = '';
|
loginConfig.value.password = '';
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
|||||||
Reference in New Issue
Block a user