fix:错误码处理
This commit is contained in:
@@ -18,6 +18,17 @@ const isLoading = ref(false);
|
||||
const errorMessage = ref('');
|
||||
|
||||
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) {
|
||||
errorMessage.value = '两次输入的密码不一致';
|
||||
return;
|
||||
@@ -27,18 +38,28 @@ const submit = async () => {
|
||||
errorMessage.value = '';
|
||||
try {
|
||||
const payload = {
|
||||
email: form.email,
|
||||
email,
|
||||
password: form.password,
|
||||
confirmPassword: form.confirmPassword,
|
||||
displayName: form.displayName,
|
||||
displayName,
|
||||
preferredCurrency: 'CNY'
|
||||
};
|
||||
const { data } = await apiClient.post('/auth/register', payload);
|
||||
authStore.setSession(data.tokens, data.user);
|
||||
router.push('/');
|
||||
} catch (error) {
|
||||
console.warn('register failed', error);
|
||||
errorMessage.value = '注册失败,请稍后再试或联系支持。';
|
||||
} catch (err: any) {
|
||||
console.warn('register failed', err);
|
||||
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 {
|
||||
isLoading.value = false;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user