初始化前端目录
This commit is contained in:
@@ -39,6 +39,9 @@ import { UserOutlined, LockOutlined } from '@ant-design/icons-vue'
|
||||
import type { LoginParams, LoginResult } from '@/api/auth'
|
||||
import { apiLogin } from '@/api/auth'
|
||||
|
||||
import { useAuthStore } from '@/store/index' // 引入状态管理
|
||||
|
||||
|
||||
const router = useRouter()
|
||||
const formRef = ref()
|
||||
const loading = ref(false)
|
||||
@@ -55,20 +58,23 @@ const handleSubmit = async () => {
|
||||
if (loading.value) return;
|
||||
|
||||
loading.value = true;
|
||||
const authStore = useAuthStore(); // 提前获取store实例,避免多次调用
|
||||
try {
|
||||
// 更清晰的变量命名,API返回通常包含code、data、msg等字段
|
||||
const response: LoginResult = await apiLogin(form);
|
||||
// 检查请求是否成功(通常200为成功状态码)
|
||||
if (response.code === 200 && response.data) {
|
||||
localStorage.setItem('userInfo', JSON.stringify(response.data));
|
||||
const token = response.data.token;
|
||||
localStorage.setItem('token', token );
|
||||
localStorage.setItem('userInfo', response.data);
|
||||
authStore.setAuthInfo(token, response.data);
|
||||
message.success(response.msg || '登录成功');
|
||||
// 延迟跳转提升用户体验
|
||||
setTimeout(() => {
|
||||
router.replace('/dashboard');
|
||||
router.replace('/index/console');
|
||||
}, 800);
|
||||
return; // 成功后终止函数,避免执行后续错误提示
|
||||
}
|
||||
|
||||
// 处理业务错误(如账号密码错误)
|
||||
message.error(response.msg || '登录失败,请检查账号密码');
|
||||
} catch (error) {
|
||||
|
||||
Reference in New Issue
Block a user