page.tsx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. "use client";
  2. import { useState, type FormEvent } from "react";
  3. import { useRouter } from "next/navigation";
  4. import { API_BASE_URL } from "@/lib/config";
  5. export default function AgentLoginPage() {
  6. const [username, setUsername] = useState("");
  7. const [password, setPassword] = useState("");
  8. const [loading, setLoading] = useState(false);
  9. const [error, setError] = useState("");
  10. const router = useRouter();
  11. // 客服登录
  12. async function handleLogin(e: FormEvent<HTMLFormElement>) {
  13. e.preventDefault(); // 阻止默认行为
  14. if (!username || !password) {
  15. setError("用户名和密码不能为空");
  16. return;
  17. }
  18. setLoading(true);
  19. setError("");
  20. try {
  21. const res = await fetch(`${API_BASE_URL}/login`, {
  22. method: "POST",
  23. headers: { "Content-Type": "application/json" },
  24. body: JSON.stringify({ username, password }),
  25. });
  26. const data = await res.json();
  27. if (res.ok) {
  28. // 登录成功,保存用户信息到 localStorage
  29. localStorage.setItem("agent_user_id", String(data.user_id));
  30. localStorage.setItem("agent_username", data.username);
  31. localStorage.setItem("agent_role", data.role);
  32. // 跳转到客服工作台(三栏布局)
  33. router.push("/agent/dashboard");
  34. } else {
  35. // 登录失败,显示错误信息
  36. setError(data.error || data.message || "登录失败");
  37. }
  38. } catch (error) {
  39. console.error("登录失败:", error);
  40. setError("登录失败,请检查网络连接");
  41. } finally {
  42. setLoading(false);
  43. }
  44. }
  45. return (
  46. <div className="flex justify-center items-center min-h-screen bg-gradient-to-r from-blue-500 via-purple-500 to-pink-500">
  47. <div className="bg-white p-6 rounded-lg shadow-lg w-full sm:w-96">
  48. <h1 className="text-center text-2xl font-bold mb-2 text-gray-800">
  49. 客服登录
  50. </h1>
  51. <p className="text-center text-sm text-gray-500 mb-6">
  52. 管理员和客服请在此登录
  53. </p>
  54. <form onSubmit={handleLogin}>
  55. <input
  56. type="text"
  57. placeholder="用户名"
  58. value={username}
  59. onChange={(e) => setUsername(e.target.value)}
  60. className="w-full p-3 mb-4 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-400"
  61. disabled={loading}
  62. />
  63. <input
  64. type="password"
  65. placeholder="密码"
  66. value={password}
  67. onChange={(e) => setPassword(e.target.value)}
  68. className="w-full p-3 mb-4 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-400"
  69. disabled={loading}
  70. />
  71. {error && (
  72. <div className="mb-4 p-3 bg-red-50 border border-red-200 rounded-md text-red-600 text-sm">
  73. {error}
  74. </div>
  75. )}
  76. <button
  77. type="submit"
  78. disabled={loading}
  79. className="w-full bg-blue-500 text-white py-3 rounded-md hover:bg-blue-600 transition-colors disabled:bg-gray-300 disabled:cursor-not-allowed"
  80. >
  81. {loading ? "登录中..." : "登录"}
  82. </button>
  83. </form>
  84. <div className="mt-4 text-center text-xs text-gray-400">
  85. <p>默认管理员账号:admin / admin123</p>
  86. </div>
  87. </div>
  88. </div>
  89. );
  90. }