async function login(username, password) {
    try {
        const response = await fetch('http://localhost:3000/login', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify({
                username,
                password
            })
        });

        const data = await response.json();

        if (!response.ok) {
            throw new Error(data.error || '登录失败');
        }

        console.log('登录成功:', data.message);
        return data;
    } catch (error) {
        console.error('登录错误:', error.message);
        throw error;
    }
}

// 使用示例 - 已注册用户
login('newuser', 'password123')
    .then(() => console.log('登录流程完成'))
    .catch(() => console.log('登录流程失败'));