This commit is contained in:
Leo_Ding 2025-12-03 09:31:26 +08:00
commit a7f3076229
3 changed files with 50 additions and 62 deletions

View File

@ -34,7 +34,8 @@ request.interceptors.response.use(
(response) => {
// 假设后端返回格式为 { code: 200, data: ..., message: '' }
const { code, data, message } = response.data;
if (code === 200) {
console.log('请求成功:', data);
if (code === 1) {
return data;
} else {
// 可抛出业务错误

View File

@ -1,29 +1,16 @@
<template>
<a-form :model="formState" name="normal_login" class="login-form" @finish="onFinish" @finish-failed="onFinishFailed"
ref="formRef">
<a-form-item label="账号" name="username" :rules="[{ required: true, message: '请输入用户名!' }]">
<a-input v-model:value="formState.username">
<template #prefix>
<UserOutlined class="site-form-item-icon" />
</template>
</a-input>
<a-form ref="formRef" :model="formState" name="normal_login" class="login-form" @finish="onFinish"
@finish-failed="onFinishFailed">
<a-form-item label="账号" name="phone" :rules="[{ required: true, message: '请输入用户名!' }]">
<a-input v-model:value="formState.phone" />
</a-form-item>
<a-form-item label="密码" name="password" :rules="[{ required: true, message: '请输入密码!' }]">
<a-input-password v-model:value="formState.password">
<template #prefix>
<LockOutlined class="site-form-item-icon" />
</template>
</a-input-password>
<!-- 手动控制密码字段的错误状态 -->
<a-form-item label="密码" name="code" :validate-status="codeValidateStatus" :help="codeHelp"
:rules="[{ required: true, message: '请输入密码!' }]">
<a-input-password v-model:value="formState.code" />
</a-form-item>
<!-- 可选记住我 -->
<!--
<a-form-item name="remember" no-style>
<a-checkbox v-model:checked="formState.remember">记住我</a-checkbox>
</a-form-item>
-->
<a-form-item>
<a-button type="primary" html-type="submit" block class="login-form-button">
@ -33,66 +20,66 @@
</template>
<script lang="ts" setup>
import { reactive, ref } from 'vue';
import { UserOutlined, LockOutlined } from '@ant-design/icons-vue';
import { reactive, ref, shallowRef } from 'vue';
import { useRouter } from 'vue-router';
import {login} from '@/apis/login';
// md5import md5 from 'md5';
// import md5 from 'md5'; // 使
import { login } from '@/apis/login';
import type { FormInstance } from 'ant-design-vue';
const router = useRouter();
const formRef = ref();
const formRef = ref<FormInstance>();
const codeValidateStatus = shallowRef<'success' | 'warning' | 'error' | ''>('');
const codeHelp = shallowRef('');
interface FormState {
username: string;
password: string;
phone: string;
code: string;
remember: boolean;
}
const formState = reactive<FormState>({
username: '',
password: '',
phone: '',
code: '',
remember: true,
});
//
const clearCodeError = () => {
codeValidateStatus.value = '';
codeHelp.value = '';
};
const setCodeError = (msg: string) => {
codeValidateStatus.value = 'error';
codeHelp.value = msg;
};
const onFinish = async (values: FormState) => {
//
// const loginData = { ...values, password: md5(values.password) };
clearCodeError();
const loginData = {
...values,
login_method: 'SMS'
};
try {
const { success } = await login(values); // apis.login { success: boolean }
const res:any = await login(loginData);
console.log('登录请求成功:', res);
// tokenlocalStorage
const token = res?.access_token;
if (success) {
//
router.push('/platform');
} else {
//
formRef.value?.setFields([
{ name: 'password', errors: ['用户名或密码错误'] }
]);
if (!token) {
throw new Error('登录失败:未返回有效凭证');
}
} catch (error) {
localStorage.setItem('token', token);
router.push('/layout/home');
} catch (error: any) {
console.error('登录请求失败:', error);
//
formRef.value?.setFields([
{ name: 'password', errors: ['网络错误,请稍后重试'] }
]);
setCodeError(error.message || '用户名或密码错误');
}
};
const onFinishFailed = (errorInfo: any) => {
console.log('表单验证失败:', errorInfo);
// Ant Design Vue
};
</script>
<style scoped>
.login-form {
max-width: 300px;
}
.login-form-button {
width: 100%;
}
</style>
</script>

View File

@ -11,7 +11,7 @@
<a-tab-pane key="account" tab="登录">
<Account />
</a-tab-pane>
<a-tab-pane key="mobile" tab="注册" disabled>Tab 2</a-tab-pane>
<a-tab-pane key="mobile" tab="注册">Tab 2</a-tab-pane>
</a-tabs>
</div>
</div>