blob: a22cee0c5eced1280876bc698c0ae9a126f6a611 [file] [log] [blame]
LaoeGaoci85307e62025-05-30 23:28:42 +08001'use client';
lfmylbhf839e5a12025-06-09 18:22:25 +08002import React, { useRef, useState } from 'react';
3import { Button } from "primereact/button";
4// 输入框
5import { InputText } from "primereact/inputtext";
6// 消息提醒
7import { Toast } from 'primereact/toast';
8// 接口传输
9import axios from "axios";
10// 页面跳转
11import { useRouter } from "next/navigation";
12// 密码
13import { Password } from 'primereact/password';
14// 样式
15import './login.scss';
LaoeGaoci85307e62025-05-30 23:28:42 +080016
lfmylbhf839e5a12025-06-09 18:22:25 +080017export default function LoginPage() {
18 // 路由
19 const router = useRouter();
20 // 消息提醒
21 const toast = useRef<Toast>(null);
22
23 const [loginFormData, setLoginFormData] = useState({
24 username: '',
25 password: '',
26 })
27
28 // 获取userId
29 const fetchUserId = async () => {
30 try {
31 const response = await axios.get(process.env.PUBLIC_URL + `/user/getId`, {
32 params: { username: loginFormData.username , password: loginFormData.password }
33 });
34 console.log('用户id:', response.data)
35 localStorage.setItem("user", JSON.stringify({ Id: response.data }));
36 } catch (error) {
37 console.error('获取用户id失败:', error);
38 toast.current?.show({ severity: 'error', summary: 'Error', detail: '获取用户id失败' });
39 }
40 }
41
42 // 处理用户登录
43 const handleLogin = async () => {
44 try {
45 const postData = {
46 username: loginFormData.username,
47 password: loginFormData.password,
48 }
49 const response = await axios.post(process.env.PUBLIC_URL + `/user/login`, postData, {
50 // eslint-disable-next-line @typescript-eslint/no-unused-vars
51 validateStatus: (status) => true // 接受所有状态码
52 });
53
54 if (response.status === 200) {
55 console.log('用户登录成功');
56 toast.current?.show({ severity: 'success', summary: 'Success', detail: '登录成功' });
57 // 设置userId
58 fetchUserId()
59 router.push('/');
60 } else if (response.status === 406) {
61 console.log('用户名不存在');
62 toast.current?.show({ severity: 'error', summary: 'Error', detail: '用户名不存在' });
63 } else if (response.status === 408) {
64 console.error('密码错误');
65 toast.current?.show({ severity: 'error', summary: 'Error', detail: '密码错误' });
66 }
67
68 } catch (error) {
69 console.error('登录失败:', error);
70 toast.current?.show({ severity: 'error', summary: 'Error', detail: '登录失败' });
71 }
72 }
73
74 return (
75 <div className='login-container'>
76 <Toast ref={toast}></Toast>
77 <h1 className="form-header">登录</h1>
78 <div className="form-field">
79 <div className="form-field-header">
80 <label htmlFor="username">用户名</label>
81 </div>
82 <InputText
83 id="username"
84 value={loginFormData.username}
85 onChange={(e) => setLoginFormData(prev => ({
86 ...prev, // 复制顶层所有属性
87 username: e.target.value
88 }))}
89 className="w-full"
90 />
91 </div>
92 <div className="form-field">
93 <div className="form-field-header">
94 <label htmlFor="password">密码</label>
95 </div>
96 <Password
97 id="password"
98 value={loginFormData.password}
99 onChange={(e) => setLoginFormData(prev => ({
100 ...prev,
101 password: e.target.value
102 }))}
103 className="w-full"
104 toggleMask
105 />
106 </div>
107 <div className="form-operation">
108 <Button label="确定" onClick={handleLogin} className='form-operation-button'/>
109 <Button label="注册" onClick={() => router.push("/user/register")} className='form-operation-button'/>
110 </div>
111 </div>
112 )
LaoeGaoci85307e62025-05-30 23:28:42 +0800113};