fix:错误码处理
This commit is contained in:
@@ -18,6 +18,17 @@ const isLoading = ref(false);
|
|||||||
const errorMessage = ref('');
|
const errorMessage = ref('');
|
||||||
|
|
||||||
const submit = async () => {
|
const submit = async () => {
|
||||||
|
const email = form.email.trim();
|
||||||
|
const displayName = form.displayName.trim();
|
||||||
|
|
||||||
|
if (!displayName) {
|
||||||
|
errorMessage.value = '请输入昵称';
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (!email) {
|
||||||
|
errorMessage.value = '请输入邮箱地址';
|
||||||
|
return;
|
||||||
|
}
|
||||||
if (form.password !== form.confirmPassword) {
|
if (form.password !== form.confirmPassword) {
|
||||||
errorMessage.value = '两次输入的密码不一致';
|
errorMessage.value = '两次输入的密码不一致';
|
||||||
return;
|
return;
|
||||||
@@ -27,18 +38,28 @@ const submit = async () => {
|
|||||||
errorMessage.value = '';
|
errorMessage.value = '';
|
||||||
try {
|
try {
|
||||||
const payload = {
|
const payload = {
|
||||||
email: form.email,
|
email,
|
||||||
password: form.password,
|
password: form.password,
|
||||||
confirmPassword: form.confirmPassword,
|
confirmPassword: form.confirmPassword,
|
||||||
displayName: form.displayName,
|
displayName,
|
||||||
preferredCurrency: 'CNY'
|
preferredCurrency: 'CNY'
|
||||||
};
|
};
|
||||||
const { data } = await apiClient.post('/auth/register', payload);
|
const { data } = await apiClient.post('/auth/register', payload);
|
||||||
authStore.setSession(data.tokens, data.user);
|
authStore.setSession(data.tokens, data.user);
|
||||||
router.push('/');
|
router.push('/');
|
||||||
} catch (error) {
|
} catch (err: any) {
|
||||||
console.warn('register failed', error);
|
console.warn('register failed', err);
|
||||||
errorMessage.value = '注册失败,请稍后再试或联系支持。';
|
const status = err?.response?.status as number | undefined;
|
||||||
|
const backendMsg = err?.response?.data?.message as string | undefined;
|
||||||
|
if (!status) {
|
||||||
|
errorMessage.value = '无法连接服务器,请检查网络或 API 地址配置。';
|
||||||
|
} else if (status === 409) {
|
||||||
|
errorMessage.value = '该邮箱已注册,请直接登录或使用“忘记密码”。';
|
||||||
|
} else if (status === 400 || status === 422) {
|
||||||
|
errorMessage.value = backendMsg ?? '输入不合法,请检查表单内容。';
|
||||||
|
} else {
|
||||||
|
errorMessage.value = backendMsg ?? '注册失败,请稍后再试或联系支持。';
|
||||||
|
}
|
||||||
} finally {
|
} finally {
|
||||||
isLoading.value = false;
|
isLoading.value = false;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user